interceptor.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * by 菲鸽 on 2025-08-19
  3. * 路由拦截,通常也是登录拦截
  4. * 黑白名单的配置,请看 config.ts 文件, EXCLUDE_PAGE_LIST
  5. */
  6. import { useTokenStore } from '@/store/token'
  7. import { tabbarStore } from '@/tabbar/store'
  8. import { getLastPage, parseUrlToObj } from '@/utils/index'
  9. import { EXCLUDE_PAGE_LIST, isNeedLoginMode, LOGIN_PAGE, LOGIN_PAGE_LIST } from './config'
  10. export const FG_LOG_ENABLE = false
  11. // 黑名单登录拦截器 - (适用于大部分页面不需要登录,少部分页面需要登录)
  12. export const navigateToInterceptor = {
  13. // 注意,这里的url是 '/' 开头的,如 '/pages/index/index',跟 'pages.json' 里面的 path 不同
  14. // 增加对相对路径的处理,BY 网友 @ideal
  15. invoke({ url, query }: { url: string, query?: Record<string, string> }) {
  16. if (url === undefined) {
  17. return
  18. }
  19. let { path, query: _query } = parseUrlToObj(url)
  20. FG_LOG_ENABLE && console.log('路由拦截器 1: url->', url, ', query ->', query)
  21. const myQuery = { ..._query, ...query }
  22. // /pages/route-interceptor/index?name=feige&age=30
  23. FG_LOG_ENABLE && console.log('路由拦截器 2: path->', path, ', _query ->', _query)
  24. FG_LOG_ENABLE && console.log('路由拦截器 3: myQuery ->', myQuery)
  25. // 处理相对路径
  26. if (!path.startsWith('/')) {
  27. const currentPath = getLastPage()?.route || ''
  28. const normalizedCurrentPath = currentPath.startsWith('/') ? currentPath : `/${currentPath}`
  29. const baseDir = normalizedCurrentPath.substring(0, normalizedCurrentPath.lastIndexOf('/'))
  30. path = `${baseDir}/${path}`
  31. }
  32. // 处理直接进入路由非首页时,tabbarIndex 不正确的问题
  33. tabbarStore.setAutoCurIdx(path)
  34. if (LOGIN_PAGE_LIST.includes(path)) {
  35. FG_LOG_ENABLE && console.log('命中了 LOGIN_PAGE_LIST')
  36. return true // 明确表示允许路由继续执行
  37. }
  38. let fullPath = path
  39. if (myQuery) {
  40. fullPath += `?${Object.keys(myQuery).map(key => `${key}=${myQuery[key]}`).join('&')}`
  41. }
  42. const redirectUrl = `${LOGIN_PAGE}?redirect=${encodeURIComponent(fullPath)}`
  43. const tokenStore = useTokenStore()
  44. FG_LOG_ENABLE && console.log('tokenStore.hasLogin:', tokenStore.hasLogin)
  45. // #region 1/2 需要登录的情况 ---------------------------
  46. if (isNeedLoginMode) {
  47. if (tokenStore.hasLogin) {
  48. return true // 明确表示允许路由继续执行
  49. }
  50. else {
  51. // 需要登录里面的 EXCLUDE_PAGE_LIST 表示白名单,可以直接通过
  52. if (EXCLUDE_PAGE_LIST.includes(path)) {
  53. return true // 明确表示允许路由继续执行
  54. }
  55. // 否则需要重定向到登录页
  56. else {
  57. FG_LOG_ENABLE && console.log('1 isNeedLogin redirectUrl:', redirectUrl)
  58. uni.navigateTo({ url: redirectUrl })
  59. return false // 明确表示阻止原路由继续执行
  60. }
  61. }
  62. }
  63. // #endregion 1/2 需要登录的情况 ---------------------------
  64. // #region 2/2 不需要登录的情况 ---------------------------
  65. else {
  66. // 不需要登录里面的 EXCLUDE_PAGE_LIST 表示黑名单,需要重定向到登录页
  67. if (EXCLUDE_PAGE_LIST.includes(path)) {
  68. FG_LOG_ENABLE && console.log('2 isNeedLogin redirectUrl:', redirectUrl)
  69. uni.navigateTo({ url: redirectUrl })
  70. return false // 明确表示阻止原路由继续执行
  71. }
  72. }
  73. // #endregion 2/2 不需要登录的情况 ---------------------------
  74. return true // 明确表示允许路由继续执行
  75. },
  76. }
  77. export const routeInterceptor = {
  78. install() {
  79. uni.addInterceptor('navigateTo', navigateToInterceptor)
  80. uni.addInterceptor('reLaunch', navigateToInterceptor)
  81. uni.addInterceptor('redirectTo', navigateToInterceptor)
  82. uni.addInterceptor('switchTab', navigateToInterceptor)
  83. },
  84. }