2025-06-16 14:42:26 +08:00
|
|
|
|
/*
|
|
|
|
|
|
* @Author: 田鑫
|
|
|
|
|
|
* @Date: 2023-02-17 11:58:44
|
2025-07-08 16:55:04 +08:00
|
|
|
|
* @LastEditors: rd 1344903914@qq.com
|
|
|
|
|
|
* @LastEditTime: 2025-07-08 14:50:57
|
2025-06-16 14:42:26 +08:00
|
|
|
|
* @Description:
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
import axios from 'axios';
|
|
|
|
|
|
import type { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
|
2025-06-25 18:26:03 +08:00
|
|
|
|
import { handleUserLogout, goUserLogin } from '@/utils/user';
|
|
|
|
|
|
import { useEnterpriseStore } from '@/stores/modules/enterprise';
|
|
|
|
|
|
import pinia from '@/stores';
|
2025-06-20 06:10:15 -04:00
|
|
|
|
|
|
|
|
|
|
const contentType = 'application/json';
|
|
|
|
|
|
const requestTimeout = 30000;
|
|
|
|
|
|
|
2025-06-25 18:26:03 +08:00
|
|
|
|
enum HttpStatusCode {
|
2025-07-05 18:01:26 +08:00
|
|
|
|
Success = 200,
|
2025-06-25 18:26:03 +08:00
|
|
|
|
BadRequest = 400, // 请求参数错误
|
|
|
|
|
|
Unauthorized = 401, // token 无效或过期
|
|
|
|
|
|
NotFound = 404,
|
|
|
|
|
|
InternalServerError = 500,
|
|
|
|
|
|
}
|
2025-06-21 16:57:01 +08:00
|
|
|
|
|
2025-06-16 14:42:26 +08:00
|
|
|
|
//* 导出Request类,可以用来自定义传递配置来创建实例
|
|
|
|
|
|
export class Request {
|
|
|
|
|
|
//* axios 实例
|
|
|
|
|
|
private instance: AxiosInstance;
|
|
|
|
|
|
//* 基础配置
|
2025-06-20 06:10:15 -04:00
|
|
|
|
private baseConfig: AxiosRequestConfig = {
|
|
|
|
|
|
baseURL: import.meta.env.EO_API_URL,
|
|
|
|
|
|
timeout: requestTimeout,
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
'Content-Type': contentType,
|
|
|
|
|
|
},
|
|
|
|
|
|
};
|
2025-06-16 14:42:26 +08:00
|
|
|
|
|
|
|
|
|
|
public constructor(config: AxiosRequestConfig) {
|
|
|
|
|
|
this.instance = axios.create(Object.assign(this.baseConfig, config));
|
|
|
|
|
|
|
|
|
|
|
|
this.instance.interceptors.request.use(
|
|
|
|
|
|
(config: AxiosRequestConfig) => {
|
2025-06-23 05:58:04 -04:00
|
|
|
|
const store = useEnterpriseStore(pinia);
|
|
|
|
|
|
|
2025-06-20 06:10:15 -04:00
|
|
|
|
const token = localStorage.getItem('accessToken') as string;
|
|
|
|
|
|
config.headers!.Authorization = token;
|
2025-06-20 16:58:03 +08:00
|
|
|
|
|
2025-07-08 16:55:04 +08:00
|
|
|
|
if (store.enterpriseInfo) {
|
|
|
|
|
|
config.headers!['enterprise-id'] = store.enterpriseInfo.id;
|
2025-06-20 16:58:03 +08:00
|
|
|
|
}
|
2025-06-23 05:58:04 -04:00
|
|
|
|
|
2025-06-16 14:42:26 +08:00
|
|
|
|
return config;
|
|
|
|
|
|
},
|
|
|
|
|
|
(err: any) => {
|
|
|
|
|
|
return Promise.reject(err);
|
|
|
|
|
|
},
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
this.instance.interceptors.response.use(
|
|
|
|
|
|
(res: AxiosResponse) => {
|
2025-06-20 06:10:15 -04:00
|
|
|
|
const { data } = res;
|
|
|
|
|
|
switch (data.code) {
|
2025-07-05 18:01:26 +08:00
|
|
|
|
case HttpStatusCode.Success:
|
2025-06-20 06:10:15 -04:00
|
|
|
|
return data;
|
|
|
|
|
|
default:
|
|
|
|
|
|
return Promise.reject(data);
|
2025-06-16 14:42:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
(err: any) => {
|
2025-07-05 18:01:26 +08:00
|
|
|
|
const { response } = err;
|
|
|
|
|
|
const status = response?.status;
|
|
|
|
|
|
let errMessage = response?.data?.message ?? err.message;
|
2025-06-25 18:26:03 +08:00
|
|
|
|
|
2025-07-05 18:01:26 +08:00
|
|
|
|
switch (status) {
|
|
|
|
|
|
case HttpStatusCode.InternalServerError:
|
|
|
|
|
|
errMessage = '系统繁忙,请稍后再试或联系管理员。';
|
|
|
|
|
|
break;
|
|
|
|
|
|
case HttpStatusCode.NotFound:
|
|
|
|
|
|
errMessage = '接口不存在';
|
|
|
|
|
|
break;
|
2025-06-25 18:26:03 +08:00
|
|
|
|
case HttpStatusCode.Unauthorized:
|
2025-07-05 18:01:26 +08:00
|
|
|
|
handleUserLogout();
|
2025-06-25 18:26:03 +08:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-05 18:01:26 +08:00
|
|
|
|
AMessage.error(errMessage);
|
2025-06-16 14:42:26 +08:00
|
|
|
|
return Promise.reject(err.response);
|
|
|
|
|
|
},
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//* 定义请求方法
|
|
|
|
|
|
public request<T = any>(config: AxiosRequestConfig): Promise<T> {
|
|
|
|
|
|
return this.instance.request(config);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public get<T = any>(url: string, params?: any, config?: AxiosRequestConfig): Promise<T> {
|
|
|
|
|
|
return this.instance.get(url, { params, ...config });
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public post<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T> {
|
|
|
|
|
|
return this.instance.post(url, data, config);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public put<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T> {
|
|
|
|
|
|
return this.instance.put(url, data, config);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public delete<T = any>(url: string, config?: AxiosRequestConfig): Promise<T> {
|
|
|
|
|
|
return this.instance.delete(url, config);
|
|
|
|
|
|
}
|
2025-06-18 18:00:33 +08:00
|
|
|
|
|
|
|
|
|
|
public patch<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T> {
|
|
|
|
|
|
return this.instance.patch(url, data, config);
|
|
|
|
|
|
}
|
2025-06-16 14:42:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//* 默认导出Request实例
|
|
|
|
|
|
export default new Request({});
|