app.js
6.05 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
var mta=require('mta-wechat-analysis')
var uri_dc = require('utils/uri.js')
import PubSub from 'utils/PubSub'
global.regeneratorRuntime = require('lib/regenerator/runtime-module')
const { regeneratorRuntime } = global
Array.prototype._indexOf = function (val) {
for (var i = 0; i < this.length; i++) {
if (this[i] == val) return i;
}
return -1;
};
Array.prototype.remove = function(val) {
var index = this._indexOf(val);
if (index > -1) {
this.splice(index, 1);
}
};
App({
openid:"",
system:{},
shareTicket:"",
onLaunch: async function (opt) {
console.log('onLaunch',opt,wx.env);
mta.App.init({
"appID":"500667280",
"eventID":"500667287",
"statPullDownFresh":true,
"statShareApp":true,
"statReachBottom":true,
"lauchOpts":opt, //渠道分析
});
if(opt.shareTicket){
this.shareTicket=opt.shareTicket;
}
this.getSystemInfo();
this.openid=await this.getOpenid();
console.log('this.openid',this.openid,mta);
},
onShow:async function(){
//this._getOpenid();
if (wx.canIUse('getUpdateManager')) {
const updateManager = wx.getUpdateManager();
updateManager.onCheckForUpdate(function (res) {
// 请求完新版本信息的回调
console.log('onCheckForUpdate',res)
})
updateManager.onUpdateReady(function (res) {
console.log('onUpdateReady',res)
updateManager.applyUpdate();
wx.showToast({
title:'版本已更新',
duration:2000,
mask:true,
success:function(){
}
})
})
updateManager.onUpdateFailed(function (res) {
// 新的版本下载失败
console.log('onUpdateFailed',res)
})
}
try{
let sms_code= await this.getClipboardData();
wx.setClipboardData({
data:"?"
})
console.log('SMS.code',sms_code);
wx.reLaunch({
url:'/pages/customer/index?sms_code='+sms_code
});
}catch (e){
console.log('SMS.code.catch',e);
}
},
async getSystemInfo(){
this._getSystemInfo().then((res)=>{
console.log('wx.getSystemInfo',res);
this.system=res;
}).catch(res=>{
this.getSystemInfo();
})
},
_getSystemInfo(){
return new Promise((success,fail)=>{
wx.getSystemInfo({
success:(res)=>{
if(res.errMsg=='getSystemInfo:ok'){
success(res);
}else{
fail(res);
}
},
fail:(res)=>{
fail(res);
}
})
})
},
async _login(){
return new Promise((success,fail)=>{
wx.login({
success: (login_res)=>{
console.log('wx.login',login_res)
success(login_res);
},
fail:(res)=>{
fail(res);
}
})
})
},
async _getLiteOpenidByCode(code){
return new Promise((success,fail)=>{
wx.request({
url: uri_dc.HOST__LINE+'App.Reserve.GetLiteOpenid',
method: 'POST',
data: {code:code},
header: {
'content-type': 'application/x-www-form-urlencoded'
},
success: function (res) {
try{
let openid=res.data.data.openid;
wx.setStorage({key:'customer_openid',data:openid});
wx.setStorage({key:'customer_session_key',data:res.data.data.session_key});
success(openid);
}catch(e){
console.log(res);
fail(res);
}
},
fail: function (res) {
console.log(res);
fail(res);
}
})
})
},
async _getOpenid(){
try{
let login_res=await this._login();
let openid=await this._getLiteOpenidByCode(login_res.code)
return openid
}catch(e){
console.log('_getOpenid.catch',e)
return this._getOpenid();
}
},
async getOpenid(){
let openid=wx.getStorageSync('customer_openid');
if(!openid){
openid= await this._getOpenid();
}
return openid;
},
getClipboardData(){
return new Promise((success,fail)=>{
wx.getClipboardData({
success:(res)=>{
let match=res.data.match(/#(.*)#/);
if(match && res.data.indexOf('一键订')>0){
success(match[1])
}else{
fail()
}
},
fail:()=>{
fail()
}
})
})
},
onHide(){
console.log('onHide');
PubSub.emit('appHide')
let time=new Date()/1000;
wx.getStorageInfo({
success:(res)=>{
console.log(res.keys);
if(res.keys.length>0){
res.keys.forEach(key=>{
wx.getStorage({
key:key,
success:(ca)=>{
//console.log('obHide',ca.data.timeout,time);
if(ca.data.timeout && ca.data.timeout<time && ca.data.timeout!=-1){
wx.removeStorage({key:key})
}
}
})
})
}
},
})
}
})