-
Notifications
You must be signed in to change notification settings - Fork 57
/
request.js
65 lines (60 loc) · 2.06 KB
/
request.js
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
function requestPostApi(url, params, sourceObj, successFun, failFun) {
requestApi(url, params, 'POST', sourceObj, successFun, failFun)
}
function requestGetApi(url, params, sourceObj, successFun, failFun) {
requestApi(url, params, 'GET', sourceObj, successFun, failFun)
}
function requestApi(url, params, method, sourceObj, successFun, failFun) {
wx.showLoading({
mask: true,
})
if (wx.getStorageSync('openId')) {
params["openId"] = wx.getStorageSync('openId');
wx.request({
url: url,
method: method,
data: params,
header: {
'Content-Type': "application/x-www-form-urlencoded",
},
success: function (res) {
typeof successFun == 'function' && successFun(res.data, sourceObj);
},
fail: function (res) {
typeof failFun == 'function' && failFun(res.data, sourceObj);
},
complete: function(res){
wx.hideLoading();
}
})
} else {
console.log("无openId")
wx.login({
success: res => {
console.log(res.code)
wx.request({
url: "http://127.0.0.1:10521/api/wx/auth",//更改为你自己的服务端地址
method: "POST",
data: {
js_code: res.code
},
header: {
'Content-Type': "application/x-www-form-urlencoded",
},
success: function (res) {
wx.setStorageSync('openId', res.data.data);
params["openId"] = wx.getStorageSync('openId');
requestApi(url, params, method, sourceObj, successFun, failFun);
},
fail: function (res) {
console.error(res);
}
})
}
})
}
}
module.exports = {
requestPostApi,
requestGetApi
}