From 35dba0861ecc4cd0069ca4a0fc4e24de1ce3c979 Mon Sep 17 00:00:00 2001 From: lipenghui Date: Sat, 20 Jan 2024 18:40:22 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=A2=9E=E5=8A=A0=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E8=A7=A3=E5=AF=86=EF=BC=9B=E8=AF=B7=E6=B1=82=E5=A4=B4(hulk-aut?= =?UTF-8?q?h)=E5=8A=A0=E5=AF=86=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/store/moules/userStore/index.ts | 10 ++++++---- src/utils/axios/index.ts | 20 ++++++++++++++------ src/utils/index.ts | 4 ++++ 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/store/moules/userStore/index.ts b/src/store/moules/userStore/index.ts index 7bb60ba..ec26db4 100644 --- a/src/store/moules/userStore/index.ts +++ b/src/store/moules/userStore/index.ts @@ -43,6 +43,8 @@ export const useUserStore = defineStore('useUserStore', { async login(params: TokenParams) { return new Promise((resolve, reject) => { token(params).then((res) => { + console.log(res) + this.setToken(crypto.encryptAES(res.access_token, crypto.localKey)) this.setUserInfo(crypto.encryptAES(JSON.stringify(res), crypto.localKey)) this.getChatInfoFun() @@ -63,10 +65,10 @@ export const useUserStore = defineStore('useUserStore', { * @description: logout */ async logout(goLogin = false) { - // this.$reset() - // localStorage.clear() - // // 清空数据 - // goLogin && router.push(PageEnum.BASE_LOGIN) + this.$reset() + localStorage.clear() + // 清空数据 + goLogin && router.push(PageEnum.BASE_LOGIN) }, }, persist: [ diff --git a/src/utils/axios/index.ts b/src/utils/axios/index.ts index 0dbe768..7062147 100644 --- a/src/utils/axios/index.ts +++ b/src/utils/axios/index.ts @@ -13,13 +13,16 @@ import { AxiosRetry } from './axiosRetry' import { useMessage } from '@/hooks/useMessage' import { ContentTypeEnum, HttpErrorMsgEnum, HttpSuccessEnum, RequestEnum, ResultEnum } from '@/enums/httpEnum' import { isEmpty, isNull, isString, isUndefined } from '@/utils/is' -import { deepMerge, setObjToUrlParams } from '@/utils' +import { deepMerge, setObjToUrlParams, strHasArr } from '@/utils' +import crypto from '@/utils/crypto' import { useUserStore } from '@/store/moules/userStore/index' const { createMessage, createErrorModal, createSuccessModal } = useMessage() // 请求白名单,无须token的接口 const whiteList: string[] = ['/login', '/refresh-token'] +// 不需要解密接口白名单 +const notDecryptWhiteList = ['/hulk-auth/oauth/token'] /** * @description: 数据处理,方便区分多种处理方式 @@ -40,12 +43,17 @@ const transform: AxiosTransform = { // 不进行任何处理,直接返回 // 用于页面代码可能需要直接获取code,data,message这些信息时开启 - if (!isTransformResponse) - return res.data + if (!isTransformResponse) { + if (strHasArr(res.config.url!, notDecryptWhiteList)) { + return res.data + } + else { + return JSON.parse(crypto.decryptAES(res.data as unknown as string, crypto.aesKey)) + } + } // 错误的时候返回 - - const { data } = res + const data = JSON.parse(crypto.decryptAES(res.data as unknown as string, crypto.aesKey)) if (!data) { // return '[HTTP] Request has no return value'; @@ -189,7 +197,7 @@ const transform: AxiosTransform = { if (token && !isToken) { // jwt token (config as Recordable).headers[import.meta.env.VITE_GLOB_APP_TOKEN_KEY] = options.tokenScheme - ? `${options.tokenScheme} ${token}` + ? `${options.tokenScheme} ${crypto.encryptAES(token, crypto.cryptoKey)}` : token } diff --git a/src/utils/index.ts b/src/utils/index.ts index ca17b0e..fb06886 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -79,3 +79,7 @@ export function openWindow( window.open(url, target, feature.join(',')) } + +export function strHasArr(str: string, arr: string[]) { + return arr.some(item => str.includes(item)) +}