西藏巴青项目

tabBar.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. /** 运行时会改 TabBar 的 uni API(非 Tab 页调用会报 not TabBar page) */
  10. const TABBAR_API_NAMES = [
  11. 'setTabBarStyle',
  12. 'setTabBarItem',
  13. 'setTabBarBadge',
  14. 'removeTabBarBadge',
  15. 'showTabBarRedDot',
  16. 'hideTabBarRedDot',
  17. 'showTabBar',
  18. 'hideTabBar',
  19. 'setTabBarMidButton'
  20. ]
  21. function readUniTabPagePaths() {
  22. try {
  23. const list =
  24. (typeof __uniConfig !== 'undefined' &&
  25. __uniConfig.tabBar &&
  26. __uniConfig.tabBar.list) ||
  27. []
  28. list.forEach((item) => {
  29. const path = normalizePageRoute(item.pagePath || '')
  30. if (path) {
  31. TAB_PAGE_PATHS.add(path)
  32. }
  33. })
  34. } catch (e) {
  35. // ignore
  36. }
  37. }
  38. function normalizePageRoute(route) {
  39. if (!route) {
  40. return ''
  41. }
  42. let r = String(route).split('?')[0].replace(/^\//, '')
  43. if (r.endsWith('.html')) {
  44. r = r.slice(0, -5)
  45. }
  46. const prefixes = ['bqH5/', 'bqjy/']
  47. for (const prefix of prefixes) {
  48. if (r.startsWith(prefix)) {
  49. r = r.slice(prefix.length)
  50. }
  51. }
  52. return r
  53. }
  54. function getCurrentPageRoute() {
  55. const stack = getCurrentPages()
  56. if (!stack.length) {
  57. return ''
  58. }
  59. const cur = stack[stack.length - 1]
  60. return cur.route || cur.$page?.route || cur.$page?.fullPath || ''
  61. }
  62. /**
  63. * 当前栈顶是否为原生 TabBar 页
  64. */
  65. export function isTabBarPage() {
  66. const route = normalizePageRoute(getCurrentPageRoute())
  67. return !!route && TAB_PAGE_PATHS.has(route)
  68. }
  69. function noopTabBarResult(name) {
  70. return { errMsg: `${name}:ok` }
  71. }
  72. function finishNoopTabBarCall(name, options) {
  73. const result = noopTabBarResult(name)
  74. if (options && typeof options.success === 'function') {
  75. options.success(result)
  76. }
  77. if (options && typeof options.complete === 'function') {
  78. options.complete(result)
  79. }
  80. return Promise.resolve(result)
  81. }
  82. /**
  83. * H5 生产包中 addInterceptor 偶发拦不住框架内部调用,直接包装 uni API 更稳
  84. */
  85. function wrapTabBarApi(name) {
  86. if (typeof uni === 'undefined' || typeof uni[name] !== 'function') {
  87. return
  88. }
  89. const raw = uni[name].bind(uni)
  90. uni[name] = function patchedTabBarApi(options) {
  91. const opts = options || {}
  92. if (!isTabBarPage()) {
  93. return finishNoopTabBarCall(name, opts)
  94. }
  95. try {
  96. const ret = raw(opts)
  97. if (ret && typeof ret.catch === 'function') {
  98. ret.catch((err) => {
  99. const msg = err && err.errMsg ? String(err.errMsg) : ''
  100. if (msg.includes('TabBar') && msg.includes('fail')) {
  101. return
  102. }
  103. throw err
  104. })
  105. }
  106. return ret
  107. } catch (err) {
  108. const msg = err && err.errMsg ? String(err.errMsg) : ''
  109. if (msg.includes('TabBar') && msg.includes('fail')) {
  110. return finishNoopTabBarCall(name, opts)
  111. }
  112. throw err
  113. }
  114. }
  115. }
  116. function installTabBarApiGuard() {
  117. readUniTabPagePaths()
  118. TABBAR_API_NAMES.forEach(wrapTabBarApi)
  119. if (!uni.addInterceptor) {
  120. return
  121. }
  122. TABBAR_API_NAMES.forEach((name) => {
  123. if (typeof uni[name] !== 'function') {
  124. return
  125. }
  126. uni.addInterceptor(name, {
  127. invoke() {
  128. if (!isTabBarPage()) {
  129. return false
  130. }
  131. }
  132. })
  133. })
  134. }
  135. installTabBarApiGuard()
  136. /**
  137. * 原生 tabBar 的 text 不能写进 pages.json 的 i18n,需在运行时按当前语言更新(仅 Tab 页)
  138. * @param {(key: string) => string} t 如 (k) => this.$t(k)
  139. */
  140. export function syncTabBarText(t) {
  141. if (!isTabBarPage()) {
  142. return
  143. }
  144. TAB_KEYS.forEach((key, index) => {
  145. uni.setTabBarItem({
  146. index,
  147. text: t(key)
  148. })
  149. })
  150. }