fixed: 当接口返回401时直接跳转到登录页
This commit is contained in:
parent
dc677c0c5a
commit
6252553aee
@ -73,6 +73,10 @@ export const router = createRouter({
|
||||
path: '/',
|
||||
component: () => import('./pages/Layout.vue'),
|
||||
children: routes
|
||||
},
|
||||
{
|
||||
path: '/:pathMatch(.*)*',
|
||||
redirect:'/'
|
||||
}
|
||||
],
|
||||
history: createWebHashHistory()
|
||||
|
@ -1,16 +1,24 @@
|
||||
import axios from 'axios'
|
||||
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({
|
||||
baseURL: '/api/v2',
|
||||
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) {
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
const { url, method, data, baseURL, getOriginResult } = options;
|
||||
@ -20,6 +28,9 @@ export function request<T>(options: RequestOption) {
|
||||
method: method || 'get',
|
||||
data,
|
||||
baseURL,
|
||||
headers: {
|
||||
'Authorization': getToken()
|
||||
}
|
||||
}).then(res => {
|
||||
if (res.status != 200) {
|
||||
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
|
||||
if (code == 0) {
|
||||
resolve(data as unknown as T)
|
||||
} else if(code == 401){
|
||||
// reject(new BizError('获取登录凭证失败,请重新登录', code, data))
|
||||
processUnauthorized();
|
||||
} else {
|
||||
reject(new BizError(message, code, data))
|
||||
}
|
||||
}).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))
|
||||
})
|
||||
})
|
||||
|
@ -42,6 +42,7 @@ export default defineConfig(({ mode }) => {
|
||||
}
|
||||
},
|
||||
server: {
|
||||
port:10086,
|
||||
proxy: {
|
||||
'/api': {
|
||||
target: devServerTarget,
|
||||
|
Loading…
x
Reference in New Issue
Block a user