app.js
3.72 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
var uri_dc = require('utils/uri.js')
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);
if(opt.shareTicket){
this.shareTicket=opt.shareTicket;
}
this.getSystemInfo();
this.openid=await this.getOpenid();
console.log('this.openid',this.openid);
},
onShow:async function(){
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)
})
}
},
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)=>{
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) {
let openid=res.data.data.openid;
wx.setStorage({key:'customer_openid',data:openid});
success(openid);
},
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){
return this._getOpenid();
}
},
async getOpenid(){
let openid=wx.getStorageSync('customer_openid');
if(!openid){
openid= await this._getOpenid();
}
return openid;
},
})