index.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <template>
  2. <view class="tab-bar-wrap" v-if="isLoggedIn">
  3. <!-- 普通用户TabBar -->
  4. <view v-if="currentRole !== 'teacher'" class="tab-bar">
  5. <view
  6. v-for="(item, index) in normalTabs"
  7. :key="index"
  8. class="tab-item"
  9. :class="{ active: currentIndex === index }"
  10. @click="switchTab(item, index)"
  11. >
  12. <image
  13. class="tab-icon"
  14. :src="currentIndex === index ? item.selectedIcon : item.icon"
  15. mode="aspectFit"
  16. ></image>
  17. <text class="tab-text">{{ item.text }}</text>
  18. </view>
  19. </view>
  20. <!-- 规划师TabBar -->
  21. <view v-else class="tab-bar">
  22. <view
  23. v-for="(item, index) in teacherTabs"
  24. :key="index"
  25. class="tab-item"
  26. :class="{ active: currentIndex === index }"
  27. @click="switchTab(item, index)"
  28. >
  29. <image
  30. class="tab-icon"
  31. :src="currentIndex === index ? item.selectedIcon : item.icon"
  32. mode="aspectFit"
  33. ></image>
  34. <text class="tab-text">{{ item.text }}</text>
  35. </view>
  36. </view>
  37. </view>
  38. </template>
  39. <script>
  40. export default {
  41. data() {
  42. return {
  43. currentIndex: 0,
  44. normalTabs: [
  45. { text: '首页', pagePath: '/pages/index/index', icon: '/static/tab-home.png', selectedIcon: '/static/tab-home-active.png' },
  46. { text: '任务', pagePath: '/pages/tasks/tasks', icon: '/static/tab-task.png', selectedIcon: '/static/tab-task-active.png' },
  47. { text: '心愿单', pagePath: '/pages/rewards/rewards', icon: '/static/tab-reward.png', selectedIcon: '/static/tab-reward-active.png' },
  48. { text: '我的', pagePath: '/pages/profile/profile', icon: '/static/tab-profile.png', selectedIcon: '/static/tab-profile-active.png' }
  49. ],
  50. teacherTabs: [
  51. { text: '首页', pagePath: '/pages/teacher/index', icon: '/static/tab-home.png', selectedIcon: '/static/tab-home-active.png' },
  52. { text: '家庭', pagePath: '/pages/teacher/families', icon: '/static/tab-family.png', selectedIcon: '/static/tab-family-active.png' },
  53. { text: '团队', pagePath: '/pages/teacher/team', icon: '/static/tab-team.png', selectedIcon: '/static/tab-team-active.png' },
  54. { text: '成长档案', pagePath: '/pages/growth/index', icon: '/static/tab-growth.png', selectedIcon: '/static/tab-growth-active.png' },
  55. { text: '我的', pagePath: '/pages/teacher/teacher-profile', icon: '/static/tab-profile.png', selectedIcon: '/static/tab-profile-active.png' }
  56. ]
  57. }
  58. },
  59. computed: {
  60. isLoggedIn() {
  61. return !!uni.getStorageSync('token')
  62. },
  63. currentRole() {
  64. return uni.getStorageSync('currentRole') || 'parent'
  65. }
  66. },
  67. mounted() {
  68. this.updateSelected()
  69. uni.$on('switchRole', () => {
  70. this.updateSelected()
  71. })
  72. },
  73. methods: {
  74. updateSelected() {
  75. const pages = getCurrentPages()
  76. if (pages.length) {
  77. const currentPage = pages[pages.length - 1]
  78. const currentPath = '/' + currentPage.route
  79. if (this.currentRole === 'teacher') {
  80. const idx = this.teacherTabs.findIndex(t => t.pagePath === currentPath)
  81. this.currentIndex = idx >= 0 ? idx : 0
  82. } else {
  83. const idx = this.normalTabs.findIndex(t => t.pagePath === currentPath)
  84. this.currentIndex = idx >= 0 ? idx : 0
  85. }
  86. }
  87. },
  88. switchTab(item, index) {
  89. this.currentIndex = index
  90. // 原生TabBar页使用switchTab,非Tab页使用redirectTo(比reLaunch轻量)
  91. const nativeTabPages = ['/pages/index/index', '/pages/discover/index', '/pages/shop/index', '/pages/profile/profile']
  92. if (nativeTabPages.includes(item.pagePath)) {
  93. uni.switchTab({ url: item.pagePath })
  94. } else {
  95. uni.redirectTo({ url: item.pagePath })
  96. }
  97. }
  98. }
  99. }
  100. </script>
  101. <style scoped>
  102. .tab-bar-wrap {
  103. position: fixed;
  104. bottom: 0;
  105. left: 0;
  106. right: 0;
  107. z-index: 999;
  108. }
  109. .tab-bar {
  110. display: flex;
  111. height: 100rpx;
  112. background: #fff;
  113. padding-bottom: env(safe-area-inset-bottom);
  114. border-top: 1rpx solid #eee;
  115. }
  116. .tab-item {
  117. flex: 1;
  118. display: flex;
  119. flex-direction: column;
  120. align-items: center;
  121. justify-content: center;
  122. }
  123. .tab-icon {
  124. width: 48rpx;
  125. height: 48rpx;
  126. margin-bottom: 4rpx;
  127. }
  128. .tab-text {
  129. font-size: 22rpx;
  130. color: #999;
  131. }
  132. .tab-item.active .tab-text {
  133. color: #F97316;
  134. }
  135. </style>