初始化项目
This commit is contained in:
44
attractor-ui/permission.js
Normal file
44
attractor-ui/permission.js
Normal file
@@ -0,0 +1,44 @@
|
||||
import { getToken } from '@/utils/auth'
|
||||
|
||||
// 登录页面
|
||||
const loginPage = '/pages/login'
|
||||
|
||||
// 页面白名单
|
||||
const whiteList = [
|
||||
'/pages/login',
|
||||
'/pages/register',
|
||||
'/pages/common/webview/index'
|
||||
]
|
||||
|
||||
// 检查地址白名单
|
||||
function checkWhite(url) {
|
||||
const path = (url || '').split('?')[0]
|
||||
return whiteList.indexOf(path) !== -1
|
||||
}
|
||||
|
||||
// 页面跳转验证拦截器
|
||||
const list = ['navigateTo', 'redirectTo', 'reLaunch', 'switchTab']
|
||||
list.forEach(item => {
|
||||
uni.addInterceptor(item, {
|
||||
invoke(to) {
|
||||
const hasToken = !!getToken()
|
||||
if (hasToken) {
|
||||
if (to.url === loginPage) {
|
||||
uni.reLaunch({ url: '/pages/devices/devices' })
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
if (checkWhite(to.url)) {
|
||||
return true
|
||||
}
|
||||
|
||||
uni.reLaunch({ url: loginPage })
|
||||
return false
|
||||
},
|
||||
fail(err) {
|
||||
console.log(err)
|
||||
}
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user