app.js
1.8 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
//app.js
App({
onLaunch: function () {
// 展示本地存储能力
var logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
// 获取用户信息
},
post: function (url, data, showLoad) {
/**
* 自定义post函数,返回Promise
* +-------------------
* @param {String} url 接口网址
* @param {arrayObject} data 要传的数组对象 like: {name: 'name', age: 32}
* +-------------------
* @return {Promise} promise 返回promise供后续操作
*/
if (showLoad == undefined || showLoad){
wx.showLoading({
title: '加载中',
})
}
wx.showNavigationBarLoading()
var promise = new Promise((resolve, reject) => {
//init
let that = this;
let postData = data;
//网络请求
wx.request({
url: this.globalData.baseUrl + url,
data: postData,
method: 'POST',
header: {
'content-type': 'application/x-www-form-urlencoded',
'XX-Token': wx.getStorageSync('token')
},
success: function (res) {//返回取得的数据
if (res.data.code == '1') {
resolve(res.data.data);
} else if (res.data.code == '201') {
resolve(res.data);
} else {
wx.showModal({
title: '提示',
content: res.data.msg,
showCancel: false
})
reject(res.data)
}
(showLoad || showLoad == undefined) ? wx.hideLoading() : "";
wx.hideNavigationBarLoading()
},
fail: function (e) {
reject('网络出错');
(showLoad || showLoad == undefined) ? wx.hideLoading() : "";
wx.hideNavigationBarLoading()
}
})
});
return promise;
},
globalData: {
userInfo: null,
baseUrl:'http://lqz.w.brotop.cn'
}
})