-
Notifications
You must be signed in to change notification settings - Fork 104
/
app.js
80 lines (72 loc) · 1.87 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
//app.js
import wxutil from './miniprogram_npm/@yyjeffrey/wxutil/index'
import { Auth } from './models/auth'
App({
globalData: {
appId: wx.getAccountInfoSync().miniProgram.appId,
githubURI: 'YYJeffrey/july_client',
githubURL: 'https://github.com/YYJeffrey/july_client',
likeAuthor: 'https://img.yejiefeng.com/qr/qr_like.png', // 作者的赞赏码
userDetail: null, // 用户信息详情
tokenExpires: 86400 * 27 // Token过期时间
},
onLaunch() {
this.getUserDetail()
wxutil.autoUpdate()
},
/**
* 获取用户详情
*/
getUserDetail() {
const deadtime = wxutil.getStorage('userDetail_deadtime')
if (deadtime) {
const remainTime = parseInt(deadtime) - Date.parse(new Date()) / 1000
// 令牌剩余 25% 自动续期
if (remainTime <= this.globalData.tokenExpires * 0.25) {
this.passiveAuth()
}
}
const userDetail = wxutil.getStorage('userDetail')
if (userDetail) {
this.globalData.userDetail = userDetail
} else {
this.globalData.userDetail = null
}
},
/**
* 被动授权
*/
passiveAuth() {
wx.login({
success: async (res) => {
const info = await Auth.passive(res.code)
if (info.code === 0) {
const userDetail = info.data
wxutil.setStorage('userDetail', userDetail, this.globalData.tokenExpires)
this.globalData.userDetail = userDetail
}
}
})
},
/**
* 获取请求头
*/
getHeader() {
let header = {}
if (this.globalData.userDetail) {
header['Authorization'] = 'Token ' + this.globalData.userDetail.token
}
return header
},
/**
* Token无效跳转授权页
*/
gotoAuthPage(res) {
if (res.code == 10120 || res.code == 10121) {
wx.removeStorageSync('userDetail')
wx.navigateTo({
url: '/pages/auth/index'
})
}
}
})