feat: 添加登录路由拦截

This commit is contained in:
范鹏 2025-01-03 15:01:22 +08:00
parent fbed073568
commit b83de41177
6 changed files with 21 additions and 25 deletions

View File

@ -6,24 +6,19 @@
import { useUserStore } from '@/store'
import { getNeedLoginPages, needLoginPages as _needLoginPages } from '@/utils'
// TODO Check
const loginRoute = '/pages/login/index'
const loginRoute = '/pages/login/login'
const isLogined = () => {
const loginStatus = () => {
const userStore = useUserStore()
return userStore.isLogined
return userStore.loginStatus
}
const isDev = import.meta.env.DEV
// 黑名单登录拦截器 - (适用于大部分页面不需要登录,少部分页面需要登录)
const navigateToInterceptor = {
// 注意这里的url是 '/' 开头的,如 '/pages/index/index',跟 'pages.json' 里面的 path 不同
invoke({ url }: { url: string }) {
// console.log(url) // /pages/route-interceptor/index?name=feige&age=30
const path = url.split('?')[0]
let needLoginPages: string[] = []
// 为了防止开发时出现BUG这里每次都获取一下。生产环境可以移到函数外性能更好
if (isDev) {
needLoginPages = getNeedLoginPages()
} else {
@ -33,12 +28,11 @@ const navigateToInterceptor = {
if (!isNeedLogin) {
return true
}
const hasLogin = isLogined()
const hasLogin = loginStatus()
if (hasLogin) {
return true
}
const redirectRoute = `${loginRoute}?redirect=${encodeURIComponent(url)}`
uni.navigateTo({ url: redirectRoute })
uni.navigateTo({ url: loginRoute })
return false
}
}

View File

@ -76,7 +76,8 @@
"style": {
"navigationStyle": "custom",
"disableScroll": true
}
},
"needLogin": false
},
{
"path": "pages/login/login",
@ -84,7 +85,8 @@
"style": {
"navigationStyle": "custom",
"disableScroll": true
}
},
"needLogin": false
},
{
"path": "pages/mine/mine",
@ -100,7 +102,8 @@
"style": {
"navigationStyle": "custom",
"disableScroll": true
}
},
"needLogin": false
},
{
"path": "pages/workbench/workbench",

View File

@ -3,7 +3,8 @@
style: {
navigationStyle: 'custom',
disableScroll: true
}
},
needLogin: false
}
</route>
<template>

View File

@ -3,7 +3,8 @@
style: {
navigationStyle: 'custom',
disableScroll: true
}
},
needLogin: false
}
</route>
<template>

View File

@ -3,7 +3,8 @@
style: {
navigationStyle: 'custom',
disableScroll: true
}
},
needLogin: false
}
</route>
<template>

View File

@ -78,23 +78,21 @@ export const getUrlObj = (url: string) => {
* keypages key, key
*/
export const getAllPages = (key = 'needLogin') => {
// 这里处理主包
const mainPages = [
...pages
.filter(page => !key || page[key])
.filter(page => !key || page[key] !== false)
.map(page => ({
...page,
path: `/${page.path}`
}))
]
// 这里处理分包
const subPages: any[] = []
subPackages.forEach(subPageObj => {
// console.log(subPageObj)
const { root } = subPageObj
subPageObj.pages
.filter(page => !key || page[key])
.filter(page => !key || page[key] !== false)
.forEach((page: { path: string } & Record<string, any>) => {
subPages.push({
...page,
@ -102,9 +100,7 @@ export const getAllPages = (key = 'needLogin') => {
})
})
})
const result = [...mainPages, ...subPages]
// console.log(`getAllPages by ${key} result: `, result)
return result
return [...mainPages, ...subPages]
}
/**