detail.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // pages/detail/detail.js
  2. const contentApi = require('../../api/content');
  3. const chapterApi = require('../../api/chapter');
  4. const bookshelfApi = require('../../api/bookshelf');
  5. const userUtil = require('../../utils/user');
  6. Page({
  7. data: {
  8. contentId: null,
  9. content: null,
  10. chapters: [],
  11. loading: true,
  12. contentType: null,
  13. inBookshelf: false,
  14. showChapterList: false
  15. },
  16. onLoad(options) {
  17. const contentId = parseInt(options.id);
  18. if (!contentId) {
  19. wx.showToast({
  20. title: '参数错误',
  21. icon: 'none'
  22. });
  23. setTimeout(() => {
  24. wx.navigateBack();
  25. }, 1500);
  26. return;
  27. }
  28. this.setData({ contentId });
  29. this.loadContentDetail();
  30. },
  31. // 加载内容详情
  32. async loadContentDetail() {
  33. try {
  34. const content = await contentApi.getContentDetail(this.data.contentId);
  35. this.setData({
  36. content,
  37. contentType: content.contentType
  38. });
  39. wx.setNavigationBarTitle({
  40. title: content.title
  41. });
  42. // 增加浏览量
  43. try {
  44. const viewData = await contentApi.increaseViewCount(this.data.contentId);
  45. if (viewData && typeof viewData.viewCount === 'number') {
  46. this.setData({
  47. 'content.viewCount': viewData.viewCount
  48. });
  49. }
  50. } catch (error) {
  51. console.error('更新浏览量失败:', error);
  52. }
  53. // 加载详情后,再加载章节和检查书架状态
  54. this.loadChapters();
  55. this.checkBookshelfStatus();
  56. } catch (error) {
  57. wx.showToast({
  58. title: error || '加载失败',
  59. icon: 'none'
  60. });
  61. } finally {
  62. this.setData({ loading: false });
  63. }
  64. },
  65. // 加载章节列表
  66. async loadChapters() {
  67. try {
  68. let chapters = [];
  69. if (this.data.contentType === 1) {
  70. // 电子书章节
  71. chapters = await chapterApi.getBookChapterList(this.data.contentId);
  72. } else if (this.data.contentType === 2) {
  73. // 听书章节
  74. chapters = await chapterApi.getAudioChapterList(this.data.contentId);
  75. }
  76. this.setData({ chapters });
  77. } catch (error) {
  78. console.error('加载章节失败:', error);
  79. this.setData({ chapters: [] });
  80. }
  81. },
  82. // 检查书架状态
  83. async checkBookshelfStatus() {
  84. if (!userUtil.isLogin()) return;
  85. try {
  86. const userInfo = userUtil.getUserInfo();
  87. const isIn = await bookshelfApi.checkInBookshelf(userInfo.userId, this.data.contentId);
  88. this.setData({ inBookshelf: isIn });
  89. } catch (error) {
  90. console.error('检查书架状态失败:', error);
  91. }
  92. },
  93. // 加入/移出书架
  94. async toggleBookshelf() {
  95. if (!userUtil.isLogin()) {
  96. wx.showToast({
  97. title: '请先登录',
  98. icon: 'none'
  99. });
  100. setTimeout(() => {
  101. wx.reLaunch({
  102. url: '/pages/login/login'
  103. });
  104. }, 1500);
  105. return;
  106. }
  107. try {
  108. const userInfo = userUtil.getUserInfo();
  109. const { contentId, inBookshelf } = this.data;
  110. if (inBookshelf) {
  111. // 移出书架
  112. await bookshelfApi.removeFromBookshelf(userInfo.userId, contentId);
  113. this.setData({ inBookshelf: false });
  114. wx.showToast({
  115. title: '已移出书架',
  116. icon: 'success'
  117. });
  118. } else {
  119. // 加入书架
  120. await bookshelfApi.addToBookshelf(userInfo.userId, contentId);
  121. this.setData({ inBookshelf: true });
  122. wx.showToast({
  123. title: '已加入书架',
  124. icon: 'success'
  125. });
  126. }
  127. } catch (error) {
  128. wx.showToast({
  129. title: error || '操作失败',
  130. icon: 'none'
  131. });
  132. }
  133. },
  134. // 开始阅读/听书
  135. startRead() {
  136. if (!userUtil.isLogin()) {
  137. wx.showToast({
  138. title: '请先登录',
  139. icon: 'none'
  140. });
  141. setTimeout(() => {
  142. wx.reLaunch({
  143. url: '/pages/login/login'
  144. });
  145. }, 1500);
  146. return;
  147. }
  148. const { contentType, contentId } = this.data;
  149. if (contentType === 1) {
  150. // 电子书
  151. if (this.data.chapters.length === 0) {
  152. wx.showToast({
  153. title: '暂无章节',
  154. icon: 'none'
  155. });
  156. return;
  157. }
  158. // 跳转到阅读页,默认第一章
  159. const firstChapter = this.data.chapters[0];
  160. wx.navigateTo({
  161. url: `/pages/read/read?contentId=${contentId}&chapterId=${firstChapter.chapterId}`
  162. });
  163. } else if (contentType === 2) {
  164. // 听书
  165. if (this.data.chapters.length === 0) {
  166. wx.showToast({
  167. title: '暂无章节',
  168. icon: 'none'
  169. });
  170. return;
  171. }
  172. // 跳转到听书页,默认第一章
  173. const firstChapter = this.data.chapters[0];
  174. wx.navigateTo({
  175. url: `/pages/audio/audio?contentId=${contentId}&audioId=${firstChapter.audioId}`
  176. });
  177. }
  178. },
  179. // 显示/隐藏章节列表
  180. toggleChapterList() {
  181. this.setData({
  182. showChapterList: !this.data.showChapterList
  183. });
  184. },
  185. // 选择章节
  186. selectChapter(e) {
  187. const index = e.currentTarget.dataset.index;
  188. const chapter = this.data.chapters[index];
  189. const { contentType, contentId } = this.data;
  190. this.setData({ showChapterList: false });
  191. if (contentType === 1) {
  192. wx.navigateTo({
  193. url: `/pages/read/read?contentId=${contentId}&chapterId=${chapter.chapterId}`
  194. });
  195. } else if (contentType === 2) {
  196. wx.navigateTo({
  197. url: `/pages/audio/audio?contentId=${contentId}&audioId=${chapter.audioId}`
  198. });
  199. }
  200. },
  201. // 封面加载错误
  202. onCoverError(e) {
  203. console.error('封面加载失败:', e);
  204. }
  205. });