index.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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-home/index', icon: '/static/tab-action.png', selectedIcon: '/static/tab-action-active.png' },
  46. { text: '心', pagePath: '/pages/mind/index', icon: '/static/tab-mind.png', selectedIcon: '/static/tab-mind-active.png' },
  47. { text: '身', pagePath: '/pages/body/index', icon: '/static/tab-body.png', selectedIcon: '/static/tab-body-active.png' },
  48. { text: '智', pagePath: '/pages/wisdom/index', icon: '/static/tab-wisdom.png', selectedIcon: '/static/tab-wisdom-active.png' },
  49. { text: '富', pagePath: '/pages/wealth/index', icon: '/static/tab-wealth.png', selectedIcon: '/static/tab-wealth-active.png' }
  50. ],
  51. teacherTabs: [
  52. { text: '首页', pagePath: '/pages/teacher/index', icon: '/static/tab-action.png', selectedIcon: '/static/tab-action-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. // 注意:switchTab 只能跳转 pages.json 中 tabBar.list 注册的页面,否则会触发路由系统错误
  93. const nativeTabPages = ['/pages/index-home/index', '/pages/mind/index', '/pages/body/index', '/pages/wisdom/index', '/pages/wealth/index']
  94. if (nativeTabPages.includes(item.pagePath)) {
  95. uni.switchTab({ url: item.pagePath })
  96. } else {
  97. uni.redirectTo({ url: item.pagePath })
  98. }
  99. }
  100. }
  101. }
  102. </script>
  103. <style scoped>
  104. .tab-bar-wrap {
  105. position: fixed;
  106. bottom: 0;
  107. left: 0;
  108. right: 0;
  109. z-index: 999;
  110. }
  111. .tab-bar {
  112. display: flex;
  113. height: 100rpx;
  114. background: #fff;
  115. padding-bottom: env(safe-area-inset-bottom);
  116. border-top: 1rpx solid #eee;
  117. }
  118. .tab-item {
  119. flex: 1;
  120. display: flex;
  121. flex-direction: column;
  122. align-items: center;
  123. justify-content: center;
  124. }
  125. .tab-icon {
  126. width: 48rpx;
  127. height: 48rpx;
  128. margin-bottom: 4rpx;
  129. }
  130. .tab-text {
  131. font-size: 22rpx;
  132. color: #999;
  133. }
  134. .tab-item.active .tab-text {
  135. color: #F97316;
  136. }
  137. </style>