|
@@ -0,0 +1,117 @@
|
|
|
+import axios from 'axios';
|
|
|
+// import { createDiscreteApi } from 'naive-ui';
|
|
|
+
|
|
|
+import { tansParams, LocalCache, getQueryParamsAsObject } from "@/utils/tools";
|
|
|
+
|
|
|
+import type { Result } from '@/types/data';
|
|
|
+import type { AxiosInstance, AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig, AxiosError } from 'axios';
|
|
|
+
|
|
|
+// import type { NotificationApi } from "naive-ui";
|
|
|
+
|
|
|
+// const { notification } = createDiscreteApi(["notification"]);
|
|
|
+
|
|
|
+const CACHE_KEY = 'userInfo';
|
|
|
+const url = import.meta.env.VITE_BASE_URL;
|
|
|
+const prefix = import.meta.env.VITE_BASE_PREFIX;
|
|
|
+const baseURL = url + prefix;
|
|
|
+
|
|
|
+enum errorCode {
|
|
|
+ '请求错误' = 400,
|
|
|
+ '未授权,请重新登录' = 401,
|
|
|
+ '拒绝访问' = 403,
|
|
|
+ '请求出错' = 404,
|
|
|
+ '请求超时' = 408,
|
|
|
+ '服务器错误' = 500,
|
|
|
+ '服务未实现' = 501,
|
|
|
+ '网络错误' = 502,
|
|
|
+ '服务不可用' = 503,
|
|
|
+ '网络超时' = 504,
|
|
|
+ 'HTTP版本不受支持' = 505
|
|
|
+}
|
|
|
+
|
|
|
+// const showNotification = (type: keyof NotificationApi = 'error', meta: string) => {
|
|
|
+// notification[type]({
|
|
|
+// content: '提示',
|
|
|
+// meta,
|
|
|
+// duration: 3 * 1000,
|
|
|
+// keepAliveOnHover: true
|
|
|
+// })
|
|
|
+// }
|
|
|
+
|
|
|
+export class Request {
|
|
|
+
|
|
|
+ private instance: AxiosInstance;
|
|
|
+
|
|
|
+ private baseConfig: AxiosRequestConfig = { baseURL, timeout: 30 * 1000 };
|
|
|
+
|
|
|
+ constructor(config: AxiosRequestConfig = {}) {
|
|
|
+ this.instance = axios.create({ ...this.baseConfig, ...config });
|
|
|
+
|
|
|
+ this.instance.interceptors.request.use((config: InternalAxiosRequestConfig<Result>) => {
|
|
|
+ if (config.method === "get" && config.params) {
|
|
|
+ let url = config.url + '?' + tansParams(config.params);
|
|
|
+ url = url.slice(0, -1);
|
|
|
+ config.params = {};
|
|
|
+ config.url = url;
|
|
|
+ }
|
|
|
+ const urlParams = getQueryParamsAsObject();
|
|
|
+
|
|
|
+ const userInfo = LocalCache.getCache(CACHE_KEY);
|
|
|
+
|
|
|
+ config.headers.Authorization = urlParams.token || userInfo?.token ;
|
|
|
+
|
|
|
+ return config;
|
|
|
+ }, (err: any) => {
|
|
|
+ return Promise.reject(err)
|
|
|
+ });
|
|
|
+
|
|
|
+ this.instance.interceptors.response.use(res => {
|
|
|
+ const { success, message } = res.data;
|
|
|
+ // !success && showNotification("error", message);
|
|
|
+ return success ? res.data : Promise.reject(res);
|
|
|
+ }, (error: AxiosError) => {
|
|
|
+ const errorMessage = errorCode[error.response?.status as number] || '未知错误';
|
|
|
+ // showNotification("error", errorMessage);
|
|
|
+ return error;
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ public request(config: AxiosRequestConfig): Promise<AxiosResponse> {
|
|
|
+ return this.instance.request(config);
|
|
|
+ }
|
|
|
+
|
|
|
+ public get<T = any>(
|
|
|
+ url: string,
|
|
|
+ config?: AxiosRequestConfig
|
|
|
+ ): Promise<Result<T>> {
|
|
|
+ return this.instance.get(url, config);
|
|
|
+ }
|
|
|
+
|
|
|
+ public post<T = any>(
|
|
|
+ url: string,
|
|
|
+ data?: any,
|
|
|
+ config?: AxiosRequestConfig
|
|
|
+ ): Promise<Result<T>> {
|
|
|
+ return this.instance.post(url, data, config);
|
|
|
+ }
|
|
|
+
|
|
|
+ public put<T = any>(
|
|
|
+ url: string,
|
|
|
+ data?: any,
|
|
|
+ config?: AxiosRequestConfig
|
|
|
+ ): Promise<Result<T>> {
|
|
|
+ return this.instance.put(url, data, config);
|
|
|
+ }
|
|
|
+
|
|
|
+ public delete<T = any>(
|
|
|
+ url: string,
|
|
|
+ config?: AxiosRequestConfig
|
|
|
+ ): Promise<Result<T>> {
|
|
|
+ return this.instance.delete(url, config);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+export default new Request({
|
|
|
+ baseURL,
|
|
|
+ timeout: 10 * 1000
|
|
|
+});
|