西藏巴青项目

tabBar.js 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /** 与 pages.json tabBar.list[].pagePath 一致(无开头 /) */
  2. const TAB_PAGE_PATHS = new Set([
  3. 'pages/home/index',
  4. 'pages/order/index',
  5. 'pages/market/index',
  6. 'pages/mine/index'
  7. ])
  8. const TABBAR_API_NAMES = [
  9. 'setTabBarStyle',
  10. 'setTabBarItem',
  11. 'setTabBarBadge',
  12. 'removeTabBarBadge',
  13. 'showTabBarRedDot',
  14. 'hideTabBarRedDot',
  15. 'showTabBar',
  16. 'hideTabBar',
  17. 'setTabBarMidButton'
  18. ]
  19. function normalizePageRoute(route) {
  20. if (!route) {
  21. return ''
  22. }
  23. let r = String(route).replace(/^\//, '')
  24. const prefixes = ['bqH5/', 'bqjy/']
  25. for (const prefix of prefixes) {
  26. if (r.startsWith(prefix)) {
  27. r = r.slice(prefix.length)
  28. }
  29. }
  30. return r
  31. }
  32. export function isTabBarPage() {
  33. const stack = getCurrentPages()
  34. if (!stack.length) {
  35. return false
  36. }
  37. const cur = stack[stack.length - 1]
  38. const route = normalizePageRoute(cur.route || '')
  39. return TAB_PAGE_PATHS.has(route)
  40. }
  41. function installTabBarApiGuard() {
  42. if (typeof uni === 'undefined' || !uni.addInterceptor) {
  43. return
  44. }
  45. TABBAR_API_NAMES.forEach((name) => {
  46. if (typeof uni[name] !== 'function') {
  47. return
  48. }
  49. uni.addInterceptor(name, {
  50. invoke() {
  51. if (!isTabBarPage()) {
  52. return false
  53. }
  54. }
  55. })
  56. })
  57. }
  58. installTabBarApiGuard()