Files
lingji-work-fe/src/api/index.ts

122 lines
3.5 KiB
TypeScript
Raw Normal View History

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';
2025-09-05 11:30:31 +08:00
import { message } from 'ant-design-vue';
2025-06-16 14:42:26 +08:00
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';
2025-08-04 18:05:51 +08:00
import { glsWithCatch } from '@/utils/stroage';
2025-06-25 18:26:03 +08:00
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 {
Success = 200,
2025-06-25 18:26:03 +08:00
BadRequest = 400, // 请求参数错误
Unauthorized = 401, // token 无效或过期
NotFound = 404,
InternalServerError = 500,
}
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) => {
const store = useEnterpriseStore(pinia);
2025-08-04 18:05:51 +08:00
const token = glsWithCatch('accessToken');
2025-06-20 06:10:15 -04:00
config.headers!.Authorization = token;
2025-07-08 16:55:04 +08:00
if (store.enterpriseInfo) {
config.headers!['enterprise-id'] = store.enterpriseInfo.id;
}
2025-06-16 14:42:26 +08:00
return config;
},
(err: any) => {
return Promise.reject(err);
},
);
this.instance.interceptors.response.use(
(res: AxiosResponse) => {
const { data, status } = res;
switch (status) {
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) => {
const { response } = err;
const status = response?.status;
let errMessage = response?.data?.message ?? err.message;
switch (status) {
case HttpStatusCode.InternalServerError:
errMessage = '系统繁忙,请稍后再试或联系管理员。';
break;
case HttpStatusCode.NotFound:
errMessage = '接口不存在';
break;
2025-06-25 18:26:03 +08:00
case HttpStatusCode.Unauthorized:
handleUserLogout();
2025-06-25 18:26:03 +08:00
break;
}
2025-09-05 11:30:31 +08:00
message.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);
}
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({});