TypeTabComponent.uvue 3.7 KB
<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>