From 3a5d1a5757c0a2be17e6dd370cbb023ddbb30d5e Mon Sep 17 00:00:00 2001
From: 无木 <netfan@foxmail.com>
Date: 星期五, 20 八月 2021 16:55:42 +0800
Subject: [PATCH] fix: refresh failed while token invalid

---
 src/utils/http/axios/checkStatus.ts |    4 ++--
 src/store/modules/user.ts           |   10 ++++++----
 CHANGELOG.zh_CN.md                  |    1 +
 src/router/guard/permissionGuard.ts |    7 ++++++-
 4 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/CHANGELOG.zh_CN.md b/CHANGELOG.zh_CN.md
index 2757cbc..d848220 100644
--- a/CHANGELOG.zh_CN.md
+++ b/CHANGELOG.zh_CN.md
@@ -10,6 +10,7 @@
 - **鍏跺畠**
   - 淇閮ㄥ垎灏佽缁勪欢鍦ㄤ娇鐢ㄦ彃妲芥椂鎶ラ敊鐨勯棶棰�
   - 淇`useECharts`鐨刞theme`鍙傛暟涓嶈捣浣滅敤鐨勯棶棰�
+  - 淇`Token`澶辨晥鏃讹紝鎸� F5 鍒锋柊椤甸潰鍙兘浼氬嚭鐜伴〉闈㈠姞杞藉紓甯哥殑闂
 
 ## 2.7.1(2021-08-16)
 
diff --git a/src/router/guard/permissionGuard.ts b/src/router/guard/permissionGuard.ts
index bbe28ef..09027b8 100644
--- a/src/router/guard/permissionGuard.ts
+++ b/src/router/guard/permissionGuard.ts
@@ -72,7 +72,12 @@
 
     // get userinfo while last fetch time is empty
     if (userStore.getLastUpdateTime === 0) {
-      await userStore.getUserInfoAction();
+      try {
+        await userStore.getUserInfoAction();
+      } catch (err) {
+        next();
+        return;
+      }
     }
 
     if (permissionStore.getIsDynamicAddedRoute) {
diff --git a/src/store/modules/user.ts b/src/store/modules/user.ts
index 67a6611..8b8a24b 100644
--- a/src/store/modules/user.ts
+++ b/src/store/modules/user.ts
@@ -128,10 +128,12 @@
      * @description: logout
      */
     async logout(goLogin = false) {
-      try {
-        await doLogout();
-      } catch {
-        console.log('娉ㄩ攢Token澶辫触');
+      if (this.token) {
+        try {
+          await doLogout();
+        } catch {
+          console.log('娉ㄩ攢Token澶辫触');
+        }
       }
       this.setToken(undefined);
       this.setSessionTimeout(false);
diff --git a/src/utils/http/axios/checkStatus.ts b/src/utils/http/axios/checkStatus.ts
index 1c132ee..68c2ecf 100644
--- a/src/utils/http/axios/checkStatus.ts
+++ b/src/utils/http/axios/checkStatus.ts
@@ -28,9 +28,9 @@
     // Jump to the login page if not logged in, and carry the path of the current page
     // Return to the current page after successful login. This step needs to be operated on the login page.
     case 401:
-      errMessage = t('sys.api.errMsg401');
+      userStore.setToken(undefined);
+      errMessage = msg || t('sys.api.errMsg401');
       if (stp === SessionTimeoutProcessingEnum.PAGE_COVERAGE) {
-        userStore.setToken(undefined);
         userStore.setSessionTimeout(true);
       } else {
         userStore.logout(true);

--
Gitblit v1.8.0