TypeTabComponent.uvue
3.7 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<template>
<view class="type-tab">
<scroll-view class="type-box" scroll-x="true" :show-scrollbar="false" scroll-with-animation>
<view class="type-scroll-content">
<view
class="tab-item"
:class="[tabClass(index)]"
v-for="(i, index) in typeList"
:key="index"
@click.stop="tabClick(index, i)"
>
<view class="text flex justify-center align-center" :class="[tabIndex == index ? 'active' : '']">
<text class="tab-name">{{ getTabName(i) }}</text>
<text v-if="isShowCount" class="count ml-8">{{ (getTabCount(i) > 99) ? '99+' : getTabCount(i).toString() }}</text>
</view>
</view>
</view>
</scroll-view>
</view>
</template>
<script setup lang="uts">
import { ref, computed } from 'vue';
import { get } from '@/utils/common.uts';
// Props 类型:使用 any[] 避免响应式对象类型转换问题
const props = withDefaults(defineProps<{
typeList: any[];
tabIndex: number;
nameKey?: string;
isShowCount?: boolean;
}>(), {
nameKey: 'tabName',
isShowCount: false
});
// Emits
const emit = defineEmits<{
(e: 'tabClick', index: number, item: any): void;
}>();
// 辅助方法:安全访问响应式对象的属性
// 响应式对象在 app 端无法通过 get() 访问,使用 JSON 序列化/反序列化解包
const getTabName = (item: any): string => {
// 先尝试 get()
let name = get(item, props.nameKey);
if (name != null) {
return name as string;
}
// get() 失败,尝试 JSON 序列化/反序列化解包响应式对象
try {
const jsonStr = JSON.stringify(item);
const parsed = JSON.parse(jsonStr) as any;
if (parsed != null) {
const name1 = get(parsed, props.nameKey);
if (name1 != null) {
return name1 as string;
}
const name2 = get(parsed, 'tabName');
if (name2 != null) {
return name2 as string;
}
}
} catch (e: any) {
// 静默失败
}
return '未知';
};
const getTabCount = (item: any): number => {
// 先尝试 get()
let count = get(item, 'count');
if (count != null) {
return count as number;
}
// get() 失败,尝试 JSON 序列化/反序列化解包响应式对象
try {
const jsonStr = JSON.stringify(item);
const parsed = JSON.parse(jsonStr) as any;
if (parsed != null) {
const countValue = get(parsed, 'count');
if (countValue != null) {
return countValue as number;
}
}
} catch (e: any) {
// 静默失败
}
return 0;
};
// 方法
const tabClass = (index: number): string => {
let className = '';
if (props.tabIndex == index) {
className = 'active ';
if (index == 0) className += 'first-nav';
else className += 'second-nav';
}
return className;
};
const tabClick = (index: number, item: any) => {
emit('tabClick', index, item);
};
</script>
<style>
.type-tab {
position: relative;
overflow: hidden;
}
.type-box {
position: relative;
width: 100%;
background-color: #eef4fe;
height: 48px;
border-radius: 24px 24px 0 0;
overflow: hidden;
z-index: 0;
}
.type-scroll-content {
display: flex;
flex-direction: row;
align-items: center;
flex-wrap: nowrap;
}
.tab-item {
flex-shrink: 0;
position: relative;
margin-right: 8px;
}
.tab-item .text {
position: relative;
height: 48px;
padding: 0 32px;
transition: all 0.15s;
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
}
.tab-item .text .tab-name {
font-weight: 400;
font-size: 14px;
color: #292d33;
text-align: center;
}
.tab-item .text.active .tab-name {
font-weight: 500;
font-size: 16px;
color: #3b73ff;
}
.tab-item .text .count {
font-size: 12px;
margin-left: 4px;
color: #292d33;
}
.flex {
display: flex;
flex-direction: row;
}
.justify-center {
justify-content: center;
}
.align-center {
align-items: center;
}
.ml-8 {
margin-left: 8px;
}
</style>