-
Notifications
You must be signed in to change notification settings - Fork 135
/
http.ts
executable file
·103 lines (85 loc) · 2.69 KB
/
http.ts
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import axios from 'axios'
import type { AxiosError, AxiosInstance, AxiosRequestConfig } from 'axios'
import CONFIG from '@/config'
import type { IStore } from '@/store'
import { message, notification } from 'antd'
import { logout } from '@/utils'
interface RespData {
success: boolean
errorCode: number
msg?: string
data?: any
[key: string]: any
}
let exiting = false
function handleError(error: AxiosError) {
const response = error.response
notification.error({
message: `Error Code: ${response?.status ?? -1}`,
description: response?.statusText ?? '服务器出小差'
})
}
interface IAxiosInstance {
get(url: string, config?: AxiosRequestConfig): Promise<Record<string, any>>
delete(url: string, config?: AxiosRequestConfig): Promise<Record<string, any>>
post(url: string, data?: any, config?: AxiosRequestConfig): Promise<Record<string, any>>
put(url: string, data?: any, config?: AxiosRequestConfig): Promise<Record<string, any>>
}
const httpInstance: IAxiosInstance & AxiosInstance = axios.create({
timeout: 60000,
baseURL: CONFIG.http.baseURL
})
httpInstance.defaults.headers.common.isLoading = 'true'
httpInstance.defaults.headers.common.errorAlert = 'true'
Object.setPrototypeOf(httpInstance, axios)
// 主要处理防止store未挂载访问
export function setupInterceptor(store: IStore) {
httpInstance.interceptors.request.use(function (config) {
const method = config.method
const userState = store.getState().user.userInfo
if (userState.token) {
if (config.headers) {
config.headers.token = userState.token as string
}
}
const data: Record<string, any> = {}
if (method === 'post' || method === 'put') {
if (config.data instanceof FormData) {
for (let key in data) {
config.data.append(key, data[key])
}
} else {
config.data = Object.assign(data, config.data)
}
}
return config
}, function (error) {
handleError(error)
return Promise.reject(error)
})
httpInstance.interceptors.response.use(function (res) {
const headers = res.config.headers
const data: RespData = res.data
if (data.success && headers?.successAlert) {
message.success(data.msg || '操作成功')
}
if (data.errorCode === 401 && !exiting) {
exiting = true
logout()
}
if (!data.success) {
if (headers?.errorAlert) {
notification.error({
message: `Error Code: ${data.errorCode ?? -1}`,
description: data.msg ?? '服务器出小差'
})
}
throw res
}
return res.data?.data || res
}, function (error) {
handleError(error)
return Promise.reject(error)
})
}
export default httpInstance