114 lines
3.1 KiB
TypeScript
114 lines
3.1 KiB
TypeScript
/*
|
||
* @Author: 田鑫
|
||
* @Date: 2023-02-17 11:58:44
|
||
* @LastEditors: Please set LastEditors
|
||
* @LastEditTime: 2025-06-23 05:51:32
|
||
* @Description:
|
||
*/
|
||
|
||
import axios from 'axios';
|
||
import type { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
|
||
import { handleUserLogout, goUserLogin } from '@/utils/user';
|
||
import { useEnterpriseStore } from '@/stores/modules/enterprise';
|
||
import pinia from '@/stores';
|
||
|
||
const contentType = 'application/json';
|
||
const requestTimeout = 30000;
|
||
|
||
enum HttpStatusCode {
|
||
OK = 200,
|
||
BadRequest = 400, // 请求参数错误
|
||
Unauthorized = 401, // token 无效或过期
|
||
NotFound = 404,
|
||
InternalServerError = 500,
|
||
}
|
||
|
||
//* 导出Request类,可以用来自定义传递配置来创建实例
|
||
export class Request {
|
||
//* axios 实例
|
||
private instance: AxiosInstance;
|
||
//* 基础配置
|
||
private baseConfig: AxiosRequestConfig = {
|
||
baseURL: import.meta.env.EO_API_URL,
|
||
timeout: requestTimeout,
|
||
headers: {
|
||
'Content-Type': contentType,
|
||
},
|
||
};
|
||
|
||
public constructor(config: AxiosRequestConfig) {
|
||
this.instance = axios.create(Object.assign(this.baseConfig, config));
|
||
|
||
this.instance.interceptors.request.use(
|
||
(config: AxiosRequestConfig) => {
|
||
const store = useEnterpriseStore(pinia);
|
||
const enterprise = store.getEnterpriseInfo();
|
||
|
||
const token = localStorage.getItem('accessToken') as string;
|
||
config.headers!.Authorization = token;
|
||
|
||
if (enterprise) {
|
||
config.headers!['enterprise-id'] = enterprise.id;
|
||
}
|
||
|
||
return config;
|
||
},
|
||
(err: any) => {
|
||
return Promise.reject(err);
|
||
},
|
||
);
|
||
|
||
this.instance.interceptors.response.use(
|
||
(res: AxiosResponse) => {
|
||
const { data } = res;
|
||
switch (data.code) {
|
||
case HttpStatusCode.OK:
|
||
return data;
|
||
default:
|
||
return Promise.reject(data);
|
||
}
|
||
},
|
||
(err: any) => {
|
||
const { message, code } = err.response?.data ?? {};
|
||
AMessage.error(message ?? err.message);
|
||
|
||
switch (code) {
|
||
case HttpStatusCode.Unauthorized:
|
||
goUserLogin();
|
||
break;
|
||
}
|
||
|
||
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);
|
||
}
|
||
}
|
||
|
||
//* 默认导出Request实例
|
||
export default new Request({});
|