indexPage.vue 8.73 KB
<template>
    <customNav :status-bar-height="statusBarHeight"></customNav>
    <view class="page">
        <!-- 进度条 -->
        <view class="stepBox" :style="{ paddingTop: statusBarHeight + 56 + 'px' }">
            <progressBox ref="progressBoxRef" />
        </view>
        <!-- 内容区域 -->
        <view class="contentBox">
            <stepOne ref="stepOneRef" v-show="stepActive == 'ONE'" @oneData="stepEmitOneData" />
            <stepTwo ref="stepTwoRef" v-show="stepActive == 'TWO'" @twoData="stepEmitTwoData" />
            <stepThree ref="stepThreeRef" v-show="stepActive == 'THREE'" @threeData="stepEmitThreeData" />
        </view>
        <!--底部提交按钮-->
        <view class="bottom-fixed">
            <view v-if="stepActive == 'ONE'" class="submitBut but1" @click="nextStep('TWO')"
                :class="{ 'disabled': pageData.businessModel && pageData.cityCode }">下一步</view>
            <view v-if="stepActive == 'TWO'" class="width-calc-36">
                <view class="plainBtn" @click="upStep('ONE')">上一步</view>
                <view class="submitBut but2"
                    :class="{ 'disabled': pageData.vehicleCondition && pageData.vehicleAge && pageData.vehicleFuelType && pageData.vehicleType }"
                    @click="nextStep('THREE')">下一步</view>
            </view>
            <view v-if="stepActive == 'THREE'" class="width-calc-36">
                <view class="plainBtn" @click="upStep('TWO')">上一步</view>
                <view class="submitBut disabled but2" @click="handleSubmit(1)">
                    保存偏好
                </view>
            </view>
        </view>
    </view>
</template>

<script lang="ts">
import { Component, Vue } from 'vue-facing-decorator';
import { onLoad } from "@dcloudio/uni-app";
import stepOne from "@/module/preference/components/step-one.vue";
import stepTwo from "@/module/preference/components/step-two.vue";
import stepThree from "@/module/preference/components/step-three.vue";
import { PreferenceController } from "@/api/PreferenceController";
import customNav from "@/module/preference/components/customNav.vue";
import progressBox from "@/module/preference/components/progressBox.vue";


@Component({ components: { stepOne, stepTwo, stepThree, customNav, progressBox } })

export default class indexPage extends Vue {
    statusBarHeight = 0;// 用于接收头部总高度,设置内容区 padding
    //页面进度
    stepActive = 'ONE';//ONE,TWO,THREE
    //页面提交数据
    pageData = {
        businessModel: '1',
        cityCode: '',
        cityName: '',
        cityCn: '',
        vehicleCondition: '1',
        vehicleAge: '1',
        vehicleFuelType: '1',
        vehicleType: '1',
        brandList: [] as string[],
    }
    oldPageData = {}
    created(): void {
        onLoad((option) => {
            this.statusBarHeight = uni.getWindowInfo().statusBarHeight;
            this.getPreferenceDetailsData();
        });
    }

    //获取详情数据
    getPreferenceDetailsData() {
        PreferenceController.getPreferenceDetails(this.axios, { isIndexPage: false }).then((response: any) => {
            const data = response.data || {};
            if (data.success) {
                const itemData = data.data || {};
                this.oldPageData = itemData;
                this.pageData.businessModel = itemData.businessModel || '1';
                this.pageData.cityCode = itemData.cityCode || '';
                this.pageData.cityName = itemData.cityName || '';
                this.pageData.cityCn = itemData.cityCn || '';
                this.pageData.vehicleCondition = itemData.vehicleCondition || '1';
                this.pageData.vehicleAge = itemData.vehicleAge || '1';
                this.pageData.vehicleFuelType = itemData.vehicleFuelType || '1';
                this.pageData.vehicleType = itemData.vehicleType || '1';
                this.pageData.brandList = JSON.parse(itemData.brandList || '[]');
                this.$refs.stepOneRef.initData(this.pageData);
                this.$refs.stepTwoRef.initData(this.pageData);
                this.$refs.stepThreeRef.initData(this.pageData);
            }
        });
    }
    //下一步
    nextStep(value: string) {
        if (value === 'TWO') {
            const isValid = this.pageData?.businessModel && this.pageData?.cityCode;
            if (!isValid) return false;
        } else if (value === 'THREE') {
            const isValid = this.pageData?.vehicleCondition &&
                this.pageData?.vehicleAge &&
                this.pageData?.vehicleFuelType &&
                this.pageData?.vehicleType;
            if (!isValid) return false;
        }
        this.handleSubmit(0)
        this.stepActive = value;
        this.$refs.progressBoxRef.initData({ stepActive: value });
    }

    //上一步
    upStep(value: string) {
        this.stepActive = value;
        this.$refs.progressBoxRef.initData({ stepActive: value });
    }

    //保存 status 0 提交 1 完成
    handleSubmit(status: number) {
        let postData = {}
        if (this.stepActive == 'ONE') {
            postData = {
                ...this.pageData,
                vehicleCondition: this.oldPageData.vehicleCondition || '',
                vehicleAge: this.oldPageData.vehicleAge || '',
                vehicleFuelType: this.oldPageData.vehicleFuelType || '',
                vehicleType: this.oldPageData.vehicleType || '',
                brandList: JSON.stringify(this.oldPageData.brandList || []),
                status: status,
            }
        } else if (this.stepActive == 'TWO') {
            postData = {
                ...this.pageData,
                brandList: JSON.stringify(this.oldPageData.brandList || []),
                status: status,
            }
        } else {
            postData = {
                ...this.pageData,
                status: status,
                brandList: JSON.stringify(this.pageData.brandList)
            };
        }
        uni.showLoading({ title: '保存中...', mask: true });
        PreferenceController.savePreference(this.axios, postData).then((response: any) => {
            const data = response.data || {};
            if (data.success && status == 1) {
                setTimeout(() => {
                    uni.showToast({ title: '恭喜你填写完成,将为您推荐精选车辆', icon: 'none' });
                }, 1200);
                uni.switchTab({ url: `/pages/tabbar/HomePage` });
            }
        }).finally(() => {
            uni.hideLoading();
        });
    }

    stepEmitOneData(data: any) {
        this.pageData.businessModel = data.businessModel || '';
        this.pageData.cityCode = data.cityCode || '';
        this.pageData.cityName = data.cityName || '';
        this.pageData.cityCn = data.cityCn || '';
        console.log('stepEmitData', this.pageData);
    }

    stepEmitTwoData(data: any) {
        this.pageData.vehicleCondition = data.vehicleCondition || '';
        this.pageData.vehicleAge = data.vehicleAge || '';
        this.pageData.vehicleFuelType = data.vehicleFuelType || '';
        this.pageData.vehicleType = data.vehicleType || '';
        console.log('stepEmitData', this.pageData);
    }

    stepEmitThreeData(data: any) {
        this.pageData.brandList = (data.brandList || []).map((item: any) => ({ name: item.name || '', id: item.id || '' }));
        console.log('stepEmitData', this.pageData);
    }
}
</script>

<style scoped lang="scss">
.page {
    width: 100vw;
    height: 100vh;
    background-color: #f8f9fb;
}

.stepBox {
    width: 100%;
    height: 74px;
    float: left;
    background: #f7f9fc;
}

.step-image {
    width: 100%;
    height: 57px;
    z-index: -1;
    margin: 0 auto;
}

.contentBox {
    width: 100%;
    float: left;
}

.bottom-fixed {
    width: 100%;
    background: #ffffff;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 80px;
}

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

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

.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;
}

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