forked from cqululei/wecqupt
-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.js
433 lines (431 loc) · 12.8 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//app.js
var mta = require('./utils/mta_analysis.js')
const ald = require('./utils/ald-stat.js')
App({
version: 'v2.1.11', //版本号
scene: 1001, //场景值
shareTicket: null, //分享获取相同信息所需ticket
session_id: null,
is_login: false,
redirect: false,
onLaunch: function(options) {
console.log(options)
// 如果有新的版本强制更新版本
if (wx.getUpdateManager) {
const updateManager = wx.getUpdateManager();
updateManager.onUpdateReady(() => {
updateManager.applyUpdate();
});
}
var _this = this;
if (options.scene) {
_this.scene = options.scene;
}
mta.App.init({
"appID": require('config').mta_app_id,
"lauchOpts": options, //渠道分析,需在onLaunch方法传入options,如onLaunch:function(options){...}
"statPullDownFresh": true, // 使用分析-下拉刷新次数/人数,必须先开通自定义事件,并配置了合法的eventID
"statShareApp": true, // 使用分析-分享次数/人数,必须先开通自定义事件,并配置了合法的eventID
"statReachBottom": true // 使用分析-页面触底次数/人数,必须先开通自定义事件,并配置了合法的eventID
});
_this.shareTicket = options.shareTicket;
//读取缓存
try {
var data = wx.getStorageInfoSync();
if (data && data.keys.length) {
data.keys.forEach(function(key) {
var value = wx.getStorageSync(key);
if (value) {
_this.cache[key] = value;
}
});
if (_this.cache.version !== _this.version) {
_this.cache = {};
wx.clearStorage();
} else {
_this.session_id = _this.cache.session_id;
_this.user.wx_info = _this.cache.wx_info;
_this.user.auth_user = _this.cache.auth_user;
_this.user.student = _this.cache.student;
_this.user.teacher = _this.cache.teacher;
}
}
} catch (e) {
console.warn('获取缓存失败');
}
},
//保存缓存
saveCache: function(key, value) {
if (!key || !value) {
return;
}
var _this = this;
_this.cache[key] = value;
wx.setStorage({
key: key,
data: value
});
},
//清除缓存
removeCache: function(key) {
if (!key) {
return;
}
var _this = this;
_this.cache[key] = '';
wx.removeStorage({
key: key
});
},
//后台切换至前台时
onShow: function() {
},
checkCache: function() {
var _this = this;
return new Promise(function(resolve, reject) {
if (_this.util.isEmptyObject(_this.user.wx_info)) {
_this.initWechatUser().then(function() {
if (_this.util.isEmptyObject(_this.user.auth_user)) {
_this.initSchoolUser().then(function() {
resolve();
}).catch(resolve)
} else {
resolve();
}
}).catch(reject)
} else {
resolve();
}
})
},
//判断是否有登录信息,让分享时自动登录
loginLoad: function() {
var _this = this;
return new Promise(function(resolve, reject) {
if (!_this.session_id) { //无登录信息
_this.session_login().then(function() {
_this.is_login = true
console.log("session登陆")
_this.checkCache().then(function () {
resolve();
})
}).catch(function(res) {
reject(res);
});
} else if (!_this.is_login) { //有登录信息,并且为初始化程序
console.log("检查登陆状态")
_this.check_session().then(function() {
_this.is_login = true
_this.checkCache().then(function () {
resolve();
})
}).catch(function(res) {
console.log(res)
_this.session_login().then(function () {
_this.is_login = true
console.log("重新登陆")
_this.checkCache().then(function () {
resolve();
})
}).catch(function(res) {
console.log(res)
});
})
} else {
resolve();
}
})
},
wx_request: function(enpoint, method, data) {
// 全局网络请求封装
var _this = this;
return new Promise(function(resolve, reject) {
const session_id = _this.session_id
let header = {}
if (session_id) {
header = {
'content-type': 'application/json',
'session-id': session_id
}
}
wx.request({
url: _this.server + enpoint,
data: data || '',
method: method || 'GET',
header: header,
success: function(res) {
if (res.data.status == 10000) {
console.log('重新登录')
_this.session_login().then(function () {
// 成功回调
_this.wx_request(enpoint, method, data).then(function (res) {
resolve(res);
}).catch(function (res) {
reject(res);
});
return;
}).catch(function (res) {
console.log(res);
reject(res);
});
} else {
resolve(res);
}
},
fail: function(res) {
_this.showLoadToast("网络出错")
reject(res);
}
});
})
},
check_session: function() {
var _this = this;
return new Promise(function(resolve, reject) {
wx.checkSession({
success: function() {
wx.request({
url: _this.server + '/mini_program/check_session',
data: {
'session_id': _this.session_id
},
method: "POST",
success: function(res) {
if (!res.data) {
// 网络出错
_this.showLoadToast("服务器出错")
reject();
} else {
if (res.data.status == 200) {
console.log("session状态有效")
resolve();
} else if (res.data.status == 403) {
_this.session_login().then(function() {
resolve();
}).catch(function (res) {
console.log(res);
reject(res);
});
}
}
},
fail: function(res) {
_this.showLoadToast("网络出错")
reject(res);
}
});
},
fail: function (res) {
// 没有登录微信
reject(res);
}
})
});
},
session_login: function() {
var _this = this;
return new Promise(function(resolve, reject) {
wx.showNavigationBarLoading();
wx.login({
success: function(res) {
wx.request({
method: 'POST',
url: _this.server + '/mini_program/session_login',
data: {
code: res.code
},
success: function(res) {
if (res.data && res.data.status === 200) {
var status = false,
data = res.data.data;
//判断缓存是否有更新
if (_this.cache.version !== _this.version) {
_this.saveCache('version', _this.version);
status = true;
}
console.log(data.login_require)
_this.aldstat.sendSession(data.session_id)
_this.saveCache('session_id', data.session_id);
_this.session_id = data.session_id;
if (data.login_require) {
wx.getSetting({
success(res) {
if (!res.authSetting['scope.userInfo']) {
wx.authorize({
scope: 'scope.userInfo',
success() {
console.log("已取得用户授权")
_this.getUserInfo().then(function () {
resolve()
});
},
fail() {
wx.navigateTo({
url: '/pages/authorize/index'
});
}
})
} else {
_this.getUserInfo().then(function () {
resolve()
});
}
}
})
} else {
console.log("session登陆成功")
resolve();
}
} else {
//清除缓存
if (_this.cache) {
_this.cache = {};
wx.clearStorage();
}
reject();
}
},
fail: function(res) {
var status = '';
// 判断是否有缓存
console.warn(res);
_this.g_status = '网络错误';
console.warn(status);
reject(res);
},
complete: function() {
wx.hideNavigationBarLoading();
}
});
}
});
})
},
getUserInfo: function() {
var _this = this;
return new Promise(function(resolve, reject) {
//获取微信用户信息
wx.getUserInfo({
withCredentials: true,
success: function(res) {
var info = res;
// _this.user.wxinfo = info.userInfo;
if (!info.encryptedData || !info.iv) {
reject('无关联AppID');
return;
}
wx.request({
method: 'POST',
url: _this.server + '/mini_program/wechat_login',
data: {
session_id: _this.session_id,
key: info.encryptedData,
iv: info.iv,
rawData: info.rawData,
signature: info.signature
},
success: function(res) {
if (res.data && res.data.status === 200) {
console.log("获取用户信息成功")
_this.checkCache().then(function () {
resolve(res);
}).catch(function (e){
console.log(e)
})
} else {
_this.showLoadToast("服务器异常")
}
},
fail: function(res) {
_this.showLoadToast("网络出错")
},
complete: function() {}
});
},
fail: function(res) {
// 提示用户授权页面
_this.g_status = '未授权';
wx.navigateTo({
url: '/pages/authorize/index'
});
reject(res);
}
});
})
},
initWechatUser: function() {
// 获取必要的微信信息
var _this = this;
return new Promise(function(resolve, reject) {
_this.wx_request("/mini_program/wechat_user").then(function(res) {
if (res.data.status === 200) {
_this.user.wx_info = res.data.data;
_this.saveCache('wx_info', res.data.data);
resolve();
} else {
reject(res);
}
}).catch(function(res) {
reject(res);
})
})
},
initSchoolUser: function() {
// 获取必要的绑定信息
var _this = this;
return new Promise(function(resolve, reject) {
_this.wx_request("/school_sys/user_info").then(function(res) {
if (res.data.status === 200) {
var data = res.data.data;
_this.user.auth_user = data.auth_user;
_this.saveCache('auth_user', data.auth_user);
if (_this.user.auth_user.user_type === 0) {
_this.saveCache('student', data.user_info);
_this.user.student = data.user_info;
} else if (_this.user.auth_user.user_type === 1) {
_this.saveCache('teacher', data.user_info);
_this.user.teacher = data.user_info;
}
resolve();
} else {
reject(res);
}
}).catch(function(res) {
reject(res);
})
})
},
showErrorModal: function(content, title) {
wx.showModal({
title: title || '加载失败',
content: content || '未知错误',
confirmColor: "#1f7bff",
showCancel: false
});
},
showLoadToast: function(title, duration) {
wx.showToast({
title: title || '加载中',
icon: 'loading',
mask: true,
duration: duration || 10000
});
},
cache: {},
server: require('config').server,
news_server: require('config').news_server,
user: {
//微信数据
wx_info: {},
//认证数据
auth_user: {},
//学生数据
student: {},
//教师数据
teacher: {},
//学校参数
school: {}
},
banner_show: false,
util: require('./utils/util')
})