| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- // pages/bookshelf/bookshelf.js
- const bookshelfApi = require('../../api/bookshelf');
- const contentApi = require('../../api/content');
- const userUtil = require('../../utils/user');
- Page({
- data: {
- bookshelf: [],
- contentList: [],
- loading: false
- },
- onLoad() {
- this.loadBookshelf();
- },
- onShow() {
- this.loadBookshelf();
- },
- // 加载书架
- async loadBookshelf() {
- if (!userUtil.isLogin()) {
- this.setData({ bookshelf: [], contentList: [] });
- return;
- }
- this.setData({ loading: true });
- try {
- const userInfo = userUtil.getUserInfo();
- const bookshelf = await bookshelfApi.getBookshelfList(userInfo.userId);
-
- // 获取书籍详情
- const contentList = [];
- for (const item of bookshelf) {
- try {
- const content = await contentApi.getContentDetail(item.contentId);
- contentList.push({
- ...content,
- addTime: item.addTime,
- lastAccessTime: item.lastAccessTime
- });
- } catch (error) {
- console.error('加载书籍详情失败:', error);
- }
- }
-
- this.setData({
- bookshelf,
- contentList
- });
-
- // 同时保存到本地
- wx.setStorageSync('bookshelf', contentList);
- } catch (error) {
- console.error('加载书架失败:', error);
- // 失败时从本地加载
- const localBookshelf = wx.getStorageSync('bookshelf') || [];
- this.setData({ contentList: localBookshelf });
- } finally {
- this.setData({ loading: false });
- }
- },
- // 跳转到详情
- goToDetail(e) {
- const contentId = e.currentTarget.dataset.id;
- wx.navigateTo({
- url: `/pages/detail/detail?id=${contentId}`
- });
- }
- });
|