| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- // src/permission.js
- import { getToken } from '@/utils/auth'
-
- // 页面白名单
- const whiteList = [
- '/pages/index/index', // 首页无需登录
- '/pages/login',
- '/pages/register'
- ]
-
- // 检查地址白名单
- function checkWhite(url) {
- const path = url.split('?')[0]
- return whiteList.indexOf(path) !== -1
- }
-
- // 页面跳转验证拦截器
- let list = ["navigateTo", "redirectTo", "reLaunch", "switchTab"]
- list.forEach(item => {
- uni.addInterceptor(item, {
- invoke(to) {
- // 始终允许访问白名单页面
- if (checkWhite(to.url)) {
- return true
- }
-
- // 检查登录状态
- if (getToken()) {
- return true
- } else {
- // 非白名单页面重定向到登录
- uni.reLaunch({ url: "/pages/login" })
- return false
- }
- },
- fail(err) {
- console.log(err)
- }
- })
- })
|