Browse Source

:zap: 优化 check-token

lbw 1 year ago
parent
commit
1c4ba2f5dd
3 changed files with 31 additions and 23 deletions
  1. 29 21
      src/api/login.js
  2. 1 1
      src/page/index/index.vue
  3. 1 1
      src/views/gen/preview.vue

+ 29 - 21
src/api/login.js

@@ -19,6 +19,7 @@ import qs from 'qs'
 import store from '@/store'
 import store from '@/store'
 import {getStore, setStore} from "@/util/store.js";
 import {getStore, setStore} from "@/util/store.js";
 import website from "@/const/website.js";
 import website from "@/const/website.js";
+import {validatenull} from "@/util/validate";
 
 
 const scope = 'server'
 const scope = 'server'
 
 
@@ -128,10 +129,10 @@ export const logout = () => {
  * 校验令牌,若有效期小于半小时自动续期
  * 校验令牌,若有效期小于半小时自动续期
  * @param refreshLock
  * @param refreshLock
  */
  */
-export const checkToken = (refreshLock, $store) => {
+export const checkToken = (refreshTime, refreshLock, $store) => {
   const token = store.getters.access_token
   const token = store.getters.access_token
   // 获取当前选中的 basic 认证信息
   // 获取当前选中的 basic 认证信息
-  let basicAuth = getStore({name: 'basicAuth'})
+  const basicAuth = getStore({ name: 'basicAuth' })
 
 
   request({
   request({
     url: '/admin/token/check_token',
     url: '/admin/token/check_token',
@@ -140,28 +141,35 @@ export const checkToken = (refreshLock, $store) => {
       Authorization: basicAuth
       Authorization: basicAuth
     },
     },
     method: 'get',
     method: 'get',
-    params: {token}
-  }).then(response => {
-    const expire = response && response.data && response.data.exp
-    if (expire) {
-      const expiredPeriod = expire * 1000 - new Date().getTime()
-      //小于半小时自动续约
-      if (expiredPeriod <= 30 * 60 * 1000) {
-        if (!refreshLock) {
-          refreshLock = true
-          $store.dispatch('RefreshToken')
-            .catch(() => {
-              clearInterval(this.refreshTime)
-            })
-          refreshLock = false
-        }
-      }
-    }
-  }).catch(error => {
-    console.error(error)
+    params: { token }
   })
   })
+      .then((response) => {
+        if (validatenull(response)) {
+          clearInterval(refreshTime)
+          return
+        }
+
+        const expire = response && response.data && response.data.exp
+        if (expire) {
+          const expiredPeriod = expire * 1000 - new Date().getTime()
+          //小于半小时自动续约
+          if (expiredPeriod <= 10 * 60 * 1000) {
+            if (!refreshLock) {
+              refreshLock = true
+              $store.dispatch('RefreshToken').catch(() => {
+                clearInterval(refreshTime)
+              })
+              refreshLock = false
+            }
+          }
+        }
+      })
+      .catch((error) => {
+        console.error(error)
+      })
 }
 }
 
 
+
 /**
 /**
  * 注册用户
  * 注册用户
  */
  */

+ 1 - 1
src/page/index/index.vue

@@ -102,7 +102,7 @@ export default {
     // 实时检测刷新token
     // 实时检测刷新token
     refreshToken() {
     refreshToken() {
       this.refreshTime = setInterval(() => {
       this.refreshTime = setInterval(() => {
-        checkToken(this.refreshLock, this.$store)
+        checkToken(this.refreshTime, this.refreshLock, this.$store)
       }, 60000)
       }, 60000)
     }
     }
   }
   }

+ 1 - 1
src/views/gen/preview.vue

@@ -1,7 +1,7 @@
 <template>
 <template>
   <el-row>
   <el-row>
     <el-col :span="7">
     <el-col :span="7">
-      <el-tree :data="fileTree" :expand-on-click-node="false" default-expand-all highlight-current @node-click="handleNodeClick" />
+      <el-tree :data="fileTree" :expand-on-click-node="false" highlight-current @node-click="handleNodeClick" />
     </el-col>
     </el-col>
     <el-col :span="17">
     <el-col :span="17">
       <el-tabs v-model="activeName" type="card">
       <el-tabs v-model="activeName" type="card">