index.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { pages, subPackages, tabBar } from '@/pages.json'
  2. import { path } from 'node:path'
  3. const getLastPage = () => {
  4. // getCurrentPages() 至少有1个元素,所以不再额外判断
  5. // const lastPage = getCurrentPages().at(-1)
  6. // 上面那个在低版本安卓中打包回报错,所以改用下面这个【虽然我加了src/interceptions/prototype.ts,但依然报错】
  7. const pages = getCurrentPages()
  8. return pages[pages.length - 1]
  9. }
  10. /** 判断当前页面是否是tabbar页 */
  11. export const getIsTabbar = () => {
  12. if (!tabBar) {
  13. return false
  14. }
  15. if (!tabBar.list.length) {
  16. // 通常有tabBar的话,list不能有空,且至少有2个元素,这里其实不用处理
  17. return false
  18. }
  19. const lastPage = getLastPage()
  20. const currPath = lastPage.route
  21. return !!tabBar.list.find((e) => e.pagePath === currPath)
  22. }
  23. /**
  24. * 获取当前页面路由的 path 路径和 redirectPath 路径
  25. * path 如 ‘/pages/login/index’
  26. * redirectPath 如 ‘/pages/demo/base/route-interceptor’
  27. */
  28. export const currRoute = () => {
  29. const lastPage = getLastPage()
  30. const currRoute = (lastPage as any).$page
  31. // console.log('lastPage.$page:', currRoute)
  32. // console.log('lastPage.$page.fullpath:', currRoute.fullPath)
  33. // console.log('lastPage.$page.options:', currRoute.options)
  34. // console.log('lastPage.options:', (lastPage as any).options)
  35. // 经过多端测试,只有 fullPath 靠谱,其他都不靠谱
  36. let path = ''
  37. // #ifndef MP-WEIXIN
  38. const { fullPath } = currRoute as { fullPath: string }
  39. path = fullPath
  40. // #endif
  41. // #ifdef MP-WEIXIN
  42. path = lastPage.route
  43. // #endif
  44. // console.log(fullPath)
  45. // eg: /pages/login/index?redirect=%2Fpages%2Fdemo%2Fbase%2Froute-interceptor (小程序)
  46. // eg: /pages/login/index?redirect=%2Fpages%2Froute-interceptor%2Findex%3Fname%3Dfeige%26age%3D30(h5)
  47. return getUrlObj(path)
  48. }
  49. const ensureDecodeURIComponent = (url: string) => {
  50. if (url.startsWith('%')) {
  51. return ensureDecodeURIComponent(decodeURIComponent(url))
  52. }
  53. return url
  54. }
  55. /**
  56. * 解析 url 得到 path 和 query
  57. * 比如输入url: /pages/login/index?redirect=%2Fpages%2Fdemo%2Fbase%2Froute-interceptor
  58. * 输出: {path: /pages/login/index, query: {redirect: /pages/demo/base/route-interceptor}}
  59. */
  60. export const getUrlObj = (url: string) => {
  61. const [path, queryStr] = url.split('?')
  62. // console.log(path, queryStr)
  63. if (!queryStr) {
  64. return {
  65. path,
  66. query: {},
  67. }
  68. }
  69. const query: Record<string, string> = {}
  70. queryStr.split('&').forEach((item) => {
  71. const [key, value] = item.split('=')
  72. // console.log(key, value)
  73. query[key] = ensureDecodeURIComponent(value) // 这里需要统一 decodeURIComponent 一下,可以兼容h5和微信y
  74. })
  75. return { path, query }
  76. }
  77. /**
  78. * 得到所有的需要登录的pages,包括主包和分包的
  79. * 这里设计得通用一点,可以传递key作为判断依据,默认是 needLogin, 与 route-block 配对使用
  80. * 如果没有传 key,则表示所有的pages,如果传递了 key, 则表示通过 key 过滤
  81. */
  82. export const getAllPages = (key = 'needLogin') => {
  83. // 这里处理主包
  84. const mainPages = [
  85. ...pages
  86. .filter((page) => !key || page[key])
  87. .map((page) => ({
  88. ...page,
  89. path: `/${page.path}`,
  90. })),
  91. ]
  92. // 这里处理分包
  93. const subPages: any[] = []
  94. subPackages.forEach((subPageObj) => {
  95. // console.log(subPageObj)
  96. const { root } = subPageObj
  97. subPageObj.pages
  98. .filter((page) => !key || page[key])
  99. .forEach((page: { path: string } & Record<string, any>) => {
  100. subPages.push({
  101. ...page,
  102. path: `/${root}/${page.path}`,
  103. })
  104. })
  105. })
  106. const result = [...mainPages, ...subPages]
  107. // console.log(`getAllPages by ${key} result: `, result)
  108. return result
  109. }
  110. /**
  111. * 得到所有的需要登录的pages,包括主包和分包的
  112. * 只得到 path 数组
  113. */
  114. export const getNeedLoginPages = (): string[] => getAllPages('needLogin').map((page) => page.path)
  115. /**
  116. * 得到所有的需要登录的pages,包括主包和分包的
  117. * 只得到 path 数组
  118. */
  119. export const needLoginPages: string[] = getAllPages('needLogin').map((page) => page.path)