-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
134 lines (124 loc) · 4.54 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
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
//app.js
App({
onLaunch: function (options) {
console.log(options);
this.globalData.systemInfo = wx.getSystemInfoSync();
console.log(this.globalData.systemInfo);
this.globalData.rpxRatio = this.globalData.systemInfo.windowWidth / 750;
this.doLogin();
},
globalData: {
userInfo: null,
systemInfo: null,
rpxRatio: 1,
token: null,
},
doLogin: function (successCallback = function(){}) {
var thisPage = this;
if (thisPage.globalData.token != null) {
return;
}
wx.login({
success: function (loginRes) {
// console.log(loginRes);
if (loginRes.code) {
wx.request({
url: "https://www.91shoujike.com/api/login?code=" + loginRes.code,
method: "POST",
data: {},
success: function (res) {
console.log(res);
if (res.statusCode == 200) {
if (res.data.code == 200) {
thisPage.globalData.token = res.data.data.token;
successCallback();
console.log("登录成功!");
} else {
console.log("登录失败!" + res.data.msg);
}
} else {
thisPage._showMaintainTip();
}
},
fail: function () {
thisPage._showMaintainTip();
}
});
} else {
console.log("登录失败!" + loginRes.errMsg);
}
}
});
},
callApi: function (apiUrl, requestData, successCallback = function(){}) {
var thisPage = this;
if (thisPage.globalData.token == null) {
thisPage.doLogin(function () {
thisPage.callApi(apiUrl, requestData, successCallback);
});
return;
}
wx.request({
url: apiUrl + "?token=" + thisPage.globalData.token,
method: "POST",
data: requestData,
header: {
"Content-Type": "application/x-www-form-urlencoded"
},
success: function (res) {
console.log(res);
if (res.statusCode == 200) {
if (res.data.code == -2) {
console.log(res.data.msg);
thisPage.globalData.token = null;
thisPage.doLogin(function () {
thisPage.callApi(apiUrl, requestData, successCallback);
});
return;
} else if (res.data.code == 200) {
successCallback(res.data);
console.log("请求成功!" + apiUrl);
} else {
console.log(res.data.msg);
}
} else {
console.log("服务器异常!" + apiUrl);
}
},
fail: function () {
console.log("Call api error: " + apiUrl);
}
});
},
doUploadFile: function(apiUrl, fileFieldName, filePath, postData = {}, successCallback = function(){}) {
var thisPage = this;
wx.uploadFile({
url: apiUrl + "?token=" + thisPage.globalData.token,
name: fileFieldName,
filePath: filePath,
header: {
"Content-Type": "multipart/form-data"
},
formData: postData,
success: function(res) {
console.log(res);
successCallback(JSON.parse(res.data));
},
fail: function(res) {
wx.showToast({
title: "上传图片失败,请稍后再试",
icon: "none"
});
}
});
},
_showMaintainTip: function () {
wx.showModal({
title: "提示",
content: "服务器正在维护中。预计恢复时间2小时",
success: function(res) {
res.confirm ? console.log("用户点击确定") : res.cancel && console.log("用户点击取消");
}
});
},
})