Quellcode durchsuchen

feature: 处理token过期无感加载登录信息数据

Damon vor 7 Monaten
Ursprung
Commit
7350ae7cb7
3 geänderte Dateien mit 66 neuen und 43 gelöschten Zeilen
  1. 47 1
      pages/index/index.js
  2. 13 32
      pages/login/login.js
  3. 6 10
      utils/util.js

+ 47 - 1
pages/index/index.js

@@ -1,6 +1,6 @@
 const app = getApp();
 const {
-  getBanner,
+  login,
   listByDevice
 } = require('../../utils/api.js');
 
@@ -77,6 +77,7 @@ Page({
 
   onLoad(options) {
     var that = this;
+    that.onLoginLoad();
     that.onDeviceLoad();
     that.onUserInfoLoad();
     that.onBannerLoad();
@@ -87,6 +88,51 @@ Page({
     that.onBannerShow();
   },
 
+  onLoginLoad() {
+    var that = this;
+    var userInfo = wx.getStorageSync("userInfo") || "";
+    if (!strings.isEmpty(userInfo)) {
+      var phone = userInfo.phone;
+      if (!strings.isEmpty(phone)) {
+        that.loginReset(phone, null, null, null);
+      };
+    }
+  },
+
+  ///重新登录
+  loginReset(phone, succeedCall1, succeedCall2, failCall) {
+    // {"openid":"oZHPJ5JcNem2och3ToaocggmUENw","userId":933625,"deviceUid":1,"accessToken":"PUK8WVnLbIFsWYjSteitmg3OhllpbUMvIPKUtfJgwhRtpl1jx4Fm1NkHQIiZYhHz","phone":"15915390284","headUrl":"https://music-play.oss-cn-shenzhen.aliyuncs.com/user/headurl/20220316194115703366576.png","nickname":"儿子的爸爸叫啥"}
+    var that = this;
+    wx.login({
+      success: res0 => {
+        // {"openid":"oZHPJ5JcNem2och3ToaocggmUENw","unionid":null,"userId":933625,"deviceUid":1,"accessToken":"kaKdNAb9eO0iUGKsaLP18UE0wRwrvoCr4opjNC3AieaigH4Fu1IspQKi2zTPgLNG","phone":"15915390284","headUrl":"https://music-play.oss-cn-shenzhen.aliyuncs.com/user/headurl/20220316194115703366576.png","nickname":"儿子的爸爸叫啥","isNewUser":false}
+        login({
+          code: res0.code,
+          phone: phone,
+        }).then((res1) => {
+          app.globalData.userInfo = res1;
+          store.setStore("userInfo", res1);
+          store.setStore("token", JSON.stringify({
+            id: res1.userId,
+            token: res1.accessToken,
+          }));
+          that.onUserInfoLoad();
+          if (succeedCall1 != null) {
+            succeedCall1();
+          }
+          if (succeedCall2 != null) {
+            succeedCall2();
+          }
+        });
+      },
+      fail(fail4) {
+        if (failCall != null) {
+          failCall();
+        }
+      },
+    });
+  },
+
   onDeviceLoad() {
     var that = this;
     var deviceList = store.getStore("deviceList");

+ 13 - 32
pages/login/login.js

@@ -1,5 +1,3 @@
-// pages/login/login.ts;
-const app = getApp();
 const {
   login,
   register
@@ -48,7 +46,6 @@ Page({
           wx.showLoading({
             title: '加载中',
           });
-
           // {"openid":"oZHPJ5JcNem2och3ToaocggmUENw","unionid":null,"userId":null,"deviceUid":null,"accessToken":null,"phone":null,"headUrl":"","nickname":"","isNewUser":true}
           login({
             code: res1.code,
@@ -59,35 +56,19 @@ Page({
               code: phoneCode,
               unShowLoad: true,
             }).then((res3) => {
-              // {"openid":"oZHPJ5JcNem2och3ToaocggmUENw","userId":933625,"deviceUid":1,"accessToken":"PUK8WVnLbIFsWYjSteitmg3OhllpbUMvIPKUtfJgwhRtpl1jx4Fm1NkHQIiZYhHz","phone":"15915390284","headUrl":"https://music-play.oss-cn-shenzhen.aliyuncs.com/user/headurl/20220316194115703366576.png","nickname":"儿子的爸爸叫啥"}
-              wx.login({
-                success: res4 => {
-                  // {"openid":"oZHPJ5JcNem2och3ToaocggmUENw","unionid":null,"userId":933625,"deviceUid":1,"accessToken":"kaKdNAb9eO0iUGKsaLP18UE0wRwrvoCr4opjNC3AieaigH4Fu1IspQKi2zTPgLNG","phone":"15915390284","headUrl":"https://music-play.oss-cn-shenzhen.aliyuncs.com/user/headurl/20220316194115703366576.png","nickname":"儿子的爸爸叫啥","isNewUser":false}
-                  login({
-                    code: res4.code,
-                    phone: res3.phone,
-                  }).then((res5) => {
-                    wx.showToast({
-                      title: "登录成功",
-                      icon: 'success',
-                      duration: 2000
-                    });
-                    app.globalData.userInfo = res5;
-                    wx.setStorageSync("userInfo", res5);
-                    wx.setStorageSync("token", JSON.stringify({
-                      id: res5.userId,
-                      token: res5.accessToken,
-                    }), );
-
-                    getCurrentPages()[0].onUserInfoLoad();
-                    setTimeout(() => {
-                      route_util.redirectTo(route_constant.deviceList);
-                    }, 2000);
-                  });
-                },
-                fail(fail4) {
-                  wx.hideLoading();
-                },
+              var phone = res3.phone;
+              getCurrentPages()[0].loginReset(phone, function () {
+                wx.showToast({
+                  title: "登录成功",
+                  icon: 'success',
+                  duration: 2000
+                });
+              }, function () {
+                setTimeout(() => {
+                  route_util.redirectTo(route_constant.deviceList);
+                }, 2000);
+              }, function () {
+                wx.hideLoading();
               });
             });
           });

+ 6 - 10
utils/util.js

@@ -108,17 +108,13 @@ function request(url, data = {}, method = "GET", header = urlencodeType, isToken
           resolve(res.data.data);
         } else if (res.data.code === 401) {
           resolve(res.data);
-          wx.showToast({
-            title: '登录过期'
-          })
+          var pages = getCurrentPages();
+          pages[0].onLoginLoad();
           // token过期
-          try {
-            getCurrentPages()[0].logOut();
-          } catch (e) {
-            console.log("可能没有这个缓存")
-          };
-          // getCurrentPages()[0].login();
-          // request(url,data,method,header,isToken);
+          // try {
+          //   getCurrentPages()[0].logOut();
+          // } catch (e) {
+          // };
         } else {
           resolve(res.data);
           _show_error(res.data.code)