index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. Component({
  2. data: {
  3. selected: 0,
  4. color: "#999999",
  5. selectedColor: "#FF6600",
  6. backgroundColor: "#FFFFFF",
  7. list: [{
  8. pagePath: "/pages/index/index",
  9. iconPath: "/static/tabbar/home.png",
  10. selectedIconPath: "/static/tabbar/home-active.png",
  11. text: "首页"
  12. }, {
  13. pagePath: "/pages/community/index",
  14. iconPath: "/static/tabbar/community.png",
  15. selectedIconPath: "/static/tabbar/community-active.png",
  16. text: "社区"
  17. }, {
  18. pagePath: "/pages/legou/index",
  19. iconPath: "/static/tabbar/legou.png",
  20. selectedIconPath: "/static/tabbar/legou-active.png",
  21. text: "乐购",
  22. isSpecial: true // 标记为特殊按钮
  23. }, {
  24. pagePath: "/pages/cart/index",
  25. iconPath: "/static/tabbar/cart.png",
  26. selectedIconPath: "/static/tabbar/cart-active.png",
  27. text: "购物车"
  28. }, {
  29. pagePath: "/pages/user/index",
  30. iconPath: "/static/tabbar/user.png",
  31. selectedIconPath: "/static/tabbar/user-active.png",
  32. text: "我的"
  33. }]
  34. },
  35. attached() {
  36. // 延迟获取当前页面,确保页面已经完全加载
  37. setTimeout(() => {
  38. this.setTabBarIndex()
  39. }, 100)
  40. },
  41. methods: {
  42. switchTab(e) {
  43. try {
  44. const data = e.currentTarget.dataset
  45. const url = data.path
  46. const index = data.index
  47. console.log('切换到tab:', url, 'index:', index)
  48. // 更新选中状态
  49. this.setData({
  50. selected: index
  51. })
  52. // 页面跳转
  53. wx.switchTab({
  54. url,
  55. success: () => {
  56. console.log('页面跳转成功:', url)
  57. // 跳转成功后更新全局tabBar状态
  58. try {
  59. if (typeof this.getTabBar === 'function' && this.getTabBar()) {
  60. this.getTabBar().setData({
  61. selected: index
  62. })
  63. }
  64. } catch (error) {
  65. console.log('更新全局tabBar状态失败:', error)
  66. }
  67. },
  68. fail: (error) => {
  69. console.error('页面跳转失败:', error)
  70. }
  71. })
  72. } catch (error) {
  73. console.error('切换tab失败:', error)
  74. }
  75. },
  76. // 设置当前tabBar索引
  77. setTabBarIndex() {
  78. try {
  79. const pages = getCurrentPages()
  80. if (!pages || pages.length === 0) {
  81. console.log('页面栈为空,稍后重试')
  82. // 如果页面栈为空,延迟重试
  83. setTimeout(() => {
  84. this.setTabBarIndex()
  85. }, 200)
  86. return
  87. }
  88. const currentPage = pages[pages.length - 1]
  89. if (!currentPage || !currentPage.route) {
  90. console.log('当前页面信息不完整,使用默认索引')
  91. this.setData({ selected: 0 })
  92. return
  93. }
  94. const url = '/' + currentPage.route
  95. console.log('当前页面路径:', url)
  96. const index = this.data.list.findIndex(item => item.pagePath === url)
  97. if (index !== -1) {
  98. this.setData({
  99. selected: index
  100. })
  101. console.log('设置tabBar选中索引:', index)
  102. } else {
  103. console.log('未找到匹配的tabBar页面,使用默认索引')
  104. this.setData({ selected: 0 })
  105. }
  106. } catch (error) {
  107. console.error('设置tabBar索引失败:', error)
  108. // 出错时设置默认选中首页
  109. this.setData({ selected: 0 })
  110. }
  111. },
  112. // 更新选中状态的方法,供页面调用
  113. updateSelected(index) {
  114. try {
  115. console.log('更新tabBar选中状态:', index)
  116. this.setData({
  117. selected: index
  118. })
  119. } catch (error) {
  120. console.error('更新选中状态失败:', error)
  121. }
  122. }
  123. }
  124. })