index.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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/body/index', icon: '/static/tab-body.png', selectedIcon: '/static/tab-body-active.png' },
  47. { text: '智', pagePath: '/pages/wisdom/index', icon: '/static/tab-wisdom.png', selectedIcon: '/static/tab-wisdom-active.png' },
  48. { text: '心', pagePath: '/pages/mind/index', icon: '/static/tab-mind.png', selectedIcon: '/static/tab-mind-active.png' },
  49. { text: '我的', pagePath: '/pages/profile/profile', icon: '/static/tab-profile.png', selectedIcon: '/static/tab-profile-active.png' }
  50. ],
  51. teacherTabs: [
  52. { text: '首页', pagePath: '/pages/teacher/index', icon: '/static/tab-home.png', selectedIcon: '/static/tab-home-active.png' },
  53. { text: '家庭', pagePath: '/pages/teacher/families', icon: '/static/tab-family.png', selectedIcon: '/static/tab-family-active.png' },
  54. { text: '团队', pagePath: '/pages/teacher/team', icon: '/static/tab-team.png', selectedIcon: '/static/tab-team-active.png' },
  55. { text: '成长档案', pagePath: '/pages/growth/index', icon: '/static/tab-growth.png', selectedIcon: '/static/tab-growth-active.png' },
  56. { text: '我的', pagePath: '/pages/teacher/teacher-profile', icon: '/static/tab-profile.png', selectedIcon: '/static/tab-profile-active.png' }
  57. ]
  58. }
  59. },
  60. computed: {
  61. isLoggedIn() {
  62. return !!uni.getStorageSync('token')
  63. },
  64. currentRole() {
  65. return uni.getStorageSync('currentRole') || 'parent'
  66. }
  67. },
  68. mounted() {
  69. this.updateSelected()
  70. uni.$on('switchRole', () => {
  71. this.updateSelected()
  72. })
  73. },
  74. methods: {
  75. updateSelected() {
  76. const pages = getCurrentPages()
  77. if (pages.length) {
  78. const currentPage = pages[pages.length - 1]
  79. const currentPath = '/' + currentPage.route
  80. if (this.currentRole === 'teacher') {
  81. const idx = this.teacherTabs.findIndex(t => t.pagePath === currentPath)
  82. this.currentIndex = idx >= 0 ? idx : 0
  83. } else {
  84. const idx = this.normalTabs.findIndex(t => t.pagePath === currentPath)
  85. this.currentIndex = idx >= 0 ? idx : 0
  86. }
  87. }
  88. },
  89. switchTab(item, index) {
  90. this.currentIndex = index
  91. // 原生TabBar页使用switchTab,非Tab页使用redirectTo(比reLaunch轻量)
  92. const nativeTabPages = ['/pages/index/index', '/pages/body/index', '/pages/wisdom/index', '/pages/mind/index', '/pages/profile/profile']
  93. if (nativeTabPages.includes(item.pagePath)) {
  94. uni.switchTab({ url: item.pagePath })
  95. } else {
  96. uni.redirectTo({ url: item.pagePath })
  97. }
  98. }
  99. }
  100. }
  101. </script>
  102. <style scoped>
  103. .tab-bar-wrap {
  104. position: fixed;
  105. bottom: 0;
  106. left: 0;
  107. right: 0;
  108. z-index: 999;
  109. }
  110. .tab-bar {
  111. display: flex;
  112. height: 100rpx;
  113. background: #fff;
  114. padding-bottom: env(safe-area-inset-bottom);
  115. border-top: 1rpx solid #eee;
  116. }
  117. .tab-item {
  118. flex: 1;
  119. display: flex;
  120. flex-direction: column;
  121. align-items: center;
  122. justify-content: center;
  123. }
  124. .tab-icon {
  125. width: 48rpx;
  126. height: 48rpx;
  127. margin-bottom: 4rpx;
  128. }
  129. .tab-text {
  130. font-size: 22rpx;
  131. color: #999;
  132. }
  133. .tab-item.active .tab-text {
  134. color: #F97316;
  135. }
  136. </style>