forked from vincenth520/pinche_xcx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
97 lines (91 loc) · 2.24 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//app.js
var util = require('utils/util.js');
App({
onLaunch: function () {
var that = this;
//小程序初始化先判断用户是否登录
wx.checkSession({
success: function(){
wx.getStorage({
key: 'sk',
success: function(res) {
var sk = res.data;
util.req('user/vaild_sk', { "sk": sk }, function (data) {
if (data.status == 1) {
that.globalData.sk = sk;
} else {
that.login();
return;
}
})
},
fail:function() {
that.login();
return;
}
})
wx.getStorage({
key: 'userInfo',
success: function(res) {
that.globalData.userInfo = res.data;
},
fail:function() {
that.login();
}
});
},
fail: function(){
//登录态过期
that.login() //重新登录
}
})
},
login:function(){
var that = this;
wx.login({
success: function (res) {
wx.getUserInfo({
success: function(userinfo){
util.req('user/login', {
"code": res.code,
"encryptedData": userinfo.encryptedData,
"iv": userinfo.iv
}, function (data) {
that.setUserInfo(data.user);
that.setSk(data.sk);
})
},
fail: function(res) {
that.loginFail();
}
})
}
})
} ,
loginFail: function () {
var that = this;
wx.showModal({
content: '登录失败,请允许获取用户信息,如不显示请删除小程序重新进入',
showCancel: false
});
that.login();
},
setUserInfo:function(data){ //将用户信息缓存保存
this.globalData.userInfo = data;
wx.setStorage({
key:"userInfo",
data:data
})
},
setSk:function(data){ //将用户信息缓存保存
this.globalData.sk = data;
wx.setStorage({
key:"sk",
data:data
})
},
globalData:{
userInfo:null,
sk:null
}
})