优化登录跳转接口
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import type { AxiosRequestConfig, Method } from 'axios'
|
||||
import axios from 'axios'
|
||||
import { ElLoading, ElNotification, type LoadingOptions } from 'element-plus'
|
||||
import { nextTick } from 'vue'
|
||||
import { refreshToken } from '/@/api/common'
|
||||
import { i18n } from '/@/lang/index'
|
||||
import router from '/@/router/index'
|
||||
@@ -20,6 +21,12 @@ const loadingInstance: LoadingInstance = {
|
||||
count: 0,
|
||||
}
|
||||
|
||||
/** 请求是否后台 /admin/ 接口(不依赖当前路由,避免 loading 等场景误判为前台) */
|
||||
function isAdminBackendRequest(config: AxiosRequestConfig): boolean {
|
||||
const u = `${config.baseURL ?? ''}${config.url ?? ''}`
|
||||
return /\/admin\//i.test(u)
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据运行环境获取基础请求URL
|
||||
*/
|
||||
@@ -112,6 +119,25 @@ function createAxios<Data = any, T = ApiPromise<Data>>(axiosConfig: AxiosRequest
|
||||
|
||||
if (response.config.responseType == 'json') {
|
||||
if (response.data && response.data.code !== 1) {
|
||||
const needLogin =
|
||||
response.data.data &&
|
||||
typeof response.data.data === 'object' &&
|
||||
response.data.data.type === 'need login'
|
||||
if (needLogin) {
|
||||
const isAdminAppFlag = isAdminApp() || isAdminBackendRequest(response.config)
|
||||
if (isAdminAppFlag) {
|
||||
adminInfo.removeToken()
|
||||
} else {
|
||||
userInfo.removeToken()
|
||||
}
|
||||
const loginRouteName = isAdminAppFlag ? 'adminLogin' : 'userLogin'
|
||||
if (router.currentRoute.value.name !== loginRouteName) {
|
||||
nextTick(() => {
|
||||
void router.replace({ name: loginRouteName })
|
||||
})
|
||||
}
|
||||
return Promise.reject(response.data)
|
||||
}
|
||||
if (response.data.code == 409) {
|
||||
if (!window.tokenRefreshing) {
|
||||
window.tokenRefreshing = true
|
||||
|
||||
Reference in New Issue
Block a user