request.js
1.25 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
import {
host
} from '../config.js'
const request = options => {
return new Promise((resolve, reject) => {
const {
data,
method,
url,
header
} = options
if (data && method !== 'get'&&!header) {
options.data = JSON.stringify(data)
}
options.url = `${host}` + url
wx.request({
header: header?header:{
'Content-Type': 'application/json'
},
...options,
success: function (res) {
// console.log(res);
// wx.request成功发出了请求,无论返回什么http状态码,都会走success
if (res.statusCode === 200) {
resolve(res.data)
} else {
// reject(res.data)
wx.showToast({
title: res.data.error,
icon: 'none',
duration: 2000
})
}
},
fail: function (res) {
// 遇到断网,域名解析有问题,或者尤其是我们去调用restful api时,可能会在url格式,参数类型上出些问题,这些情况下才会调用到fail
reject(res.data)
wx.hideLoading()
wx.showToast({
title: '接口调用失败',
icon: 'none',
duration: 2000
})
}
})
})
}
export default request