listen.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. <template>
  2. <view class="container">
  3. <!-- 搜索栏 -->
  4. <view class="search-bar">
  5. <input class="search-input" v-model="searchKeyword" placeholder="搜索听书:书名/作者/主播" confirm-type="search" @confirm="submitSearch" />
  6. <text class="search-icon" @click="submitSearch">🔍</text>
  7. </view>
  8. <!-- 推荐横幅轮播 -->
  9. <view class="banner-section" v-if="bannerBooks && bannerBooks.length > 0">
  10. <swiper
  11. class="banner-swiper"
  12. :indicator-dots="bannerBooks.length > 1"
  13. :autoplay="bannerBooks.length > 1"
  14. :interval="3000"
  15. :duration="500"
  16. indicator-color="rgba(255,255,255,0.5)"
  17. indicator-active-color="#FF1744"
  18. :circular="bannerBooks.length > 1"
  19. >
  20. <swiper-item v-for="(book, index) in bannerBooks" :key="book.id || index">
  21. <view class="banner-content" @click="goToListenDetail(book)">
  22. <image class="banner-cover" :src="book.image" mode="aspectFill" @error="handleBannerImageError(index)"></image>
  23. <view class="banner-info">
  24. <text class="banner-title">{{ book.title || '听书推荐' }}</text>
  25. <text class="banner-subtitle" v-if="book.subtitle">{{ book.subtitle }}</text>
  26. <text class="banner-desc" v-if="book.desc">{{ book.desc }}</text>
  27. <text class="banner-tag" v-if="book.tag">{{ book.tag }}</text>
  28. </view>
  29. </view>
  30. </swiper-item>
  31. </swiper>
  32. </view>
  33. <!-- 分类导航 -->
  34. <view class="category-nav">
  35. <view class="category-item" v-for="(item, index) in categories" :key="index" @click="handleCategoryClick(item, index)">
  36. <view class="category-icon" :style="{ backgroundColor: item.color }">
  37. <text class="icon-text">{{ item.icon }}</text>
  38. </view>
  39. <text class="category-text">{{ item.name }}</text>
  40. </view>
  41. </view>
  42. <scroll-view class="scroll-content" scroll-y>
  43. <!-- 最近上新 -->
  44. <view class="section" v-if="recentBooks.length > 0">
  45. <view class="section-header">
  46. <text class="section-title">最近上新</text>
  47. <text class="section-more" @click.stop="goToMoreBooks('recent')">更多 ></text>
  48. </view>
  49. <view class="book-grid">
  50. <view class="book-item" v-for="(book, index) in recentBooks" :key="book.id || index" @click="goToListenDetail(book)">
  51. <view class="book-cover-wrapper">
  52. <image class="book-cover" :src="book.image" mode="aspectFill" :lazy-load="true"></image>
  53. <view class="play-icon-overlay">
  54. <text class="play-icon">▶</text>
  55. </view>
  56. </view>
  57. <text class="book-name">{{ book.title }}</text>
  58. </view>
  59. </view>
  60. </view>
  61. <!-- 猜你喜欢 -->
  62. <view class="section" v-if="recommendBooks.length > 0">
  63. <view class="section-header">
  64. <text class="section-title">猜你喜欢</text>
  65. <text class="section-more" @click.stop="goToMoreBooks('guess')">更多 ></text>
  66. </view>
  67. <view class="book-list">
  68. <view class="book-list-item" v-for="(book, index) in recommendBooks" :key="book.id || index" @click="goToListenDetail(book)">
  69. <image class="book-cover-small" :src="book.image" mode="aspectFill" :lazy-load="true"></image>
  70. <view class="book-info">
  71. <text class="book-title">{{ book.title }}</text>
  72. <text class="book-desc">{{ book.desc }}</text>
  73. <text class="book-author">{{ book.author }}</text>
  74. <view class="play-button" @click.stop="playBook(book)">
  75. <text class="play-button-icon">▶</text>
  76. <text class="play-button-text">播放</text>
  77. </view>
  78. </view>
  79. </view>
  80. </view>
  81. </view>
  82. <!-- 加载中 -->
  83. <view class="loading-section" v-if="isLoading">
  84. <text class="loading-text">加载中...</text>
  85. </view>
  86. <!-- 空状态 -->
  87. <view class="empty-section" v-if="!isLoading && recentBooks.length === 0 && recommendBooks.length === 0">
  88. <text class="empty-text">暂无听书内容</text>
  89. </view>
  90. </scroll-view>
  91. </view>
  92. </template>
  93. <script>
  94. import { getRecentAudiobooks, getRecommendAudiobooks, getBannersByCode } from '../../utils/api.js'
  95. export default {
  96. data() {
  97. return {
  98. searchKeyword: '',
  99. bannerBooks: [],
  100. categories: [
  101. { name: '全部分类', icon: '📄', color: '#81C784' },
  102. { name: '畅听榜', icon: '👑', color: '#FFD54F' },
  103. { name: '热门听书', icon: '🔥', color: '#E57373' },
  104. { name: '随身听', icon: '🎧', color: '#81C784' },
  105. { name: '有声小说', icon: '📚', color: '#4FC3F7' }
  106. ],
  107. recentBooks: [],
  108. recommendBooks: [],
  109. isLoading: false
  110. }
  111. },
  112. onLoad() {
  113. this.loadData()
  114. },
  115. methods: {
  116. submitSearch() {
  117. const kw = (this.searchKeyword || '').trim()
  118. if (!kw) {
  119. uni.showToast({ title: '请输入关键词', icon: 'none' })
  120. return
  121. }
  122. uni.navigateTo({
  123. url: `/pages/search/search?mode=audio&keyword=${encodeURIComponent(kw)}`
  124. })
  125. },
  126. async loadData() {
  127. try {
  128. this.isLoading = true
  129. // 从数据库加载轮播图
  130. await this.loadBanners()
  131. // 加载最近上新
  132. const recentRes = await getRecentAudiobooks(8)
  133. if (recentRes && recentRes.code === 200 && recentRes.data) {
  134. this.recentBooks = recentRes.data.map(item => ({
  135. id: item.id,
  136. title: item.title,
  137. image: item.image || item.cover || 'https://picsum.photos/seed/default/200/300',
  138. author: item.author || ''
  139. }))
  140. }
  141. // 加载推荐听书
  142. const recommendRes = await getRecommendAudiobooks(10)
  143. if (recommendRes && recommendRes.code === 200 && recommendRes.data) {
  144. this.recommendBooks = recommendRes.data.map(item => ({
  145. id: item.id,
  146. title: item.title,
  147. desc: item.brief || item.desc || '',
  148. author: item.author || '',
  149. image: item.image || item.cover || 'https://picsum.photos/seed/default/200/300'
  150. }))
  151. }
  152. } catch (e) {
  153. console.error('加载听书数据失败:', e)
  154. uni.showToast({
  155. title: '加载失败,请重试',
  156. icon: 'none'
  157. })
  158. } finally {
  159. this.isLoading = false
  160. }
  161. },
  162. async loadBanners() {
  163. try {
  164. console.log('开始加载听书轮播图...')
  165. const res = await getBannersByCode('audio_banner')
  166. console.log('轮播图API响应:', res)
  167. if (res && res.code === 200) {
  168. if (Array.isArray(res.data) && res.data.length > 0) {
  169. this.bannerBooks = res.data.map(b => ({
  170. id: b.targetId || b.id,
  171. title: b.title || '听书推荐',
  172. subtitle: b.narrator ? `主播:${b.narrator}` : '',
  173. desc: b.desc || b.brief || '',
  174. tag: b.targetType === 'audiobook' ? '听书' : (b.targetType === 'book' ? '书籍' : ''),
  175. image: b.image || 'https://picsum.photos/seed/default/200/300',
  176. targetType: b.targetType,
  177. targetId: b.targetId,
  178. link: b.link
  179. }))
  180. console.log('轮播图数据加载成功,共', this.bannerBooks.length, '条')
  181. } else {
  182. console.warn('轮播图数据为空')
  183. this.bannerBooks = []
  184. }
  185. } else {
  186. console.warn('轮播图API返回错误:', res)
  187. this.bannerBooks = []
  188. }
  189. } catch (e) {
  190. console.error('加载轮播图失败:', e)
  191. this.bannerBooks = []
  192. // 不显示错误提示,避免影响用户体验
  193. }
  194. },
  195. handleCategoryClick(item, index) {
  196. if (index === 0) {
  197. uni.showToast({
  198. title: '全部分类',
  199. icon: 'none'
  200. })
  201. } else if (index === 1) {
  202. uni.navigateTo({
  203. url: '/pages/ranking/ranking'
  204. })
  205. } else if (index === 2) {
  206. uni.navigateTo({
  207. url: '/pages/hot-listen/hot-listen'
  208. })
  209. } else if (index === 3) {
  210. uni.navigateTo({
  211. url: '/pages/portable-listen/portable-listen'
  212. })
  213. } else if (index === 4) {
  214. uni.navigateTo({
  215. url: '/pages/audio-novel/audio-novel'
  216. })
  217. }
  218. },
  219. goToListenDetail(book) {
  220. if (!book) {
  221. uni.showToast({
  222. title: '听书信息不完整',
  223. icon: 'none'
  224. })
  225. return
  226. }
  227. // 如果是从轮播图点击,需要根据targetType和targetId判断跳转
  228. if (book.targetType && book.targetId) {
  229. if (book.targetType === 'audiobook') {
  230. uni.navigateTo({
  231. url: `/pages/listen-detail/listen-detail?audiobookId=${book.targetId}`
  232. })
  233. return
  234. } else if (book.targetType === 'book') {
  235. uni.navigateTo({
  236. url: `/pages/book-detail/book-detail?bookId=${book.targetId}`
  237. })
  238. return
  239. } else if (book.targetType === 'url' && book.link) {
  240. // H5可直接跳转,小程序需要特殊处理
  241. // #ifdef H5
  242. window.location.href = book.link
  243. // #endif
  244. // #ifdef MP-WEIXIN
  245. uni.showToast({
  246. title: '暂不支持外部链接',
  247. icon: 'none'
  248. })
  249. // #endif
  250. return
  251. }
  252. }
  253. // 普通书籍列表点击,使用book.id
  254. if (!book.id) {
  255. uni.showToast({
  256. title: '听书信息不完整',
  257. icon: 'none'
  258. })
  259. return
  260. }
  261. uni.navigateTo({
  262. url: `/pages/listen-detail/listen-detail?audiobookId=${book.id}`
  263. })
  264. },
  265. playBook(book) {
  266. // 跳转到详情页,然后播放第一章节
  267. this.goToListenDetail(book)
  268. },
  269. goToMoreBooks(type) {
  270. uni.navigateTo({
  271. url: `/pages/more-listen-books/more-listen-books?type=${type}`
  272. })
  273. },
  274. handleBannerImageError(index) {
  275. // 图片加载失败时使用备用图片
  276. if (this.bannerBooks[index]) {
  277. this.bannerBooks[index].image = 'https://picsum.photos/seed/default/200/300'
  278. }
  279. }
  280. }
  281. }
  282. </script>
  283. <style scoped>
  284. .container {
  285. width: 100%;
  286. height: 100vh;
  287. background-color: #FFFFFF;
  288. display: flex;
  289. flex-direction: column;
  290. padding-top: 60px;
  291. box-sizing: border-box;
  292. }
  293. .search-bar {
  294. position: relative;
  295. padding: 20rpx 30rpx;
  296. background-color: #FFFFFF;
  297. }
  298. .search-input {
  299. width: 100%;
  300. height: 70rpx;
  301. background-color: #F5F5F5;
  302. border-radius: 35rpx;
  303. padding: 0 80rpx 0 30rpx;
  304. font-size: 28rpx;
  305. color: #333333;
  306. box-sizing: border-box;
  307. }
  308. .search-icon {
  309. position: absolute;
  310. right: 50rpx;
  311. top: 50%;
  312. transform: translateY(-50%);
  313. font-size: 32rpx;
  314. }
  315. .banner-section {
  316. padding: 20rpx 30rpx;
  317. background-color: #FFFFFF;
  318. }
  319. .banner-swiper {
  320. width: 100%;
  321. height: 280rpx;
  322. border-radius: 16rpx;
  323. overflow: hidden;
  324. }
  325. .banner-content {
  326. display: flex;
  327. background: linear-gradient(135deg, #FFE5F1 0%, #FFF0F5 100%);
  328. border-radius: 16rpx;
  329. padding: 30rpx;
  330. box-shadow: 0 4rpx 12rpx rgba(0,0,0,0.1);
  331. height: 100%;
  332. width: 100%;
  333. box-sizing: border-box;
  334. }
  335. .banner-cover {
  336. width: 160rpx;
  337. height: 220rpx;
  338. border-radius: 8rpx;
  339. margin-right: 30rpx;
  340. flex-shrink: 0;
  341. box-shadow: 0 4rpx 12rpx rgba(0,0,0,0.15);
  342. background-color: #F5F5F5;
  343. object-fit: cover;
  344. }
  345. .banner-info {
  346. flex: 1;
  347. display: flex;
  348. flex-direction: column;
  349. justify-content: center;
  350. min-width: 0;
  351. }
  352. .banner-title {
  353. font-size: 36rpx;
  354. font-weight: bold;
  355. color: #333333;
  356. margin-bottom: 10rpx;
  357. overflow: hidden;
  358. text-overflow: ellipsis;
  359. white-space: nowrap;
  360. }
  361. .banner-subtitle {
  362. font-size: 28rpx;
  363. color: #666666;
  364. margin-bottom: 10rpx;
  365. overflow: hidden;
  366. text-overflow: ellipsis;
  367. white-space: nowrap;
  368. }
  369. .banner-desc {
  370. font-size: 24rpx;
  371. color: #999999;
  372. margin-bottom: 15rpx;
  373. display: -webkit-box;
  374. -webkit-box-orient: vertical;
  375. -webkit-line-clamp: 2;
  376. overflow: hidden;
  377. }
  378. .banner-tag {
  379. font-size: 22rpx;
  380. color: #FF1744;
  381. border: 1rpx solid #FF1744;
  382. border-radius: 8rpx;
  383. padding: 4rpx 12rpx;
  384. align-self: flex-start;
  385. }
  386. .category-nav {
  387. display: flex;
  388. justify-content: space-around;
  389. padding: 40rpx 20rpx;
  390. background-color: #FFFFFF;
  391. margin-top: 20rpx;
  392. margin-bottom: 20rpx;
  393. border-radius: 16rpx;
  394. margin-left: 30rpx;
  395. margin-right: 30rpx;
  396. box-shadow: 0 4rpx 12rpx rgba(0,0,0,0.05);
  397. }
  398. .category-item {
  399. display: flex;
  400. flex-direction: column;
  401. align-items: center;
  402. }
  403. .category-icon {
  404. width: 90rpx;
  405. height: 90rpx;
  406. border-radius: 50%;
  407. display: flex;
  408. align-items: center;
  409. justify-content: center;
  410. margin-bottom: 15rpx;
  411. }
  412. .icon-text {
  413. font-size: 44rpx;
  414. }
  415. .category-text {
  416. font-size: 24rpx;
  417. color: #333333;
  418. }
  419. .scroll-content {
  420. flex: 1;
  421. width: 100%;
  422. padding-bottom: 20rpx;
  423. }
  424. .section {
  425. padding: 40rpx 30rpx;
  426. background-color: #FFFFFF;
  427. margin-bottom: 20rpx;
  428. }
  429. .section-header {
  430. display: flex;
  431. justify-content: space-between;
  432. align-items: center;
  433. margin-bottom: 30rpx;
  434. }
  435. .section-title {
  436. font-size: 36rpx;
  437. font-weight: bold;
  438. color: #333333;
  439. }
  440. .section-more {
  441. font-size: 28rpx;
  442. color: #999999;
  443. }
  444. .book-grid {
  445. display: flex;
  446. flex-wrap: wrap;
  447. justify-content: space-between;
  448. }
  449. .book-item {
  450. width: calc(25% - 15rpx);
  451. margin-bottom: 30rpx;
  452. display: flex;
  453. flex-direction: column;
  454. align-items: center;
  455. text-align: center;
  456. }
  457. .book-cover-wrapper {
  458. position: relative;
  459. width: 100%;
  460. padding-bottom: 140%;
  461. height: 0;
  462. border-radius: 8rpx;
  463. overflow: hidden;
  464. margin-bottom: 15rpx;
  465. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.1);
  466. background-color: #F5F5F5;
  467. }
  468. .book-cover {
  469. position: absolute;
  470. top: 0;
  471. left: 0;
  472. width: 100%;
  473. height: 100%;
  474. }
  475. .play-icon-overlay {
  476. position: absolute;
  477. bottom: 0;
  478. right: 0;
  479. width: 48rpx;
  480. height: 48rpx;
  481. background-color: #4CAF50;
  482. border-radius: 50%;
  483. display: flex;
  484. align-items: center;
  485. justify-content: center;
  486. transform: translate(25%, 25%);
  487. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.2);
  488. }
  489. .play-icon {
  490. font-size: 24rpx;
  491. color: #FFFFFF;
  492. margin-left: 4rpx;
  493. }
  494. .book-name {
  495. font-size: 24rpx;
  496. color: #333333;
  497. display: block;
  498. overflow: hidden;
  499. text-overflow: ellipsis;
  500. white-space: nowrap;
  501. width: 100%;
  502. }
  503. .book-list {
  504. display: flex;
  505. flex-direction: column;
  506. }
  507. .book-list-item {
  508. display: flex;
  509. margin-bottom: 30rpx;
  510. align-items: center;
  511. }
  512. .book-cover-small {
  513. width: 120rpx;
  514. height: 160rpx;
  515. border-radius: 8rpx;
  516. margin-right: 20rpx;
  517. flex-shrink: 0;
  518. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.1);
  519. background-color: #F5F5F5;
  520. }
  521. .book-info {
  522. flex: 1;
  523. display: flex;
  524. flex-direction: column;
  525. justify-content: space-between;
  526. min-width: 0;
  527. }
  528. .book-title {
  529. font-size: 32rpx;
  530. font-weight: bold;
  531. color: #333333;
  532. margin-bottom: 10rpx;
  533. overflow: hidden;
  534. text-overflow: ellipsis;
  535. white-space: nowrap;
  536. }
  537. .book-desc {
  538. font-size: 26rpx;
  539. color: #666666;
  540. line-height: 1.4;
  541. margin-bottom: 10rpx;
  542. display: -webkit-box;
  543. -webkit-box-orient: vertical;
  544. -webkit-line-clamp: 1;
  545. overflow: hidden;
  546. }
  547. .book-author {
  548. font-size: 24rpx;
  549. color: #999999;
  550. margin-bottom: 15rpx;
  551. overflow: hidden;
  552. text-overflow: ellipsis;
  553. white-space: nowrap;
  554. }
  555. .play-button {
  556. background-color: #4CAF50;
  557. color: #FFFFFF;
  558. border-radius: 30rpx;
  559. padding: 10rpx 25rpx;
  560. font-size: 26rpx;
  561. display: flex;
  562. align-items: center;
  563. justify-content: center;
  564. width: fit-content;
  565. }
  566. .play-button-icon {
  567. font-size: 24rpx;
  568. margin-right: 10rpx;
  569. }
  570. .play-button-text {
  571. line-height: 1;
  572. }
  573. .loading-section {
  574. padding: 100rpx 30rpx;
  575. text-align: center;
  576. }
  577. .loading-text {
  578. font-size: 28rpx;
  579. color: #999999;
  580. }
  581. .empty-section {
  582. padding: 100rpx 30rpx;
  583. text-align: center;
  584. }
  585. .empty-text {
  586. font-size: 28rpx;
  587. color: #999999;
  588. }
  589. </style>