2023-01-29 13:44:19 +08:00
|
|
|
|
import axios from "axios";
|
2023-06-29 17:20:47 +08:00
|
|
|
|
import { get } from "lodash-es";
|
2025-04-12 23:59:03 +08:00
|
|
|
|
import { errorLog, errorCreate } from "./tools";
|
2023-01-29 13:44:19 +08:00
|
|
|
|
import { env } from "/src/utils/util.env";
|
2025-04-12 23:59:03 +08:00
|
|
|
|
import { useUserStore } from "/@/store/user";
|
2026-02-11 00:07:29 +08:00
|
|
|
|
import { useProjectStore } from "../store/project";
|
2025-04-17 22:34:21 +08:00
|
|
|
|
|
|
|
|
|
|
export class CodeError extends Error {
|
|
|
|
|
|
code: number;
|
|
|
|
|
|
data?: any;
|
|
|
|
|
|
constructor(message: string, code: number, data?: any) {
|
|
|
|
|
|
super(message);
|
|
|
|
|
|
this.code = code;
|
|
|
|
|
|
this.data = data;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-29 13:44:19 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @description 创建请求实例
|
|
|
|
|
|
*/
|
|
|
|
|
|
function createService() {
|
|
|
|
|
|
// 创建一个 axios 实例
|
|
|
|
|
|
const service = axios.create();
|
|
|
|
|
|
// 请求拦截
|
|
|
|
|
|
service.interceptors.request.use(
|
2025-04-12 23:59:03 +08:00
|
|
|
|
config => config,
|
|
|
|
|
|
error => {
|
2023-01-29 13:44:19 +08:00
|
|
|
|
// 发送失败
|
|
|
|
|
|
console.log(error);
|
|
|
|
|
|
return Promise.reject(error);
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
// 响应拦截
|
|
|
|
|
|
service.interceptors.response.use(
|
2025-04-12 23:59:03 +08:00
|
|
|
|
response => {
|
2023-01-29 13:44:19 +08:00
|
|
|
|
if (response.config.responseType === "blob") {
|
|
|
|
|
|
return response;
|
|
|
|
|
|
}
|
2024-12-23 00:24:31 +08:00
|
|
|
|
//@ts-ignore
|
2025-05-10 17:29:10 +08:00
|
|
|
|
if (response.config.returnOriginRes) {
|
2024-12-23 00:24:31 +08:00
|
|
|
|
return response;
|
|
|
|
|
|
}
|
2023-01-29 13:44:19 +08:00
|
|
|
|
// dataAxios 是 axios 返回数据中的 data
|
|
|
|
|
|
const dataAxios = response.data;
|
2024-11-01 00:59:09 +08:00
|
|
|
|
|
2024-11-01 10:23:27 +08:00
|
|
|
|
// @ts-ignore
|
2024-11-01 00:59:09 +08:00
|
|
|
|
if (response.config.unpack === false) {
|
|
|
|
|
|
//如果不需要解包
|
|
|
|
|
|
return dataAxios;
|
|
|
|
|
|
}
|
2025-12-03 00:57:17 +08:00
|
|
|
|
//@ts-ignore
|
|
|
|
|
|
const showErrorNotify = response?.config?.showErrorNotify;
|
2023-01-29 13:44:19 +08:00
|
|
|
|
// 这个状态码是和后端约定的
|
2025-04-19 14:25:56 +08:00
|
|
|
|
if (dataAxios?.code === undefined) {
|
2023-06-27 09:29:43 +08:00
|
|
|
|
// 如果没有 code 代表这不是项目后端开发的接口
|
2025-12-03 00:57:17 +08:00
|
|
|
|
errorCreate(`非标准返回:${dataAxios}, ${response.config.url}`, showErrorNotify);
|
2023-01-29 13:44:19 +08:00
|
|
|
|
return dataAxios;
|
2025-04-19 14:25:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
const { code } = dataAxios;
|
|
|
|
|
|
// 有 code 代表这是一个后端接口 可以进行进一步的判断
|
|
|
|
|
|
switch (code) {
|
|
|
|
|
|
case 0:
|
|
|
|
|
|
// [ 示例 ] code === 0 代表没有错误
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
|
return dataAxios?.data;
|
|
|
|
|
|
default:
|
|
|
|
|
|
// 不是正确的 code
|
|
|
|
|
|
const errorMessage = dataAxios.msg || dataAxios.message || "未知错误";
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
|
if (response?.config?.onError) {
|
|
|
|
|
|
const err = new CodeError(errorMessage, dataAxios.code, dataAxios.data);
|
2024-10-10 14:28:46 +08:00
|
|
|
|
// @ts-ignore
|
2025-04-19 14:25:56 +08:00
|
|
|
|
response.config.onError(err);
|
|
|
|
|
|
}
|
2025-12-28 23:36:53 +08:00
|
|
|
|
errorCreate(`${errorMessage} (请求接口: ${response.config.url})`, showErrorNotify, dataAxios);
|
2023-01-29 13:44:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
},
|
2025-04-12 23:59:03 +08:00
|
|
|
|
error => {
|
2023-01-29 13:44:19 +08:00
|
|
|
|
const status = get(error, "response.status");
|
|
|
|
|
|
switch (status) {
|
|
|
|
|
|
case 400:
|
|
|
|
|
|
error.message = "请求错误";
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 401:
|
|
|
|
|
|
error.message = "未授权,请登录";
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 403:
|
|
|
|
|
|
error.message = "拒绝访问";
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 404:
|
2024-12-02 23:17:40 +08:00
|
|
|
|
error.message = `请求地址出错`;
|
2023-01-29 13:44:19 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case 408:
|
|
|
|
|
|
error.message = "请求超时";
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 500:
|
|
|
|
|
|
error.message = "服务器内部错误";
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 501:
|
|
|
|
|
|
error.message = "服务未实现";
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 502:
|
|
|
|
|
|
error.message = "网关错误";
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 503:
|
|
|
|
|
|
error.message = "服务不可用";
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 504:
|
|
|
|
|
|
error.message = "网关超时";
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 505:
|
|
|
|
|
|
error.message = "HTTP版本不受支持";
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
2025-12-28 23:36:53 +08:00
|
|
|
|
error.message += `( 请求接口:${error.response?.config?.url})`;
|
2024-10-22 18:46:29 +08:00
|
|
|
|
errorLog(error, error?.response?.config?.showErrorNotify);
|
2023-01-29 13:44:19 +08:00
|
|
|
|
if (status === 401) {
|
2025-03-09 01:08:57 +08:00
|
|
|
|
const userStore = useUserStore();
|
2025-04-24 23:54:09 +08:00
|
|
|
|
userStore.logout(true, true);
|
2023-01-29 13:44:19 +08:00
|
|
|
|
}
|
2024-10-03 01:29:12 +08:00
|
|
|
|
|
|
|
|
|
|
if (error?.config?.onError) {
|
|
|
|
|
|
error.config.onError(error);
|
|
|
|
|
|
}
|
2023-01-29 13:44:19 +08:00
|
|
|
|
return Promise.reject(error);
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
return service;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @description 创建请求方法
|
|
|
|
|
|
* @param {Object} service axios 实例
|
|
|
|
|
|
*/
|
2023-06-25 15:30:18 +08:00
|
|
|
|
function createRequestFunction(service: any) {
|
|
|
|
|
|
return function (config: any) {
|
2023-01-29 13:44:19 +08:00
|
|
|
|
const configDefault = {
|
|
|
|
|
|
headers: {
|
2025-04-12 23:59:03 +08:00
|
|
|
|
"Content-Type": get(config, "headers.Content-Type", "application/json"),
|
2026-02-11 00:07:29 +08:00
|
|
|
|
} as any,
|
2025-05-26 22:44:56 +08:00
|
|
|
|
timeout: 30000,
|
2023-01-29 13:44:19 +08:00
|
|
|
|
baseURL: env.API,
|
2025-04-12 23:59:03 +08:00
|
|
|
|
data: {},
|
2023-01-29 13:44:19 +08:00
|
|
|
|
};
|
2026-02-11 00:07:29 +08:00
|
|
|
|
const projectStore = useProjectStore();
|
|
|
|
|
|
|
2026-02-21 23:20:26 +08:00
|
|
|
|
if (projectStore.isEnterprise && !config.url.startsWith("/sys") && !config.url.startsWith("http")) {
|
2026-02-11 00:07:29 +08:00
|
|
|
|
configDefault.headers["project-id"] = projectStore.currentProjectId;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-29 13:44:19 +08:00
|
|
|
|
const userStore = useUserStore();
|
|
|
|
|
|
const token = userStore.getToken;
|
|
|
|
|
|
if (token != null) {
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
|
configDefault.headers.Authorization = token;
|
|
|
|
|
|
}
|
|
|
|
|
|
return service(Object.assign(configDefault, config));
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 用于真实网络请求的实例和请求方法
|
|
|
|
|
|
export const service = createService();
|
|
|
|
|
|
export const request = createRequestFunction(service);
|