selectVehicleBrand.vue 7.16 KB
<template>
    <div>
        <van-index-bar :index-list="indexList">
            <template v-for="(letter) in indexList" :key="letter">
                <van-index-anchor class="index-anchor" :index="letter">{{ letter }}</van-index-anchor>
                <div v-for="(brand, index) in getBrandsByLetter(letter)" :key="brand.id">
                    <van-cell>
                        <template slot="title">
                            <div class="brand-cell" @click="bindClick(brand)">
                                <image class="brand-image" :src="brand.logoUrl" mode="aspectFill" />
                                {{ brand.name }}
                                <div class="check-icon">
                                    <image v-if="!brand.selected"
                                        src="https://img02.glsx.com.cn/aus/images/preference/selectFalse.png"
                                        class="check-icon-image" />
                                    <image v-else src="https://img02.glsx.com.cn/aus/images/preference/selectTrue.png"
                                        class="check-icon-image" />
                                </div>
                            </div>
                        </template>
                    </van-cell>
                </div>
            </template>
        </van-index-bar>
        <div class="bottom-height-100"></div>
        <div class="bottom-fixed">
            <div class="width-calc-36">
                <div class="plainBtn" @click="handleCancel()">取消</div>
                <div class="submitBut but2" :class="{ 'disabled': selectedBrand.length }" @click="handleSubmit()">
                    确定(已选{{ selectedBrand.length }}个)
                </div>
            </div>
        </div>
    </div>
</template>
<script lang="ts">
import { Component, Ref, Vue } from "vue-facing-decorator";
import { onLoad, onShow } from "@dcloudio/uni-app";
import { GscApiController } from "@/api/GscApiController";


type Brand = {
    id: string;
    letter: string,
    name: string;
    logoUrl: string;
    childs: Brand[];
    selected: boolean;
}

@Component({
    components: {}
})

export default class selectVehicleBrand extends Vue {

    brands: Brand[] = [];
    selectedBrand: Brand[] = [];

    indexList: string[] = [
        'A', 'B', 'C', 'D', 'E', 'F', 'G',
        'H', 'I', 'J', 'K', 'L', 'M', 'N',
        'O', 'P', 'Q', 'R', 'S', 'T',
        'U', 'V', 'W', 'X', 'Y', 'Z'
    ];

    @Ref
    popup: any;

    created(): void {
        onLoad((option) => {
            this.loadData();
        });
        onShow(options => {
        });
    }

    //获取品牌列表数据
    loadData(): void {
        uni.showLoading({ title: '正在加载中...', mask: true });
        const cacheBrands = uni.getStorageSync('cacheBrands');
        if (cacheBrands && cacheBrands.length > 0) {
            this.brands = cacheBrands;
        } else {
            GscApiController.getCarBrandComplete(this.axios).then(res => {
                const brandRes: any = res.data;
                this.brands = brandRes.data.carinfos;
                uni.setStorageSync('cacheBrands', this.brands);
            });
        }

        //默认回显已经选中的品牌
        const storagVehicleBrandList = JSON.parse(uni.getStorageSync('storagVehicleBrandList') || '[]');
        this.brands = this.brands.map(item => {
            const brandObj = storagVehicleBrandList.find((brand: { id: string; }) => brand.id === item.id) || {};
            item.selected = brandObj.id ? true : false;
            return item;
        });
        this.selectedBrand = this.brands.filter(brand => brand.selected);
        setTimeout(() => {
            uni.hideLoading();
        }, 600);
    }

    getBrandsByLetter(letter: string): Brand[] {
        return this.brands.filter(brand => brand.letter === letter);
    }

    bindClick(brand: any) {
        // 找到品牌在原始数组中的索引
        const brandIndex = this.brands.findIndex(item => item.id === brand.id);
        // 如果品牌已选中,则直接取消选择
        if (this.brands[brandIndex].selected) {
            this.brands[brandIndex].selected = false;
        } else {
            // 检查是否超过最大选择数量
            if (this.selectedBrand.length >= 10) {
                uni.showToast({
                    title: '最多可选10个品牌',
                    icon: 'none'
                });
                return;
            }
            this.brands[brandIndex].selected = true;
        }
        // 更新选中品牌列表
        this.selectedBrand = this.brands.filter(brand => brand.selected);
    }

    handleSubmit() {
        const that = this;
        if (!that.selectedBrand.length) {
            return console.log('请选择品牌');
        }
        console.log('onReturnData', that.selectedBrand);
        const eventChannel = this.getOpenerEventChannel();
        eventChannel.emit('onReturnData', { data: that.selectedBrand });
        uni.navigateBack();
    }

    handleCancel(): void {
        uni.navigateBack();
    }
}
</script>

<style lang="scss" scoped>
.bottom-fixed {
    width: 100%;
    background: #ffffff;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 80px;
}

.width-calc-36 {
    width: -webkit-calc(100% - 36px);
    width: -moz-calc(100% - 36px);
    width: calc(100% - 36px);
    float: left;
    margin: 8px 18px 0;
}

.submitBut {
    height: 48px;
    line-height: 48px;
    border-radius: 48px;
    background: #185efe80;
    float: left;
    text-align: center;
    color: #ffffff;
    font-size: 16px;
    cursor: pointer;
}

.but2 {
    width: -webkit-calc(62% - 16px);
    width: -moz-calc(62% - 16px);
    width: calc(62% - 16px);
    margin-left: 16px;
}

.disabled {
    background: #185efe !important;
    color: #ffffff !important;
}

.plainBtn {
    width: 38%;
    height: 48px;
    line-height: 48px;
    border-radius: 48px;
    background: #f8f9fc;
    float: left;
    text-align: center;
    color: #333333;
    font-size: 16px;
    cursor: pointer;
}

.brands-container {
    height: 100vh;
    background-color: #f5f5f5;
}

.brand-cell {
    display: flex;
    align-items: center;
    gap: 8px;
    position: relative;
    color: #292d33;
    font-size: 16px;
    font-weight: 400;
    font-family: "HarmonyOSSansSC";
}

.check-icon {
    position: absolute;
    right: 40px;
    top: 50%;
    transform: translateY(-50%);
    width: 16px;
    height: 16px;
}

.check-icon-image {
    width: 16px;
    height: 16px;
}

.brand-image {
    width: 32px;
    height: 32px;
    margin-right: 12px;
}

.popup-content {
    width: 260px;
}

.scroll-content {
    height: calc(100vh - 120px);
}


page {

    .title-class {
        flex: none !important;
    }

    .value-class {
        //flex: none !important;
    }

}

.bottom-height-100 {
    width: 100%;
    height: 100px;
}
</style>

<style>
.van-index-anchor {
    background: #F7F8FA !important;
}

.van-index-bar__index--active,
.van-index-bar__index:active {
    color: #ffffff !important;
    background-color: #000000 !important;
    border-radius: 4px;
}

.van-index-anchor--active {
    color: #757b85 !important;
    font-size: 12px;
    font-weight: 常规体;
    font-family: "苹方-简";
}
</style>