Browse Source

fix: 修复bug;

qiansailong 1 year ago
parent
commit
d2e84080ab
4 changed files with 114 additions and 56 deletions
  1. 39 4
      pages/bookshelf/bookshelf.js
  2. 46 48
      pages/category/category.js
  3. 28 3
      pages/mine/mine.js
  4. 1 1
      pages/video/video.js

+ 39 - 4
pages/bookshelf/bookshelf.js

@@ -18,12 +18,12 @@ Page({
     loading: false,
     hasMore: true,
     page: 1,
-    size: 10
+    size: 10,
+    isLoggedIn: false
   },
 
   onLoad() {
-    this.loadBookList()
-    this.interstitalLoad()
+    this.autoLogin()
   },
 
   onShow() {
@@ -32,7 +32,10 @@ Page({
       page: 1,
       bookList: []
     })
-    this.loadBookList()
+
+    if (this.data.isLoggedIn) {
+      this.loadBookList()
+    }
   },
 
   onTabItemTap(item) {
@@ -42,6 +45,38 @@ Page({
     }, 1000)
   },
 
+  // 自动登录
+  async autoLogin() {
+    try {
+      // 检查是否已经登录
+      const token = wx.getStorageSync('accessToken');
+      if (token) {
+        this.setData({
+          isLoggedIn: true
+        });
+        // 已经登录
+        console.log('已经登录')
+        // 页面首次加载时的处理
+        this.loadBookList()
+        this.interstitalLoad()
+        return;
+      }
+      // 未登录,执行登录
+      const res = await getApp().login();
+      this.setData({
+        isLoggedIn: true
+      });
+      console.log('重新登录', res)
+      // 页面首次加载时的处理
+      if (res.accessToken) {
+        this.loadBookList();
+      }
+      this.interstitalLoad();
+    } catch (error) {
+      console.error('登录失败:', error);
+    }
+  },
+
   // 插屏广告加载
   interstitalLoad() {
     let adId = apple.ads.insertAdId

+ 46 - 48
pages/category/category.js

@@ -10,37 +10,29 @@ import {
 } from '../../config/config'
 
 let interstitialAd = null
-const app = getApp()
 
 Page({
     data: {
-        gender: 'male', // 当前性别选择
-        currentCategory: '', // 当前选中的分类
-        categories: [],
-        bookList: [],
-        loading: false,
-        keyword: '',
-        page: 1,
-        size: 10,
-        hasMore: true, // 是否还有更多数据,
-        isLoggedIn: false, //是否登录
+      gender: 'male', // 当前性别选择
+      currentCategory: '', // 当前选中的分类
+      categories: [],
+      bookList: [],
+      loading: false,
+      keyword: '',
+      page: 1,
+      size: 10,
+      hasMore: true, // 是否还有更多数据,
+      isLoggedIn: false, //是否登录
     },
 
     onLoad: function (options) {
-        app.autoLogin();
-        if (app.globalData.isLogin) {
-            setTimeout(() => {
-                console.log('加载分类列表')
-                // 页面首次加载时的处理
-                this.syncGenderState();
-                this.interstitalLoad();
-            }, 1500);
-        }
+        this.autoLogin();
     },
 
     onShow: function () {
-        // 每次页面显示时同步性别状态
+      if (this.data.isLoggedIn) {
         this.syncGenderState();
+      }
     },
 
     onTabItemTap: function (item) {
@@ -48,37 +40,39 @@ Page({
         setTimeout(() => {
             this.interstitalPlayFn()
         }, 1000)
-        // tabBar 点击时同步性别状态
-        this.syncGenderState();
+        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;
-            }
-            // 未登录,执行登录
-            const res = this.login(promotionid);
-            this.setData({
-                isLoggedIn: true
-            });
-            // 页面首次加载时的处理
-            this.syncGenderState();
-            this.interstitalLoad();
-        } catch (error) {
-            console.error('登录失败:', error);
+      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);
+      }
     },
 
     // 插屏广告加载
@@ -115,6 +109,8 @@ Page({
                 gender
             });
             await this.loadCategories();
+        } else {
+          await this.loadCategories();
         }
     },
 
@@ -130,7 +126,9 @@ Page({
             page: 1,
             hasMore: true
         });
-        await this.loadCategories();
+        if (this.data.isLoggedIn) {
+          await this.loadCategories();
+        }
     },
 
     // 加载分类列表

+ 28 - 3
pages/mine/mine.js

@@ -14,9 +14,7 @@ Page({
     },
 
     onLoad: function (options) {
-        app.autoLogin();
-        this.interstitalLoad();
-        // console.log('userCode:', app.globalData)
+        this.autoLogin();
         this.setData({
             userCode: app.globalData.userCode
         })
@@ -32,6 +30,33 @@ Page({
         console.log(item.index)
     },
 
+    // 自动登录
+    async autoLogin() {
+      try {
+        // 检查是否已经登录
+        const token = wx.getStorageSync('accessToken');
+        if (token) {
+          this.setData({
+            isLoggedIn: true
+          });
+          // 已经登录
+          console.log('已经登录')
+          // 页面首次加载时的处理
+          this.interstitalLoad()
+          return;
+        }
+        // 未登录,执行登录
+        const res = await getApp().login();
+        this.setData({
+          isLoggedIn: true
+        });
+        console.log('重新登录', res)
+        this.interstitalLoad();
+      } catch (error) {
+        console.error('登录失败:', error);
+      }
+    },
+
     // 插屏广告加载
     interstitalLoad() {
         let adId = apple.ads.insertAdId

+ 1 - 1
pages/video/video.js

@@ -121,7 +121,7 @@ Page({
                 }, 500);
             } else {
                 // 第二次进入把isEnterVideoDetail恢复默认值
-                app.globalData.isEnterVideoDetail = false;
+                app.globalData.isEnterVideoDetail = true;
             }
         }