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
|
|
|
|
* 路由拦截,通常也是登录拦截
|
2025-09-01 14:28:02 +08:00
|
|
|
|
* 黑白名单的配置,请看 config.ts 文件, EXCLUDE_LOGIN_PATH_LIST
|
2024-03-27 17:20:05 +08:00
|
|
|
|
*/
|
2025-08-22 16:16:17 +08:00
|
|
|
|
import { useTokenStore } from '@/store/token'
|
2025-09-01 15:40:24 +08:00
|
|
|
|
import { isPageTabbar, tabbarStore } from '@/tabbar/store'
|
2025-09-01 14:40:08 +08:00
|
|
|
|
import { getAllPages, getLastPage, parseUrlToObj } from '@/utils/index'
|
2025-09-01 15:40:24 +08:00
|
|
|
|
import { EXCLUDE_LOGIN_PATH_LIST, HOME_PAGE, isNeedLoginMode, LOGIN_PAGE } from './config'
|
2024-03-27 17:20:05 +08:00
|
|
|
|
|
2025-09-01 14:40:08 +08:00
|
|
|
|
const isDev = import.meta.env.DEV
|
2025-08-29 15:15:14 +08:00
|
|
|
|
export const FG_LOG_ENABLE = false
|
2025-09-01 15:40:24 +08:00
|
|
|
|
export function judgeIsExcludePath(path: string) {
|
|
|
|
|
|
if (!isDev) {
|
|
|
|
|
|
return EXCLUDE_LOGIN_PATH_LIST.includes(path)
|
|
|
|
|
|
}
|
|
|
|
|
|
const allExcludeLoginPages = getAllPages('excludeLoginPath') // dev 环境下,需要每次都重新获取,否则新配置就不会生效
|
|
|
|
|
|
return EXCLUDE_LOGIN_PATH_LIST.includes(path) || (isDev && allExcludeLoginPages.some(page => page.path === path))
|
|
|
|
|
|
}
|
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-09-01 15:40:24 +08:00
|
|
|
|
FG_LOG_ENABLE && console.log('\n\n路由拦截器:-------------------------------------')
|
2025-08-29 15:15:14 +08:00
|
|
|
|
FG_LOG_ENABLE && console.log('路由拦截器 1: url->', url, ', query ->', query)
|
2025-08-21 20:10:47 +08:00
|
|
|
|
const myQuery = { ..._query, ...query }
|
2025-08-21 18:46:54 +08:00
|
|
|
|
// /pages/route-interceptor/index?name=feige&age=30
|
2025-08-29 15:15:14 +08:00
|
|
|
|
FG_LOG_ENABLE && console.log('路由拦截器 2: path->', path, ', _query ->', _query)
|
|
|
|
|
|
FG_LOG_ENABLE && 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-09-01 15:40:24 +08:00
|
|
|
|
const tokenStore = useTokenStore()
|
|
|
|
|
|
FG_LOG_ENABLE && console.log('tokenStore.hasLogin:', tokenStore.hasLogin)
|
|
|
|
|
|
|
|
|
|
|
|
// 不管黑白名单,登录了就直接去吧(但是当前不能是登录页)
|
|
|
|
|
|
if (tokenStore.hasLogin) {
|
|
|
|
|
|
if (path !== LOGIN_PAGE) {
|
|
|
|
|
|
return true // 明确表示允许路由继续执行
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
console.log('已经登录,但是还在登录页', myQuery.redirect)
|
|
|
|
|
|
const url = myQuery.redirect || HOME_PAGE
|
|
|
|
|
|
if (isPageTabbar(url)) {
|
|
|
|
|
|
uni.switchTab({ url })
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
uni.navigateTo({ url })
|
|
|
|
|
|
}
|
|
|
|
|
|
return true // 明确表示阻止原路由继续执行
|
|
|
|
|
|
}
|
2024-03-27 17:20:05 +08:00
|
|
|
|
}
|
2025-08-25 14:57:19 +08:00
|
|
|
|
let fullPath = path
|
2025-08-18 21:24:05 +08:00
|
|
|
|
|
2025-08-21 20:10:47 +08:00
|
|
|
|
if (myQuery) {
|
2025-08-25 14:57:19 +08:00
|
|
|
|
fullPath += `?${Object.keys(myQuery).map(key => `${key}=${myQuery[key]}`).join('&')}`
|
2024-03-27 17:20:05 +08:00
|
|
|
|
}
|
2025-08-25 14:57:19 +08:00
|
|
|
|
const redirectUrl = `${LOGIN_PAGE}?redirect=${encodeURIComponent(fullPath)}`
|
2025-08-19 17:07:42 +08:00
|
|
|
|
|
2025-09-01 15:40:24 +08:00
|
|
|
|
// #region 1/2 需要登录的情况(白名单策略) ---------------------------
|
2025-08-22 17:48:25 +08:00
|
|
|
|
if (isNeedLoginMode) {
|
2025-09-01 15:40:24 +08:00
|
|
|
|
// 需要登录里面的 EXCLUDE_LOGIN_PATH_LIST 表示白名单,可以直接通过
|
|
|
|
|
|
if (judgeIsExcludePath(path)) {
|
2025-08-29 15:08:59 +08:00
|
|
|
|
return true // 明确表示允许路由继续执行
|
2025-08-19 17:07:42 +08:00
|
|
|
|
}
|
2025-09-01 15:40:24 +08:00
|
|
|
|
// 否则需要重定向到登录页
|
2025-08-19 17:07:42 +08:00
|
|
|
|
else {
|
2025-09-01 15:40:24 +08:00
|
|
|
|
FG_LOG_ENABLE && console.log('1 isNeedLogin(白名单策略) redirectUrl:', redirectUrl)
|
|
|
|
|
|
uni.navigateTo({ url: redirectUrl })
|
|
|
|
|
|
return false // 明确表示阻止原路由继续执行
|
2025-08-19 17:07:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-01 15:40:24 +08:00
|
|
|
|
// #endregion 1/2 需要登录的情况(白名单策略) ---------------------------
|
2025-08-18 21:24:05 +08:00
|
|
|
|
|
2025-09-01 15:40:24 +08:00
|
|
|
|
// #region 2/2 不需要登录的情况(黑名单策略) ---------------------------
|
2025-08-19 17:07:42 +08:00
|
|
|
|
else {
|
2025-09-01 14:28:02 +08:00
|
|
|
|
// 不需要登录里面的 EXCLUDE_LOGIN_PATH_LIST 表示黑名单,需要重定向到登录页
|
2025-09-01 15:40:24 +08:00
|
|
|
|
if (judgeIsExcludePath(path)) {
|
|
|
|
|
|
FG_LOG_ENABLE && console.log('2 isNeedLogin(黑名单策略) redirectUrl:', redirectUrl)
|
|
|
|
|
|
|
|
|
|
|
|
// 如果当前路由是登录页,那就重定向走
|
|
|
|
|
|
const { path, query } = parseUrlToObj(redirectUrl)
|
|
|
|
|
|
if (path === LOGIN_PAGE) {
|
|
|
|
|
|
console.log('path:', path)
|
|
|
|
|
|
console.log('query:', query)
|
|
|
|
|
|
uni.navigateTo({ url: query.redirect })
|
|
|
|
|
|
return false // 明确表示阻止原路由继续执行
|
|
|
|
|
|
}
|
2025-08-19 17:07:42 +08:00
|
|
|
|
uni.navigateTo({ url: redirectUrl })
|
2025-09-01 15:40:24 +08:00
|
|
|
|
return false // 修改为false,阻止原路由继续执行
|
2025-08-19 17:07:42 +08:00
|
|
|
|
}
|
2025-08-18 21:24:05 +08:00
|
|
|
|
}
|
2025-09-01 15:40:24 +08:00
|
|
|
|
// #endregion 2/2 不需要登录的情况(黑名单策略) ---------------------------
|
2025-08-29 15:08:59 +08:00
|
|
|
|
return true // 明确表示允许路由继续执行
|
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
|
|
|
|
},
|
|
|
|
|
|
}
|