-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
47 lines (45 loc) · 1.13 KB
/
app.js
File metadata and controls
47 lines (45 loc) · 1.13 KB
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({
globalData: {
baseUrl: 'https://api.zhengw-tech.com'
},
request(options) {
const token = wx.getStorageSync('jwtToken')
return new Promise((resolve, reject) => {
wx.request({
...options,
url: `${this.globalData.baseUrl}${options.url}`,
header: {
...options.header,
'Cookie': token ? `jwtToken=${token}` : ''
},
success: (res) => {
// 统一处理未授权的情况
if (res.statusCode === 401 || res.data.status === 401) {
wx.removeStorageSync('jwtToken')
wx.redirectTo({
url: '/pages/login/login'
})
reject(new Error('未授权'))
return
}
resolve(res)
},
fail: reject
})
})
},
onLaunch() {
// 展示本地存储能力
const logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
// 检查登录状态
const token = wx.getStorageSync('jwtToken');
if (!token) {
wx.reLaunch({
url: '/pages/login/login'
});
}
}
})