interceptor.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. // 黑名单登录拦截器 - (适用于大部分页面不需要登录,少部分页面需要登录)
  11. export const navigateToInterceptor = {
  12. // 注意,这里的url是 '/' 开头的,如 '/pages/index/index',跟 'pages.json' 里面的 path 不同
  13. // 增加对相对路径的处理,BY 网友 @ideal
  14. invoke({ url, query }: { url: string, query?: Record<string, string> }) {
  15. if (url === undefined) {
  16. return
  17. }
  18. let { path, query: _query } = parseUrlToObj(url)
  19. console.log('路由拦截器 1: url->', url, ', query ->', query)
  20. const myQuery = { ..._query, ...query }
  21. // /pages/route-interceptor/index?name=feige&age=30
  22. console.log('路由拦截器 2: path->', path, ', _query ->', _query)
  23. console.log('路由拦截器 3: myQuery ->', myQuery)
  24. // 处理相对路径
  25. if (!path.startsWith('/')) {
  26. const currentPath = getLastPage()?.route || ''
  27. const normalizedCurrentPath = currentPath.startsWith('/') ? currentPath : `/${currentPath}`
  28. const baseDir = normalizedCurrentPath.substring(0, normalizedCurrentPath.lastIndexOf('/'))
  29. path = `${baseDir}/${path}`
  30. }
  31. // 处理直接进入路由非首页时,tabbarIndex 不正确的问题
  32. tabbarStore.setAutoCurIdx(path)
  33. if (LOGIN_PAGE_LIST.includes(path)) {
  34. console.log('命中了 LOGIN_PAGE_LIST')
  35. return
  36. }
  37. if (myQuery) {
  38. path += `?${Object.keys(myQuery).map(key => `${key}=${myQuery[key]}`).join('&')}`
  39. }
  40. const redirectUrl = `${LOGIN_PAGE}?redirect=${encodeURIComponent(path)}`
  41. const tokenStore = useTokenStore()
  42. console.log('tokenStore.hasLogin:', tokenStore.hasLogin)
  43. // #region 1/2 需要登录的情况 ---------------------------
  44. if (isNeedLoginMode) {
  45. if (tokenStore.hasLogin) {
  46. return
  47. }
  48. else {
  49. if (EXCLUDE_PAGE_LIST.includes(path)) {
  50. return
  51. }
  52. else {
  53. console.log('isNeedLogin redirectUrl:', redirectUrl)
  54. uni.navigateTo({ url: redirectUrl })
  55. }
  56. }
  57. }
  58. // #endregion 1/2 需要登录的情况 ---------------------------
  59. // #region 2/2 不需要登录的情况 ---------------------------
  60. else {
  61. if (EXCLUDE_PAGE_LIST.includes(path)) {
  62. console.log('isNeedLogin redirectUrl:', redirectUrl)
  63. uni.navigateTo({ url: redirectUrl })
  64. }
  65. }
  66. // #endregion 2/2 不需要登录的情况 ---------------------------
  67. },
  68. }
  69. export const routeInterceptor = {
  70. install() {
  71. uni.addInterceptor('navigateTo', navigateToInterceptor)
  72. uni.addInterceptor('reLaunch', navigateToInterceptor)
  73. uni.addInterceptor('redirectTo', navigateToInterceptor)
  74. uni.addInterceptor('switchTab', navigateToInterceptor)
  75. },
  76. }