|
@@ -1,13 +1,37 @@
|
|
|
|
|
+import { isMp } from '@uni-helper/uni-env'
|
|
|
/**
|
|
/**
|
|
|
* by 菲鸽 on 2025-08-19
|
|
* by 菲鸽 on 2025-08-19
|
|
|
* 路由拦截,通常也是登录拦截
|
|
* 路由拦截,通常也是登录拦截
|
|
|
* 黑、白名单的配置,请看 config.ts 文件, EXCLUDE_LOGIN_PATH_LIST
|
|
* 黑、白名单的配置,请看 config.ts 文件, EXCLUDE_LOGIN_PATH_LIST
|
|
|
*/
|
|
*/
|
|
|
-import { tabbarStore } from '@/tabbar/store'
|
|
|
|
|
-import { getAllPages, getLastPage, parseUrlToObj } from '@/utils/index'
|
|
|
|
|
|
|
+import { useTokenStore } from '@/store/token'
|
|
|
|
|
+import { isPageTabbar, tabbarStore } from '@/tabbar/store'
|
|
|
|
|
+import { getAllPages, getLastPage, HOME_PAGE, parseUrlToObj } from '@/utils/index'
|
|
|
|
|
+import { toLoginPage } from '@/utils/toLoginPage'
|
|
|
|
|
+import { EXCLUDE_LOGIN_PATH_LIST, isNeedLoginMode, LOGIN_PAGE, LOGIN_PAGE_ENABLE_IN_MP, NOT_FOUND_PAGE } from './config'
|
|
|
|
|
|
|
|
export const FG_LOG_ENABLE = false
|
|
export const FG_LOG_ENABLE = false
|
|
|
|
|
|
|
|
|
|
+let excludeListInited = false // 标记 EXCLUDE_LOGIN_PATH_LIST 是否已经根据 getAllPages('excludeLoginPath') 做过一次性补充(仅非开发环境)
|
|
|
|
|
+export function judgeIsExcludePath(path: string) {
|
|
|
|
|
+ const isDev = import.meta.env.DEV
|
|
|
|
|
+ if (!isDev) {
|
|
|
|
|
+ // edit by 芋艿:非开发环境下,只初始化一次
|
|
|
|
|
+ if (!excludeListInited) {
|
|
|
|
|
+ const pages = getAllPages('excludeLoginPath')
|
|
|
|
|
+ pages.forEach((page) => {
|
|
|
|
|
+ if (!EXCLUDE_LOGIN_PATH_LIST.includes(page.path)) {
|
|
|
|
|
+ EXCLUDE_LOGIN_PATH_LIST.push(page.path)
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ excludeListInited = true
|
|
|
|
|
+ }
|
|
|
|
|
+ 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))
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
export const navigateToInterceptor = {
|
|
export const navigateToInterceptor = {
|
|
|
// 注意,这里的url是 '/' 开头的,如 '/pages/index/index',跟 'pages.json' 里面的 path 不同
|
|
// 注意,这里的url是 '/' 开头的,如 '/pages/index/index',跟 'pages.json' 里面的 path 不同
|
|
|
// 增加对相对路径的处理,BY 网友 @ideal
|
|
// 增加对相对路径的处理,BY 网友 @ideal
|
|
@@ -35,6 +59,7 @@ export const navigateToInterceptor = {
|
|
|
// 处理路由不存在的情况
|
|
// 处理路由不存在的情况
|
|
|
if (path !== '/' && !getAllPages().some(page => page.path !== path)) {
|
|
if (path !== '/' && !getAllPages().some(page => page.path !== path)) {
|
|
|
console.warn('路由不存在:', path)
|
|
console.warn('路由不存在:', path)
|
|
|
|
|
+ uni.navigateTo({ url: NOT_FOUND_PAGE })
|
|
|
return false // 明确表示阻止原路由继续执行
|
|
return false // 明确表示阻止原路由继续执行
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -46,6 +71,66 @@ export const navigateToInterceptor = {
|
|
|
|
|
|
|
|
// 处理直接进入路由非首页时,tabbarIndex 不正确的问题
|
|
// 处理直接进入路由非首页时,tabbarIndex 不正确的问题
|
|
|
tabbarStore.setAutoCurIdx(path)
|
|
tabbarStore.setAutoCurIdx(path)
|
|
|
|
|
+
|
|
|
|
|
+ // 小程序里面使用平台自带的登录,则不走下面的逻辑
|
|
|
|
|
+ if (isMp && !LOGIN_PAGE_ENABLE_IN_MP) {
|
|
|
|
|
+ return true // 明确表示允许路由继续执行
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ 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 false // 明确表示阻止原路由继续执行
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ let fullPath = path
|
|
|
|
|
+
|
|
|
|
|
+ if (Object.keys(myQuery).length) {
|
|
|
|
|
+ fullPath += `?${Object.keys(myQuery).map(key => `${key}=${myQuery[key]}`).join('&')}`
|
|
|
|
|
+ }
|
|
|
|
|
+ const redirectQuery = `?redirect=${encodeURIComponent(fullPath)}`
|
|
|
|
|
+
|
|
|
|
|
+ // #region 1/2 默认需要登录的情况(白名单策略) ---------------------------
|
|
|
|
|
+ if (isNeedLoginMode) {
|
|
|
|
|
+ // 需要登录里面的 EXCLUDE_LOGIN_PATH_LIST 表示白名单,可以直接通过
|
|
|
|
|
+ if (judgeIsExcludePath(path)) {
|
|
|
|
|
+ return true // 明确表示允许路由继续执行
|
|
|
|
|
+ }
|
|
|
|
|
+ // 否则需要重定向到登录页
|
|
|
|
|
+ else {
|
|
|
|
|
+ if (path === LOGIN_PAGE) {
|
|
|
|
|
+ return true // 明确表示允许路由继续执行
|
|
|
|
|
+ }
|
|
|
|
|
+ FG_LOG_ENABLE && console.log('1 isNeedLogin(白名单策略) url:', fullPath)
|
|
|
|
|
+ toLoginPage({ queryString: redirectQuery })
|
|
|
|
|
+ return false // 明确表示阻止原路由继续执行
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ // #endregion 1/2 默认需要登录的情况(白名单策略) ---------------------------
|
|
|
|
|
+
|
|
|
|
|
+ // #region 2/2 默认不需要登录的情况(黑名单策略) ---------------------------
|
|
|
|
|
+ else {
|
|
|
|
|
+ // 不需要登录里面的 EXCLUDE_LOGIN_PATH_LIST 表示黑名单,需要重定向到登录页
|
|
|
|
|
+ if (judgeIsExcludePath(path)) {
|
|
|
|
|
+ FG_LOG_ENABLE && console.log('2 isNeedLogin(黑名单策略) url:', fullPath)
|
|
|
|
|
+ toLoginPage({ queryString: redirectQuery })
|
|
|
|
|
+ return false // 修改为false,阻止原路由继续执行
|
|
|
|
|
+ }
|
|
|
|
|
+ return true // 明确表示允许路由继续执行
|
|
|
|
|
+ }
|
|
|
|
|
+ // #endregion 2/2 默认不需要登录的情况(黑名单策略) ---------------------------
|
|
|
},
|
|
},
|
|
|
}
|
|
}
|
|
|
|
|
|