| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- // 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);
- }
- });
|