手持机
This commit is contained in:
115
RuoYi-App-master/permission.js
Normal file
115
RuoYi-App-master/permission.js
Normal file
@@ -0,0 +1,115 @@
|
||||
import { getToken } from '@/utils/auth'
|
||||
|
||||
// 登录页面
|
||||
const loginPage = "/pages/login"
|
||||
|
||||
// 标记是否由navigateBack触发的reLaunch
|
||||
let isNavigateBackTriggered = false
|
||||
|
||||
// 获取当前页面路径
|
||||
function getCurrentPageUrl() {
|
||||
const pages = getCurrentPages()
|
||||
const currentPage = pages[pages.length - 1]
|
||||
return currentPage ? currentPage.route : ''
|
||||
}
|
||||
|
||||
// 页面白名单
|
||||
const whiteList = [
|
||||
'/pages/login', '/pages/register', '/pages/common/webview/index','/pages/setting'
|
||||
]
|
||||
|
||||
// 检查地址白名单
|
||||
function checkWhite(url) {
|
||||
// 确保url以/开头
|
||||
const normalizedUrl = url.startsWith('/') ? url : '/' + url
|
||||
const path = normalizedUrl.split('?')[0]
|
||||
return whiteList.indexOf(path) !== -1
|
||||
}
|
||||
|
||||
// 页面跳转验证拦截器
|
||||
let list = ["navigateTo", "redirectTo", "reLaunch", "switchTab"]
|
||||
list.forEach(item => {
|
||||
uni.addInterceptor(item, {
|
||||
invoke(to) {
|
||||
console.log(`${item} 拦截器被触发,目标页面:`, to.url)
|
||||
console.log('当前 Token 状态:', getToken() ? '存在' : '不存在')
|
||||
|
||||
// 如果是由navigateBack触发的reLaunch,直接放行
|
||||
if (item === 'reLaunch' && isNavigateBackTriggered) {
|
||||
console.log('由navigateBack触发的reLaunch,直接放行')
|
||||
return true
|
||||
}
|
||||
|
||||
if (getToken()) {
|
||||
console.log('Token 存在,检查是否为登录页面')
|
||||
if (to.url === loginPage) {
|
||||
console.log('当前为登录页面,重定向到首页')
|
||||
uni.reLaunch({ url: "/" })
|
||||
}
|
||||
return true
|
||||
} else {
|
||||
console.log('Token 不存在,检查是否在白名单中')
|
||||
if (checkWhite(to.url)) {
|
||||
console.log('目标页面在白名单中,允许访问')
|
||||
return true
|
||||
}
|
||||
console.log('页面不在白名单中,准备跳转到登录页')
|
||||
uni.showToast({
|
||||
icon: 'none',
|
||||
title: '登录已过期,请重新登录'
|
||||
})
|
||||
setTimeout(() => {
|
||||
console.log('执行跳转到登录页')
|
||||
uni.reLaunch({ url: loginPage })
|
||||
}, 1500)
|
||||
return false
|
||||
}
|
||||
},
|
||||
fail(err) {
|
||||
console.error(err)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
// 单独处理返回按钮操作
|
||||
uni.addInterceptor('navigateBack', {
|
||||
invoke(e) {
|
||||
console.log('navigateBack 拦截器被触发', e)
|
||||
console.log('当前 Token 状态:', getToken() ? '存在' : '不存在')
|
||||
|
||||
if (!getToken()) {
|
||||
const currentPage = getCurrentPageUrl()
|
||||
console.log('当前页面路径:', currentPage)
|
||||
|
||||
// 确保currentPage不为空且有内容
|
||||
if (currentPage && currentPage.trim() && checkWhite(currentPage)) {
|
||||
console.log('当前页面在白名单中,允许返回')
|
||||
return true
|
||||
}
|
||||
|
||||
console.log('Token 不存在且页面不在白名单中,准备跳转到登录页')
|
||||
uni.showToast({
|
||||
icon: 'none',
|
||||
title: '登录已过期,请重新登录'
|
||||
})
|
||||
|
||||
// 设置标志,表明当前的reLaunch是由navigateBack触发的
|
||||
isNavigateBackTriggered = true;
|
||||
setTimeout(() => {
|
||||
console.log('执行跳转到登录页')
|
||||
uni.reLaunch({ url: loginPage })
|
||||
// 重置标志
|
||||
setTimeout(() => {
|
||||
isNavigateBackTriggered = false;
|
||||
}, 100);
|
||||
}, 1500)
|
||||
return false
|
||||
}
|
||||
|
||||
console.log('Token 存在,允许正常返回')
|
||||
return true
|
||||
},
|
||||
fail(err) {
|
||||
console.error('navigateBack 拦截器执行失败:', err)
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user