import { getCategoryList, getNovelSearch } from '../../api/api' import { goToBookDetail, SendEvent } from '../../utils/util' import { apple } from '../../config/config' let interstitialAd = null let app = getApp() Page({ data: { gender: 'male', // 当前性别选择 currentCategory: '', // 当前选中的分类 categories: [], bookList: [], loading: false, keyword: '', page: 1, size: 10, hasMore: true, // 是否还有更多数据, isLoggedIn: false, //是否登录 }, onLoad: function (options) { this.autoLogin(); }, onShow: function () { if (this.data.isLoggedIn) { this.syncGenderState(); } }, onTabItemTap: function (item) { console.log(item.index) setTimeout(() => { this.interstitalPlayFn() }, 1000) if (this.data.isLoggedIn) { this.syncGenderState(); } }, // 自动登录 async autoLogin() { try { // 检查是否已经登录 const token = wx.getStorageSync('accessToken'); if (token) { this.setData({ isLoggedIn: true }); // 已经登录 console.log('已经登录') // 页面首次加载时的处理 this.syncGenderState(); this.interstitalLoad(); return; } // 未登录,执行登录 getApp().login().then(res => { this.setData({ isLoggedIn: true }); this.syncGenderState(); this.interstitalLoad(); console.log('重新登录', res) }) } 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) }) } }, // 同步性别状态 syncGenderState: async function () { const gender = wx.getStorageSync('gender') || 'male'; if (gender !== this.data.gender) { this.setData({ gender }); await this.loadCategories(); } else { await this.loadCategories(); } }, // 切换性别 switchGender: async function (e) { const { gender } = e.detail; this.setData({ gender: gender, currentCategory: '', bookList: [], page: 1, hasMore: true }); SendEvent('click_classify_type', {"gender": gender, "uuid": app.globalData.uuid}); if (this.data.isLoggedIn) { await this.loadCategories(); } }, // 加载分类列表 async loadCategories() { try { const channel = this.data.gender === 'male' ? "man" : "woman"; const result = await getCategoryList(channel); if (result && Array.isArray(result)) { this.setData({ categories: result, currentCategory: result[0]?.code || '', page: 1, hasMore: true }); // 加载第一个分类的书籍 if (result[0]?.code) { this.loadBookList(result[0].code); } } } catch (error) { console.error('加载分类失败:', error); wx.showToast({ title: '加载分类失败', icon: 'none' }); } }, // 切换分类 switchCategory: function (e) { const category = e.currentTarget.dataset.category; this.setData({ currentCategory: category, bookList: [], page: 1, hasMore: true }); SendEvent('click_classify_name', {"category": category, "uuid": app.globalData.uuid}); this.loadBookList(category); }, // 加载书籍列表 async loadBookList(category) { if (this.data.loading) return; // 只检查loading状态,移除hasMore检查 this.setData({ loading: true }); try { const params = { keyword: this.data.keyword, page: this.data.page, size: this.data.size, category: category }; const result = await getNovelSearch(params); if (result && result.records) { const newList = this.data.page === 1 ? result.records : [...this.data.bookList, ...result.records]; const hasMore = !result.last; this.setData({ bookList: newList, loading: false, hasMore: hasMore, page: hasMore ? this.data.page + 1 : this.data.page }); } else { this.setData({ loading: false, hasMore: false }); } } catch (error) { console.error('加载书籍列表失败:', error); this.setData({ loading: false, hasMore: false }); wx.showToast({ title: '加载失败,请重试', icon: 'none' }); } }, // 监听滚动到底部 onScrollToLower: function () { if (this.data.currentCategory && this.data.hasMore) { this.loadBookList(this.data.currentCategory); } }, // 跳转到书籍详情 handleBookTap: function (e) { const { id, wxbookid } = e.currentTarget.dataset; SendEvent('click_classfy_novel', {"novel_id": id, "wx_book_id": wxbookid, "uuid": app.globalData.uuid}) goToBookDetail({ bookId: id, wxBookId: wxbookid }); }, // 跳转到搜索页面 goToSearch: function (e) { // 如果有输入内容,则带上关键词 const keyword = e.detail?.value || ''; wx.navigateTo({ url: `/pages/search/search?keyword=${encodeURIComponent(keyword)}` }); } })