| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- 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 {
- // 检查是否已经登录
- if (app.globalData.accessToken) {
- 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)}`,
- });
- },
- });
|