47 lines
1.2 KiB
TypeScript
47 lines
1.2 KiB
TypeScript
/**
|
|
* 路由拦截,通常也是登录拦截
|
|
* 可以设置路由白名单,或者黑名单,看业务需要选哪一个
|
|
* 我这里应为大部分都可以随便进入,所以使用黑名单
|
|
*/
|
|
import { useUserStore } from '@/store'
|
|
import { getNeedLoginPages, needLoginPages as _needLoginPages } from '@/utils'
|
|
|
|
const loginRoute = '/pages/login/login'
|
|
|
|
const loginStatus = () => {
|
|
const userStore = useUserStore()
|
|
return userStore.loginStatus
|
|
}
|
|
|
|
const isDev = import.meta.env.DEV
|
|
|
|
const navigateToInterceptor = {
|
|
invoke({ url }: { url: string }) {
|
|
const path = url.split('?')[0]
|
|
let needLoginPages: string[] = []
|
|
if (isDev) {
|
|
needLoginPages = getNeedLoginPages()
|
|
} else {
|
|
needLoginPages = _needLoginPages
|
|
}
|
|
const isNeedLogin = needLoginPages.includes(path)
|
|
if (!isNeedLogin) {
|
|
return true
|
|
}
|
|
const hasLogin = loginStatus()
|
|
if (hasLogin) {
|
|
return true
|
|
}
|
|
uni.navigateTo({ url: loginRoute })
|
|
return false
|
|
}
|
|
}
|
|
|
|
export const routeInterceptor = {
|
|
install() {
|
|
uni.addInterceptor('navigateTo', navigateToInterceptor)
|
|
uni.addInterceptor('reLaunch', navigateToInterceptor)
|
|
uni.addInterceptor('redirectTo', navigateToInterceptor)
|
|
}
|
|
}
|