| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- 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
- });
- }
- })
|