Przeglądaj źródła

Merge branch 'develop' of http://code.nyzhwlw.com:10202/yf_zhb/bigdata_WX into develop

allen 1 dzień temu
rodzic
commit
ee485100ea
1 zmienionych plików z 37 dodań i 16 usunięć
  1. 37 16
      util/api.js

+ 37 - 16
util/api.js

@@ -4,6 +4,37 @@
 // let BASE_URL = 'http://192.168.1.107:8000';
 let BASE_URL = 'http://218.28.198.186:10508';
 // let BASE_URL = 'https://uat.hnyfwlw.com'
+
+// token 失效全局拦截:清除登录态并跳转登录页(并发请求去重)
+let isRedirecting = false;
+const handleTokenExpired = () => {
+  uni.removeStorageSync('session_key');
+  uni.removeStorageSync('isLink'); // 清理旧版本遗留的跳转标记
+  // 并发请求同时返回 403 时只处理一次
+  if (isRedirecting) {
+    return;
+  }
+  isRedirecting = true;
+  // 已在登录页则不重复跳转
+  const pages = getCurrentPages();
+  const currentRoute = pages.length ? pages[pages.length - 1].route : '';
+  if (currentRoute === 'pages/login/login') {
+    isRedirecting = false;
+    return;
+  }
+  uni.showToast({
+    title: '登录已过期,请重新登录!',
+    icon: 'none',
+  });
+  // reLaunch 清空页面栈,避免返回键回到已失效页面
+  uni.reLaunch({
+    url: '/pages/login/login',
+    complete: () => {
+      isRedirecting = false;
+    },
+  });
+};
+
 export const myRequest = (options) => {
   // BASE_URL=uni.getStorageSync('http')
   // if(BASE_URL==''){
@@ -44,25 +75,15 @@ export const myRequest = (options) => {
       },
       data: data,
       success: (res) => {
-        if(res.data.code == 200){
+        if (res.data.code == 200) {
           resolve(res.data.data);
-          return
+          return;
         }
         if (res.data.errorCode == 403) {
-          uni.removeStorageSync('session_key');
-          uni.showToast({
-            title: '登录已过期,请重新登录!',
-            icon: 'none',
-          });
-          if (uni.getStorageSync('isLink')) {
-            return false;
-          } else {
-            uni.setStorageSync('isLink', true);
-            return uni.navigateTo({
-              url: '/pages/login/login',
-            });
-          }
+          handleTokenExpired();
+          return reject(res.data.message || '登录已过期');
         }
+
         if (res.data.message) {
           if (
             res.data.message !== '识别无结果' &&
@@ -72,7 +93,7 @@ export const myRequest = (options) => {
               title: res.data.message,
               icon: 'none',
             });
-            return reject(res)
+            return reject(res);
           } else {
             resolve(res.data.data);
           }