Files
lpt-fe/src/utils/request.ts
T
developer 52b59019d0 fix: 兼容业务层 401 响应码
- 除 HTTP 状态码 401 外,同时处理业务层 code=401 的响应
- 两种 401 均清除登录态并跳转登录页
2026-07-12 13:44:15 +08:00

106 lines
3.4 KiB
TypeScript

import axios from 'axios';
import { ElMessage } from "element-plus";
import router from "@/router";
const basic_url = import.meta.env.VITE_BASE_URL;
const axiosInstance = axios.create({
baseURL: basic_url,
timeout: 600000,
withCredentials: true
});
const handleError = (error: any) => {
// 401 未授权(HTTP 层):token 过期或未登录
if (error?.response?.status === 401) {
localStorage.removeItem("isLoggedIn");
router.push("/login");
return Promise.reject(error);
}
// 401 未授权(业务层):后端 GlobalExceptionHandler 返回 code=401
if (error && typeof error === "object" && error.code === 401) {
localStorage.removeItem("isLoggedIn");
router.push("/login");
return Promise.reject(error);
}
// 其他业务异常(如 code !== 200)透传,由调用方自行提示
if (error && typeof error === "object" && "code" in error && !error.response) {
return Promise.reject(error);
}
if (error.response) {
const backendMessage = error.response?.data?.message || JSON.stringify(error.response?.data);
ElMessage.error(`请求失败,服务器返回: ${backendMessage}`);
} else if (error.request) {
ElMessage.error("请求未收到响应,请检查接口运行状态");
} else {
ElMessage.error(error.message || "请求失败");
}
throw new Error("网络不通畅,请检查您的网络连接或服务器状态");
};
const validateResponse = (res: any) => {
const result = res.data;
if (result.code !== 200) {
ElMessage.error(result.message || '请求失败');
return Promise.reject(result);
}
return result;
};
function get(url: string, params: Record<string, any>): Promise<any>;
function get(url: string): Promise<any>;
function get(url: string, params: Record<string, any> = {}) {
return axiosInstance.get(url, { params })
.then(validateResponse)
.catch(handleError);
}
const post = (url: string, data: any = null, config: any = {}) => {
if (config.params) {
// 如果传入 config.params,则作为 URL 参数
return axiosInstance.post(url, data, { params: config.params, ...config })
.then(validateResponse)
.catch(handleError);
} else {
// 默认 POST JSON
return axiosInstance.post(url, data, config)
.then(validateResponse)
.catch(handleError);
}
};
const put = (url: string, data: any, config: any = {}) => {
return axiosInstance.put(url, data, config)
.then(validateResponse)
.catch(handleError);
};
const del = (url: string, params: Record<string, any> = {}, config: any = {}) => {
return axiosInstance.delete(url, { params, ...config })
.then(validateResponse)
.catch(handleError);
};
const requestNotImplemented = (method: string) => {
ElMessage.error(`请求方法 ${method} 未实现`);
throw new Error(`请求方法 ${method} 未实现`);
};
const request = (method: string, url: string, paramsOrData: any) => {
switch (method.toLowerCase()) {
case 'get': return get(url, paramsOrData);
case 'post': return post(url, paramsOrData);
case 'put': return put(url, paramsOrData);
case 'delete': return del(url, paramsOrData);
default: return requestNotImplemented(method);
}
};
export default { get, post, put, del, request };