interceptor.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* eslint-disable brace-style */ // 原因:unibest 官方维护的代码,尽量不要大概,避免难以合并
  2. import { isMp } from '@uni-helper/uni-env'
  3. /**
  4. * by 菲鸽 on 2025-08-19
  5. * 路由拦截,通常也是登录拦截
  6. * 黑、白名单的配置,请看 config.ts 文件, EXCLUDE_LOGIN_PATH_LIST
  7. */
  8. import { useTokenStore } from '@/store/token'
  9. import { isPageTabbar, tabbarStore } from '@/tabbar/store'
  10. import { getAllPages, getLastPage, HOME_PAGE, parseUrlToObj } from '@/utils/index'
  11. import { toLoginPage } from '@/utils/toLoginPage'
  12. import { EXCLUDE_LOGIN_PATH_LIST, isNeedLoginMode, LOGIN_PAGE, LOGIN_PAGE_ENABLE_IN_MP, NOT_FOUND_PAGE } from './config'
  13. export const FG_LOG_ENABLE = false
  14. let excludeListInited = false // 标记 EXCLUDE_LOGIN_PATH_LIST 是否已经根据 getAllPages('excludeLoginPath') 做过一次性补充(仅非开发环境)
  15. export function judgeIsExcludePath(path: string) {
  16. const isDev = import.meta.env.DEV
  17. if (!isDev) {
  18. // edit by 芋艿:非开发环境下,只初始化一次
  19. if (!excludeListInited) {
  20. const pages = getAllPages('excludeLoginPath')
  21. pages.forEach((page) => {
  22. if (!EXCLUDE_LOGIN_PATH_LIST.includes(page.path)) {
  23. EXCLUDE_LOGIN_PATH_LIST.push(page.path)
  24. }
  25. })
  26. excludeListInited = true
  27. }
  28. return EXCLUDE_LOGIN_PATH_LIST.includes(path)
  29. }
  30. const allExcludeLoginPages = getAllPages('excludeLoginPath') // dev 环境下,需要每次都重新获取,否则新配置就不会生效
  31. return EXCLUDE_LOGIN_PATH_LIST.includes(path) || (isDev && allExcludeLoginPages.some(page => page.path === path))
  32. }
  33. export const navigateToInterceptor = {
  34. // 注意,这里的url是 '/' 开头的,如 '/pages/index/index',跟 'pages.json' 里面的 path 不同
  35. // 增加对相对路径的处理,BY 网友 @ideal
  36. invoke({ url, query }: { url: string, query?: Record<string, string> }) {
  37. if (url === undefined) {
  38. return
  39. }
  40. let { path, query: _query } = parseUrlToObj(url)
  41. FG_LOG_ENABLE && console.log('\n\n路由拦截器:-------------------------------------')
  42. FG_LOG_ENABLE && console.log('路由拦截器 1: url->', url, ', query ->', query)
  43. const myQuery = { ..._query, ...query }
  44. // /pages/route-interceptor/index?name=feige&age=30
  45. FG_LOG_ENABLE && console.log('路由拦截器 2: path->', path, ', _query ->', _query)
  46. FG_LOG_ENABLE && console.log('路由拦截器 3: myQuery ->', myQuery)
  47. // 处理相对路径
  48. if (!path.startsWith('/')) {
  49. const currentPath = getLastPage()?.route || ''
  50. const normalizedCurrentPath = currentPath.startsWith('/') ? currentPath : `/${currentPath}`
  51. const baseDir = normalizedCurrentPath.substring(0, normalizedCurrentPath.lastIndexOf('/'))
  52. path = `${baseDir}/${path}`
  53. }
  54. // 处理路由不存在的情况
  55. if (path !== '/' && !getAllPages().some(page => page.path !== path)) {
  56. console.warn('路由不存在:', path)
  57. uni.navigateTo({ url: NOT_FOUND_PAGE })
  58. return false // 明确表示阻止原路由继续执行
  59. }
  60. // 插件页面
  61. if (url.startsWith('plugin://')) {
  62. FG_LOG_ENABLE && console.log('路由拦截器 4: plugin:// 路径 ==>', url)
  63. path = url
  64. }
  65. // 处理直接进入路由非首页时,tabbarIndex 不正确的问题
  66. tabbarStore.setAutoCurIdx(path)
  67. // 小程序里面使用平台自带的登录,则不走下面的逻辑
  68. if (isMp && !LOGIN_PAGE_ENABLE_IN_MP) {
  69. return true // 明确表示允许路由继续执行
  70. }
  71. const tokenStore = useTokenStore()
  72. FG_LOG_ENABLE && console.log('tokenStore.hasLogin:', tokenStore.hasLogin)
  73. // 不管黑白名单,登录了就直接去吧(但是当前不能是登录页)
  74. if (tokenStore.hasLogin) {
  75. if (path !== LOGIN_PAGE) {
  76. return true // 明确表示允许路由继续执行
  77. } else {
  78. console.log('已经登录,但是还在登录页', myQuery.redirect)
  79. const url = myQuery.redirect || HOME_PAGE
  80. if (isPageTabbar(url)) {
  81. uni.switchTab({ url })
  82. } else {
  83. uni.navigateTo({ url })
  84. }
  85. return false // 明确表示阻止原路由继续执行
  86. }
  87. }
  88. let fullPath = path
  89. if (Object.keys(myQuery).length) {
  90. fullPath += `?${Object.keys(myQuery).map(key => `${key}=${myQuery[key]}`).join('&')}`
  91. }
  92. const redirectQuery = `?redirect=${encodeURIComponent(fullPath)}`
  93. // #region 1/2 默认需要登录的情况(白名单策略) ---------------------------
  94. if (isNeedLoginMode) {
  95. // 需要登录里面的 EXCLUDE_LOGIN_PATH_LIST 表示白名单,可以直接通过
  96. if (judgeIsExcludePath(path)) {
  97. return true // 明确表示允许路由继续执行
  98. }
  99. // 否则需要重定向到登录页
  100. else {
  101. if (path === LOGIN_PAGE) {
  102. return true // 明确表示允许路由继续执行
  103. }
  104. FG_LOG_ENABLE && console.log('1 isNeedLogin(白名单策略) url:', fullPath)
  105. toLoginPage({ queryString: redirectQuery })
  106. return false // 明确表示阻止原路由继续执行
  107. }
  108. }
  109. // #endregion 1/2 默认需要登录的情况(白名单策略) ---------------------------
  110. // #region 2/2 默认不需要登录的情况(黑名单策略) ---------------------------
  111. else {
  112. // 不需要登录里面的 EXCLUDE_LOGIN_PATH_LIST 表示黑名单,需要重定向到登录页
  113. if (judgeIsExcludePath(path)) {
  114. FG_LOG_ENABLE && console.log('2 isNeedLogin(黑名单策略) url:', fullPath)
  115. toLoginPage({ queryString: redirectQuery })
  116. return false // 修改为false,阻止原路由继续执行
  117. }
  118. return true // 明确表示允许路由继续执行
  119. }
  120. // #endregion 2/2 默认不需要登录的情况(黑名单策略) ---------------------------
  121. },
  122. }
  123. export const routeInterceptor = {
  124. install() {
  125. uni.addInterceptor('navigateTo', navigateToInterceptor)
  126. uni.addInterceptor('reLaunch', navigateToInterceptor)
  127. uni.addInterceptor('redirectTo', navigateToInterceptor)
  128. uni.addInterceptor('switchTab', navigateToInterceptor)
  129. },
  130. }