2024-03-27 17:20:05 +08:00
|
|
|
|
/**
|
2025-08-19 17:07:42 +08:00
|
|
|
|
* by 菲鸽 on 2025-08-19
|
2024-03-27 17:20:05 +08:00
|
|
|
|
* 路由拦截,通常也是登录拦截
|
|
|
|
|
|
* 可以设置路由白名单,或者黑名单,看业务需要选哪一个
|
|
|
|
|
|
* 我这里应为大部分都可以随便进入,所以使用黑名单
|
|
|
|
|
|
*/
|
|
|
|
|
|
import { useUserStore } from '@/store'
|
2025-08-18 22:01:55 +08:00
|
|
|
|
import { tabbarStore } from '@/tabbar/store'
|
2025-08-21 20:22:46 +08:00
|
|
|
|
import { getLastPage, parseUrlToObj } from '@/utils/index'
|
2025-08-19 17:07:42 +08:00
|
|
|
|
import { EXCLUDE_PAGE_LIST, isNeedLogin, LOGIN_PAGE, LOGIN_PAGE_LIST } from '../login/config'
|
2024-03-27 17:20:05 +08:00
|
|
|
|
|
|
|
|
|
|
// 黑名单登录拦截器 - (适用于大部分页面不需要登录,少部分页面需要登录)
|
2025-08-07 18:22:57 +08:00
|
|
|
|
export const navigateToInterceptor = {
|
2025-04-28 18:18:12 +08:00
|
|
|
|
// 注意,这里的url是 '/' 开头的,如 '/pages/index/index',跟 'pages.json' 里面的 path 不同
|
|
|
|
|
|
// 增加对相对路径的处理,BY 网友 @ideal
|
2025-08-21 16:18:28 +08:00
|
|
|
|
invoke({ url, query }: { url: string, query?: Record<string, string> }) {
|
2025-08-18 22:01:55 +08:00
|
|
|
|
if (url === undefined) {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-08-21 20:22:46 +08:00
|
|
|
|
let { path, query: _query } = parseUrlToObj(url)
|
2025-08-21 18:46:54 +08:00
|
|
|
|
|
2025-08-21 20:10:47 +08:00
|
|
|
|
console.log('路由拦截器 1: url->', url, ', query ->', query)
|
|
|
|
|
|
const myQuery = { ..._query, ...query }
|
2025-08-21 18:46:54 +08:00
|
|
|
|
// /pages/route-interceptor/index?name=feige&age=30
|
2025-08-21 20:10:47 +08:00
|
|
|
|
console.log('路由拦截器 2: path->', path, ', _query ->', _query)
|
|
|
|
|
|
console.log('路由拦截器 3: myQuery ->', myQuery)
|
2025-04-25 17:07:12 +08:00
|
|
|
|
|
|
|
|
|
|
// 处理相对路径
|
|
|
|
|
|
if (!path.startsWith('/')) {
|
2025-08-18 21:24:05 +08:00
|
|
|
|
const currentPath = getLastPage()?.route || ''
|
2025-04-25 17:07:12 +08:00
|
|
|
|
const normalizedCurrentPath = currentPath.startsWith('/') ? currentPath : `/${currentPath}`
|
|
|
|
|
|
const baseDir = normalizedCurrentPath.substring(0, normalizedCurrentPath.lastIndexOf('/'))
|
|
|
|
|
|
path = `${baseDir}/${path}`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-18 22:01:55 +08:00
|
|
|
|
// 处理直接进入路由非首页时,tabbarIndex 不正确的问题
|
2025-08-21 20:10:47 +08:00
|
|
|
|
tabbarStore.setAutoCurIdx(path)
|
2025-08-18 22:01:55 +08:00
|
|
|
|
|
2025-08-21 20:10:47 +08:00
|
|
|
|
if (LOGIN_PAGE_LIST.includes(path)) {
|
2025-08-21 18:46:54 +08:00
|
|
|
|
console.log('命中了 LOGIN_PAGE_LIST')
|
2025-08-18 21:24:05 +08:00
|
|
|
|
return
|
2024-03-27 17:20:05 +08:00
|
|
|
|
}
|
2025-08-18 21:24:05 +08:00
|
|
|
|
|
2025-08-21 20:10:47 +08:00
|
|
|
|
if (myQuery) {
|
|
|
|
|
|
path += `?${Object.keys(myQuery).map(key => `${key}=${myQuery[key]}`).join('&')}`
|
2024-03-27 17:20:05 +08:00
|
|
|
|
}
|
2025-08-19 17:07:42 +08:00
|
|
|
|
const redirectUrl = `${LOGIN_PAGE}?redirect=${encodeURIComponent(path)}`
|
|
|
|
|
|
|
2025-08-18 21:24:05 +08:00
|
|
|
|
const userStore = useUserStore()
|
2025-08-21 18:46:54 +08:00
|
|
|
|
console.log('userStore.hasLogin:', userStore.hasLogin)
|
2025-08-18 21:52:32 +08:00
|
|
|
|
|
2025-08-19 17:07:42 +08:00
|
|
|
|
// #region 1/2 需要登录的情况 ---------------------------
|
|
|
|
|
|
if (isNeedLogin) {
|
|
|
|
|
|
if (userStore.hasLogin) {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
2025-08-21 20:10:47 +08:00
|
|
|
|
if (EXCLUDE_PAGE_LIST.includes(path)) {
|
2025-08-19 17:07:42 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
2025-08-21 18:46:54 +08:00
|
|
|
|
console.log('isNeedLogin redirectUrl:', redirectUrl)
|
2025-08-19 17:07:42 +08:00
|
|
|
|
uni.navigateTo({ url: redirectUrl })
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// #endregion 1/2 需要登录的情况 ---------------------------
|
2025-08-18 21:24:05 +08:00
|
|
|
|
|
2025-08-19 17:07:42 +08:00
|
|
|
|
// #region 2/2 不需要登录的情况 ---------------------------
|
|
|
|
|
|
else {
|
|
|
|
|
|
if (EXCLUDE_PAGE_LIST.includes(path)) {
|
2025-08-21 18:46:54 +08:00
|
|
|
|
console.log('isNeedLogin redirectUrl:', redirectUrl)
|
2025-08-19 17:07:42 +08:00
|
|
|
|
uni.navigateTo({ url: redirectUrl })
|
|
|
|
|
|
}
|
2025-08-18 21:24:05 +08:00
|
|
|
|
}
|
2025-08-19 17:07:42 +08:00
|
|
|
|
// #endregion 2/2 不需要登录的情况 ---------------------------
|
2024-03-27 17:20:05 +08:00
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export const routeInterceptor = {
|
|
|
|
|
|
install() {
|
|
|
|
|
|
uni.addInterceptor('navigateTo', navigateToInterceptor)
|
|
|
|
|
|
uni.addInterceptor('reLaunch', navigateToInterceptor)
|
|
|
|
|
|
uni.addInterceptor('redirectTo', navigateToInterceptor)
|
2024-12-31 12:18:17 +08:00
|
|
|
|
uni.addInterceptor('switchTab', navigateToInterceptor)
|
2024-03-27 17:20:05 +08:00
|
|
|
|
},
|
|
|
|
|
|
}
|