feat: 添加登录路由拦截
This commit is contained in:
parent
fbed073568
commit
b83de41177
@ -6,24 +6,19 @@
|
|||||||
import { useUserStore } from '@/store'
|
import { useUserStore } from '@/store'
|
||||||
import { getNeedLoginPages, needLoginPages as _needLoginPages } from '@/utils'
|
import { getNeedLoginPages, needLoginPages as _needLoginPages } from '@/utils'
|
||||||
|
|
||||||
// TODO Check
|
const loginRoute = '/pages/login/login'
|
||||||
const loginRoute = '/pages/login/index'
|
|
||||||
|
|
||||||
const isLogined = () => {
|
const loginStatus = () => {
|
||||||
const userStore = useUserStore()
|
const userStore = useUserStore()
|
||||||
return userStore.isLogined
|
return userStore.loginStatus
|
||||||
}
|
}
|
||||||
|
|
||||||
const isDev = import.meta.env.DEV
|
const isDev = import.meta.env.DEV
|
||||||
|
|
||||||
// 黑名单登录拦截器 - (适用于大部分页面不需要登录,少部分页面需要登录)
|
|
||||||
const navigateToInterceptor = {
|
const navigateToInterceptor = {
|
||||||
// 注意,这里的url是 '/' 开头的,如 '/pages/index/index',跟 'pages.json' 里面的 path 不同
|
|
||||||
invoke({ url }: { url: string }) {
|
invoke({ url }: { url: string }) {
|
||||||
// console.log(url) // /pages/route-interceptor/index?name=feige&age=30
|
|
||||||
const path = url.split('?')[0]
|
const path = url.split('?')[0]
|
||||||
let needLoginPages: string[] = []
|
let needLoginPages: string[] = []
|
||||||
// 为了防止开发时出现BUG,这里每次都获取一下。生产环境可以移到函数外,性能更好
|
|
||||||
if (isDev) {
|
if (isDev) {
|
||||||
needLoginPages = getNeedLoginPages()
|
needLoginPages = getNeedLoginPages()
|
||||||
} else {
|
} else {
|
||||||
@ -33,12 +28,11 @@ const navigateToInterceptor = {
|
|||||||
if (!isNeedLogin) {
|
if (!isNeedLogin) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
const hasLogin = isLogined()
|
const hasLogin = loginStatus()
|
||||||
if (hasLogin) {
|
if (hasLogin) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
const redirectRoute = `${loginRoute}?redirect=${encodeURIComponent(url)}`
|
uni.navigateTo({ url: loginRoute })
|
||||||
uni.navigateTo({ url: redirectRoute })
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -76,7 +76,8 @@
|
|||||||
"style": {
|
"style": {
|
||||||
"navigationStyle": "custom",
|
"navigationStyle": "custom",
|
||||||
"disableScroll": true
|
"disableScroll": true
|
||||||
}
|
},
|
||||||
|
"needLogin": false
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"path": "pages/login/login",
|
"path": "pages/login/login",
|
||||||
@ -84,7 +85,8 @@
|
|||||||
"style": {
|
"style": {
|
||||||
"navigationStyle": "custom",
|
"navigationStyle": "custom",
|
||||||
"disableScroll": true
|
"disableScroll": true
|
||||||
}
|
},
|
||||||
|
"needLogin": false
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"path": "pages/mine/mine",
|
"path": "pages/mine/mine",
|
||||||
@ -100,7 +102,8 @@
|
|||||||
"style": {
|
"style": {
|
||||||
"navigationStyle": "custom",
|
"navigationStyle": "custom",
|
||||||
"disableScroll": true
|
"disableScroll": true
|
||||||
}
|
},
|
||||||
|
"needLogin": false
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"path": "pages/workbench/workbench",
|
"path": "pages/workbench/workbench",
|
||||||
|
|||||||
@ -3,7 +3,8 @@
|
|||||||
style: {
|
style: {
|
||||||
navigationStyle: 'custom',
|
navigationStyle: 'custom',
|
||||||
disableScroll: true
|
disableScroll: true
|
||||||
}
|
},
|
||||||
|
needLogin: false
|
||||||
}
|
}
|
||||||
</route>
|
</route>
|
||||||
<template>
|
<template>
|
||||||
|
|||||||
@ -3,7 +3,8 @@
|
|||||||
style: {
|
style: {
|
||||||
navigationStyle: 'custom',
|
navigationStyle: 'custom',
|
||||||
disableScroll: true
|
disableScroll: true
|
||||||
}
|
},
|
||||||
|
needLogin: false
|
||||||
}
|
}
|
||||||
</route>
|
</route>
|
||||||
<template>
|
<template>
|
||||||
|
|||||||
@ -3,7 +3,8 @@
|
|||||||
style: {
|
style: {
|
||||||
navigationStyle: 'custom',
|
navigationStyle: 'custom',
|
||||||
disableScroll: true
|
disableScroll: true
|
||||||
}
|
},
|
||||||
|
needLogin: false
|
||||||
}
|
}
|
||||||
</route>
|
</route>
|
||||||
<template>
|
<template>
|
||||||
|
|||||||
@ -78,23 +78,21 @@ export const getUrlObj = (url: string) => {
|
|||||||
* 如果没有传 key,则表示所有的pages,如果传递了 key, 则表示通过 key 过滤
|
* 如果没有传 key,则表示所有的pages,如果传递了 key, 则表示通过 key 过滤
|
||||||
*/
|
*/
|
||||||
export const getAllPages = (key = 'needLogin') => {
|
export const getAllPages = (key = 'needLogin') => {
|
||||||
// 这里处理主包
|
|
||||||
const mainPages = [
|
const mainPages = [
|
||||||
...pages
|
...pages
|
||||||
.filter(page => !key || page[key])
|
.filter(page => !key || page[key] !== false)
|
||||||
.map(page => ({
|
.map(page => ({
|
||||||
...page,
|
...page,
|
||||||
path: `/${page.path}`
|
path: `/${page.path}`
|
||||||
}))
|
}))
|
||||||
]
|
]
|
||||||
// 这里处理分包
|
|
||||||
const subPages: any[] = []
|
const subPages: any[] = []
|
||||||
subPackages.forEach(subPageObj => {
|
subPackages.forEach(subPageObj => {
|
||||||
// console.log(subPageObj)
|
|
||||||
const { root } = subPageObj
|
const { root } = subPageObj
|
||||||
|
|
||||||
subPageObj.pages
|
subPageObj.pages
|
||||||
.filter(page => !key || page[key])
|
.filter(page => !key || page[key] !== false)
|
||||||
.forEach((page: { path: string } & Record<string, any>) => {
|
.forEach((page: { path: string } & Record<string, any>) => {
|
||||||
subPages.push({
|
subPages.push({
|
||||||
...page,
|
...page,
|
||||||
@ -102,9 +100,7 @@ export const getAllPages = (key = 'needLogin') => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
const result = [...mainPages, ...subPages]
|
return [...mainPages, ...subPages]
|
||||||
// console.log(`getAllPages by ${key} result: `, result)
|
|
||||||
return result
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user