diff --git a/src/store/user.ts b/src/store/user.ts index 5598eb3..29ac8f7 100644 --- a/src/store/user.ts +++ b/src/store/user.ts @@ -4,7 +4,6 @@ */ import { produce } from 'immer'; -// import { setCookie } from 'nookies'; import { create } from 'zustand'; import { devtools, persist, createJSONStorage } from 'zustand/middleware'; diff --git a/src/utils/auth.ts b/src/utils/auth.ts index f9bdffb..722b74d 100644 --- a/src/utils/auth.ts +++ b/src/utils/auth.ts @@ -11,20 +11,24 @@ export const checkAuthOffline: () => boolean = () => { }; export const getAuthorization = async () => { - const { token, expires } = await parseCookies(); - if (!token) return; - - if (token && !isExpToken(parseInt(expires ?? 0))) { - return token; - } else if (token) { - logout(); - // return tokenRefresh(); - // token 过期了,重新登录请求然后给请求头设置好token - // 获取token的方法等也可以根据authType的不同进行自定义设置 - // const res = await getToken("账号密码等"); - // setSession("local", tokenKey, res?.data); - // authorization = res?.data; - } + const { token } = await parseCookies(); + return token || ''; + // if (!token) return ''; + // const exp = parseInt(expires ?? 0); + // console.log(111, !isExpToken(exp)); + + // if (!isExpToken(exp)) { + // return token; + // } else { + // logout(); + // return ''; + // return tokenRefresh(); + // token 过期了,重新登录请求然后给请求头设置好token + // 获取token的方法等也可以根据authType的不同进行自定义设置 + // const res = await getToken("账号密码等"); + // setSession("local", tokenKey, res?.data); + // authorization = res?.data; + // } }; export function setAccessToken({ token, expires }: { token: string; expires: string }) { diff --git a/src/utils/request/client/utils.ts b/src/utils/request/client/utils.ts index aceb1ee..26a91d5 100644 --- a/src/utils/request/client/utils.ts +++ b/src/utils/request/client/utils.ts @@ -67,7 +67,12 @@ export const checkResponseState = async (response: FetchResponse) => { */ if ([400, 401, 10000].includes(code)) { // 判断是否有 token - if (checkAuthOffline()) logout(); + if (checkAuthOffline()) { + logout(); + window.location.reload(); + } else { + localStorage.clear(); + } // 打开登录框 openLogin();