Browse Source

优化首页数据加载逻辑,重构性别状态管理,新增性别初始化和更新检查功能,改进数据加载方式,提升用户体验。

yangwenlan 1 năm trước cách đây
mục cha
commit
1adc18446f
1 tập tin đã thay đổi với 64 bổ sung46 xóa
  1. 64 46
      pages/index/index.js

+ 64 - 46
pages/index/index.js

@@ -48,14 +48,17 @@ Page({
   },
 
   onShow() {
+    // 每次页面显示时检查是否需要更新数据
     if (this.data.isLoggedIn) {
-      this.syncGenderState();
+      this.checkAndUpdateGender();
     }
   },
 
   onTabItemTap() {
-    // tabBar 点击时同步性别状态
-    this.syncGenderState();
+    // tabBar 点击时检查是否需要更新数据
+    if (this.data.isLoggedIn) {
+      this.checkAndUpdateGender();
+    }
   },
 
   // 自动登录
@@ -65,34 +68,53 @@ Page({
       const token = wx.getStorageSync('accessToken');
       if (token) {
         this.setData({ isLoggedIn: true });
-        this.syncGenderState();
+        // 获取性别设置并加载数据
+        this.initGenderAndLoadData();
         return;
       }
-
+      
+      // 未登录,执行登录
       const res = await getApp().login();
       this.setData({ isLoggedIn: true });
-      this.syncGenderState();
+      // 登录成功后获取性别设置并加载数据
+      this.initGenderAndLoadData();
+      
     } catch (error) {
       console.error('登录失败:', error);
+      // 登录失败也尝试加载数据,使用默认性别
+      this.initGenderAndLoadData();
     }
   },
 
-  // 同步性别状态
-  async syncGenderState() {
+  // 初始化性别设置并加载数据
+  initGenderAndLoadData() {
+    // 获取保存的性别设置,如果没有则使用默认值
     const gender = wx.getStorageSync('gender') || 'male';
-    if (gender !== this.data.gender) {
-      this.setData({ gender });
-      await this.loadData();
+    this.setData({ gender });
+    // 加载所有数据
+    this.loadAllData();
+  },
+
+  // 检查并更新性别,如果有变化则重新加载数据
+  checkAndUpdateGender() {
+    const savedGender = wx.getStorageSync('gender') || 'male';
+    if (savedGender !== this.data.gender) {
+      this.setData({ gender: savedGender });
+      this.loadAllData();
     }
   },
 
-  // 加载数据
-  async loadData() {
-    // 从本地存储读取性别设置
-    this.syncGenderState();
-    // 获取banner数据 channel 频道 1 男 2 女
-    this.getBannerList();
-    this.getCardNovels();
+  // 加载所有数据
+  loadAllData() {
+    wx.showLoading({ title: '加载中...' });
+    
+    // 并行请求数据
+    Promise.all([
+      this.getBannerList(),
+      this.getCardNovels()
+    ]).finally(() => {
+      wx.hideLoading();
+    });
   },
   
   // 切换性别分类
@@ -105,22 +127,10 @@ Page({
     
     // 保存性别设置到本地存储
     wx.setStorageSync('gender', gender);
-    
     this.setData({ gender });
-    // 重新加载数据
-    this.loadData();
-  },
-  
-  // 加载性别相关数据
-  loadGenderData: function(gender) {
-    wx.showLoading({
-      title: '加载中...'
-    });
     
-    // 模拟加载数据
-    setTimeout(() => {
-      wx.hideLoading();
-    }, 500);
+    // 重新加载数据
+    this.loadAllData();
   },
   
   // 跳转到搜索页面
@@ -219,26 +229,27 @@ Page({
   // 获取banner列表
   async getBannerList() {
     try {
-      const bannerList = await getBannerList(this.data.gender === 'male' ? 1 : 2);
-      this.setData({
-        bannerList
-      });
+      const channel = this.data.gender === 'male' ? 1 : 2;
+      const bannerList = await getBannerList(channel);
+      this.setData({ bannerList });
+      return bannerList;
     } catch (error) {
       console.error('获取banner失败:', error);
       wx.showToast({
         title: '获取banner失败',
         icon: 'none'
       });
+      return [];
     }
   },
 
   async getCardNovels() {
     try {
-      const cardNovels = await getCardNovels(this.data.gender === 'male' ? 1 : 2);
-      this.setData({
-        cardNovels:cardNovels
-      })
-      // 默认使用第一组数据作为主编推荐
+      const channel = this.data.gender === 'male' ? 1 : 2;
+      const cardNovels = await getCardNovels(channel);
+      this.setData({ cardNovels });
+      
+      // 处理主编推荐数据
       if (cardNovels[0]) {
         const [firstBook, ...restBooks] = cardNovels[0].novelList;
         this.setData({
@@ -262,7 +273,7 @@ Page({
         });
       }
       
-      // 第二组数据作为全网热推
+      // 处理全网热推数据
       if (cardNovels[1]) {
         this.setData({
           hotBooks: cardNovels[1].novelList.map(novel => ({
@@ -278,27 +289,34 @@ Page({
         });
       }
 
-      // 第三组数据作为强力推荐
+      // 处理强力推荐数据
       if (cardNovels[2] && cardNovels[2].novelList.length > 0) {
         this.setData({
           strongBooks: cardNovels[2].novelList.slice(0, 4).map(novel => ({
             id: novel.id,
             title: novel.title,
-            cover: novel.cover
-          }))
+            cover: novel.cover,
+            wxBookId: novel.wxBookId,
+            chapterId: novel.chapterId
+          })),
+          showStrongRecommend: true
         });
       } else {
         // 如果没有第三组数据,隐藏强力推荐模块
         this.setData({
-          strongBooks: []
+          strongBooks: [],
+          showStrongRecommend: false
         });
       }
+      
+      return cardNovels;
     } catch (error) {
       console.error('获取卡片小说失败:', error);
       wx.showToast({
         title: '获取卡片小说失败',
         icon: 'none'
       });
+      return [];
     }
   },