File size: 2,248 Bytes
f0953a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import axios, { AxiosResponse, AxiosRequestConfig } from "axios";
import { ElMessage } from "element-plus";
import { isMobileDevice } from "@/utils/index";
import { showNotify } from "vant";
import { RequestResult } from "../types/response";
import { STORAGE_KEYS } from "@/constants/storage";

const errorMessage = (message: string) => {
  if (isMobileDevice()) {
    console.log(message);
    showNotify({
      type: "danger",
      message,
    });
    return;
  }
  ElMessage.error(message);
};

const axiosInstance = axios.create({
  baseURL: import.meta.env.VITE_API_BASE_URL as string,
  timeout: 16000,
  withCredentials: true,
  headers: {
    "Content-Type": "application/json",
  },
});

function isLoginAndRedirect(url: string) {
  return url.includes("/api/user/login") || url.includes("/api/user/register");
}

axiosInstance.interceptors.request.use(
  (config) => {
    const token = localStorage.getItem(STORAGE_KEYS.TOKEN);
    if (token) {
      config.headers.Authorization = `Bearer ${token}`;
    } else if (!isLoginAndRedirect(config.url || "")) {
      errorMessage("请先登录");
      window.location.href = "/login";
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

axiosInstance.interceptors.response.use(
  (response: AxiosResponse) => {
    const res = response.data;
    return res;
  },
  (error) => {
    if (error.response.status === 401) {
      errorMessage("登录过期,请重新登录");
      localStorage.removeItem(STORAGE_KEYS.TOKEN);
      window.location.href = "/login";
      return Promise.reject(new Error("登录过期,请重新登录"));
    }
    errorMessage(error.response.statusText);
    return Promise.reject(new Error(error.response.statusText));
  }
);

const request = {
  get: <T>(url: string, config?: AxiosRequestConfig): Promise<RequestResult<T>> => {
    return axiosInstance.get(url, { ...config });
  },
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  post: <T, D = any>(
    url: string,
    data: D,
    config?: AxiosRequestConfig
  ): Promise<RequestResult<T>> => {
    return axiosInstance.post(url, data, { ...config });
  },
  put: axiosInstance.put,
  delete: axiosInstance.delete,
};

export default request;