filter.js
6.34 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
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
const restapi = require('restapi.js')
const app = getApp();
const whitePage = ['pages/tab/index/index'];
/**
* 身份验证过滤器
* @param pageObj 页面对象
*/
function identityFilter(pageObj) {
var enterTime = new Date();
if(pageObj.onLoad){
let _onLoad = pageObj.onLoad;
pageObj.onLoad = function(op){
console.log(op);
let currentInstance = getPageInstance();
if(op.scene){
var scene = decodeURIComponent(op.scene);
var qrCodeScene = {
id: scene.split('=')[1]
}
console.log(qrCodeScene);
restapi.qrCodeScene(qrCodeScene, function(res){
if(res.returnCode =='0'){
var scenestr = res.data;
if (scenestr){
scenestr.split('&').forEach((item) => {
const key = item.split("=")[0]
const val = item.split("=")[1];
op[key] = val;
})
console.log(op);
console.log('filter op', op);
}
}
_onLoad.call(currentInstance, op);
})
}else{
_onLoad.call(currentInstance, op);
}
}
}
if (pageObj.onShow) {
let _onShow = pageObj.onShow;
pageObj.onShow = function () {
let currentInstance = getPageInstance();
var flag = true;
for (var i in whitePage){
if (currentInstance.route == whitePage[i]){
flag = false;
break;
}
}
if (!flag){
_onShow.call(currentInstance);
}
identityCheck(() => {
// 关闭授权窗
currentInstance.setData({
authToast: false
});
currentInstance.onLogin();
if(flag){
_onShow.call(currentInstance);
}
}, authType => {
// 弹出授权窗口
if(!(app.globalData.authUserInfo && app.globalData.authPhone)) {
currentInstance.setData({
authToast: true,
authType: authType
});
}
// 完成授权后回调
app.userInfoReadyCallback = userInfo => {
currentInstance.onLogin();
if(flag){
_onShow.call(currentInstance);
}
}
});
}
}
if (pageObj.onHide) {
let _onHide = pageObj.onHide;
pageObj.onHide = function () {
let currentInstance = getPageInstance();
pageVisitCollect(currentInstance.route, enterTime);
_onHide.call(currentInstance);
}
}
if (pageObj.onUnload) {
let _onUnload = pageObj.onUnload;
pageObj.onUnload = function () {
let currentInstance = getPageInstance();
pageVisitCollect(currentInstance.route, enterTime);
_onUnload.call(currentInstance);
}
}
return pageObj;
}
/**
* 身份验证
*/
function identityCheck(success, fail){
// 获取用户信息
wx.login({
success: response => {
app.globalData.code = response.code;
userAuth(success, fail);
}
})
}
/**
* 用户授权
*/
function userAuth(success, fail){
let currentInstance = getPageInstance();
console.log(app.globalData)
// 已授权微信用户信息
if (app.globalData.authUserInfo) {
// 已授权手机号
if (app.globalData.authPhone) {
// 停掉检测任务
clearInterval(currentInstance.data.authInterval);
// 授权成功
success();
} else {
// 开启授权检测任务
checkAuthInterval();
// 未授权手机号
fail(false);
}
return;
} else {
fail(true);
}
// 开启新的授权检测任务
checkAuthInterval();
restapi.jscodeLogin(app.globalData.code, app.globalData.appId, function (response) {
console.log(response);
switch (response.returnCode) {
case '0': // 获取微信用户信息成功
app.globalData.userInfo = response.data;
if(app.globalData.userInfo.isLogin == 'Y'){
// 退出登录,重新授权用户信息(单独授权手机号,由于code失效导致解密异常,后期处理)
app.globalData.authUserInfo = true;
if (response.data.accountId) { // 已获取手机号
app.globalData.authPhone = true;
// 登录成功回调
app.globalData.loginSuccessCallback(response.data);
success();
} else { // 未获取手机号,授权获取
fail(false);
}
}else{
// 已退出登录,授权获取用户信息
fail(true);
}
break;
case '-1':
// 获取微信用户信息为空,授权获取
if (response.errId == '200205') {
fail(true);
}
break;
default:
wx.showToast({
title: response.message
})
}
});
}
/**
* 轮询检测授权
*/
function checkAuthInterval(){
let currentInstance = getPageInstance();
// 停掉原有检测任务
clearInterval(currentInstance.data.authInterval);
var authInterval = setInterval(function(){
// 已授权微信用户信息
if (app.globalData.authUserInfo) {
// 已授权手机号
if (app.globalData.authPhone) {
// 关闭授权窗口
currentInstance.setData({
authToast: false
});
console.log("clear interval:" + currentInstance.data.authInterval);
clearInterval(currentInstance.data.authInterval);
// 登录成功回调
app.globalData.loginSuccessCallback(app.globalData.userInfo);
} else {
// 切换到手机号授权窗口
currentInstance.setData({
authType: false
});
}
}
}, 1000);
// 保存任务ID
currentInstance.setData({
authInterval: authInterval
});
}
function getPageInstance() {
var pages = getCurrentPages();
return pages[pages.length - 1];
}
/**
* 页面访问收集
* @param {页面路径} page
* @param {进入时间} enterTime
*/
function pageVisitCollect(page, enterTime){
var data = {
path: page,
enterTime: enterTime,
leaveTime: new Date,
merchantId: app.globalData.extConfig.merchantId,
appId: app.globalData.appId
}
restapi.pageVisitCollect(JSON.stringify(data), function(res){});
}
exports.identityFilter = identityFilter;