-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
80 lines (74 loc) · 1.81 KB
/
app.js
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
var server = require('./utils/server');
App({
globalData: {
hasLogin: false,
userInfo: {},
orderType: {
1: 'booking',
2: 'order',
}
},
onLaunch: function () {
var self = this;
var rd_session = wx.getStorageSync('rd_session');
console.log('rd_session', rd_session)
if (!rd_session) {
console.log('没登陆');
self.login();
} else {
wx.checkSession({
success: function () {
// 登录态未过期
console.log('登录态未过期')
self.rd_session = rd_session;
self.getUserInfo();
},
fail: function () {
//登录态过期
console.log('登陆过期');
self.login();
}
})
}
},
onShow: function () {
},
onHide: function () {
},
rd_session: null,
login: function() {
var self = this;
wx.login({
success: function (res) {
server.getJSON('/WxAppApi/setUserSessionKey', { code: res.code }, async function (res) {
self.rd_session = res.data.session_key;
self.globalData.hasLogin = true;
wx.setStorageSync('rd_session', self.rd_session);
await self.getUserInfo();
},function(err){ console.log(err); return false;});
}
});
},
getUserInfo: function() {
var self = this;
wx.getUserInfo({
success: function(res) {
server.postJSON('/WxAppApi/checkSignature', {
rd_session: self.rd_session,
encryptedData: res.encryptedData,
iv: res.iv
}, function (res) {
let userInfo = {};
userInfo.id = res.data.id;
let jsonInfo = JSON.parse(res.data.info);
userInfo.nickName = jsonInfo.nickName;
userInfo.avatarUrl = jsonInfo.avatarUrl;
self.globalData.userInfo = userInfo;
if (res.data.errorcode) {
// TODO:验证有误处理
}
});
}
});
}
})