schangxiang@126.com
2025-05-21 aab29f4290c968665312bfc98c5598a25a4debf9
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
import { default as axios, AxiosRequestConfig } from 'axios'
import { Session } from '@/utils/storage'
 
// 配置新建一个 axios 实例
const service = axios.create({
  baseURL: '/',
  timeout: 50000,
  headers: {
    'Content-Type': 'application/json;charset=UTF-8',
    'X-Requested-With': 'XMLHttpRequest',
    'Accept-Language': 'zh-Hans',
  },
})
 
// 请求前
service.interceptors.request.use(
  async (config: any) => {
    const token = Session.get('Token') || import.meta.env.VITE_TOKEN
    // console.log('Token', token);
    if (token) config.headers['Authorization'] = `Bearer ${token}`
    config.headers['X-Requested-With'] = 'XMLHttpRequest'
    // if (typeof config.data !== 'object') config.data = JSON.stringify(config.data)
    return config
  },
  (error: any) => {
    return Promise.reject(error)
  }
)
 
// 响应后
service.interceptors.response.use(
  (response: any) => {
    const { data } = response
    // if (data.statusCode == 401) {
    //   ElMessage.error('请登录');
    //   router.push({ name: '/login' })
    //   return;
    // }
    return data
  },
  (error: any) => {
    // Session.remove('Token')
    return Promise.reject(error)
  }
)
 
type Data = unknown
 
type Request = {
  <D = Data>(url: string, config?: AxiosRequestConfig): Promise<D>
  get<D = Data>(url: string, config?: AxiosRequestConfig): Promise<D>
  delete<D = Data>(url: string, config?: AxiosRequestConfig): Promise<D>
  post<D = Data>(
    url: string,
    data?: any,
    config?: AxiosRequestConfig
  ): Promise<D>
  put<D = Data>(
    url: string,
    data?: any,
    config?: AxiosRequestConfig
  ): Promise<D>
} & typeof service
 
const request = service as Request
export { request as default, request }