|
@@ -10,7 +10,7 @@ import i18n from '@/lang'
|
|
|
import * as crypto from './crypto'
|
|
|
let currentRequests = 0
|
|
|
const { VITE_APP_BASE_API, VITE_USE_CRYPTO } = import.meta.env
|
|
|
-
|
|
|
+const userErrorCodeList = ['NOT_LOGIN', 'ACCOUNT_HAS_EXPIRED', 'ACCOUNT_LOGGED_IN_ELESWHERE', 'USER_NOT_FOUND', 'USER_IS_DISABLED', 'ROLE_IS_DISABLED']
|
|
|
export class AxiosWrapper {
|
|
|
constructor({ baseUrl = VITE_APP_BASE_API, unwrap = true } = {}) {
|
|
|
this.baseUrl = baseUrl
|
|
@@ -79,50 +79,8 @@ export class AxiosWrapper {
|
|
|
baseURL: this.baseUrl,
|
|
|
withCredentials: true
|
|
|
})
|
|
|
- // #region 加密部分
|
|
|
- // 加密,需要根据env文件判断是否启用加密
|
|
|
- if (JSON.parse(VITE_USE_CRYPTO)) {
|
|
|
- // 生成随机字符串
|
|
|
- const reqCode = crypto.getRandomString()
|
|
|
- axios.interceptors.request.use(async (config) => {
|
|
|
- // 加密真实使用的加密字符串
|
|
|
- const deReqCode = crypto.pemEncrypt(reqCode)
|
|
|
- // 加密数据并替换
|
|
|
- config.transformRequest = (data) => {
|
|
|
- if (data) {
|
|
|
- console.group('请求')
|
|
|
- console.log(`加密串:${reqCode}`)
|
|
|
- console.log(`原数据:`)
|
|
|
- // 加密 加密字符串
|
|
|
- const strData = JSON.stringify(data)
|
|
|
- console.log(strData)
|
|
|
- // 加密数据
|
|
|
- const enCodeData = crypto.encrypt(reqCode, strData)
|
|
|
- console.log(`加密后数据:`)
|
|
|
- console.log(enCodeData)
|
|
|
- // 替换数据位置
|
|
|
- return JSON.stringify({ data: enCodeData })
|
|
|
- }
|
|
|
- return undefined
|
|
|
- }
|
|
|
- // 添加请求头,将 加密的真实使用加密字符串附上
|
|
|
- config.headers['api-token'] = deReqCode
|
|
|
- return config
|
|
|
- })
|
|
|
- axios.interceptors.response.use(
|
|
|
- (response) => {
|
|
|
- if (get(response, 'data.data')) {
|
|
|
- let data = crypto.decrypt(reqCode, get(response, 'data.data'))
|
|
|
- const dobj = JSON.parse(data || '{}')
|
|
|
- const others = pick(get(response, 'data'), ['errcode', 'errmsg'])
|
|
|
- response.data = { ...others, ...dobj }
|
|
|
- }
|
|
|
- return response
|
|
|
- },
|
|
|
- (error) => Promise.reject(error)
|
|
|
- )
|
|
|
- }
|
|
|
- // #endregion
|
|
|
+ AxiosWrapper.toCropty(axios)
|
|
|
+
|
|
|
const token = localStorage.getItem('token')
|
|
|
if (token) axios.defaults.headers.common['token'] = token
|
|
|
|
|
@@ -139,15 +97,19 @@ export class AxiosWrapper {
|
|
|
if (errcode) {
|
|
|
console.warn(`[${uri}] fail: ${errcode}-${errmsg} ${details}`)
|
|
|
if (errcode !== 0) {
|
|
|
- console.log(router)
|
|
|
- if (errcode.includes('FRAMEERROR_401')) {
|
|
|
- // await ElMessageBox.alert(errmsg, i18n.global.t('common.user_confirm'), {
|
|
|
- // confirmButtonText: i18n.global.t('common.re_login'),
|
|
|
- // type: 'error',
|
|
|
- // callback: (act) => {
|
|
|
- // // router.replace('/login')
|
|
|
- // }
|
|
|
- // })
|
|
|
+ if (userErrorCodeList.includes(errcode)) {
|
|
|
+ const nowRouteFullPath = router.currentRoute.value.fullPath
|
|
|
+ ElMessageBox.confirm(errmsg, i18n.global.t('common.user_confirm'), {
|
|
|
+ confirmButtonText: i18n.global.t('common.re_login'),
|
|
|
+ cancelButtonText: i18n.global.t('common.reload'),
|
|
|
+ type: 'error'
|
|
|
+ })
|
|
|
+ .then(() => {
|
|
|
+ if (nowRouteFullPath !== '/login') window.location.href = `${import.meta.env.VITE_BASE_URL}/login`
|
|
|
+ })
|
|
|
+ .catch(() => {
|
|
|
+ if (nowRouteFullPath !== '/login') location.reload()
|
|
|
+ })
|
|
|
}
|
|
|
}
|
|
|
return returnRes
|
|
@@ -181,4 +143,51 @@ export class AxiosWrapper {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ static toCropty(axios) {
|
|
|
+ // #region 加密部分
|
|
|
+ // 加密,需要根据env文件判断是否启用加密
|
|
|
+ if (JSON.parse(VITE_USE_CRYPTO)) {
|
|
|
+ // 生成随机字符串
|
|
|
+ const reqCode = crypto.getRandomString()
|
|
|
+ axios.interceptors.request.use(async (config) => {
|
|
|
+ // 加密真实使用的加密字符串
|
|
|
+ const deReqCode = crypto.pemEncrypt(reqCode)
|
|
|
+ // 加密数据并替换
|
|
|
+ config.transformRequest = (data) => {
|
|
|
+ if (data) {
|
|
|
+ console.group('请求')
|
|
|
+ console.log(`加密串:${reqCode}`)
|
|
|
+ console.log(`原数据:`)
|
|
|
+ // 加密 加密字符串
|
|
|
+ const strData = JSON.stringify(data)
|
|
|
+ console.log(strData)
|
|
|
+ // 加密数据
|
|
|
+ const enCodeData = crypto.encrypt(reqCode, strData)
|
|
|
+ console.log(`加密后数据:`)
|
|
|
+ console.log(enCodeData)
|
|
|
+ // 替换数据位置
|
|
|
+ return JSON.stringify({ data: enCodeData })
|
|
|
+ }
|
|
|
+ return undefined
|
|
|
+ }
|
|
|
+ // 添加请求头,将 加密的真实使用加密字符串附上
|
|
|
+ config.headers['api-token'] = deReqCode
|
|
|
+ return config
|
|
|
+ })
|
|
|
+ axios.interceptors.response.use(
|
|
|
+ (response) => {
|
|
|
+ if (get(response, 'data.data')) {
|
|
|
+ let data = crypto.decrypt(reqCode, get(response, 'data.data'))
|
|
|
+ const dobj = JSON.parse(data || '{}')
|
|
|
+ const others = pick(get(response, 'data'), ['errcode', 'errmsg'])
|
|
|
+ response.data = { ...others, ...dobj }
|
|
|
+ }
|
|
|
+ return response
|
|
|
+ },
|
|
|
+ (error) => Promise.reject(error)
|
|
|
+ )
|
|
|
+ }
|
|
|
+ // #endregion
|
|
|
+ }
|
|
|
}
|