|
|
//app.js
|
|
|
import promisify from './libs/promisify';
|
|
|
import objectAssign from './vendors/object-assign';
|
|
|
import {WeToast} from './vendors/toast/wetoast';
|
|
|
import { API_HOST, SERVICE_HOST, MINI_APP_TYPE} from './libs/config';
|
|
|
import {GET, POST} from './libs/request';
|
|
|
import {Encrypt,Decrypt} from './libs/aes.js';
|
|
|
let crypto = require('./libs/cryptojs/cryptojs.js').Crypto
|
|
|
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 config from './config';
|
|
|
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({
|
|
|
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)
|
|
|
// logEvent(YB_WX_SCAN_CODE, 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)
|
|
|
},
|
|
|
|
|
|
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);
|
|
|
},
|
|
|
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: {},
|
|
|
},
|
|
|
|
|
|
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 () {
|
|
|
// console.log('getShareInfo');
|
|
|
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)
|
|
|
});
|
|
|
},
|
|
|
|
|
|
fastloginSwitch: function () {
|
|
|
let that = this
|
|
|
let param = {
|
|
|
method: 'app.passport.fastloginSwitch',
|
|
|
};
|
|
|
GET(API_HOST, param)
|
|
|
.then(json => {
|
|
|
// if (!json || !json.code || json.code != 200 || !json.data) return;
|
|
|
// if (!json.data || !json.data.sk) return;
|
|
|
|
|
|
// let p2SecretKey = json.data.sk;
|
|
|
// if (!p2SecretKey) return;
|
|
|
// that.globalData.p2SecretKey = p2SecretKey;
|
|
|
|
|
|
// wx.setStorage({
|
|
|
// key: "p2SecretKey",
|
|
|
// data: p2SecretKey
|
|
|
// });
|
|
|
})
|
|
|
|
|
|
.catch(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 config.appid;
|
|
|
},
|
|
|
mtainit(e) {
|
|
|
mta.App.init({
|
|
|
"appID": "500570715",
|
|
|
"eventID": "500570797",
|
|
|
});
|
|
|
// console.log("init mta");
|
|
|
},
|
|
|
WeToast
|
|
|
})
|
|
|
|
|
|
|
...
|
...
|
|