47 lines
1.2 KiB
TypeScript
Raw Normal View History

2024-12-31 10:05:55 +08:00
/**
*
*
* 便使
*/
import { useUserStore } from '@/store'
import { getNeedLoginPages, needLoginPages as _needLoginPages } from '@/utils'
2025-01-03 15:01:22 +08:00
const loginRoute = '/pages/login/login'
2024-12-31 10:05:55 +08:00
2025-01-03 15:01:22 +08:00
const loginStatus = () => {
2024-12-31 10:05:55 +08:00
const userStore = useUserStore()
2025-01-03 15:01:22 +08:00
return userStore.loginStatus
2024-12-31 10:05:55 +08:00
}
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
}
2025-01-03 15:01:22 +08:00
const hasLogin = loginStatus()
2024-12-31 10:05:55 +08:00
if (hasLogin) {
return true
}
2025-01-03 15:01:22 +08:00
uni.navigateTo({ url: loginRoute })
2024-12-31 10:05:55 +08:00
return false
2024-12-31 10:19:20 +08:00
}
2024-12-31 10:05:55 +08:00
}
export const routeInterceptor = {
install() {
uni.addInterceptor('navigateTo', navigateToInterceptor)
uni.addInterceptor('reLaunch', navigateToInterceptor)
uni.addInterceptor('redirectTo', navigateToInterceptor)
2024-12-31 10:19:20 +08:00
}
2024-12-31 10:05:55 +08:00
}