TabBar.vue
4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<template>
<div class="fixed bottom-0 w-full max-w-md mx-auto z-50">
<div class="backdrop-blur-md bg-white/70 border-t border-gray-200 px-4 py-2">
<div class="flex justify-around items-center">
<!-- 首页 -->
<view
@click="navigateTo('/pages/index/index')"
:class="getTabClass('index')"
>
<Home size="20" :color="getIconColor('index')" />
<span class="text-xs mt-1">首页</span>
</view>
<!-- 分类 -->
<view
@click="navigateTo('/pages/post/index')"
:class="getTabClass('post')"
>
<Category size="20" :color="getIconColor('post')" />
<span class="text-xs mt-1">分类</span>
</view>
<!-- 我要卖车 -->
<div class="relative -mt-5">
<view
@click="navigateTo('/pages/sell/index')"
class="bg-orange-500 rounded-full p-3 text-white shadow-lg"
>
<span class="text-sm font-medium">我要卖车</span>
</view>
</div>
<!-- 消息 -->
<view
@click="navigateTo('/pages/messages/index')"
:class="getTabClass('messages')"
>
<Comment size="20" :color="getIconColor('messages')" />
<span class="text-xs mt-1">消息</span>
</view>
<!-- 我的 -->
<view
@click="navigateTo('/pages/profile/index')"
:class="getTabClass('profile')"
>
<My size="20" :color="getIconColor('profile')" />
<span class="text-xs mt-1">我的</span>
</view>
</div>
</div>
</div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import Taro from '@tarojs/taro'
import { Home, Category, Comment, My } from '@nutui/icons-vue-taro'
// 当前激活的tab
const activeTab = ref('')
/**
* 获取当前页面路径并设置激活状态
*/
const getCurrentPage = () => {
const pages = Taro.getCurrentPages()
if (pages.length > 0) {
const currentPage = pages[pages.length - 1]
const route = currentPage.route
if (route.includes('index/index')) {
activeTab.value = 'index'
} else if (route.includes('post/index')) {
activeTab.value = 'post'
} else if (route.includes('sell/index')) {
activeTab.value = 'sell'
} else if (route.includes('messages/index')) {
activeTab.value = 'messages'
} else if (route.includes('profile/index')) {
activeTab.value = 'profile'
}
}
}
/**
* 导航到指定页面
* @param {string} url - 页面路径
*/
const navigateTo = (url) => {
// TODO: 等待正式接口
// if ((url === '/pages/profile/index' || url === '/pages/sell/index') && !is_auth.value) {
// Taro.showToast({
// title: '请先完善个人信息',
// icon: 'none',
// duration: 2000,
// success: (res) => {
// setTimeout(() => {
// Taro.navigateTo({
// url: '/pages/register/index'
// })
// }, 2000);
// }
// })
// return
// }
Taro.redirectTo({
url: url
})
}
/**
* 获取tab按钮的样式类
* @param {string} tab - tab标识
* @returns {string} 样式类名
*/
const getTabClass = (tab) => {
const baseClass = 'flex flex-col items-center p-2'
const activeClass = activeTab.value === tab ? 'text-orange-500' : 'text-gray-500'
return `${baseClass} ${activeClass}`
}
/**
* 获取图标颜色
* @param {string} tab - tab标识
* @returns {string} 颜色值
*/
const getIconColor = (tab) => {
return activeTab.value === tab ? '#f97316' : '#6b7280'
}
const userInfo = ref({
avatar: '',
nickname: '',
phone: '1',
gender: '',
school: '',
birthday: '',
});
const is_auth = ref(false);
onMounted(() => {
getCurrentPage()
// 获取当前用户的信息
// if (userInfo.value && userInfo.value.phone) {
// is_auth.value = true
// }
is_auth.value = false
})
</script>
<style lang="less" scoped>
/* 确保底部导航栏在最上层 */
.z-50 {
z-index: 50;
}
</style>