西藏巴青项目

tabBar.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. const TAB_KEYS = ['nav.home', 'nav.ai', 'nav.message', 'nav.mine']
  2. /** 与 pages.json tabBar.list[].pagePath 一致(无开头 /) */
  3. const TAB_PAGE_PATHS = new Set([
  4. 'pages/home/index',
  5. 'pages/ai/index',
  6. 'pages/message/index',
  7. 'pages/mine/index'
  8. ])
  9. const TABBAR_API_NAMES = [
  10. 'setTabBarStyle',
  11. 'setTabBarItem',
  12. 'setTabBarBadge',
  13. 'removeTabBarBadge',
  14. 'showTabBarRedDot',
  15. 'hideTabBarRedDot',
  16. 'showTabBar',
  17. 'hideTabBar',
  18. 'setTabBarMidButton'
  19. ]
  20. function readUniTabPagePaths() {
  21. try {
  22. const list =
  23. (typeof __uniConfig !== 'undefined' &&
  24. __uniConfig.tabBar &&
  25. __uniConfig.tabBar.list) ||
  26. []
  27. list.forEach((item) => {
  28. const path = normalizePageRoute(item.pagePath || '')
  29. if (path) {
  30. TAB_PAGE_PATHS.add(path)
  31. }
  32. })
  33. } catch (e) {
  34. // ignore
  35. }
  36. }
  37. function getPublicPathPrefixes() {
  38. const prefixes = ['bqH5/', 'bqjy/']
  39. try {
  40. const envPath = String(import.meta.env.VITE_APP_PUBLIC_PATH || '')
  41. .trim()
  42. .replace(/^\/|\/$/g, '')
  43. if (envPath) {
  44. const withSlash = `${envPath}/`
  45. if (!prefixes.includes(withSlash)) {
  46. prefixes.unshift(withSlash)
  47. }
  48. }
  49. } catch (e) {
  50. // ignore
  51. }
  52. return prefixes
  53. }
  54. function normalizePageRoute(route) {
  55. if (!route) {
  56. return ''
  57. }
  58. let r = String(route).split('?')[0].replace(/^\//, '')
  59. if (r.endsWith('.html')) {
  60. r = r.slice(0, -5)
  61. }
  62. for (const prefix of getPublicPathPrefixes()) {
  63. if (r.startsWith(prefix)) {
  64. r = r.slice(prefix.length)
  65. }
  66. }
  67. return r
  68. }
  69. /**
  70. * 当前栈顶是否为 TabBar 页(与 ruoyi-jiaoyi 一致:按 route 判断,非 Tab 页拦截全部 TabBar API)
  71. */
  72. export function isTabBarPage() {
  73. const stack = getCurrentPages()
  74. if (!stack.length) {
  75. return false
  76. }
  77. const cur = stack[stack.length - 1]
  78. const route = normalizePageRoute(cur.route || cur.$page?.route || '')
  79. return TAB_PAGE_PATHS.has(route)
  80. }
  81. function installTabBarApiGuard() {
  82. if (typeof uni === 'undefined' || !uni.addInterceptor) {
  83. return
  84. }
  85. TABBAR_API_NAMES.forEach((name) => {
  86. if (typeof uni[name] !== 'function') {
  87. return
  88. }
  89. uni.addInterceptor(name, {
  90. invoke() {
  91. if (!isTabBarPage()) {
  92. return false
  93. }
  94. }
  95. })
  96. })
  97. }
  98. readUniTabPagePaths()
  99. installTabBarApiGuard()
  100. /**
  101. * 原生 tabBar 的 text 不能写进 pages.json 的 i18n,需在运行时按当前语言更新(仅 Tab 页)
  102. * @param {(key: string) => string} t 如 (k) => this.$t(k)
  103. */
  104. export function syncTabBarText(t) {
  105. if (!isTabBarPage()) {
  106. return
  107. }
  108. TAB_KEYS.forEach((key, index) => {
  109. uni.setTabBarItem({
  110. index,
  111. text: t(key)
  112. })
  113. })
  114. }