| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- import { getCategoryList, getNovelSearch } from '../../api/api'
- import { goToBookDetail } from '../../utils/util'
- Page({
- data: {
- gender: 'male', // 当前性别选择
- currentCategory: '', // 当前选中的分类
- categories: [],
- bookList: [],
- loading: false,
- keyword: '',
- page: 1,
- size: 10,
- hasMore: true // 是否还有更多数据
- },
- onLoad: function(options) {
- // 页面首次加载时的处理
- this.syncGenderState();
- },
- onShow: function() {
- // 每次页面显示时同步性别状态
- this.syncGenderState();
- },
- onTabItemTap: function() {
- // tabBar 点击时同步性别状态
- this.syncGenderState();
- },
- // 同步性别状态
- syncGenderState: async function() {
- const gender = wx.getStorageSync('gender') || 'male';
- if (gender !== this.data.gender) {
- this.setData({ gender });
- await this.loadCategories();
- }
- },
- // 切换性别
- switchGender: async function(e) {
- const { gender } = e.detail;
- this.setData({
- gender: gender,
- currentCategory: '',
- bookList: [],
- page: 1,
- hasMore: true
- });
- 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
- });
- 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;
- goToBookDetail({
- bookId: id,
- wxBookId: wxbookid
- });
- },
- // 跳转到搜索页面
- goToSearch: function(e) {
- // 如果有输入内容,则带上关键词
- const keyword = e.detail?.value || '';
- wx.navigateTo({
- url: `/pages/search/search?keyword=${encodeURIComponent(keyword)}`
- });
- }
- })
|