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

112 lines
3.1 KiB
TypeScript
Raw Normal View History

2025-06-16 14:42:26 +08:00
/*
* @Author:
* @Date: 2023-02-17 11:58:44
2025-06-20 06:10:15 -04:00
* @LastEditors: Please set LastEditors
* @LastEditTime: 2025-06-20 06:00:54
2025-06-16 14:42:26 +08:00
* @Description:
*/
import axios from 'axios';
import type { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
2025-06-20 06:10:15 -04:00
import { handleUserLogout } from '@/utils/user';
const contentType = 'application/json';
const requestTimeout = 30000;
const HttpStatusCode = {
OK: 200,
BadRequest: 400, // 请求参数错误
Unauthorized: 401, // token 无效或过期
NotFound: 404,
InternalServerError: 500,
};
2025-06-16 14:42:26 +08:00
import { useEnterpriseStore } from '@/stores/modules/enterprise';
import pinia from '@/stores';
const store = useEnterpriseStore(pinia);
const enterprise = store.getEnterpriseInfo();
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-20 06:10:15 -04:00
const token = localStorage.getItem('accessToken') as string;
config.headers!.Authorization = token;
2025-06-16 14:42:26 +08:00
if (token) {
config.headers!.Authorization = token;
} else {
config.headers!.satoken = '123';
}
if (enterprise) {
config.headers!['enterprise-id'] = enterprise.id;
}
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) {
case HttpStatusCode.OK:
return data;
default:
return Promise.reject(data);
2025-06-16 14:42:26 +08:00
}
},
(err: any) => {
2025-06-20 06:10:15 -04:00
const message = err.response?.data?.message ?? err.message;
AMessage.error(message);
2025-06-16 14:42:26 +08:00
// 这里用来处理http常见错误进行全局提示
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({});