|
|
@@ -1,27 +1,16 @@
|
|
|
+import { getCategoryList, getNovelSearch } from '../../api/api'
|
|
|
+
|
|
|
Page({
|
|
|
data: {
|
|
|
gender: 'male', // 当前性别选择
|
|
|
- currentCategory: 'qingchun', // 当前选中的分类
|
|
|
- bookList: [
|
|
|
- {
|
|
|
- id: 1,
|
|
|
- title: '狂龙圣医',
|
|
|
- description: '大夏国谪仙少年苏浩,意外封印三魂六魄,一朝觉醒,如若龙升天。一手保命天医术,...',
|
|
|
- coverUrl: '/images/book1.jpg'
|
|
|
- },
|
|
|
- {
|
|
|
- id: 2,
|
|
|
- title: '被逼后,她成了豪门宠',
|
|
|
- description: '某诡异豪门家族的葬礼中,她从漆黑带回一个女子,听说那女子手段不凡,穿...',
|
|
|
- coverUrl: '/images/book2.jpg'
|
|
|
- },
|
|
|
- {
|
|
|
- id: 3,
|
|
|
- title: '恢复记忆后,她回归豪门找前夫',
|
|
|
- description: '江浅从人一朝落难,天之骄女跌落泥泞,传言预知生死,她以倾城之姿...',
|
|
|
- coverUrl: '/images/book3.jpg'
|
|
|
- }
|
|
|
- ]
|
|
|
+ currentCategory: '', // 当前选中的分类
|
|
|
+ categories: [],
|
|
|
+ bookList: [],
|
|
|
+ loading: false,
|
|
|
+ keyword: '',
|
|
|
+ page: 1,
|
|
|
+ size: 10,
|
|
|
+ hasMore: true // 是否还有更多数据
|
|
|
},
|
|
|
|
|
|
onLoad: function(options) {
|
|
|
@@ -40,45 +29,116 @@ Page({
|
|
|
},
|
|
|
|
|
|
// 同步性别状态
|
|
|
- syncGenderState: function() {
|
|
|
+ syncGenderState: async function() {
|
|
|
const gender = wx.getStorageSync('gender') || 'male';
|
|
|
if (gender !== this.data.gender) {
|
|
|
this.setData({ gender });
|
|
|
+ await this.loadCategories();
|
|
|
}
|
|
|
},
|
|
|
|
|
|
// 切换性别
|
|
|
- switchGender: function(e) {
|
|
|
+ switchGender: async function(e) {
|
|
|
const { gender } = e.detail;
|
|
|
this.setData({
|
|
|
gender: gender,
|
|
|
- currentCategory: 'qingchun' // 切换性别时重置分类
|
|
|
+ currentCategory: '',
|
|
|
+ bookList: [],
|
|
|
+ page: 1,
|
|
|
+ hasMore: true
|
|
|
});
|
|
|
- // 重新加载分类和书籍列表
|
|
|
- this.loadBookList('qingchun');
|
|
|
+ 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
|
|
|
+ currentCategory: category,
|
|
|
+ bookList: [],
|
|
|
+ page: 1,
|
|
|
+ hasMore: true
|
|
|
});
|
|
|
- // 加载对应分类的书籍列表
|
|
|
this.loadBookList(category);
|
|
|
},
|
|
|
|
|
|
// 加载书籍列表
|
|
|
- loadBookList: function(category) {
|
|
|
- wx.showLoading({
|
|
|
- title: '加载中...'
|
|
|
- });
|
|
|
-
|
|
|
- // 模拟接口调用
|
|
|
- setTimeout(() => {
|
|
|
- wx.hideLoading();
|
|
|
- // 实际开发中这里应该调用真实的接口,需要传入 gender 和 category 参数
|
|
|
- // this.requestBookList(this.data.gender, category);
|
|
|
- }, 500);
|
|
|
+ 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,
|
|
|
+ isOver: 0
|
|
|
+ };
|
|
|
+
|
|
|
+ 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);
|
|
|
+ }
|
|
|
}
|
|
|
})
|