commit | author | age
|
efef1c
|
1 |
import { getToken } from '@/utils/auth' |
Z |
2 |
|
|
3 |
// 登录页面 |
|
4 |
const loginPage = "/pages/login" |
|
5 |
|
|
6 |
// 页面白名单 |
|
7 |
const whiteList = [ |
|
8 |
'/pages/login', '/pages/register', '/pages/common/webview/index' |
|
9 |
] |
|
10 |
|
|
11 |
// 检查地址白名单 |
|
12 |
function checkWhite(url) { |
|
13 |
const path = url.split('?')[0] |
|
14 |
return whiteList.indexOf(path) !== -1 |
|
15 |
} |
|
16 |
|
|
17 |
// 页面跳转验证拦截器 |
|
18 |
let list = ["navigateTo", "redirectTo", "reLaunch", "switchTab"] |
|
19 |
list.forEach(item => { |
|
20 |
uni.addInterceptor(item, { |
|
21 |
invoke(to) { |
|
22 |
if (getToken()) { |
|
23 |
if (to.url === loginPage) { |
|
24 |
uni.reLaunch({ url: "/" }) |
|
25 |
} |
|
26 |
return true |
|
27 |
} else { |
|
28 |
if (checkWhite(to.url)) { |
|
29 |
return true |
|
30 |
} |
|
31 |
uni.reLaunch({ url: loginPage }) |
|
32 |
return false |
|
33 |
} |
|
34 |
}, |
|
35 |
fail(err) { |
|
36 |
console.log(err) |
|
37 |
} |
|
38 |
}) |
|
39 |
}) |