🔨 优化错误提示.
This commit is contained in:
12
orion-visor-ui/src/api/error.ts
Normal file
12
orion-visor-ui/src/api/error.ts
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
import type { AxiosResponse } from 'axios';
|
||||||
|
|
||||||
|
// api 错误
|
||||||
|
export class ApiError extends Error {
|
||||||
|
data: AxiosResponse;
|
||||||
|
|
||||||
|
constructor(message: string, data: AxiosResponse) {
|
||||||
|
super(message);
|
||||||
|
this.name = 'ApiError';
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ import { useUserStore } from '@/store';
|
|||||||
import { getToken } from '@/utils/auth';
|
import { getToken } from '@/utils/auth';
|
||||||
import { httpBaseUrl } from '@/utils/env';
|
import { httpBaseUrl } from '@/utils/env';
|
||||||
import { reLoginTipsKey } from '@/types/symbol';
|
import { reLoginTipsKey } from '@/types/symbol';
|
||||||
|
import { ApiError } from '@/api/error';
|
||||||
|
|
||||||
axios.defaults.timeout = 15000;
|
axios.defaults.timeout = 15000;
|
||||||
axios.defaults.setAuthorization = true;
|
axios.defaults.setAuthorization = true;
|
||||||
@@ -72,7 +73,7 @@ axios.interceptors.response.use(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Promise.reject(new Error(res.msg || 'Error'));
|
return Promise.reject(new ApiError(res.msg || 'Error', res));
|
||||||
},
|
},
|
||||||
(error) => {
|
(error) => {
|
||||||
// 判断是否弹出请求错误信息
|
// 判断是否弹出请求错误信息
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import type { MenuQueryResponse } from '@/api/system/menu';
|
import type { MenuQueryResponse } from '@/api/system/menu';
|
||||||
import type { AxiosResponse } from 'axios';
|
import type { HttpResponse } from '@/types/global';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -36,7 +36,7 @@ export interface UserUpdatePasswordResponse {
|
|||||||
* 获取用户聚合信息
|
* 获取用户聚合信息
|
||||||
*/
|
*/
|
||||||
export function getUserAggregateInfo() {
|
export function getUserAggregateInfo() {
|
||||||
return axios.get<AxiosResponse<UserAggregateResponse>>('/infra/user-aggregate/user', {
|
return axios.get<HttpResponse<UserAggregateResponse>>('/infra/user-aggregate/user', {
|
||||||
unwrap: true
|
unwrap: true
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,10 @@ export default function setupUserLoginInfoGuard(router: Router) {
|
|||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
Message.error('获取用户信息失败');
|
// 登录过期
|
||||||
|
if ((error as any)?.data?.data?.code !== 401) {
|
||||||
|
Message.error('获取用户信息失败');
|
||||||
|
}
|
||||||
// 获取失败退出登录
|
// 获取失败退出登录
|
||||||
await userStore.logout();
|
await userStore.logout();
|
||||||
next({
|
next({
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import { clearToken, setToken } from '@/utils/auth';
|
|||||||
import { removeRouteListener } from '@/utils/route-listener';
|
import { removeRouteListener } from '@/utils/route-listener';
|
||||||
import { getUserAggregateInfo } from '@/api/user/user-aggregate';
|
import { getUserAggregateInfo } from '@/api/user/user-aggregate';
|
||||||
import { useAppStore, useCacheStore, useMenuStore, useTabBarStore, useTipsStore } from '@/store';
|
import { useAppStore, useCacheStore, useMenuStore, useTabBarStore, useTipsStore } from '@/store';
|
||||||
|
import { ApiError } from '@/api/error';
|
||||||
|
|
||||||
const CHECK_APP_VERSION_KEY = 'check-app-version';
|
const CHECK_APP_VERSION_KEY = 'check-app-version';
|
||||||
|
|
||||||
@@ -67,9 +68,13 @@ export default defineStore('user', {
|
|||||||
|
|
||||||
// 获取用户信息
|
// 获取用户信息
|
||||||
async getUserInfo() {
|
async getUserInfo() {
|
||||||
const { data: { data }, headers } = await getUserAggregateInfo();
|
const resp = await getUserAggregateInfo();
|
||||||
|
const { data: { code, msg, data }, headers } = resp;
|
||||||
// 检查版本更新
|
// 检查版本更新
|
||||||
checkForVersionUpdate(headers?.['x-app-version']);
|
checkForVersionUpdate(headers?.['x-app-version']);
|
||||||
|
if (code !== 200) {
|
||||||
|
throw new ApiError(msg, resp);
|
||||||
|
}
|
||||||
// 设置用户信息
|
// 设置用户信息
|
||||||
this.setUserInfo({
|
this.setUserInfo({
|
||||||
id: data.user.id,
|
id: data.user.id,
|
||||||
|
|||||||
Reference in New Issue
Block a user