85 lines
1.8 KiB
Vue
85 lines
1.8 KiB
Vue
<script>
|
|
import { isLoggedIn } from '@/utils/auth'
|
|
|
|
// 需要登录才能访问的页面路径
|
|
const AUTH_PAGES = [
|
|
'pages/points/index',
|
|
'pages/points/detail',
|
|
'pages/mine/index',
|
|
'pages/mine/profile',
|
|
'pages/mine/verify-records',
|
|
'pages/coupon/my-coupons',
|
|
'pages/coupon/store-coupons',
|
|
'pages/merchant/index',
|
|
'pages/merchant/records'
|
|
]
|
|
|
|
/**
|
|
* 路由守卫:拦截页面跳转,未登录时重定向到登录页
|
|
*/
|
|
function setupRouteGuard() {
|
|
// 拦截 uni.navigateTo
|
|
const originalNavigateTo = uni.navigateTo
|
|
uni.navigateTo = function(options) {
|
|
if (checkAuth(options.url)) {
|
|
return originalNavigateTo.call(this, options)
|
|
}
|
|
}
|
|
|
|
// 拦截 uni.redirectTo
|
|
const originalRedirectTo = uni.redirectTo
|
|
uni.redirectTo = function(options) {
|
|
if (checkAuth(options.url)) {
|
|
return originalRedirectTo.call(this, options)
|
|
}
|
|
}
|
|
|
|
// 拦截 uni.switchTab
|
|
const originalSwitchTab = uni.switchTab
|
|
uni.switchTab = function(options) {
|
|
if (checkAuth(options.url)) {
|
|
return originalSwitchTab.call(this, options)
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 检查页面是否需要登录
|
|
* @param {string} url - 页面路径
|
|
* @returns {boolean} 是否允许访问
|
|
*/
|
|
function checkAuth(url) {
|
|
if (!url) return true
|
|
// 去掉开头的 / 和查询参数
|
|
const path = url.replace(/^\//, '').split('?')[0]
|
|
const needAuth = AUTH_PAGES.some(p => path === p)
|
|
if (needAuth && !isLoggedIn()) {
|
|
uni.navigateTo({ url: '/pages/login/index' })
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
export default {
|
|
onLaunch: function() {
|
|
console.log('App Launch')
|
|
setupRouteGuard()
|
|
},
|
|
onShow: function() {
|
|
console.log('App Show')
|
|
},
|
|
onHide: function() {
|
|
console.log('App Hide')
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<style>
|
|
/* 全局公共样式 */
|
|
page {
|
|
background-color: #f5f5f5;
|
|
font-size: 28rpx;
|
|
color: #333333;
|
|
}
|
|
</style>
|