app.js
11 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
import {WeToast} from './vendors/toast/wetoast';
import { ttLogin, addLoginListenner } from './login/utils/login/login';
import { Event } from './login/utils/index';
import config from './config.js';
import api from './common/api.js';
import md5 from './vendors/md5';
import { isStringEmpty, getYHStorageSync} from './utils/util';
import {Encrypt,Decrypt} from './libs/aes.js';
// let crypto = require('./libs/cryptojs/cryptojs.js').Crypto
global.event = new Event();
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: config.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 res = tt.getSystemInfoSync();
if (!res.screenHeight) {
res.screenHeight = res.windowHeight;
};
if (!res.screenWidth) {
res.screenWidth = res.windowWidth;
};
this.globalData.systemInfo = res;
ttLogin();
addLoginListenner();
this.checkUDID();
this.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);
};
let that = this;
tt.checkSession({
success (res) {
//登录态未过期
that.getWechatThirdSession();
that.getUserInfo();
that.getUnionID();
that.getUser_union_type();
},
fail (res) {
//登录态过期
tt.setStorage({
key: "WXThird_session",
data: ''
});
tt.setStorage({
key: "userInfo",
data: {}
});
tt.setStorage({
key: "unionID",
data: ""
});
tt.setStorage({
key: "user_union_type",
data: ""
});
}
});
this.getSessionkey();
let timestamp = new Date().getTime() + ''
this.globalData.sid = md5(timestamp);
},
//当应用程序进入前台显示状态时触发
onShow(options) {
let scene = options ? options.scene : '';
this.globalData.currentScene = scene;
this.showYOHOIconCount = 0;
},
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 value = this.globalData.WXThird_session
if (!isStringEmpty(value)) {
this.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);
tt.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;
tt.setStorage({
key: "sessionkey",
data: this.globalData.sessionkey
})
},
setWechatThirdSession: function (session) {
this.globalData.WXThird_session = session;
tt.setStorage({
key: "WXThird_session",
data: this.globalData.WXThird_session
})
},
setWXUnionID: function (unionID) {
this.globalData.WXUnion_ID = unionID;
tt.setStorage({
key: "unionID",
data: unionID
})
},
setOpenID:function(openID){
if(openID=='') return
this.globalData.openID = openID;
tt.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;
tt.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 || 0;
if (!uid || uid === 0) {
this.getUserInfo();
uid = this.globalData.userInfo.uid || 0;
}
return uid;
},
getMiniappType:function(){
return config.mini_app_type
},
clearUserSession() {},
getSessionkey: function() {
let sessionKey = getYHStorageSync('sessionkey', 'app');
if (sessionKey) {
this.globalData.sessionkey = sessionKey;
}
return sessionKey || '';
},
//获取加签的秘钥
getSimplePise: function () {
let data = {
"udid": this.globalData.udid,
"method": 'resources.simple.pice'
};
let method = "GET";
let header = {
'Content-Type': 'application/x-www-form-urlencoded'
};
let that = this;
tt.request({
url:config.domains.api,
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;
tt.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;
api.get({data: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 () {
// console.log('getShareInfo');
let param = {
share_id: this.globalData.shareId,
}
let that = this;
api.get({
url: '/operations/api/v5/webshare/getShare',
data: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 {
tt.setStorageSync('udid', udidValue)
} catch (e) {
}
}
//获取签名秘钥
} catch (e) {
// Do something when catch error
// console.log("checkUDID error: " + e)
}
},
checkNetworkType:function(){
let net = '';
tt.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)
}
})
this.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;
},
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;
tt.setStorage({
key: "union_type_time",
data: timeS,
});
tt.setStorage({
key: "union_type",
data: union_type
});
}
},
getPvid() {
return md5(`${new Date().getTime()}${this.getUUID()}`).toString();
},
getAppId() {
return config.appid;
},
WeToast
})