123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- import axios from 'axios';
- import router from '@/router';
- import { useUserStore } from '@/stores/modules/userStore';
- import { createDiscreteApi } from 'naive-ui';
- 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 useStore = useUserStore();
- export const url = import.meta.env.VITE_BASE_URL;
- export const prefix = import.meta.env.VITE_BASE_PREFIX;
- export 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>) => {
- // const { token } = useStore.userInfo;
-
- /**
- * 环境区分 - 仅限于测试环境使用
- * */
- // config.headers.port = localStorage.getItem("ENV");
- // token && (config.headers.Authorization = 'Bearer ' + token);
- return config;
- }, (err: any) => {
- return Promise.reject(err)
- });
- this.instance.interceptors.response.use(res => {
- if ( res.config.onDownloadProgress ) return res;
-
- const { code } = res.data;
- if ( code == 200 ) {
- return res.data;
- }
- if ( code == 401 ) {
- showNotification('error', 'Token已失效')
- router.push("/login");
- }
- return Promise.reject(res.data);
- }, (error: AxiosError) => {
- if (error.code === 'ECONNABORTED' ) {
- showNotification('warning', '链接超时,请刷新后重试')
- }
- return Promise.reject(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 const streamHttp = new Request({
- baseURL,
- timeout: 3 * 60 * 1000
- })
- export default new Request({
- baseURL,
- timeout: 3 * 60 * 1000
- });
|