app.js
14 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
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
//app.js
import objectAssign from './vendors/object-assign';
import {WeToast} from './vendors/toast/wetoast';
import { APP_ID, API_HOST, SERVICE_HOST, MINI_APP_TYPE} from './libs/config';
import {GET, POST} from './libs/request';
import md5 from './vendors/md5';
var mta = require('./vendors/mta_analysis.js')
import { wechatLoginAction } from './utils/login';
import { isStringEmpty, getYHStorageSync} from './utils/util';
import './router/index';
import event from './utils/event.js';
// import { logEvent,
// YB_LAUNCH_APP,
// YB_EXIT_APP,
// YB_ENTER_FOREGROUND,
// YB_ENTER_BACKGROUND,
// YB_PAGE_OPEN_L,
// YB_AWAKE_MP,
// YB_SERVICE_PUSH,
// YB_WX_SCAN_CODE,
// YB_SHARE_UNION_TYPE
// } from './libs/analytics.js';
import { appReport } from './libs/appReport.js';
App({
showYOHOIconCount: 0,
globalData: {
selectedChannel: 'boy',
userInfo: {},
systemInfo: null,
WXThird_session: '',
WXUnion_ID: '',
openID:'',
p2SecretKey: '',
sessionkey: '',
sid: '',
udid:'',
cid:'1',
mobileRegisterState:'',
hasReportAwakeAction: 'false',
defraudurl:'https://activity.yoho.cn/feature/45.html?title=防诈骗提醒',
miniapp_type: MINI_APP_TYPE,
union_type:'',//渠道号,大数据上传用
union_type_time: '',
ch:'',
networkType:'0',
deviceInfo:{},
currentScene: 1001,
user_union_type: '',//用户相关unionType 要与union_type区分开
user_union_type_imageUrl: '',//联盟图片
user_nounion_type_imageUrl:'',//非联盟用户分享banner图片
shareId: '',
shareInfo: {},
},
onLaunch: function (options) {
let that = this;
let res = wx.getSystemInfoSync();
if (!res.screenHeight) {
res.screenHeight = res.windowHeight;
}
if (!res.screenWidth) {
res.screenWidth = res.windowWidth;
}
this.globalData.systemInfo = res;
this.checkUDID();
that.getSimplePise()
this.checkNetworkType()
if (options && options.scene){
this.globalData.ch = options.scene
}
if (options && options.query && options.query.union_type){
this.updateUnionType(options.query.union_type)
}
wx.checkSession({
success: function () {
//登录态未过期
that.getWechatThirdSession();
that.getUserInfo();
that.getUnionID();
that.getUser_union_type();
},
fail: function () {
//登录态过期
wx.setStorage({
key: "WXThird_session",
data: ''
});
wx.setStorage({
key: "userInfo",
data: {}
});
wx.setStorage({
key: "unionID",
data: ""
});
wx.setStorage({
key: "user_union_type",
data: ""
});
}
})
this.getSessionkey();
let timestamp = new Date().getTime() + ''
this.globalData.sid = md5(timestamp);
// if (options){
// let channel = options.query.channel;
// let channelType = options.query.type;
// let params = { 'CHANNEL': channel, 'TYPE': channelType };
// logEvent(YB_LAUNCH_APP, params, this);
// }
setTimeout(function () {
wechatLoginAction(function (response) {
that.getShareInfo();
});
}, 1000);
this.mtainit(options);
this.addListener();
},
addListener() {
event.on('anchor-click', params => { // anchor组件中点击时上报楼层点击事件
// logEvent('YB_MAIN_EVENT', params);
});
},
//当应用程序进入前台显示状态时触发
onShow(options) {
let that = this
let params = {};
let shareParams = {};
let pathParam = ""
if (options && options.query) {
for (var Key in options.query) {
console.log("key:" + Key + "==value:" + options.query[Key]);
shareParams[Key] = options.query[Key];
if (pathParam.length == 0) {
pathParam = Key + '=' + options.query[Key] + '';
} else {
pathParam = pathParam + '&' + '' + Key + '=' + options.query[Key] + '';
}
}
}
// logEvent(YB_SHARE_UNION_TYPE, shareParams, that)
let path = '';
if (options && options.path) {
path = options.path
}
if (pathParam !== "") {
path = path + "?" + pathParam
}
params = {
PAGE_PATH: path
};
// logEvent(YB_ENTER_FOREGROUND, params, that)
if (that.globalData.hasReportAwakeAction==='false'){
// logEvent(YB_AWAKE_MP, params, this);
that.globalData.hasReportAwakeAction = 'true'
}
if (options && options.scene){
appReport('start', "LIFECYCLE", "RESUME", {
'activeWay': options.scene + ""
}, this)
if (+options.scene === 1014 && options.query.msg_id) {
let param = {
MSG_ID: options.query.msg_id
}
// logEvent(YB_SERVICE_PUSH, param,that);
}
}else{
appReport('start', "LIFECYCLE", "RESUME", {}, this)
}
let scene = options ? options.scene : '';
this.globalData.currentScene = scene;
this.showYOHOIconCount = 0;
},
//当应用程序进入后台状态时触发
onHide () {
// let params = {};
// logEvent(YB_ENTER_BACKGROUND,params)
appReport('start', "LIFECYCLE", "STOP", {},this)
},
onPageNotFound(res) {
wx.redirectTo({
url: `/pages/common/notfound?path=${encodeURIComponent(res.path)}`,
})
},
getUserInfo: function () {
var value = getYHStorageSync('userInfo','app')
if (value) {
this.globalData.userInfo = value;
}
return value;
},
getMobile: function() {
return this.globalData.userInfo.mobile;
},
getUser_union_type: function () {
this.getUnionTypeWithUid(this.getUid());
},
/*
*union_type表示渠道号,在推广阶段通过商品详情页等传入。
* 接收到该参数时,赋值给全局变量时,持久化到本地。
* 在取值时,先从全局变量获取,若未获取到则从本地缓存中获取。
* 时效7天
*/
getUnion_type: function () {
try {
var value = this.checkUnion_type(this.globalData.union_type, this.globalData.union_type_time);
if (!value) {
let union_type = getYHStorageSync('union_type', 'app') ? getYHStorageSync('union_type', 'app'):"";
let union_type_time = getYHStorageSync('union_type_time', 'app') ? getYHStorageSync('union_type_time', 'app'):"";
value = this.checkUnion_type(union_type, union_type_time);
}
return value
} catch (e) {
// console.log(e)
}
},
checkUnion_type: function (union_type, union_type_time) {
if (!union_type)return '';
let time_old = union_type_time;
let time_new = new Date().getTime() + '';
var dayDiff = parseInt ((parseInt(time_new) - parseInt(time_old)) / 1000 );
if (dayDiff > 7 * 24 * 3600) {
return '';
}
return union_type;
},
getUnionID: function () {
var value = getYHStorageSync('unionID','app')
if (value) {
this.globalData.WXUnion_ID = value;
}
},
getWechatThirdSession: function () {
try {
var that = this
var value = this.globalData.WXThird_session
if (!isStringEmpty(value)) {
that.globalData.WXThird_session = value;
}else{
value = getYHStorageSync('WXThird_session','app')
}
return value
} catch (e) {
// console.log(e)
}
},
setUserInfo: function (cb) {
this.globalData.userInfo = objectAssign(this.globalData.userInfo, cb);
wx.setStorage({
key: "userInfo",
data: this.globalData.userInfo
})
if (this.globalData.userInfo.uid) {
this.getUnionTypeWithUid(this.globalData.userInfo.uid);
}
},
setSessionkey: function (sessionkey) {
this.globalData.sessionkey = sessionkey;
wx.setStorage({
key: "sessionkey",
data: this.globalData.sessionkey
})
},
setWechatThirdSession: function (session) {
this.globalData.WXThird_session = session;
wx.setStorage({
key: "WXThird_session",
data: this.globalData.WXThird_session
})
},
setWXUnionID: function (unionID) {
this.globalData.WXUnion_ID = unionID;
wx.setStorage({
key: "unionID",
data: unionID
})
},
setOpenID:function(openID){
if(openID=='') return
this.globalData.openID = openID;
wx.setStorage({
key: "openID",
data: openID
})
},
getOpenID: function () {
let openid = ''
if (!isStringEmpty(this.globalData.openID)){
openid = this.globalData.openID
}else{
openid = getYHStorageSync('openID','app')
this.globalData.openID = openid
}
return openid
},
updateUid: function (uid) {
this.getUserInfo();
this.globalData.userInfo.uid = uid;
wx.setStorage({
key: "userInfo",
data: this.globalData.userInfo
})
this.getUnionTypeWithUid(uid);
},
isLogin: function () {
return this.globalData.userInfo.uid > 0 ? true : false
},
getUid: function () {
let uid = this.globalData.userInfo.uid
if(!uid||uid===0){
this.getUserInfo();
uid = this.globalData.userInfo.uid
}
return uid;
},
getMiniappType:function(){
return MINI_APP_TYPE
},
//获取加签的秘钥
getSimplePise: function () {
let that = this
let data = {
"udid": that.globalData.udid,
"method": 'resources.simple.pice'}
let method = "GET"
let header = {
'Content-Type': 'application/x-www-form-urlencoded'
}
wx.request({
url:API_HOST,
data,
method,
header: header,
success: function (res) {
// console.log("res:", res)
if (res && res.data && res.data.data && res.data.data.sk){
that.globalData.p2SecretKey = res.data.data.sk;
wx.setStorage({
key: "p2SecretKey",
data: res.data.data.sk
});
}
},
fail: function (err) {
// console.log("err:",err)
}
});
},
/*
获取用户相关union_type
*/
getUnionTypeWithUid: function (uid) {
let param = {
method: 'app.union.shareOrder.queryUnionTypeByUid',
uid,
}
let that = this;
GET(API_HOST, param)
.then(data => {
if (data.code == 200) {
that.globalData.user_union_type = data.data.unionType;
that.globalData.user_union_type_imageUrl = data.data.imageUrl;
that.globalData.shareId = data.data.shareId;
that.globalData.user_nounion_type_imageUrl = data.data.applyImageUrl;
that.getShareInfo();
}
})
.catch(error => {
// console.log(error)
});
},
/*
获取分享信息
*/
getShareInfo: function () {
// let param = {
// share_id: this.globalData.shareId,
// }
// let that = this;
// GET(API_HOST + '/operations/api/v5/webshare/getShare', param)
// .then(data => {
// // console.log(data)
// if (data && data.code == 200) {
// that.globalData.shareInfo = data.data;
// }
// })
// .catch(error => {
// // console.log(error)
// });
},
checkUDID:function(){
let that = this
try {
var value = getYHStorageSync('udid','app')
if (value) {
that.globalData.udid = value;
}else{
let udidValue = md5(that.getUUID());
that.globalData.udid = udidValue;
try {
wx.setStorageSync('udid', udidValue)
} catch (e) {
}
}
//获取签名秘钥
} catch (e) {
// Do something when catch error
// console.log("checkUDID error: " + e)
}
},
checkNetworkType:function(){
let that = this;
let net = '';
wx.getNetworkType({
success: function (res) {
// 返回网络类型, 有效值:
// wifi/2g/3g/4g/unknown(Android下不常见的网络类型)/none(无网络)
var networkType = res.networkType
if (networkType === 'wifi') {
net = '1';
} else if (networkType === '2g') {
net = '2';
} else if (networkType === '3g') {
net = '3';
} else if (networkType === '4g') {
net = '4';
} else {
net = '0';
}
// console.log("network:",net)
},
complete:function(res){
appReport('start', "LIFECYCLE", "START", {}, that)
}
})
that.globalData.networkType = net;
},
getUUID: function () {
let value = "" + Date.now() + '-' + Math.floor(1e7 * Math.random()) + '-' + Math.random().toString(16).replace('.', '') + '-' + String(Math.random() * 31242).replace('.', '').slice(0, 8);
return value;
},
loginCallBack: function (response) {
// console.log(response)
// if (response.code === 10001) {
// this.showRelaunchModal();
// }
},
showRelaunchModal: function () {
wx.showModal({
title: '提示',
content: '使用微信小程序需要微信授权,您已经拒绝了该请求,请删除小程序重新进入。',
showCancel: false,
success: function (res) {
if (res.confirm) {
wx.navigateBack({
delta: 1
})
}
}
})
},
getSessionkey: function () {
if (this.isLogin) {
var value = getYHStorageSync('sessionkey','app')
if (value) {
// console.log(value)
this.globalData.sessionkey = value;
}
}
return undefined;
},
//更新渠道号同时更新时间戳,48小时失效
updateUnionType: function (union_type) {
if (union_type){
let timeS = new Date().getTime() + '';
this.globalData.union_type = union_type;
this.globalData.union_type_time = timeS;
wx.setStorage({
key: "union_type_time",
data: timeS,
});
wx.setStorage({
key: "union_type",
data: union_type
});
}
},
getPvid() {
return md5(`${new Date().getTime()}${this.getUUID()}`).toString();
},
getAppId() {
return APP_ID;
},
mtainit(e) {
mta.App.init({
"appID": "500570715",
"eventID": "500570797",
});
// console.log("init mta");
},
WeToast
})