fixed: 当接口返回401时直接跳转到登录页
This commit is contained in:
parent
dc677c0c5a
commit
6252553aee
@ -73,6 +73,10 @@ export const router = createRouter({
|
|||||||
path: '/',
|
path: '/',
|
||||||
component: () => import('./pages/Layout.vue'),
|
component: () => import('./pages/Layout.vue'),
|
||||||
children: routes
|
children: routes
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: '/:pathMatch(.*)*',
|
||||||
|
redirect:'/'
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
history: createWebHashHistory()
|
history: createWebHashHistory()
|
||||||
|
@ -1,16 +1,24 @@
|
|||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
import { BizError } from '@/types/core';
|
import { BizError } from '@/types/core';
|
||||||
import {getToken} from '@/service/user-store'
|
import {getToken, useUserStore} from '@/service/user-store'
|
||||||
|
import {message} from 'ant-design-vue'
|
||||||
|
|
||||||
const axiosService = axios.create({
|
const axiosService = axios.create({
|
||||||
baseURL: '/api/v2',
|
baseURL: '/api/v2',
|
||||||
timeout: 5000,
|
timeout: 5000,
|
||||||
headers: {
|
|
||||||
'Authorization': getToken()
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
function processUnauthorized (){
|
||||||
|
const store = useUserStore();
|
||||||
|
store.logout().then(async ()=>{
|
||||||
|
message.error('获取登录凭证失败,请重新登录')
|
||||||
|
})
|
||||||
|
// const err = new BizError('获取登录凭证失败,请重新登录', 401, e.response?.data);
|
||||||
|
// router.replace(`/login?redirect=${router.currentRoute.value.path}`).then(() => {
|
||||||
|
// console.log('401 show login')
|
||||||
|
// })
|
||||||
|
}
|
||||||
export function request<T>(options: RequestOption) {
|
export function request<T>(options: RequestOption) {
|
||||||
return new Promise<T>((resolve, reject) => {
|
return new Promise<T>((resolve, reject) => {
|
||||||
const { url, method, data, baseURL, getOriginResult } = options;
|
const { url, method, data, baseURL, getOriginResult } = options;
|
||||||
@ -20,6 +28,9 @@ export function request<T>(options: RequestOption) {
|
|||||||
method: method || 'get',
|
method: method || 'get',
|
||||||
data,
|
data,
|
||||||
baseURL,
|
baseURL,
|
||||||
|
headers: {
|
||||||
|
'Authorization': getToken()
|
||||||
|
}
|
||||||
}).then(res => {
|
}).then(res => {
|
||||||
if (res.status != 200) {
|
if (res.status != 200) {
|
||||||
reject(new BizError("Service Internal Exception,Please Try Later!", res.status))
|
reject(new BizError("Service Internal Exception,Please Try Later!", res.status))
|
||||||
@ -33,10 +44,23 @@ export function request<T>(options: RequestOption) {
|
|||||||
const { code, message, data } = res.data
|
const { code, message, data } = res.data
|
||||||
if (code == 0) {
|
if (code == 0) {
|
||||||
resolve(data as unknown as T)
|
resolve(data as unknown as T)
|
||||||
|
} else if(code == 401){
|
||||||
|
// reject(new BizError('获取登录凭证失败,请重新登录', code, data))
|
||||||
|
processUnauthorized();
|
||||||
} else {
|
} else {
|
||||||
reject(new BizError(message, code, data))
|
reject(new BizError(message, code, data))
|
||||||
}
|
}
|
||||||
}).catch(e => {
|
}).catch(e => {
|
||||||
|
if(e.status == 401){
|
||||||
|
if(e.response){
|
||||||
|
try{
|
||||||
|
if(e.response?.data.code == 401){
|
||||||
|
processUnauthorized()
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}catch (e){}
|
||||||
|
}
|
||||||
|
}
|
||||||
reject(new BizError(e.message, 500))
|
reject(new BizError(e.message, 500))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -42,6 +42,7 @@ export default defineConfig(({ mode }) => {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
server: {
|
server: {
|
||||||
|
port:10086,
|
||||||
proxy: {
|
proxy: {
|
||||||
'/api': {
|
'/api': {
|
||||||
target: devServerTarget,
|
target: devServerTarget,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user