-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
47 lines (47 loc) · 1.09 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
//app.js
App({
onLaunch: function() {
const thirdSession = wx.getStorageSync('thirdSession')
if (thirdSession) {
wx.checkSession({
success() {
// session_key 未过期,并且在本生命周期一直有效
},
fail() {
// session_key 已经失效,需要重新执行登录流程
this.login() //重新登录
}
})
} else {
this.login() //重新登录
}
},
login() {
// 登录
wx.login({
success: res => {
// 发送 res.code 到后台换取 openId, sessionKey, unionId
wx.request({
url: this.globalData.domain + '/getSession',
method: 'POST',
data: {
code: res.code
},
success: res => {
const {
thirdSession
} = res.data
wx.setStorageSync('thirdSession', thirdSession)
},
fail: res => {
console.log(res.errMsg)
}
})
}
})
},
globalData: {
userInfo: null,
domain: 'https://api.wechat.activechai.cn'
}
})