AppTabbar.vue
3.11 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
<template>
<view class="app-tabbar">
<view class="app-tabbar__placeholder" />
<view class="app-tabbar__wrap">
<view class="app-tabbar__panel">
<view
v-for="item in tabItems"
:key="item.name"
class="app-tabbar__item"
:class="{ 'is-active': isActive(item.name) }"
@tap="handleTabClick(item)"
>
<view class="app-tabbar__item-inner">
<view class="app-tabbar__icon">
<component
:is="item.icon"
size="18"
:color="isActive(item.name) ? activeColor : inactiveColor"
/>
</view>
<text class="app-tabbar__label">{{ item.title }}</text>
</view>
</view>
</view>
</view>
</view>
</template>
<script setup>
import Taro from '@tarojs/taro'
import { Home, Message, My } from '@nutui/icons-vue-taro'
// 迁移到独立 H5 项目时,别忘了把 miniProgram helper 一起带走。
import { isH5Env, navigateToMiniProgramPage } from '@/utils/miniProgram'
const props = defineProps({
current: {
type: String,
required: true,
},
})
const activeColor = '#a67939'
const inactiveColor = '#8b95a7'
const tabItems = [
{
name: 'home',
title: '首页',
icon: Home,
url: '/pages/index/index',
},
{
name: 'message',
title: '消息',
icon: Message,
url: '/pages/message/index',
},
{
name: 'mine',
title: '我的',
icon: My,
url: '/pages/mine/index',
},
]
const isActive = (name) => name === props.current
const handleTabClick = async (item) => {
if (!item?.url || item.name === props.current) {
return
}
if (isH5Env()) {
try {
const has_navigated = await navigateToMiniProgramPage(item.url)
if (has_navigated) {
return
}
} catch (error) {
console.error('H5 跳转小程序页面失败:', error)
}
}
Taro.redirectTo({ url: item.url })
}
</script>
<style lang="less">
.app-tabbar {
.app-tabbar__placeholder {
height: 132rpx;
flex-shrink: 0;
}
.app-tabbar__wrap {
position: fixed;
left: 0;
right: 0;
bottom: 0;
z-index: 100;
box-sizing: border-box;
}
.app-tabbar__panel {
display: flex;
align-items: stretch;
min-height: 132rpx;
padding: 16rpx 24rpx;
border-top: 2rpx solid rgba(166, 121, 57, 0.12);
background: rgba(255, 255, 255, 0.98);
box-sizing: border-box;
backdrop-filter: blur(12rpx);
}
.app-tabbar__item {
flex: 1;
min-width: 0;
}
.app-tabbar__item-inner {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 10rpx;
min-height: 100rpx;
border-radius: 20rpx;
color: #8b95a7;
transition: background-color 0.2s ease, color 0.2s ease, transform 0.2s ease;
}
.app-tabbar__icon {
display: flex;
align-items: center;
justify-content: center;
line-height: 1;
}
.app-tabbar__label {
font-size: 22rpx;
font-weight: 600;
line-height: 1.2;
}
.app-tabbar__item.is-active .app-tabbar__item-inner {
color: #a67939;
}
}
</style>