index.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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/teacher-home', 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. uni.reLaunch({ url: item.pagePath })
  91. }
  92. }
  93. }
  94. </script>
  95. <style scoped>
  96. .tab-bar-wrap {
  97. position: fixed;
  98. bottom: 0;
  99. left: 0;
  100. right: 0;
  101. z-index: 999;
  102. }
  103. .tab-bar {
  104. display: flex;
  105. height: 100rpx;
  106. background: #fff;
  107. padding-bottom: env(safe-area-inset-bottom);
  108. border-top: 1rpx solid #eee;
  109. }
  110. .tab-item {
  111. flex: 1;
  112. display: flex;
  113. flex-direction: column;
  114. align-items: center;
  115. justify-content: center;
  116. }
  117. .tab-icon {
  118. width: 48rpx;
  119. height: 48rpx;
  120. margin-bottom: 4rpx;
  121. }
  122. .tab-text {
  123. font-size: 22rpx;
  124. color: #999;
  125. }
  126. .tab-item.active .tab-text {
  127. color: #FF6B6B;
  128. }
  129. </style>