接口接口优化

This commit is contained in:
zhonghua 2026-03-11 16:46:42 +08:00
parent a3ec8f47f4
commit b12b87b17f
2 changed files with 48 additions and 28 deletions

View File

@ -1,20 +1,20 @@
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
import { message } from 'ant-design-vue';
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from "axios";
import { message } from "ant-design-vue";
// 创建axios实例
const request: AxiosInstance = axios.create({
// 由各调用方提供完整路径(例如 /api/v1/teacher/tasks这里不再追加前缀
baseURL: '',
baseURL: "",
timeout: 30000,
headers: {
'Content-Type': 'application/json',
"Content-Type": "application/json",
},
});
// 请求拦截器
request.interceptors.request.use(
(config) => {
const token = localStorage.getItem('token');
const token = localStorage.getItem("token");
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
@ -22,14 +22,20 @@ request.interceptors.request.use(
},
(error) => {
return Promise.reject(error);
}
},
);
// 响应拦截器
request.interceptors.response.use(
(response: AxiosResponse) => {
console.log(response);
// 直接返回响应数据
// @ts-ignore
if (config._new === true) {
return response.data;
} else {
return response.data.data;
}
},
(error) => {
const { response } = error;
@ -39,33 +45,33 @@ request.interceptors.response.use(
switch (status) {
case 401:
message.error('登录已过期,请重新登录');
localStorage.removeItem('token');
localStorage.removeItem('user');
localStorage.removeItem('role');
localStorage.removeItem('name');
window.location.href = '/login';
message.error("登录已过期,请重新登录");
localStorage.removeItem("token");
localStorage.removeItem("user");
localStorage.removeItem("role");
localStorage.removeItem("name");
window.location.href = "/login";
break;
case 403:
message.error('没有权限访问');
message.error("没有权限访问");
break;
case 404:
message.error('请求的资源不存在');
message.error("请求的资源不存在");
break;
case 500:
message.error('服务器错误');
message.error("服务器错误");
break;
default:
message.error(data?.message || '请求失败');
message.error(data?.message || "请求失败");
}
} else if (error.code === 'ECONNABORTED') {
message.error('请求超时');
} else if (error.code === "ECONNABORTED") {
message.error("请求超时");
} else {
message.error('网络错误');
message.error("网络错误");
}
return Promise.reject(error);
}
},
);
// 导出请求方法
@ -77,11 +83,19 @@ export const http = {
return request.get(url, config);
},
post<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T> {
post<T = any>(
url: string,
data?: any,
config?: AxiosRequestConfig,
): Promise<T> {
return request.post(url, data, config);
},
put<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T> {
put<T = any>(
url: string,
data?: any,
config?: AxiosRequestConfig,
): Promise<T> {
return request.put(url, data, config);
},
@ -89,7 +103,11 @@ export const http = {
return request.delete(url, config);
},
patch<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T> {
patch<T = any>(
url: string,
data?: any,
config?: AxiosRequestConfig,
): Promise<T> {
return request.patch(url, data, config);
},
};

View File

@ -2,9 +2,11 @@
* orval mutator - API axios
* token Result<T>
*/
import type { AxiosRequestConfig } from 'axios'
import axiosInstance from './index'
import type { AxiosRequestConfig } from "axios";
import axiosInstance from "./index";
export const request = <T>(config: AxiosRequestConfig): Promise<T> => {
return axiosInstance(config) as Promise<T>
}
// @ts-ignore
config._new = true;
return axiosInstance(config) as Promise<T>;
};