import { getBookshelfList, getBrowsingHistory } from '../../api/api' import { goToBookDetail, SendEvent } from '../../utils/util' import { apple } from '../../config/config' let interstitialAd = null let app = getApp() Page({ data: { activeTab: 'history', bookList: [], loading: false, hasMore: true, page: 1, size: 10, isLoggedIn: false }, onLoad() { this.autoLogin() }, onShow() { // 每次显示页面时刷新列表 this.setData({ page: 1, bookList: [] }) if (this.data.isLoggedIn) { this.loadBookList() } }, onTabItemTap(item) { console.log(item.index) setTimeout(() => { this.interstitalPlayFn() }, 1000) }, // 自动登录 async autoLogin() { try { // 检查是否已经登录 const token = wx.getStorageSync('accessToken'); if (token) { this.setData({ isLoggedIn: true }); // 已经登录 console.log('已经登录') // 页面首次加载时的处理 this.loadBookList() this.interstitalLoad() return; } // 未登录,执行登录 const res = await getApp().login(); this.setData({ isLoggedIn: true }); console.log('重新登录', res) // 页面首次加载时的处理 if (res.accessToken) { this.loadBookList(); } this.interstitalLoad(); } catch (error) { console.error('登录失败:', error); } }, // 插屏广告加载 interstitalLoad() { let adId = apple.ads.insertAdId interstitialAd = wx.createInterstitialAd({ adUnitId: adId }) interstitialAd.onLoad(() => { console.log('视频页插屏广告onLoad') }) interstitialAd.onError((err) => { console.log('视频页插屏广告onError', err) }) interstitialAd.onClose(() => { console.log('视频页插屏广告onClose') }) }, // 插屏广告展示 interstitalPlayFn() { if (interstitialAd) { interstitialAd.show().catch((err) => { console.error(err) }) } }, // 切换标签 switchTab(e) { const tab = e.currentTarget.dataset.tab this.setData({ activeTab: tab, page: 1, bookList: [] }) this.loadBookList() }, // 加载书架列表 async loadBookList() { if (this.data.loading) return this.setData({ loading: true }) try { let result = []; // 根据当前激活的标签页加载不同的数据 if (this.data.activeTab === 'history') { // 加载阅读历史记录 result = await getBrowsingHistory(); } else { // 加载书架列表 result = await getBookshelfList(); } if (Array.isArray(result)) { this.setData({ bookList: result, loading: false, hasMore: false // 目前接口不支持分页,所以直接设置为false }) } else { this.setData({ loading: false, hasMore: false }) } } catch (error) { console.error('加载数据失败:', error) this.setData({ loading: false, hasMore: false }) wx.showToast({ title: '加载失败,请重试', icon: 'none' }) } }, // 加载更多 loadMore() { if (this.data.hasMore) { this.loadBookList() } }, // 跳转到书籍详情 goToBookDetail(e) { const book = e.currentTarget.dataset.book; console.log(book) SendEvent('click_shelf_novel', { "video_id": book.novelId, "wx_book_id": book.wxBookId, "chapter_no": book.novelChapterId, "uuid": app.globalData.uuid }); // 使用公共的goToBookDetail函数 goToBookDetail({ bookId: book.novelId, wxBookId: book.wxBookId, chapterId: book.novelChapterId }); } })