Переглянути джерело

登录token增加有效期

Yue 2 роки тому
батько
коміт
eaff527b45

+ 1 - 0
src/core/config/Index.ts

@@ -1,6 +1,7 @@
 const configs = {
   BASE_URL: import.meta.env.VITE_APP_API_URL,
   DATE_FORMAT_STRING: "YYYY年 MM月 DD日",
+  AUTH_EXPIRE_MINUTES: 60, //登录时效
   MAIN_ROUTER_NAME: "dashboard",
   SIGN_ROUTE_NAME: "sign-in",
 }

+ 18 - 3
src/core/services/JwtService.ts

@@ -1,12 +1,27 @@
 import storage from "@/core/utils/storages"
+import configs from "../config/Index"
 const ID_TOKEN_KEY = "id_token" as string
 const ID_TENANT_KEY = "id_tenant" as string
 
 /**
  * @description 获取令牌
  */
-export const getToken = (): string | null => {
-  return storage.getItem(ID_TOKEN_KEY)
+export const getToken = (isCheck = true): string | null | undefined => {
+  const arr = storage.getItem(ID_TOKEN_KEY)?.split("@@")
+  let token
+  if (arr && arr.length == 2) {
+    if (isCheck) {
+      const diff = new Date().getTime() - Number(arr[0])
+      const expire = configs.AUTH_EXPIRE_MINUTES * 60 * 1000
+      token = arr[1]
+      if (diff > expire) {
+        console.error("AUTH过期,请重新登录")
+        return null
+      }
+      saveToken(token)
+    }
+  }
+  return token
 }
 
 /**
@@ -14,7 +29,7 @@ export const getToken = (): string | null => {
  * @param token: string
  */
 export const saveToken = (token: string): void => {
-  storage.setItem(ID_TOKEN_KEY, token)
+  storage.setItem(ID_TOKEN_KEY, `${new Date().getTime()}@@${token}`)
 }
 
 /**

+ 1 - 1
src/core/services/RequestService.ts

@@ -5,7 +5,7 @@ import VueAxios from "vue-axios"
 import JwtService from "@/core/services/JwtService"
 import configs from "@/core/config/Index"
 import { dialog, toast } from "@/core/utils/message"
-import router from "@/router/Index"
+import router from "@/router"
 
 export interface errorObj {
   message: string

+ 2 - 0
src/stores/_auth.ts

@@ -7,6 +7,7 @@ import storages from "@/core/utils/storages"
 export interface User {
   userName: string
   userType: string
+  orgId: string
   token: string
 }
 
@@ -45,6 +46,7 @@ export const useAuthStore = defineStore("auth", () => {
           setAuth({
             userName: result.authName || "",
             userType: result.userType || "0",
+            orgId: result.enterpriseId || "",
             token: result.auth,
           })
           resolve(result.loginMsg)