forked from zhuhaojia360/wxTimeCard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
259 lines (249 loc) · 10.6 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
//app.js
App({
onLaunch: function () {
console.log('onLaunch');
wx.clearStorageSync();
},
onShow: function () {
// Do something when show.
console.log('onShow');
},
onHide: function () {
// Do something when hide.
console.log('onHide');
},
onError: function (msg) {
console.log('onError');
},
login: function (callBackFunc, options) {
options = options || {};
var that = this;
// 获取临时登录凭证
wx.login({
success: function (wxloginres) {
if (wxloginres.code) {
// 获取用户信息
wx.getUserInfo({
withCredentials: true,
success: function (wxgetuserres) {
// 将key和用户绑定
wx.request({
url: that.globalData.apiurl + '/wxaaction/login.do',
method: 'POST',
header: {
'content-type': 'application/x-www-form-urlencoded'
},
data: {
code: wxloginres.code,
rawData: wxgetuserres.rawData,
signature: wxgetuserres.signature,
encryptedData: wxgetuserres.encryptedData,
iv: wxgetuserres.iv
},
success: function (syncres) {
if (syncres.data.retcode !== '0' || syncres.statusCode !== 200) {
var flag = false;
if (typeof options.complete === 'function') {
flag = options.complete(syncres, 'wxsyncuser');
}
if (!flag) {
that.parseError(syncres.data);
}
return;
}
// 保存cookie
wx.setStorageSync('sessionid', syncres.header["Set-Cookie"]);
typeof callBackFunc === "function" && callBackFunc(syncres);
},
fail: function () {
},
complete: function (syncres) {
if (syncres.statusCode !== 200) {
var flag = false;
if (typeof options.complete === 'function') {
flag = options.complete(syncres, 'wxsyncuser');
}
if (!flag) {
wx.showModal({
title: '出错了',
content: '服务器繁忙,请稍后重试',
showCancel: false
});
}
}
}
});
},
fail: function (wxgetuserres) {
var flag = false;
if (typeof options.complete === 'function') {
flag = options.complete(wxgetuserres, 'wxgetuser');
}
if (!flag) {
wx.showModal({
title: '出错了',
content: '获取用户信息失败',
showCancel: false
});
}
},
complete: function (res) {
}
});
} else {
console.log('获取用户登录态code失败:' + wxloginres);
var flag = false;
if (typeof options.complete === 'function') {
flag = options.complete(wxloginres, 'wxlogincode');
}
if (!flag) {
wx.showModal({
title: '出错了',
content: '获取用户信息失败',
showCancel: false
});
}
}
},
fail: function (wxloginfail) {
var flag = false;
if (typeof options.complete === 'function') {
flag = options.complete(wxloginfail, 'wxlogin');
}
if (!flag) {
wx.showModal({
title: '出错了',
content: '获取用户信息失败',
showCancel: false
});
}
}
});
},
auth: function (callBackFunc) {
// 获取用户的当前设置
wx.getSetting({
success(res) {
if (!res.authSetting['scope.userInfo']) {
wx.hideLoading();
wx.showModal({
title: '出错了',
content: '小程序需要您的授权才能提供更好的服务哦',
showCancel: false,
success: function (res) {
if (res.confirm) {
// 调起客户端小程序设置界面
wx.openSetting();
}
}
});
// 向用户发起授权请求,该wx.authorize接口已经不支持授权弹窗了
// wx.authorize({
// scope: 'scope.userInfo',
// success() {
// callBackFunc();
// },
// fail() {
// // 调起客户端小程序设置界面
// wx.openSetting();
// }
// });
} else {
callBackFunc();
}
}
});
},
// 发起业务请求
request: function (options, notretry, noCheckAuth, noCheckSession) {
var that = this;
if (!noCheckAuth) {// 无授权
this.auth(function () {
that.request(options, false, true, false);
});
} else if (!noCheckSession) {// 是否校验session_key
// 当前session_key是否有效
wx.checkSession({
success: function () {
that.request(options, false, true, true);
},
fail: function () {
wx.removeStorageSync('sessionid');
that.request(options, false, true, true);
}
});
} else {
var sessionId = wx.getStorageSync('sessionid');
if (!sessionId) {
that.login(function () {
that.request(options, false, true, true);
}, options);
return;
} else {
var data = options.data || {};
var apiurl = options.apiurl || that.globalData.apiurl;
var header = options.header || {};
header['content-type'] = header['content-type'] || 'application/x-www-form-urlencoded';
header['cookie'] = sessionId;
wx.request({
url: apiurl + options.url,
method: options.method || 'POST',
header: header,
data: data,
success: function (res) {
if (res.data.retcode !== '0' || res.statusCode !== 200) {
if (typeof options.fail === 'function') {
options.fail(res);
} else {
that.parseError(res.data);
}
return;
}
typeof options.success === 'function' && options.success(res);
},
dataType: options.dataType || 'json',
fail: function (res) {
if (typeof options.fail === 'function') {
options.fail(res);
} else {
wx.showModal({
title: '出错了',
content: '服务器繁忙,请稍后重试',
showCancel: false
});
}
},
complete: function (res) {
var flag = false;
if (typeof options.complete === 'function') {
flag = options.complete(res, 'request');
}
if (!flag) {
if (res.statusCode !== 200) {
wx.showModal({
title: '出错了',
content: '服务器繁忙,请稍后重试',
showCancel: false
});
}
}
}
});
}
}
},
globalData: {
// apiurl: 'https://d4.infix.siemin.com/eppc-web'
apiurl: 'https://testapp.test.siemin.com/eppc-web'
// apiurl: 'https://eppc.siemin.com/wxapp'
},
parseError: function (result) {
if (result && typeof result.retcode !== 'undefined' && result.retcode !== '0') {
wx.showModal({
title: '出错了',
content: result.retmsg || '服务器繁忙,请稍后重试',
showCancel: false
});
}
}
})