| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <template>
- <view class="tab-bar-wrap" v-if="isLoggedIn">
- <!-- 普通用户TabBar -->
- <view v-if="currentRole !== 'teacher'" class="tab-bar">
- <view
- v-for="(item, index) in normalTabs"
- :key="index"
- class="tab-item"
- :class="{ active: currentIndex === index }"
- @click="switchTab(item, index)"
- >
- <image
- class="tab-icon"
- :src="currentIndex === index ? item.selectedIcon : item.icon"
- mode="aspectFit"
- ></image>
- <text class="tab-text">{{ item.text }}</text>
- </view>
- </view>
- <!-- 规划师TabBar -->
- <view v-else class="tab-bar">
- <view
- v-for="(item, index) in teacherTabs"
- :key="index"
- class="tab-item"
- :class="{ active: currentIndex === index }"
- @click="switchTab(item, index)"
- >
- <image
- class="tab-icon"
- :src="currentIndex === index ? item.selectedIcon : item.icon"
- mode="aspectFit"
- ></image>
- <text class="tab-text">{{ item.text }}</text>
- </view>
- </view>
- </view>
- </template>
- <script>
- export default {
- data() {
- return {
- currentIndex: 0,
- normalTabs: [
- { text: '首页', pagePath: '/pages/index/index', icon: '/static/tab-home.png', selectedIcon: '/static/tab-home-active.png' },
- { text: '身', pagePath: '/pages/body/index', icon: '/static/tab-body.png', selectedIcon: '/static/tab-body-active.png' },
- { text: '智', pagePath: '/pages/wisdom/index', icon: '/static/tab-wisdom.png', selectedIcon: '/static/tab-wisdom-active.png' },
- { text: '心', pagePath: '/pages/mind/index', icon: '/static/tab-mind.png', selectedIcon: '/static/tab-mind-active.png' },
- { text: '我的', pagePath: '/pages/profile/profile', icon: '/static/tab-profile.png', selectedIcon: '/static/tab-profile-active.png' }
- ],
- teacherTabs: [
- { text: '首页', pagePath: '/pages/teacher/index', icon: '/static/tab-home.png', selectedIcon: '/static/tab-home-active.png' },
- { text: '家庭', pagePath: '/pages/teacher/families', icon: '/static/tab-family.png', selectedIcon: '/static/tab-family-active.png' },
- { text: '团队', pagePath: '/pages/teacher/team', icon: '/static/tab-team.png', selectedIcon: '/static/tab-team-active.png' },
- { text: '成长档案', pagePath: '/pages/growth/index', icon: '/static/tab-growth.png', selectedIcon: '/static/tab-growth-active.png' },
- { text: '我的', pagePath: '/pages/teacher/teacher-profile', icon: '/static/tab-profile.png', selectedIcon: '/static/tab-profile-active.png' }
- ]
- }
- },
- computed: {
- isLoggedIn() {
- return !!uni.getStorageSync('token')
- },
- currentRole() {
- return uni.getStorageSync('currentRole') || 'parent'
- }
- },
- mounted() {
- this.updateSelected()
- uni.$on('switchRole', () => {
- this.updateSelected()
- })
- },
- methods: {
- updateSelected() {
- const pages = getCurrentPages()
- if (pages.length) {
- const currentPage = pages[pages.length - 1]
- const currentPath = '/' + currentPage.route
- if (this.currentRole === 'teacher') {
- const idx = this.teacherTabs.findIndex(t => t.pagePath === currentPath)
- this.currentIndex = idx >= 0 ? idx : 0
- } else {
- const idx = this.normalTabs.findIndex(t => t.pagePath === currentPath)
- this.currentIndex = idx >= 0 ? idx : 0
- }
- }
- },
- switchTab(item, index) {
- this.currentIndex = index
- // 原生TabBar页使用switchTab,非Tab页使用redirectTo(比reLaunch轻量)
- const nativeTabPages = ['/pages/index/index', '/pages/body/index', '/pages/wisdom/index', '/pages/mind/index', '/pages/profile/profile']
- if (nativeTabPages.includes(item.pagePath)) {
- uni.switchTab({ url: item.pagePath })
- } else {
- uni.redirectTo({ url: item.pagePath })
- }
- }
- }
- }
- </script>
- <style scoped>
- .tab-bar-wrap {
- position: fixed;
- bottom: 0;
- left: 0;
- right: 0;
- z-index: 999;
- }
- .tab-bar {
- display: flex;
- height: 100rpx;
- background: #fff;
- padding-bottom: env(safe-area-inset-bottom);
- border-top: 1rpx solid #eee;
- }
- .tab-item {
- flex: 1;
- display: flex;
- flex-direction: column;
- align-items: center;
- justify-content: center;
- }
- .tab-icon {
- width: 48rpx;
- height: 48rpx;
- margin-bottom: 4rpx;
- }
- .tab-text {
- font-size: 22rpx;
- color: #999;
- }
- .tab-item.active .tab-text {
- color: #F97316;
- }
- </style>
|