You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
91 lines
3.4 KiB
91 lines
3.4 KiB
import type { InternalAxiosRequestConfig } from "axios";
|
|
import { Constant, initGlobalConfig } from "@ease-forge/shared";
|
|
import { initGlobalConfigWithRuntime } from "@ease-forge/runtime";
|
|
import router from "@/router";
|
|
import { createVNode } from "vue";
|
|
import ErrorDialog from "@/components/ErrorDialog.vue";
|
|
|
|
function globalConfig() {
|
|
window.globalConfig.axiosRequestDef = () => ({
|
|
timeout: 1000 * 60 * 10,
|
|
});
|
|
window.globalConfig.customAxios = axiosInstance => {
|
|
// 全局请求拦截
|
|
axiosInstance.interceptors.request.clear();
|
|
axiosInstance.interceptors.request.use(
|
|
(request: InternalAxiosRequestConfig) => request,
|
|
(error: any) => {
|
|
const err: AxiosInterceptorError = {
|
|
rawError: error,
|
|
title: "系统错误",
|
|
message: "发送请求给服务端失败,请检查电脑网络,再重试",
|
|
status: -1,
|
|
};
|
|
return Promise.reject(err);
|
|
},
|
|
);
|
|
// 全局拦截配置
|
|
axiosInstance.interceptors.response.clear();
|
|
axiosInstance.interceptors.response.use(
|
|
(response) => {
|
|
const data = response.data;
|
|
|
|
if (data && data.success === false) {
|
|
// 显示错误对话框
|
|
system.showErrorDialog(data.failMessage || '系统异常');
|
|
|
|
// 返回一个被拒绝的 Promise,这样后续 .then() 不会执行,进入 .catch()
|
|
return Promise.reject(response);
|
|
}
|
|
|
|
// 正常情况,继续传递数据
|
|
return response;
|
|
},
|
|
(error: any) => {
|
|
const { response } = error;
|
|
const err: AxiosInterceptorError = {
|
|
rawError: error,
|
|
status: response?.status ?? -1,
|
|
title: "系统错误",
|
|
message: "",
|
|
data: response?.data,
|
|
};
|
|
if (!error || !response) {
|
|
err.message = "请求服务端异常";
|
|
} else if (response?.status === 401) {
|
|
err.title = "当前用户未登录";
|
|
err.message = "当前用户未登录,请先登录系统";
|
|
router.push({ name: "login" }).finally();
|
|
} else {
|
|
err.title = "操作失败";
|
|
const { data: { message, validMessageList } } = response;
|
|
if (validMessageList) {
|
|
err.message = "请求参数校验失败";
|
|
} else {
|
|
err.message = message ?? Constant.defHttpErrorMsg[response.status] ?? "服务器异常";
|
|
}
|
|
}
|
|
system.showDialog(createVNode(ErrorDialog, {
|
|
err,
|
|
}), {
|
|
title: '业务异常',
|
|
width: 800,
|
|
height: 120,
|
|
showClose: true,
|
|
showMax: false,
|
|
showCancelButton: false,
|
|
showOkButton: true,
|
|
okButtonText: "关闭",
|
|
}).finally();
|
|
return Promise.reject(err);
|
|
},
|
|
);
|
|
return axiosInstance;
|
|
};
|
|
initGlobalConfig();
|
|
initGlobalConfigWithRuntime();
|
|
}
|
|
|
|
export {
|
|
globalConfig,
|
|
}
|
|
|