index.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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/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. const { fullPath } = currRoute as { fullPath: string }
  36. // console.log(fullPath)
  37. // eg: /pages/login/index?redirect=%2Fpages%2Fdemo%2Fbase%2Froute-interceptor (小程序)
  38. // eg: /pages/login/index?redirect=%2Fpages%2Froute-interceptor%2Findex%3Fname%3Dfeige%26age%3D30(h5)
  39. return getUrlObj(fullPath)
  40. }
  41. const ensureDecodeURIComponent = (url: string) => {
  42. if (url.startsWith('%')) {
  43. return ensureDecodeURIComponent(decodeURIComponent(url))
  44. }
  45. return url
  46. }
  47. /**
  48. * 解析 url 得到 path 和 query
  49. * 比如输入url: /pages/login/index?redirect=%2Fpages%2Fdemo%2Fbase%2Froute-interceptor
  50. * 输出: {path: /pages/login/index, query: {redirect: /pages/demo/base/route-interceptor}}
  51. */
  52. export const getUrlObj = (url: string) => {
  53. const [path, queryStr] = url.split('?')
  54. // console.log(path, queryStr)
  55. if (!queryStr) {
  56. return {
  57. path,
  58. query: {},
  59. }
  60. }
  61. const query: Record<string, string> = {}
  62. queryStr.split('&').forEach((item) => {
  63. const [key, value] = item.split('=')
  64. // console.log(key, value)
  65. query[key] = ensureDecodeURIComponent(value) // 这里需要统一 decodeURIComponent 一下,可以兼容h5和微信y
  66. })
  67. return { path, query }
  68. }
  69. /**
  70. * 得到所有的需要登录的pages,包括主包和分包的
  71. * 这里设计得通用一点,可以传递key作为判断依据,默认是 needLogin, 与 route-block 配对使用
  72. * 如果没有传 key,则表示所有的pages,如果传递了 key, 则表示通过 key 过滤
  73. */
  74. export const getAllPages = (key = 'needLogin') => {
  75. // 这里处理主包
  76. const mainPages = [
  77. ...pages
  78. .filter((page) => !key || page[key])
  79. .map((page) => ({
  80. ...page,
  81. path: `/${page.path}`,
  82. })),
  83. ]
  84. // 这里处理分包
  85. const subPages: any[] = []
  86. subPackages.forEach((subPageObj) => {
  87. // console.log(subPageObj)
  88. const { root } = subPageObj
  89. subPageObj.pages
  90. .filter((page) => !key || page[key])
  91. .forEach((page: { path: string } & Record<string, any>) => {
  92. subPages.push({
  93. ...page,
  94. path: `/${root}/${page.path}`,
  95. })
  96. })
  97. })
  98. const result = [...mainPages, ...subPages]
  99. // console.log(`getAllPages by ${key} result: `, result)
  100. return result
  101. }
  102. /**
  103. * 得到所有的需要登录的pages,包括主包和分包的
  104. * 只得到 path 数组
  105. */
  106. export const getNeedLoginPages = (): string[] => getAllPages('needLogin').map((page) => page.path)
  107. /**
  108. * 得到所有的需要登录的pages,包括主包和分包的
  109. * 只得到 path 数组
  110. */
  111. export const needLoginPages: string[] = getAllPages('needLogin').map((page) => page.path)