Commit 5763732d by liimin

feat(components): 优化车辆品牌选择组件增加车系选择功能

- 实现品牌和车系两级选择视图,支持在品牌和车系之间切换
- 添加自定义返回按钮,车系视图时显示返回品牌列表功能
- 重构组件结构,统一使用单个 MessageBox 组件管理品牌和车系选择
- 集成缓存机制,优先从本地存储获取车系数据提升性能
- 添加选中状态高亮显示,优化用户体验
- 为 MessageBox 组件添加 z-index 属性支持层级控制
- 更新样式布局,修复选择项排列问题
1 parent 79267a73
<template> <template>
<view> <view>
<MessageBox ref="carBrandListVisible" <MessageBox ref="carBrandListVisible"
:back="showBackButton"
:is-footer="false" :is-footer="false"
position="bottom" position="bottom"
title="请选择车品牌" :title="currentView == 'brand' ? '请选择车品牌' : '请选择车系'"
@close="$emit('close')"> :z-index="10076"
<view :key="updateView" class="select-box"> @close="handleMessageBoxClose">
<!-- 自定义返回按钮 -->
<!-- <view v-if="showBackButton"
class="custom-back-btn"
@click="handleBackClick
<un-icon name="arrow-left" size="20"></un-icon>">
</vie -->
<view class="select-box">
<!-- 品牌列表 -->
<!-- <un-transition duration="50"
mode="fade"
:show="currentView == 'brand'"> -->
<view v-show="currentView == 'brand'" class="view-content">
<template v-for="(item, index) in carBrandList" <template v-for="(item, index) in carBrandList"
:key="index"> :key="index">
<view class="select-item" <view class="select-item"
:class="[{ checked: carBrand == (item as UTSJSONObject).name }]" :class="[{ checked: selectedBrandIndex == index }]"
@click="selectCar(item, false ,'')"> @click="selectBrand(item, index)">
{{ (item as UTSJSONObject).name }} {{ (item as UTSJSONObject).name }}
</view> </view>
</template> </template>
</view> </view>
</MessageBox> <!-- </un-transition> -->
<MessageBox ref="carBoxVisible"
back <!-- 车系列表 -->
:is-footer="false" <!-- <un-transition duration="50"
:max-height="1000" mode="fade"
position="bottom" :show="currentView == 'series'"> -->
title="请选择车系" <view v-show="currentView == 'series'" class="view-content">
@close="closeCarBoxVisible"> <template v-for="(item, index) in currentCarList"
<view class="select-box">
<template v-for="(item, index) in carlist"
:key="index"> :key="index">
<view class="select-item" <view class="select-item"
:class="[{ checked: carModel == (item as UTSJSONObject).name }]" :class="[{ checked: carModel == (item as UTSJSONObject).name }]"
...@@ -33,6 +44,8 @@ ...@@ -33,6 +44,8 @@
</view> </view>
</template> </template>
</view> </view>
<!-- </un-transition> -->
</view>
</MessageBox> </MessageBox>
</view> </view>
...@@ -84,9 +97,14 @@ const selectItem = ref<UTSJSONObject>({}); ...@@ -84,9 +97,14 @@ const selectItem = ref<UTSJSONObject>({});
const clickFlga = ref<boolean>(false); const clickFlga = ref<boolean>(false);
const carBrandListOff = ref<boolean>(false); const carBrandListOff = ref<boolean>(false);
const carBrandListVisible = ref(null); const carBrandListVisible = ref(null);
const carBoxVisible = ref(null);
const updateView = ref<number>(Date.now()); const updateView = ref<number>(Date.now());
const carBrandList = ref<Array<any>>([]); const carBrandList = ref<Array<any>>([]);
// 当前视图:"brand" = 品牌选择,"series" = 车系选择
const currentView = ref<"brand" | "series">("brand");
// 当前选中的品牌索引
const selectedBrandIndex = ref<number>(-1);
// 当前车系列表(根据选中品牌动态变化)
const currentCarList = ref<Array<any>>([]);
const watchStatus = computed<boolean>(() => { const watchStatus = computed<boolean>(() => {
const status = state.user?.historyStatus; const status = state.user?.historyStatus;
if (status == null) { if (status == null) {
...@@ -129,19 +147,41 @@ const defaultCarId = computed<any>(() => { ...@@ -129,19 +147,41 @@ const defaultCarId = computed<any>(() => {
const val = state.user.defaultCarId; const val = state.user.defaultCarId;
return val != null && val != 0 ? val : ""; return val != null && val != 0 ? val : "";
}); });
// 是否显示返回按钮(车系视图时显示)
const showBackButton = computed<boolean>(() => {
return currentView.value == "series";
});
function close(): void { function close(): void {
if (carBoxVisible.value != null) {
(carBoxVisible.value as ComponentPublicInstance).$callMethod("close");
}
if (carBrandListVisible.value != null) { if (carBrandListVisible.value != null) {
(carBrandListVisible.value as ComponentPublicInstance).$callMethod("close"); (carBrandListVisible.value as ComponentPublicInstance).$callMethod("close");
} }
carBrandListOff.value = false; carBrandListOff.value = false;
currentView.value = "brand"; // 重置为品牌视图
selectedBrandIndex.value = -1; // 重置选中状态
currentCarList.value = []; // 清空车系列表
}
// 处理 MessageBox 的 close 事件
function handleMessageBoxClose(): void {
// 如果当前是车系视图,点击返回按钮时切换到品牌视图
if (currentView.value == "series") {
currentView.value = "brand";
} else {
// 如果是品牌视图,点击关闭按钮才真正关闭弹窗
emit("close");
}
}
// 处理自定义返回按钮点击
function handleBackClick(): void {
currentView.value = "brand";
} }
function closeCarBoxVisible(): void { function closeCarBoxVisible(): void {
if (carBrandListOff.value) return; // 返回品牌选择
else emit("close"); currentView.value = "brand";
} }
function handleSelect(item: any): void { function handleSelect(item: any): void {
const selectItemVal = selectItem.value; const selectItemVal = selectItem.value;
...@@ -176,12 +216,18 @@ function handleSelect(item: any): void { ...@@ -176,12 +216,18 @@ function handleSelect(item: any): void {
dispatch("setCurrentCarInfo", selectItemVal); dispatch("setCurrentCarInfo", selectItemVal);
} }
(carBoxVisible.value as ComponentPublicInstance).$callMethod("close"); // 关闭弹窗并发出选择事件
if (carBrandListVisible.value != null) {
(carBrandListVisible.value as ComponentPublicInstance).$callMethod("close");
}
emit("select", { ...item, ...selectItemVal }); emit("select", { ...item, ...selectItemVal });
} }
function selectCar(e: any, auto: boolean = false, carId?: string): void {
const _brandId = (e as UTSJSONObject).brandId; // 选择品牌(切换到车系视图)
const _carId = (e as UTSJSONObject).id; function selectBrand(item: any, index: number): void {
const _brandId = (item as UTSJSONObject).brandId;
const _carId = (item as UTSJSONObject).id;
if (_brandId == null || _carId == null) { if (_brandId == null || _carId == null) {
return uni.showToast({ return uni.showToast({
title: "车型 ID 或品牌 ID 为空", title: "车型 ID 或品牌 ID 为空",
...@@ -189,12 +235,10 @@ function selectCar(e: any, auto: boolean = false, carId?: string): void { ...@@ -189,12 +235,10 @@ function selectCar(e: any, auto: boolean = false, carId?: string): void {
}); });
} }
let index = 0; selectedBrandIndex.value = index;
if (clickFlga.value) return; const _brandName = (item as UTSJSONObject).name;
clickFlga.value = true; const _carName = (item as UTSJSONObject).name;
const _brandName = (e as UTSJSONObject).brandName;
const _carName = (e as UTSJSONObject).name;
const selectData = { const selectData = {
carBrand: _brandName, carBrand: _brandName,
carBrandId: _brandId, carBrandId: _brandId,
...@@ -206,46 +250,27 @@ function selectCar(e: any, auto: boolean = false, carId?: string): void { ...@@ -206,46 +250,27 @@ function selectCar(e: any, auto: boolean = false, carId?: string): void {
const cacheKey = `${_carId}_${_brandId}_carlist`; const cacheKey = `${_carId}_${_brandId}_carlist`;
let _carList = uni.getStorageSync(cacheKey); let _carList = uni.getStorageSync(cacheKey);
// 尝试从缓存加载车系数据
if (_carList != null && _carList != "" && _carList != "null") { if (_carList != null && _carList != "" && _carList != "null") {
_carList = typeof _carList === "string" ? JSON.parse(_carList) as Array<any> : _carList as Array<any>; _carList = typeof _carList === "string" ? JSON.parse(_carList) as Array<any> : _carList as Array<any>;
clickFlga.value = false; currentCarList.value = _carList;
carlist.value = _carList; // 切换到车系视图
currentView.value = "series";
if (auto) {
if (carId != null) index = _carList.findIndex((i :any) => (i as UTSJSONObject).id == carId);
if (index < 0) index = 0;
handleSelect(_carList[index]);
} else {
(carBoxVisible.value as ComponentPublicInstance).$callMethod("open");
}
} else { } else {
if (!auto) uni.showLoading({ // 从 API 获取车系数据
title: "",
mask: true
});
}
getCarBrandsListApi({ brandId: _brandId, childBrandId: _carId }).then(res => { getCarBrandsListApi({ brandId: _brandId, childBrandId: _carId }).then(res => {
uni.hideLoading();
const typedRes = res as UTSArray<any>; const typedRes = res as UTSArray<any>;
carlist.value = typedRes; currentCarList.value = typedRes;
if (typedRes.length > 0) uni.setStorageSync(cacheKey, typedRes);
if (carlist.value != null) { if (typedRes.length > 0) {
if (auto && typedRes.length > 0) { uni.setStorageSync(cacheKey, typedRes);
if (carId != null) index = typedRes.findIndex((i: any) => (i as UTSJSONObject).id == carId);
if (index < 0) index = 0;
handleSelect(typedRes[index]);
} else {
(carBoxVisible.value as ComponentPublicInstance).$callMethod("open");
}
} }
clickFlga.value = false; // 切换到车系视图
currentView.value = "series";
}).catch(error => { }).catch(error => {
clickFlga.value = false; console.error("获取车系列表失败:", error);
uni.hideLoading();
}); });
}
} }
watch(watchStatus.value, () => { watch(watchStatus.value, () => {
...@@ -258,7 +283,6 @@ watch(updateView.value, ()=>{ ...@@ -258,7 +283,6 @@ watch(updateView.value, ()=>{
function autoSelect(): void { function autoSelect(): void {
const boutiqueDefaultCarBrandIdVal = boutiqueDefaultCarBrandId.value; const boutiqueDefaultCarBrandIdVal = boutiqueDefaultCarBrandId.value;
const defaultCarIdVal = defaultCarId.value as String;
let carIndex = 0; let carIndex = 0;
if (boutiqueDefaultCarBrandIdVal != null) { if (boutiqueDefaultCarBrandIdVal != null) {
...@@ -268,7 +292,7 @@ function autoSelect(): void { ...@@ -268,7 +292,7 @@ function autoSelect(): void {
if (carIndex < 0) carIndex = 0; if (carIndex < 0) carIndex = 0;
if (carBrandList.value.length > 0) { if (carBrandList.value.length > 0) {
selectCar(carBrandList.value[carIndex], true, defaultCarIdVal); selectBrand(carBrandList.value[carIndex], carIndex);
} }
} }
...@@ -330,7 +354,7 @@ function open(): void { ...@@ -330,7 +354,7 @@ function open(): void {
}); });
if (foundIndex != -1 && carBrandListVal.length == 1) { if (foundIndex != -1 && carBrandListVal.length == 1) {
selectCar(carBrandListVal[foundIndex], false, ""); selectBrand(carBrandListVal[foundIndex], foundIndex);
} else { } else {
if (carBrandListVisible.value != null) { if (carBrandListVisible.value != null) {
(carBrandListVisible.value as ComponentPublicInstance).$callMethod("open"); (carBrandListVisible.value as ComponentPublicInstance).$callMethod("open");
...@@ -355,15 +379,18 @@ defineExpose({ open, close, autoSelect }); ...@@ -355,15 +379,18 @@ defineExpose({ open, close, autoSelect });
<style scoped lang='scss'> <style scoped lang='scss'>
.select-box { .select-box {
margin: 20px 0; margin: 20px 0;
display: flex;
flex-wrap: wrap;
overflow-y: scroll; overflow-y: scroll;
max-height: 400px; max-height: 400px;
padding-bottom: 30px; padding-bottom: 30px;
flex-direction: row; position: relative;
.view-content {
display: flex;
flex-wrap: wrap;
width: 100%;
.select-item { .select-item {
width: 48%; width: 48%;
/* width: 23.5%; */
margin-right: 2%; margin-right: 2%;
margin-bottom: 16px; margin-bottom: 16px;
height: 34px; height: 34px;
...@@ -390,4 +417,37 @@ defineExpose({ open, close, autoSelect }); ...@@ -390,4 +417,37 @@ defineExpose({ open, close, autoSelect });
} }
} }
} }
// 淡入 + 从左滑入动画(品牌列表)
.fade-in-left {
animation: fadeInLeft 0.3s ease-out forwards;
}
// 淡入 + 从右滑入动画(车系列表)
.fade-in-right {
animation: fadeInRight 0.3s ease-out forwards;
}
}
@keyframes fadeInLeft {
from {
opacity: 0;
transform: translateX(-20px);
}
to {
opacity: 1;
transform: translateX(0);
}
}
@keyframes fadeInRight {
from {
opacity: 0;
transform: translateX(20px);
}
to {
opacity: 1;
transform: translateX(0);
}
}
</style> </style>
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
:max-height="1000" :max-height="1000"
position="bottom" position="bottom"
:title="`请选择${title}`" :title="`请选择${title}`"
:z-index="10076"
@close="handleClose"> @close="handleClose">
<view class="select-box"> <view class="select-box">
<template v-for="(item, index) in employeeList" :key="index"> <template v-for="(item, index) in employeeList" :key="index">
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
:is-footer="false" :is-footer="false"
position="center" position="center"
:title="step == 1 ? '客户信息完善' : '结果反馈'" :title="step == 1 ? '客户信息完善' : '结果反馈'"
:z-index="10075"
@close="onMessageBoxClose"> @close="onMessageBoxClose">
<view class="popup-content"> <view class="popup-content">
<!-- 内容区域 --> <!-- 内容区域 -->
......
<template> <template>
<un-popup :mode="position" <un-popup :mode="position"
:show="visible"> :show="visible"
:z-index="zIndex">
<view class="tips-wrapper" <view class="tips-wrapper"
:class="[{'radius': position === 'center'}]" :class="[{'radius': position === 'center'}]"
:style="popupStyle"> :style="popupStyle">
...@@ -15,8 +16,10 @@ ...@@ -15,8 +16,10 @@
</view> </view>
<view class="tips-title"> <view class="tips-title">
<template v-if="back"> <template v-if="back">
<view class="sprite-icon icon-back" <view class="icon-back"
@click="handleHide"> :style="{ display: 'flex', alignItems: 'center', justifyContent: 'center' }"
@click="handleBack">
<un-icon name="arrow-left" size="20"></un-icon>
</view> </view>
</template> </template>
{{ title }} {{ title }}
...@@ -93,6 +96,10 @@ const props = defineProps({ ...@@ -93,6 +96,10 @@ const props = defineProps({
showCancelBtn: { showCancelBtn: {
type: Boolean, type: Boolean,
default: true default: true
},
zIndex: {
type: Number,
default: 10_075
} }
}); });
...@@ -119,6 +126,9 @@ function close(): void { ...@@ -119,6 +126,9 @@ function close(): void {
function hide(): void { function hide(): void {
visible.value = false; visible.value = false;
} }
function handleBack(): void {
emit("close");
}
function open(): void { function open(): void {
visible.value = true; visible.value = true;
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!