-
Notifications
You must be signed in to change notification settings - Fork 242
/
app.js
95 lines (89 loc) · 2.02 KB
/
app.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
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
App({
onLaunch: function () {
//判断机型(适配iphoneX)
wx.getSystemInfo({
success: (res) => {
this.globalData.systemInfo = res;
if (res.model.search('iPhone X') !== -1) {
this.globalData.isIphoneX = true
}
}
});
},
baseUrl: 'https://www.wanandroid.com',
/**
* http请求封装
* @param method 请求方法类型
* @param url 请求路径
* @param data 请求参数
* @param loading 请求加载效果 {0: 正常加载, 1: 表单提交加载效果 }
* @param loadingMsg 请求提示信息
*/
httpBase: function (method, url, data, loading, loadingMsg) {
let _this = this;
let requestUrl = this.baseUrl + url;
if (loading) {
wx.showLoading({
title: loadingMsg || '提交中...',
mask: true
});
} else {
wx.showNavigationBarLoading()
}
function request(resolve, reject) {
wx.request({
header: {
'Content-Type': 'application/json'
},
method: method,
url: requestUrl,
data: data,
success: function (result) {
if (loading) {
wx.hideLoading();
} else {
wx.hideNavigationBarLoading()
}
let res = result.data || {};
let code = res.errorCode;
if (code !== 0) {
reject(res);
if (res.message) {
wx.showToast({
title: res.message,
icon: 'none'
});
}
} else {
resolve(res);
}
},
fail: function (res) {
reject(res);
if (loading) {
wx.hideLoading();
} else {
wx.hideNavigationBarLoading()
}
wx.showToast({
title: '网络出错',
icon: 'none'
});
}
})
}
return new Promise(request);
},
httpGet: function ({url = "", data = {}, loading = false, loadingMsg = ""} = {}) {
return this.httpBase("GET", url, data, loading, loadingMsg);
},
httpPost: function ({url = "", data = {}, loading = false, loadingMsg = ""} = {}) {
return this.httpBase("POST", url, data, loading, loadingMsg);
},
globalData: {
systemInfo: null,
userInfo: null,
version: "1.0.0",
isIphoneX: false
}
})