优化拦截器
优化错误捕获 优化全局状态管理 配置全局公用类型
This commit is contained in:
39
src/api/common.d.ts
vendored
Normal file
39
src/api/common.d.ts
vendored
Normal file
@@ -0,0 +1,39 @@
|
||||
export type ID = number | string;
|
||||
export type IDS = (number | string)[];
|
||||
|
||||
export interface BaseEntity {
|
||||
createBy?: string;
|
||||
createTime?: string;
|
||||
updateBy?: string;
|
||||
updateTime?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页查询参数
|
||||
* @param pageNum 当前页
|
||||
* @param pageSize 每页大小
|
||||
* @param orderByColumn 排序字段
|
||||
* @param isAsc 是否升序
|
||||
*/
|
||||
export interface PageQuery {
|
||||
isAsc?: string;
|
||||
orderByColumn?: string;
|
||||
pageNum?: number;
|
||||
pageSize?: number;
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
export interface ApiResponse<T = any> {
|
||||
code?: number;
|
||||
msg?: string;
|
||||
|
||||
data?: T;
|
||||
rows: T[];
|
||||
total?: number;
|
||||
|
||||
token?: string;
|
||||
|
||||
img?: string;
|
||||
uuid?: string;
|
||||
captchaOnOff?: boolean;
|
||||
}
|
||||
@@ -1,83 +1,91 @@
|
||||
import axios from 'axios';
|
||||
import router from '@/router';
|
||||
import { getToken } from '@/utils/auth';
|
||||
import { Modal, notification } from 'ant-design-vue';
|
||||
import type { AxiosRequestConfig } from "axios";
|
||||
import type { ApiResponse } from "@/api/common/model";
|
||||
import axios from "axios";
|
||||
import router from "@/router";
|
||||
import { getToken, removeToken } from "@/utils/auth";
|
||||
import { Modal, notification } from "ant-design-vue";
|
||||
import type { AxiosInstance, AxiosRequestConfig } from "axios";
|
||||
import type { ApiResponse } from "@/api/common";
|
||||
|
||||
const errCodeMap: { [key: string]: string } = {
|
||||
'403': '当前操作没有权限',
|
||||
'404': '访问资源不存在',
|
||||
'default': '系统未知错误,请反馈给管理员'
|
||||
"403": "当前操作没有权限",
|
||||
"404": "访问资源不存在",
|
||||
default: "系统未知错误,请反馈给管理员",
|
||||
};
|
||||
|
||||
// 创建axios实例
|
||||
const service = axios.create({
|
||||
const service: AxiosInstance = axios.create({
|
||||
baseURL: import.meta.env.VITE_APP_BASE_API,
|
||||
timeout: 10000,
|
||||
});
|
||||
|
||||
// 请求拦截器
|
||||
service.interceptors.request.use(
|
||||
config => {
|
||||
(config) => {
|
||||
config.headers = config.headers || {};
|
||||
config.headers['Accept-Language'] = 'zh-CN';
|
||||
config.headers["Accept-Language"] = "zh-CN";
|
||||
|
||||
const token = getToken();
|
||||
if (token) {
|
||||
config.headers['Authorization'] = `Bearer ${token}`;
|
||||
config.headers["Authorization"] = `Bearer ${token}`;
|
||||
}
|
||||
return config;
|
||||
},
|
||||
error => {
|
||||
(error) => {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
// 响应拦截器
|
||||
service.interceptors.response.use(
|
||||
response => {
|
||||
// console.log(response);
|
||||
(response) => {
|
||||
if (response.config.responseType === "blob") {
|
||||
return response.data;
|
||||
}
|
||||
|
||||
const code = response.data.code || 200;
|
||||
const data = response.data;
|
||||
|
||||
switch (code) {
|
||||
case 200:
|
||||
return data ?? response;
|
||||
return data;
|
||||
case 401:
|
||||
Modal.error({
|
||||
title: '系统提示',
|
||||
content: '登录状态已过期,请重新登录',
|
||||
title: "系统提示",
|
||||
content: "登录状态已过期,请重新登录",
|
||||
onOk: () => {
|
||||
router.push('/login')
|
||||
}
|
||||
})
|
||||
removeToken();
|
||||
router.replace("/login");
|
||||
},
|
||||
});
|
||||
return Promise.reject(data);
|
||||
default:
|
||||
const errMsg = errCodeMap[code] || data.msg || errCodeMap['default'];
|
||||
notification.error({
|
||||
message: '请求错误',
|
||||
description: errMsg,
|
||||
message: "请求错误",
|
||||
description: errCodeMap[code] || data?.msg || errCodeMap.default,
|
||||
});
|
||||
return Promise.reject(data);
|
||||
}
|
||||
},
|
||||
error => {
|
||||
error.message = error.code === "ECONNABORTED" ? '请求超时,请稍后重试' : error.message;
|
||||
(error) => {
|
||||
if (!error.__handled) {
|
||||
const message =
|
||||
error.code === "ECONNABORTED"
|
||||
? "请求超时,请稍后重试"
|
||||
: error.message || "网络异常";
|
||||
|
||||
notification.error({
|
||||
message: "网络错误",
|
||||
description: message,
|
||||
});
|
||||
|
||||
error.__handled = true;
|
||||
}
|
||||
|
||||
// 网络/服务器错误统一处理
|
||||
notification.error({
|
||||
message: "网络错误",
|
||||
description: error.message,
|
||||
});
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
// 类型检查
|
||||
function request<T = any>(config: AxiosRequestConfig): Promise<ApiResponse<T>> {
|
||||
return service(config) as unknown as Promise<ApiResponse<T>>;
|
||||
return service(config) as Promise<ApiResponse<T>>;
|
||||
}
|
||||
|
||||
export default request;
|
||||
// export default service;
|
||||
Reference in New Issue
Block a user