forked from wangcong102/xiaoyiyun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
64 lines (64 loc) · 2.04 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
//app.js
App({
// =============================
onLaunch: function () {
wx.cloud.init({
env:"cloud1-3g6ho2wo4c6d317c"
})
if (!wx.cloud) {
console.error('请使用 2.2.3 或以上的基础库以使用云能力')
}
else {
wx.cloud.init({
traceUser: true,
})
}
},
// =============================================
globalData:{
logged:false
},
onLaunch: function () {
wx.cloud.init({
env:"xiaoliu-k8902",
traceUser:true
})
var that = this
var user = wx.getStorageSync('user') || {};
var userInfo = wx.getStorageSync('userInfo') || {};
if ((!user.openid || (user.expires_in || Date.now()) < (Date.now() + 600)) && (!userInfo.nickName)) {
wx.login({
success: function (res) {
if (res.code) {
wx.getUserInfo({
success: function (res) {
var objz = {};
objz.avatarUrl = res.userInfo.avatarUrl;
objz.nickName = res.userInfo.nickName;
//console.log(objz);
wx.setStorageSync('userInfo', objz);//存储userInfo
}
});
var d = that.globalData;//这里存储了appid、secret、token串
var l = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + d.appid + '&secret=' + d.secret + '&js_code=' + res.code + '&grant_type=authorization_code';
wx.request({
url: l,
data: {},
method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
// header: {}, // 设置请求的 header
success: function (res) {
var obj = {};
obj.openid = res.data.openid;
obj.expires_in = Date.now() + res.data.expires_in;
//console.log(obj);
wx.setStorageSync('user', obj);//存储openid
}
});
} else {
console.log('获取用户登录态失败!' + res.errMsg)
}
}
});
}
},
})