index.ts 4.1 KB

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