Request.js
3.88 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import axios from 'axios'
const APP_ENV = process.env.VUE_APP_NODE_ENV
const APP_API = process.env.VUE_APP_API
const codeMessage = {
200: '服务器成功返回请求的数据。',
201: '新建或修改数据成功。',
202: '一个请求已经进入后台排队(异步任务)。',
204: '删除数据成功。',
400: '发出的请求有错误,服务器没有进行新建或修改数据的操作。',
401: '用户没有权限(令牌、用户名、密码错误)。',
403: '用户得到授权,但是访问是被禁止的。',
404: '发出的请求是不存在的,服务器没有进行操作。',
406: '请求的格式不可得。',
410: '请求的资源被永久删除。',
422: '当创建一个对象时,发生一个验证错误。',
500: '服务器发生错误,请检查服务器。',
502: '网关错误。',
503: '服务不可用,服务器暂时过载或维护。',
504: '网关超时。'
}
const instance = axios.create({
baseURL: APP_ENV === 'development' ? '/api' : APP_API
})
class Request {
constructor(baseURL) {
this.baseURL = baseURL
}
// 检查返回状态
checkStatus(response) {
console.log(2)
const responseData = response.data
const errorText = response && (codeMessage[response.status] || response.statusText)
if (response && (response.stutus === 200 || response.stutus === 304 || response.status === 400)) {
if (responseData.stutus == 0) {
return responseData
}
if (responseData.status === 401 || responseData.status === 402) {
return Promise.reject(errorText)
}
return Promise.reject(responseData)
}
Promise.reject(response)
}
// 拦截器
interceptors(instance, scope) {
// 请求拦截器
instance.interceptors.request.use(config => {
config.url = baseURL
config.scope = scope
return config
}, error => {
return Promise.reject(error)
})
// 相应拦截器
instance.interceptors.response.use(res => {
return res
}, error => {
const { response, message } = error || {}
if (message && message.includes('timeout')) {
return Promise.reject({ statusText: '网络超时,请重新尝试!' })
}
let errorInfo = response
if (!errorInfo) {
try {
const { request: { status, statusText }, config } = JSON.parse(JSON.stringify(error))
errorInfo = {
status,
statusText,
request: { responseURL: config.url }
}
} catch (e) {
errorInfo = error
}
}
return Promise.reject(errorInfo)
})
}
// 设置请求
setRequest(method, url, data, scope, file = false) {
this.interceptors(instance, scope)
const options = { method, url }
let contentType = ''
if (file) {
contentType = 'multipart/form-data'
} else if (method == 'post') {
contentType = 'application/json'
} else {
contentType = 'application/x-www-form-urlencoded; charset=UTF-8'
}
const headers = {
'X-Requested-With': 'XMLHttpRequest',
'Content-Type': contentType
}
Object.assign(options, {
headers,
[method == 'post' ? 'data' : 'params']: data
})
return instance(options).then(this.checkStatus).catch(this.error)
}
// get
get(url, data, scope) {
return this.setRequest('get', url, data, scope)
}
// post
post(url, data, scope) {
console.log(1,url)
return this.setRequest('post', url, data, scope)
}
// GET
GET(url, data, scope) {
return this.setRequest('get', url, data, scope).then(this.success)
}
// POST
POST(url, data, scope) {
return this.setRequest('post', url, data, scope).them(this.success)
}
// 错误
error(e) {
return Promise.reject(e)
}
// 成功
success(da) {
return da.data
}
// 文件成功
fileSuccess(data) {
return data
}
}
export default Request;