// pages/detail/detail.js const contentApi = require('../../api/content'); const chapterApi = require('../../api/chapter'); const bookshelfApi = require('../../api/bookshelf'); const userUtil = require('../../utils/user'); Page({ data: { contentId: null, content: null, chapters: [], loading: true, contentType: null, inBookshelf: false, showChapterList: false }, onLoad(options) { const contentId = parseInt(options.id); if (!contentId) { wx.showToast({ title: '参数错误', icon: 'none' }); setTimeout(() => { wx.navigateBack(); }, 1500); return; } this.setData({ contentId }); this.loadContentDetail(); }, // 加载内容详情 async loadContentDetail() { try { const content = await contentApi.getContentDetail(this.data.contentId); this.setData({ content, contentType: content.contentType }); wx.setNavigationBarTitle({ title: content.title }); // 增加浏览量 try { const viewData = await contentApi.increaseViewCount(this.data.contentId); if (viewData && typeof viewData.viewCount === 'number') { this.setData({ 'content.viewCount': viewData.viewCount }); } } catch (error) { console.error('更新浏览量失败:', error); } // 加载详情后,再加载章节和检查书架状态 this.loadChapters(); this.checkBookshelfStatus(); } catch (error) { wx.showToast({ title: error || '加载失败', icon: 'none' }); } finally { this.setData({ loading: false }); } }, // 加载章节列表 async loadChapters() { try { let chapters = []; if (this.data.contentType === 1) { // 电子书章节 chapters = await chapterApi.getBookChapterList(this.data.contentId); } else if (this.data.contentType === 2) { // 听书章节 chapters = await chapterApi.getAudioChapterList(this.data.contentId); } this.setData({ chapters }); } catch (error) { console.error('加载章节失败:', error); this.setData({ chapters: [] }); } }, // 检查书架状态 async checkBookshelfStatus() { if (!userUtil.isLogin()) return; try { const userInfo = userUtil.getUserInfo(); const isIn = await bookshelfApi.checkInBookshelf(userInfo.userId, this.data.contentId); this.setData({ inBookshelf: isIn }); } catch (error) { console.error('检查书架状态失败:', error); } }, // 加入/移出书架 async toggleBookshelf() { if (!userUtil.isLogin()) { wx.showToast({ title: '请先登录', icon: 'none' }); setTimeout(() => { wx.reLaunch({ url: '/pages/login/login' }); }, 1500); return; } try { const userInfo = userUtil.getUserInfo(); const { contentId, inBookshelf } = this.data; if (inBookshelf) { // 移出书架 await bookshelfApi.removeFromBookshelf(userInfo.userId, contentId); this.setData({ inBookshelf: false }); wx.showToast({ title: '已移出书架', icon: 'success' }); } else { // 加入书架 await bookshelfApi.addToBookshelf(userInfo.userId, contentId); this.setData({ inBookshelf: true }); wx.showToast({ title: '已加入书架', icon: 'success' }); } } catch (error) { wx.showToast({ title: error || '操作失败', icon: 'none' }); } }, // 开始阅读/听书 startRead() { if (!userUtil.isLogin()) { wx.showToast({ title: '请先登录', icon: 'none' }); setTimeout(() => { wx.reLaunch({ url: '/pages/login/login' }); }, 1500); return; } const { contentType, contentId } = this.data; if (contentType === 1) { // 电子书 if (this.data.chapters.length === 0) { wx.showToast({ title: '暂无章节', icon: 'none' }); return; } // 跳转到阅读页,默认第一章 const firstChapter = this.data.chapters[0]; wx.navigateTo({ url: `/pages/read/read?contentId=${contentId}&chapterId=${firstChapter.chapterId}` }); } else if (contentType === 2) { // 听书 if (this.data.chapters.length === 0) { wx.showToast({ title: '暂无章节', icon: 'none' }); return; } // 跳转到听书页,默认第一章 const firstChapter = this.data.chapters[0]; wx.navigateTo({ url: `/pages/audio/audio?contentId=${contentId}&audioId=${firstChapter.audioId}` }); } }, // 显示/隐藏章节列表 toggleChapterList() { this.setData({ showChapterList: !this.data.showChapterList }); }, // 选择章节 selectChapter(e) { const index = e.currentTarget.dataset.index; const chapter = this.data.chapters[index]; const { contentType, contentId } = this.data; this.setData({ showChapterList: false }); if (contentType === 1) { wx.navigateTo({ url: `/pages/read/read?contentId=${contentId}&chapterId=${chapter.chapterId}` }); } else if (contentType === 2) { wx.navigateTo({ url: `/pages/audio/audio?contentId=${contentId}&audioId=${chapter.audioId}` }); } }, // 封面加载错误 onCoverError(e) { console.error('封面加载失败:', e); } });