listen-detail.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. <template>
  2. <view class="container">
  3. <!-- 顶部导航栏 -->
  4. <view class="header">
  5. <view class="back-btn" @click="goBack">
  6. <text class="back-icon">←</text>
  7. </view>
  8. <view class="share-btn" @click="handleShare">
  9. <text class="share-icon">↗</text>
  10. </view>
  11. </view>
  12. <scroll-view class="scroll-content" scroll-y>
  13. <!-- 书籍信息 -->
  14. <view class="book-info-section" v-if="!isLoading">
  15. <image class="book-cover" :src="bookInfo.image || bookInfo.cover" mode="aspectFill"></image>
  16. <view class="book-details">
  17. <text class="book-title">{{ bookInfo.title }}</text>
  18. <text class="book-desc" v-if="bookInfo.desc || bookInfo.brief || bookInfo.introduction">{{ bookInfo.desc || bookInfo.brief || bookInfo.introduction }}</text>
  19. <text class="book-author" v-if="bookInfo.author">{{ bookInfo.author }}</text>
  20. <text class="book-narrator" v-if="bookInfo.narrator">主播:{{ bookInfo.narrator }}</text>
  21. <view class="action-buttons">
  22. <button class="play-all-btn" @click="playAll">播放全部</button>
  23. <button class="add-to-shelf-btn" :class="{ 'in-bookshelf': isInBookshelf }" @click="addToShelf">
  24. {{ isInBookshelf ? '已在书架' : '加入书架' }}
  25. </button>
  26. </view>
  27. </view>
  28. </view>
  29. <!-- 加载中 -->
  30. <view class="loading-section" v-if="isLoading">
  31. <text class="loading-text">加载中...</text>
  32. </view>
  33. <!-- 标签页 -->
  34. <view class="tabs">
  35. <view
  36. class="tab-item"
  37. :class="{ active: currentTab === 'catalog' }"
  38. @click="switchTab('catalog')"
  39. >
  40. <text class="tab-text">目录</text>
  41. </view>
  42. <view
  43. class="tab-item"
  44. :class="{ active: currentTab === 'comments' }"
  45. @click="switchTab('comments')"
  46. >
  47. <text class="tab-text">评论</text>
  48. </view>
  49. </view>
  50. <!-- 目录内容 -->
  51. <view class="content-section" v-if="currentTab === 'catalog'">
  52. <view
  53. class="chapter-item"
  54. v-for="(chapter, index) in chapters"
  55. :key="chapter.id || index"
  56. @click="playChapter(chapter, index)"
  57. >
  58. <text class="chapter-number">{{ index + 1 }}.</text>
  59. <view class="chapter-info">
  60. <text class="chapter-title">{{ chapter.title }}</text>
  61. <view class="chapter-meta">
  62. <view class="chapter-status" v-if="chapter.isFree">
  63. <text class="status-text">免费</text>
  64. </view>
  65. <text class="chapter-lock" v-else>🔒</text>
  66. <text class="chapter-duration">{{ chapter.duration }}</text>
  67. </view>
  68. </view>
  69. </view>
  70. <view class="empty-chapters" v-if="!isLoading && chapters.length === 0">
  71. <text class="empty-text">暂无章节</text>
  72. </view>
  73. </view>
  74. <!-- 评论内容 -->
  75. <view class="content-section" v-if="currentTab === 'comments'">
  76. <view class="comment-list">
  77. <view class="comment-item" v-for="(comment, index) in comments" :key="index">
  78. <image class="comment-avatar" :src="comment.avatar" mode="aspectFill"></image>
  79. <view class="comment-content">
  80. <view class="comment-header">
  81. <text class="comment-name">{{ comment.name }}</text>
  82. <text class="comment-date">{{ comment.date }}</text>
  83. </view>
  84. <text class="comment-text">{{ comment.content }}</text>
  85. </view>
  86. </view>
  87. </view>
  88. <view class="no-comments" v-if="comments.length === 0">
  89. <text class="no-comments-text">暂无评论</text>
  90. </view>
  91. </view>
  92. </scroll-view>
  93. </view>
  94. </template>
  95. <script>
  96. import { getAudiobookDetail, recordListeningHistory, addAudiobookToBookshelf, checkAudiobookInBookshelf, removeAudiobookFromBookshelf } from '../../utils/api.js'
  97. export default {
  98. data() {
  99. return {
  100. currentTab: 'catalog',
  101. audiobookId: null,
  102. bookInfo: {
  103. id: null,
  104. title: '',
  105. desc: '',
  106. brief: '',
  107. introduction: '',
  108. author: '',
  109. image: '',
  110. cover: '',
  111. narrator: ''
  112. },
  113. chapters: [],
  114. comments: [],
  115. isLoading: false,
  116. userInfo: null,
  117. isInBookshelf: false,
  118. checkingBookshelf: false
  119. }
  120. },
  121. onLoad(options) {
  122. // 从路由参数获取audiobookId
  123. if (options.audiobookId) {
  124. this.audiobookId = parseInt(options.audiobookId)
  125. } else if (options.bookId) {
  126. // 兼容旧的bookId参数
  127. this.audiobookId = parseInt(options.bookId)
  128. }
  129. // 加载用户信息
  130. this.loadUserInfo()
  131. // 加载听书详情(从数据库获取完整数据)
  132. if (this.audiobookId) {
  133. this.loadAudiobookDetail()
  134. } else {
  135. uni.showToast({
  136. title: '听书ID不能为空',
  137. icon: 'none'
  138. })
  139. }
  140. },
  141. onShow() {
  142. // 页面显示时重新加载用户信息和书架状态
  143. this.loadUserInfo()
  144. if (this.audiobookId && this.userInfo && this.userInfo.id) {
  145. this.checkBookshelfStatus()
  146. }
  147. },
  148. methods: {
  149. loadUserInfo() {
  150. try {
  151. const userInfo = uni.getStorageSync('userInfo')
  152. const isLogin = uni.getStorageSync('isLogin')
  153. if (userInfo && userInfo.id && isLogin) {
  154. this.userInfo = userInfo
  155. }
  156. } catch (e) {
  157. console.error('获取用户信息失败:', e)
  158. }
  159. },
  160. async loadAudiobookDetail() {
  161. try {
  162. this.isLoading = true
  163. const userId = this.userInfo ? this.userInfo.id : null
  164. const res = await getAudiobookDetail(this.audiobookId, userId)
  165. if (res && res.code === 200 && res.data) {
  166. const data = res.data
  167. // 更新听书信息
  168. if (data.audiobook) {
  169. const audiobook = data.audiobook
  170. this.bookInfo = {
  171. id: audiobook.id,
  172. title: audiobook.title || '',
  173. desc: audiobook.desc || audiobook.brief || '',
  174. brief: audiobook.brief || audiobook.desc || '',
  175. introduction: audiobook.introduction || audiobook.desc || audiobook.brief || '',
  176. author: audiobook.author || '',
  177. image: audiobook.image || audiobook.cover || '',
  178. cover: audiobook.cover || audiobook.image || '',
  179. narrator: audiobook.narrator || ''
  180. }
  181. // 如果描述为空,使用简介作为描述
  182. if (!this.bookInfo.desc && this.bookInfo.brief) {
  183. this.bookInfo.desc = this.bookInfo.brief
  184. }
  185. // 如果简介为空,使用描述作为简介
  186. if (!this.bookInfo.brief && this.bookInfo.desc) {
  187. this.bookInfo.brief = this.bookInfo.desc
  188. }
  189. }
  190. // 更新章节列表
  191. if (data.chapters && Array.isArray(data.chapters)) {
  192. this.chapters = data.chapters.map(ch => ({
  193. id: ch.id,
  194. title: ch.title,
  195. isFree: ch.isFree || false,
  196. duration: ch.durationText || ch.duration || '00:00',
  197. durationSeconds: ch.duration || 0,
  198. audioUrl: ch.audioUrl || ''
  199. }))
  200. }
  201. // 加载完成后检查书架状态
  202. if (this.userInfo && this.userInfo.id) {
  203. this.checkBookshelfStatus()
  204. }
  205. }
  206. } catch (e) {
  207. console.error('加载听书详情失败:', e)
  208. uni.showToast({
  209. title: '加载失败,请重试',
  210. icon: 'none'
  211. })
  212. } finally {
  213. this.isLoading = false
  214. }
  215. },
  216. async checkBookshelfStatus() {
  217. if (!this.userInfo || !this.userInfo.id || !this.audiobookId) {
  218. this.isInBookshelf = false
  219. return
  220. }
  221. if (this.checkingBookshelf) {
  222. return
  223. }
  224. try {
  225. this.checkingBookshelf = true
  226. const res = await checkAudiobookInBookshelf(this.userInfo.id, this.audiobookId)
  227. if (res && res.code === 200) {
  228. this.isInBookshelf = res.data === true
  229. }
  230. } catch (e) {
  231. console.error('检查书架状态失败:', e)
  232. this.isInBookshelf = false
  233. } finally {
  234. this.checkingBookshelf = false
  235. }
  236. },
  237. goBack() {
  238. uni.navigateBack()
  239. },
  240. handleShare() {
  241. uni.showToast({
  242. title: '分享功能',
  243. icon: 'none'
  244. })
  245. },
  246. switchTab(tab) {
  247. this.currentTab = tab
  248. },
  249. playAll() {
  250. const firstFreeChapter = this.chapters.find(ch => ch.isFree)
  251. if (firstFreeChapter) {
  252. const index = this.chapters.indexOf(firstFreeChapter)
  253. this.playChapter(firstFreeChapter, index)
  254. } else {
  255. uni.showModal({
  256. title: '提示',
  257. content: '该书籍需要VIP会员才能播放',
  258. showCancel: true,
  259. cancelText: '取消',
  260. confirmText: '开通VIP',
  261. success: (res) => {
  262. if (res.confirm) {
  263. uni.navigateTo({
  264. url: '/pages/vip/vip'
  265. })
  266. }
  267. }
  268. })
  269. }
  270. },
  271. async addToShelf() {
  272. // 检查用户是否登录
  273. if (!this.userInfo || !this.userInfo.id) {
  274. uni.showModal({
  275. title: '提示',
  276. content: '请先登录',
  277. showCancel: true,
  278. cancelText: '取消',
  279. confirmText: '去登录',
  280. success: (res) => {
  281. if (res.confirm) {
  282. uni.navigateTo({
  283. url: '/pages/login/login'
  284. })
  285. }
  286. }
  287. })
  288. return
  289. }
  290. // 如果已在书架中,则移除
  291. if (this.isInBookshelf) {
  292. try {
  293. const res = await removeAudiobookFromBookshelf(this.userInfo.id, this.audiobookId)
  294. if (res && res.code === 200) {
  295. this.isInBookshelf = false
  296. uni.showToast({
  297. title: '已从书架移除',
  298. icon: 'success'
  299. })
  300. } else {
  301. uni.showToast({
  302. title: res.msg || '移除失败',
  303. icon: 'none'
  304. })
  305. }
  306. } catch (e) {
  307. console.error('移除书架失败:', e)
  308. uni.showToast({
  309. title: '移除失败,请重试',
  310. icon: 'none'
  311. })
  312. }
  313. return
  314. }
  315. // 添加到书架
  316. try {
  317. const res = await addAudiobookToBookshelf(this.userInfo.id, this.audiobookId)
  318. if (res && res.code === 200) {
  319. this.isInBookshelf = true
  320. uni.showToast({
  321. title: '已加入书架',
  322. icon: 'success'
  323. })
  324. } else {
  325. uni.showToast({
  326. title: res.msg || '加入失败',
  327. icon: 'none'
  328. })
  329. }
  330. } catch (e) {
  331. console.error('加入书架失败:', e)
  332. if (e.message && e.message.includes('已在书架中')) {
  333. this.isInBookshelf = true
  334. uni.showToast({
  335. title: '已在书架中',
  336. icon: 'none'
  337. })
  338. } else {
  339. uni.showToast({
  340. title: '加入失败,请重试',
  341. icon: 'none'
  342. })
  343. }
  344. }
  345. },
  346. async playChapter(chapter, index) {
  347. if (!chapter.isFree) {
  348. // 检查用户VIP状态
  349. if (!this.userInfo || !this.userInfo.isVip) {
  350. uni.showModal({
  351. title: '提示',
  352. content: '该章节需要VIP会员才能播放',
  353. showCancel: true,
  354. cancelText: '取消',
  355. confirmText: '开通VIP',
  356. success: (res) => {
  357. if (res.confirm) {
  358. uni.navigateTo({
  359. url: '/pages/vip/vip'
  360. })
  361. }
  362. }
  363. })
  364. return
  365. }
  366. }
  367. // 记录听书历史
  368. if (this.userInfo && this.userInfo.id) {
  369. try {
  370. await recordListeningHistory(this.userInfo.id, this.audiobookId, chapter.id)
  371. } catch (e) {
  372. console.error('记录听书历史失败:', e)
  373. }
  374. }
  375. // 跳转到播放页面
  376. uni.navigateTo({
  377. url: `/pages/player/player?audiobookId=${this.audiobookId}&title=${encodeURIComponent(this.bookInfo.title)}&image=${encodeURIComponent(this.bookInfo.image)}&author=${encodeURIComponent(this.bookInfo.author)}&chapterId=${chapter.id}&chapterTitle=${encodeURIComponent(chapter.title)}&audioUrl=${encodeURIComponent(chapter.audioUrl || '')}`
  378. })
  379. }
  380. }
  381. }
  382. </script>
  383. <style scoped>
  384. .container {
  385. width: 100%;
  386. height: 100vh;
  387. background-color: #FFFFFF;
  388. display: flex;
  389. flex-direction: column;
  390. padding-top: 30px;
  391. box-sizing: border-box;
  392. }
  393. .header {
  394. display: flex;
  395. align-items: center;
  396. justify-content: space-between;
  397. padding: 20rpx 30rpx;
  398. background-color: #FFFFFF;
  399. border-bottom: 1rpx solid #E0E0E0;
  400. }
  401. .back-btn {
  402. width: 60rpx;
  403. height: 60rpx;
  404. display: flex;
  405. align-items: center;
  406. justify-content: center;
  407. }
  408. .back-icon {
  409. font-size: 40rpx;
  410. color: #333333;
  411. font-weight: bold;
  412. }
  413. .share-btn {
  414. width: 60rpx;
  415. height: 60rpx;
  416. display: flex;
  417. align-items: center;
  418. justify-content: center;
  419. }
  420. .share-icon {
  421. font-size: 36rpx;
  422. color: #333333;
  423. }
  424. .scroll-content {
  425. flex: 1;
  426. width: 100%;
  427. }
  428. .book-info-section {
  429. display: flex;
  430. padding: 40rpx 30rpx;
  431. background-color: #FFFFFF;
  432. border-bottom: 1rpx solid #F0F0F0;
  433. }
  434. .book-cover {
  435. width: 200rpx;
  436. height: 280rpx;
  437. border-radius: 8rpx;
  438. margin-right: 30rpx;
  439. flex-shrink: 0;
  440. box-shadow: 0 4rpx 12rpx rgba(0,0,0,0.15);
  441. background-color: #F5F5F5;
  442. }
  443. .book-details {
  444. flex: 1;
  445. display: flex;
  446. flex-direction: column;
  447. justify-content: flex-start;
  448. min-width: 0;
  449. }
  450. .book-title {
  451. font-size: 36rpx;
  452. font-weight: bold;
  453. color: #333333;
  454. margin-bottom: 20rpx;
  455. line-height: 1.4;
  456. }
  457. .book-desc {
  458. font-size: 26rpx;
  459. color: #666666;
  460. line-height: 1.6;
  461. margin-bottom: 20rpx;
  462. }
  463. .book-author {
  464. font-size: 24rpx;
  465. color: #999999;
  466. margin-bottom: 15rpx;
  467. }
  468. .book-narrator {
  469. font-size: 24rpx;
  470. color: #999999;
  471. margin-bottom: 30rpx;
  472. }
  473. .loading-section {
  474. padding: 100rpx 30rpx;
  475. text-align: center;
  476. }
  477. .loading-text {
  478. font-size: 28rpx;
  479. color: #999999;
  480. }
  481. .empty-chapters {
  482. padding: 100rpx 30rpx;
  483. text-align: center;
  484. }
  485. .empty-text {
  486. font-size: 28rpx;
  487. color: #999999;
  488. }
  489. .action-buttons {
  490. display: flex;
  491. gap: 20rpx;
  492. }
  493. .play-all-btn {
  494. flex: 1;
  495. height: 80rpx;
  496. background-color: #4FC3F7;
  497. color: #FFFFFF;
  498. font-size: 30rpx;
  499. border: none;
  500. border-radius: 40rpx;
  501. display: flex;
  502. align-items: center;
  503. justify-content: center;
  504. }
  505. .play-all-btn::after {
  506. border: none;
  507. }
  508. .add-to-shelf-btn {
  509. flex: 1;
  510. height: 80rpx;
  511. background-color: #FFFFFF;
  512. color: #666666;
  513. font-size: 30rpx;
  514. border: 1rpx solid #E0E0E0;
  515. border-radius: 40rpx;
  516. display: flex;
  517. align-items: center;
  518. justify-content: center;
  519. }
  520. .add-to-shelf-btn::after {
  521. border: none;
  522. }
  523. .add-to-shelf-btn.in-bookshelf {
  524. background-color: #F5F5F5;
  525. color: #999999;
  526. border-color: #E0E0E0;
  527. }
  528. .tabs {
  529. display: flex;
  530. background-color: #FFFFFF;
  531. border-bottom: 1rpx solid #E0E0E0;
  532. padding: 0 30rpx;
  533. }
  534. .tab-item {
  535. flex: 1;
  536. display: flex;
  537. align-items: center;
  538. justify-content: center;
  539. padding: 30rpx 0;
  540. position: relative;
  541. }
  542. .tab-item.active .tab-text {
  543. color: #4FC3F7;
  544. font-weight: bold;
  545. }
  546. .tab-item.active::after {
  547. content: '';
  548. position: absolute;
  549. bottom: 0;
  550. left: 50%;
  551. transform: translateX(-50%);
  552. width: 60rpx;
  553. height: 4rpx;
  554. background-color: #4FC3F7;
  555. border-radius: 2rpx;
  556. }
  557. .tab-text {
  558. font-size: 32rpx;
  559. color: #999999;
  560. }
  561. .content-section {
  562. background-color: #FFFFFF;
  563. padding: 0 30rpx;
  564. }
  565. .chapter-item {
  566. display: flex;
  567. align-items: center;
  568. padding: 30rpx 0;
  569. border-bottom: 1rpx solid #F0F0F0;
  570. }
  571. .chapter-item:last-child {
  572. border-bottom: none;
  573. }
  574. .chapter-number {
  575. font-size: 28rpx;
  576. color: #999999;
  577. margin-right: 20rpx;
  578. width: 40rpx;
  579. flex-shrink: 0;
  580. }
  581. .chapter-info {
  582. flex: 1;
  583. display: flex;
  584. flex-direction: column;
  585. min-width: 0;
  586. }
  587. .chapter-title {
  588. font-size: 30rpx;
  589. color: #333333;
  590. margin-bottom: 15rpx;
  591. overflow: hidden;
  592. text-overflow: ellipsis;
  593. white-space: nowrap;
  594. }
  595. .chapter-meta {
  596. display: flex;
  597. align-items: center;
  598. gap: 15rpx;
  599. }
  600. .chapter-status {
  601. background-color: #4FC3F7;
  602. padding: 4rpx 12rpx;
  603. border-radius: 12rpx;
  604. }
  605. .status-text {
  606. font-size: 20rpx;
  607. color: #FFFFFF;
  608. }
  609. .chapter-lock {
  610. font-size: 24rpx;
  611. color: #999999;
  612. }
  613. .chapter-duration {
  614. font-size: 24rpx;
  615. color: #999999;
  616. }
  617. .comment-list {
  618. display: flex;
  619. flex-direction: column;
  620. padding: 30rpx 0;
  621. }
  622. .comment-item {
  623. display: flex;
  624. margin-bottom: 40rpx;
  625. padding-bottom: 40rpx;
  626. border-bottom: 1rpx solid #F0F0F0;
  627. }
  628. .comment-item:last-child {
  629. border-bottom: none;
  630. padding-bottom: 0;
  631. margin-bottom: 0;
  632. }
  633. .comment-avatar {
  634. width: 80rpx;
  635. height: 80rpx;
  636. border-radius: 50%;
  637. margin-right: 20rpx;
  638. flex-shrink: 0;
  639. background-color: #F5F5F5;
  640. }
  641. .comment-content {
  642. flex: 1;
  643. display: flex;
  644. flex-direction: column;
  645. min-width: 0;
  646. }
  647. .comment-header {
  648. display: flex;
  649. justify-content: space-between;
  650. align-items: center;
  651. margin-bottom: 15rpx;
  652. }
  653. .comment-name {
  654. font-size: 28rpx;
  655. color: #333333;
  656. font-weight: bold;
  657. }
  658. .comment-date {
  659. font-size: 24rpx;
  660. color: #999999;
  661. }
  662. .comment-text {
  663. font-size: 28rpx;
  664. color: #666666;
  665. line-height: 1.6;
  666. }
  667. .no-comments {
  668. padding: 100rpx 0;
  669. text-align: center;
  670. }
  671. .no-comments-text {
  672. font-size: 28rpx;
  673. color: #999999;
  674. }
  675. </style>