所有页面未登录时, 被跳转到登录页面。

This commit is contained in:
zhonghua1
2025-12-17 10:13:04 +08:00
parent 69400a9b01
commit 74a5742e03
5 changed files with 416 additions and 3 deletions

143
App.vue
View File

@@ -6,6 +6,8 @@
// #endif
import checkIsAgree from '@/pages/uni-agree/utils/uni-agree.js';
import uniIdPageInit from '@/uni_modules/uni-id-pages/init.js';
import { store } from '@/uni_modules/uni-id-pages/common/store.js';
export default {
globalData: {
searchText: '',
@@ -21,6 +23,14 @@
initApp();
uniIdPageInit()
// 设置全局路由拦截,检查登录状态
this.setupRouteInterceptor()
// 延迟检查登录状态,确保 store 已初始化
setTimeout(() => {
this.checkCurrentPageLogin()
}, 100)
// #ifdef APP
//checkIsAgree(); APP端暂时先用原生默认生成的。目前,自定义方式启动vue界面时,原生层已经请求了部分权限这并不符合国家的法规
// #endif
@@ -44,9 +54,142 @@
},
onShow: function() {
console.log('App Show')
// 每次应用显示时也检查当前页面是否需要登录
this.checkCurrentPageLogin()
},
onHide: function() {
console.log('App Hide')
},
methods: {
/**
* 设置全局路由拦截
*/
setupRouteInterceptor() {
const app = this
// 拦截路由跳转(适用于所有平台)
uni.addInterceptor('navigateTo', {
invoke: (options) => {
if (app.shouldRedirectToLogin(options.url)) {
app.redirectToLogin()
return false // 阻止跳转
}
}
})
uni.addInterceptor('redirectTo', {
invoke: (options) => {
if (app.shouldRedirectToLogin(options.url)) {
app.redirectToLogin()
return false // 阻止跳转
}
}
})
uni.addInterceptor('switchTab', {
invoke: (options) => {
if (app.shouldRedirectToLogin(options.url)) {
app.redirectToLogin()
return false // 阻止跳转
}
}
})
uni.addInterceptor('reLaunch', {
invoke: (options) => {
if (app.shouldRedirectToLogin(options.url)) {
app.redirectToLogin()
return false // 阻止跳转
}
}
})
},
/**
* 判断是否需要跳转到登录页
*/
shouldRedirectToLogin(url) {
if (!url) return false
// 排除登录相关页面
const excludePaths = [
'/uni_modules/uni-id-pages/pages/login',
'/uni_modules/uni-id-pages/pages/register',
'/uni_modules/uni-id-pages/pages/retrieve'
]
// 检查是否在排除列表中
for (let excludePath of excludePaths) {
if (url.includes(excludePath)) {
return false
}
}
// 检查登录状态
return !this.isLoggedIn()
},
/**
* 检查是否已登录
*/
isLoggedIn() {
// 优先检查后端登录态
const backendToken = uni.getStorageSync('backend-token')
if (backendToken) {
return true
}
// 其次检查 uni-id 的登录态
return store.hasLogin
},
/**
* 跳转到登录页
*/
redirectToLogin() {
const loginPage = '/uni_modules/uni-id-pages/pages/login/login-withpwd'
const pages = getCurrentPages()
// 如果当前已经在登录页,不重复跳转
if (pages.length > 0) {
const currentPage = pages[pages.length - 1]
if (currentPage.route && currentPage.route.includes('login')) {
return
}
}
uni.reLaunch({
url: loginPage,
fail: (err) => {
console.error('跳转登录页失败:', err)
}
})
},
/**
* 检查当前页面是否需要登录
*/
checkCurrentPageLogin() {
const pages = getCurrentPages()
if (pages.length === 0) return
const currentPage = pages[pages.length - 1]
if (!currentPage || !currentPage.route) return
const currentRoute = '/' + currentPage.route
// 排除登录相关页面
if (currentRoute.includes('/uni_modules/uni-id-pages/pages/login') ||
currentRoute.includes('/uni_modules/uni-id-pages/pages/register') ||
currentRoute.includes('/uni_modules/uni-id-pages/pages/retrieve')) {
return
}
// 如果未登录,跳转到登录页
if (!this.isLoggedIn()) {
this.redirectToLogin()
}
}
}
}
</script>