西藏巴青项目

tabBar.js 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. let tabBarEntryTask = null
  22. function readUniTabPagePaths() {
  23. try {
  24. const list =
  25. (typeof __uniConfig !== 'undefined' &&
  26. __uniConfig.tabBar &&
  27. __uniConfig.tabBar.list) ||
  28. []
  29. list.forEach((item) => {
  30. const path = normalizePageRoute(item.pagePath || '')
  31. if (path) {
  32. TAB_PAGE_PATHS.add(path)
  33. }
  34. })
  35. } catch (e) {
  36. // ignore
  37. }
  38. }
  39. function getPublicPathPrefixes() {
  40. const prefixes = ['bqH5/', 'bqjy/']
  41. try {
  42. const envPath = String(import.meta.env.VITE_APP_PUBLIC_PATH || '')
  43. .trim()
  44. .replace(/^\/|\/$/g, '')
  45. if (envPath) {
  46. const withSlash = `${envPath}/`
  47. if (!prefixes.includes(withSlash)) {
  48. prefixes.unshift(withSlash)
  49. }
  50. }
  51. } catch (e) {
  52. // ignore
  53. }
  54. return prefixes
  55. }
  56. function normalizePageRoute(route) {
  57. if (!route) {
  58. return ''
  59. }
  60. let r = String(route).split('?')[0].replace(/^\//, '')
  61. if (r.endsWith('.html')) {
  62. r = r.slice(0, -5)
  63. }
  64. for (const prefix of getPublicPathPrefixes()) {
  65. if (r.startsWith(prefix)) {
  66. r = r.slice(prefix.length)
  67. }
  68. }
  69. return r
  70. }
  71. function getCurrentPageRoute() {
  72. const stack = getCurrentPages()
  73. if (stack.length) {
  74. const cur = stack[stack.length - 1]
  75. return cur.route || cur.$page?.route || cur.$page?.fullPath || ''
  76. }
  77. if (typeof window !== 'undefined' && window.location && window.location.pathname) {
  78. return window.location.pathname
  79. }
  80. return ''
  81. }
  82. function getCurrentPageMeta() {
  83. const stack = getCurrentPages()
  84. if (!stack.length) {
  85. return null
  86. }
  87. const cur = stack[stack.length - 1]
  88. return cur.$page?.meta || cur.$vm?.$page?.meta || null
  89. }
  90. /**
  91. * 路由是否对应 tabBar.list 中的页面(不等于当前栈顶一定是 Tab 上下文)
  92. */
  93. export function isTabBarRoute(route) {
  94. const normalized = normalizePageRoute(route || getCurrentPageRoute())
  95. return !!normalized && TAB_PAGE_PATHS.has(normalized)
  96. }
  97. /**
  98. * 当前栈顶是否为原生 TabBar 页(须 meta.isTabBar,不能只看 route)
  99. * H5 history 直开 /bqH5/pages/home/index 时 route 会匹配 Tab,但 meta.isTabBar 为 false
  100. */
  101. export function isTabBarPage() {
  102. const meta = getCurrentPageMeta()
  103. if (meta && typeof meta.isTabBar === 'boolean') {
  104. return meta.isTabBar
  105. }
  106. return false
  107. }
  108. /**
  109. * H5 生产环境直开/刷新 Tab URL 时,用 switchTab 切回真正的 Tab 上下文
  110. * @returns {Promise<boolean>} 切换后是否为 TabBar 页
  111. */
  112. export function ensureTabBarEntry() {
  113. if (isTabBarPage()) {
  114. return Promise.resolve(true)
  115. }
  116. if (!isTabBarRoute()) {
  117. return Promise.resolve(false)
  118. }
  119. if (tabBarEntryTask) {
  120. return tabBarEntryTask
  121. }
  122. const route = normalizePageRoute(getCurrentPageRoute())
  123. tabBarEntryTask = new Promise((resolve) => {
  124. uni.switchTab({
  125. url: `/${route}`,
  126. complete: () => {
  127. tabBarEntryTask = null
  128. resolve(isTabBarPage())
  129. }
  130. })
  131. })
  132. return tabBarEntryTask
  133. }
  134. function noopTabBarResult(name) {
  135. return { errMsg: `${name}:ok` }
  136. }
  137. function finishNoopTabBarCall(name, options) {
  138. const result = noopTabBarResult(name)
  139. if (options && typeof options.success === 'function') {
  140. options.success(result)
  141. }
  142. if (options && typeof options.complete === 'function') {
  143. options.complete(result)
  144. }
  145. return Promise.resolve(result)
  146. }
  147. function isTabBarFailMsg(msg) {
  148. return String(msg || '').includes('TabBar') && String(msg || '').includes('fail')
  149. }
  150. /**
  151. * H5 生产包中 addInterceptor 偶发拦不住框架内部调用,直接包装 uni/wx API 更稳
  152. */
  153. function wrapTabBarApi(target, name) {
  154. if (!target || typeof target[name] !== 'function') {
  155. return
  156. }
  157. const current = target[name]
  158. if (current._tabBarGuardPatched) {
  159. return
  160. }
  161. const raw = current.bind(target)
  162. function patchedTabBarApi(options) {
  163. const opts = options || {}
  164. if (!isTabBarPage()) {
  165. return finishNoopTabBarCall(name, opts)
  166. }
  167. try {
  168. const ret = raw(opts)
  169. if (ret && typeof ret.catch === 'function') {
  170. ret.catch((err) => {
  171. if (isTabBarFailMsg(err && err.errMsg)) {
  172. return finishNoopTabBarCall(name, opts)
  173. }
  174. throw err
  175. })
  176. }
  177. return ret
  178. } catch (err) {
  179. if (isTabBarFailMsg(err && err.errMsg)) {
  180. return finishNoopTabBarCall(name, opts)
  181. }
  182. throw err
  183. }
  184. }
  185. patchedTabBarApi._tabBarGuardPatched = true
  186. target[name] = patchedTabBarApi
  187. }
  188. function patchTabBarApis(target) {
  189. if (!target) {
  190. return
  191. }
  192. TABBAR_API_NAMES.forEach((name) => wrapTabBarApi(target, name))
  193. }
  194. function installTabBarInterceptors() {
  195. if (typeof uni === 'undefined' || !uni.addInterceptor) {
  196. return
  197. }
  198. TABBAR_API_NAMES.forEach((name) => {
  199. if (typeof uni[name] !== 'function') {
  200. return
  201. }
  202. uni.addInterceptor(name, {
  203. invoke() {
  204. if (!isTabBarPage()) {
  205. return false
  206. }
  207. }
  208. })
  209. })
  210. }
  211. export function installTabBarApiGuard() {
  212. readUniTabPagePaths()
  213. if (typeof uni !== 'undefined') {
  214. patchTabBarApis(uni)
  215. installTabBarInterceptors()
  216. }
  217. if (typeof wx !== 'undefined') {
  218. patchTabBarApis(wx)
  219. }
  220. }
  221. installTabBarApiGuard()
  222. /**
  223. * 原生 tabBar 的 text 不能写进 pages.json 的 i18n,需在运行时按当前语言更新(仅 Tab 页)
  224. * @param {(key: string) => string} t 如 (k) => this.$t(k)
  225. */
  226. export function syncTabBarText(t) {
  227. if (!isTabBarPage()) {
  228. return
  229. }
  230. TAB_KEYS.forEach((key, index) => {
  231. uni.setTabBarItem({
  232. index,
  233. text: t(key)
  234. })
  235. })
  236. }