运维小程序
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

index.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // custom-tab-bar/index.js
  2. Component({
  3. data: {
  4. tabList: [], // 动态生成的tab列表
  5. currentPath: '' // 当前页面路径
  6. },
  7. attached() {
  8. // 1. 先初始化tab列表(确保有数据可匹配)
  9. this.initTabList();
  10. // 2. 初始化当前路径
  11. this.updateCurrentPath();
  12. // 3. 监听小程序显示事件(覆盖切换tab、后台切前台等场景)
  13. this.appShowListener = () => {
  14. // 延迟确保页面栈已更新(真机需要更长时间)
  15. setTimeout(() => {
  16. this.updateCurrentPath();
  17. }, 300);
  18. };
  19. wx.onAppShow(this.appShowListener);
  20. },
  21. detached() {
  22. // 移除监听,避免内存泄漏
  23. wx.offAppShow(this.appShowListener);
  24. },
  25. methods: {
  26. // 初始化tab列表(根据角色筛选)
  27. initTabList() {
  28. const role = wx.getStorageSync('partnerPosition') || {};
  29. // 全部角色的tab配置
  30. const allTabs = [
  31. {
  32. pagePath: '/pages/home/index',
  33. text: '电站',
  34. iconPath: '/static/station2.png',
  35. selectedIconPath: '/static/station1.png'
  36. },
  37. {
  38. pagePath: '/pages/index/index',
  39. text: '首页',
  40. iconPath: '/static/sz3.png',
  41. selectedIconPath: '/static/sz4.png'
  42. },
  43. {
  44. pagePath: '/pages/tool/index',
  45. text: '工单',
  46. iconPath: '/static/tool.png',
  47. selectedIconPath: '/static/tool1.png'
  48. },
  49. {
  50. pagePath: '/pages/setup/index',
  51. text: '我的',
  52. iconPath: '/static/user1.png',
  53. selectedIconPath: '/static/user2.png'
  54. }
  55. ];
  56. // 客户角色的tab配置
  57. const customerTabs = [
  58. {
  59. pagePath: '/pages/home/index',
  60. text: '电站',
  61. iconPath: '/static/station2.png',
  62. selectedIconPath: '/static/station1.png'
  63. },
  64. {
  65. pagePath: '/pages/tool/index',
  66. text: '工单',
  67. iconPath: '/static/tool.png',
  68. selectedIconPath: '/static/tool1.png'
  69. },
  70. {
  71. pagePath: '/pages/setup/index',
  72. text: '我的',
  73. iconPath: '/static/user1.png',
  74. selectedIconPath: '/static/user2.png'
  75. }
  76. ];
  77. // 根据角色筛选tab
  78. let tabList = allTabs;
  79. if (role.partnerPosition === 5) {
  80. tabList = customerTabs;
  81. }
  82. // 确保tabList更新后重新校验路径
  83. this.setData({ tabList }, () => {
  84. this.updateCurrentPath();
  85. });
  86. },
  87. // 更新当前页面路径(核心方法)
  88. updateCurrentPath() {
  89. const pages = getCurrentPages();
  90. if (pages.length === 0) return;
  91. // 获取当前页面完整路径
  92. const currentPage = pages[pages.length - 1];
  93. const currentPath = `/${currentPage.route}`;
  94. // 仅在路径变化且为tab页时更新
  95. const isTabPage = this.data.tabList.some(item => item.pagePath === currentPath);
  96. if (isTabPage && this.data.currentPath !== currentPath) {
  97. this.setData({ currentPath }, () => {
  98. console.log('路径已同步:', currentPath);
  99. });
  100. }
  101. },
  102. // 切换tab页
  103. switchTab(e) {
  104. const path = e.currentTarget.dataset.path;
  105. if (!path) return;
  106. // 提前记录目标路径(用于预更新状态)
  107. const targetPath = path;
  108. wx.switchTab({
  109. url: targetPath,
  110. success: () => {
  111. // 延长延迟确保页面完全切换(解决异步滞后问题)
  112. setTimeout(() => {
  113. this.updateCurrentPath();
  114. }, 500);
  115. },
  116. fail: (err) => {
  117. console.error('切换tab失败:', err);
  118. }
  119. });
  120. }
  121. }
  122. });