index.ts 3.8 KB

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