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
| // 白名单
| const whiteList = [, // 注意入口页必须直接写 '/'
| '/pages/login/login'
| ]
| export default async function() {
| const list = ['navigateTo', 'redirectTo', 'reLaunch', 'switchTab']
| // 添加全局路由拦截
| list.forEach(item => {
| uni.addInterceptor(item, {
| // 路由前拦截
| invoke(args) {
| console.log('navigateTo路由跳转成功', args)
| const token = uni.getStorageSync('tonken')
| const url = args.url.split('?')[0]
| if (token) {
| if (args.url.includes('/pages/login/login')) {
| //跳转到首页
| uni.navigateTo({ url: '/pages/index/index', })
| return false
| }
| return args
| } else {
| let pass
| if (whiteList) {
| pass = whiteList.some(whiteItem => {
| if (typeof(whiteItem) === 'object' && whiteItem.pattern) {
| return whiteItem.pattern.test(url)
| }
| return url === whiteItem
| })
| }
| console.log('pass', pass)
| if (!pass) {
| uni.showModal({
| title: '未登录',
| content: '您未登录,需要登录后才能继续',
| showCancel: false,
| confirmText: '确定',
| success: res => {
| if (res.confirm) {
| uni.reLaunch({ url: '/pages/login/login', })
| }
| },
| })
| return false
| }
| return args
| }
| },
| // 路由后拦截
| success(res) {},
| fail(err) {}
| })
| })
| }
|
|