-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathapp.js
72 lines (72 loc) · 1.74 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
import {
getAllSubs
} from './util/getSubs'
import {
getToken
} from './api/token'
let globalData = {
url: null,
}
App({
onLaunch: function () {
// 获取subs列表并放在缓存中
getAllSubs()
// 获取用户信息
wx.getSetting({
success: res => {
if (res.authSetting['scope.userInfo']) {
// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
wx.getUserInfo({
success: res => {
wx.login({
success(res) {
if (res.code) {
getToken(res.code).then((res) => {
wx.setStorage({
data: res.token,
key: 'token'
})
})
}
}
})
}
})
}
},
fail: rej => {
console.log('error')
}
})
this.proxy()
},
// 类似vue-router中的路由卫士
// 利用监测url的变化也就是路由的变化,来监测是否有用户的授权,没授权的就返回登陆页面
proxy () {
Object.defineProperty(globalData, 'url', {
get: function (val) {
return val
},
set: function (newValue) {
wx.getSetting({
success: (res) => {
if (res.authSetting['scope.userInfo']) {
wx.navigateTo({
url: newValue,
success: globalData['success']
})
} else {
wx.navigateTo({
url: '/pages/login/login',
})
}
}
})
}
})
},
navigateTo(arg) {
globalData.url = arg.url
globalData.success = arg.success
}
})