add.vue 8.61 KB
<template>
    <div class="case-ctnr">
        <div class="main-wrap">
            <div class="global-title">{{formObj.id ? '编辑案例': '新增案例'}}</div>
            <glsx-combi-form :isInline="false" class="glsx-combi-form" ref="baseInfoRef" labelWidth="154px" defaultWidth="432px" :formObj.sync="formObj" :formData="formData" :formRules="myRule">
                <div slot="videoCoverSlot">
                    <glsx-preview :picOptions="posterOptions" :imageUrlArr="formObj.videoCoverArr" @change="changeChannelImg"></glsx-preview>
                    <div class="text-tip" style="margin: 0;">建议尺寸:272*364</div>
                </div>
                <div slot="selectBrandSlot">
                    <el-radio-group v-model="formObj.applyBrandType">
                        <el-radio :label="0">全部品牌</el-radio>
                        <el-radio :label="1">部分品牌</el-radio>
                    </el-radio-group>
                    <div v-if="formObj.applyBrandType == 1">
                        <el-button class="el-icon-plus"  size="small" @click="openBrandDialog">选择品牌</el-button>
                        <div class="brand-wrap" v-if="brandList && brandList.length">
                            <div class="brand-item" v-for="item in brandList" :key="item.id">{{ item.name }}</div>
                        </div>
                    </div>
                </div>
            </glsx-combi-form>
        </div>
        <GLFooter>
            <el-button @click="handleCancel">取消</el-button>
            <el-button type="primary" @click="handleSave">确定</el-button>
        </GLFooter>
        <selectCarSeriesDialog v-if="dialogVisible" :listData="brandList" @confirmDialog="confirmDialog" @closeDialog="openBrandDialog" :dialogVisible="dialogVisible"></selectCarSeriesDialog>
    </div>
</template>
<script>
import glsxCombiForm from '@/components/glsx-combi-form'
import selectCarSeriesDialog from './components/selectCarSeriesDialog'

export default {
    name: 'add',
    components: {
        glsxCombiForm,
        selectCarSeriesDialog
    },
    data() {
        return {
            dialogVisible: false,
            formObj:{
                id: '',
                mediaId: '',
                caseName: '',
                coverUrl: '',
                introduction: '',
                applyBrandType: 0,
                videoCoverArr: []
            },
            brandList: [],
            posterOptions: {
                width: '104px',
                height: '138px',
                key: 'posterImg',
                isClose: true
            },
            myRule:{
                caseName: [{ required: true, message: '请输入案例名称', trigger: 'change' }],
                mediaId: [{ required: true, message: '请选择视频', trigger: 'change' }],
                coverUrl: [{ required: true, validator: (rule, value, callback) => {
                    if(this.formObj.videoCoverArr?.length!=1) {
                        callback(new Error(`请上传视频封面图`))
                    }else{
                        callback()
                    }
                }, trigger: 'change' }],
                introduction: [{ required: true, message: '请输入宣传文案', trigger: 'change' }],
                brand: [{ required: true, validator: (rule, value, callback) => {
                    if(this.formObj.applyBrandType==1 && !this.brandList?.length) {
                        callback(new Error(`请选择品牌`))
                    }else{
                        callback()
                    }
                }, trigger: 'blur' }],
            },
            formData:[
                {
                    label: '案例名称',
                    name: 'caseName',
                    maxlength: 20,
                    showLimit: true,
                    type: 'input',
                    isDisabled: false
                },
                {
                    label: '选择视频',
                    name: 'mediaId',
                    type: 'select',
                    optionList:[],
                    optionId: 'id',
                    optionLabel: 'fileName',
                    isDisabled: false,
                    changeAttr:(id, row)=> {
                        const _target = row.optionList.find(item => item.id == id)
                        if(_target){
                            this.formObj.videoCoverArr = [_target.coverUrl]
                            this.formObj.introduction = _target.introduction
                        }
                        this.$refs.baseInfoRef.$refs.formRef.validateField('coverUrl')
                    }
                },
                {
                    label: '视频封面图',
                    name: 'coverUrl',
                    addHtml: 'videoCoverSlot'
                },
                {
                    label: '宣传文案',
                    placeholder: '发现频道视频宣传语',
                    name: 'introduction',
                    maxlength: 100,
                    type: 'input',
                    showLimit: true,
                    inputType: 'textarea',
                    isDisabled: false
                },
                {
                    label: '适用品牌',
                    name: 'brand',
                    addHtml: 'selectBrandSlot'
                }
            ],
        }
    },
    created(){
        this.formObj.id = this.$route.query.id || ''
        this.getVideoList()
        if(this.formObj.id){
            this.getCaseDetailApi()
        }
    },  
    methods:{
        openBrandDialog(){
            this.dialogVisible = !this.dialogVisible
        },
        getVideoList(){
            this.$apis.case.getVideoListApi({pageSize: 10000, currentPage: 1, status: 1}).then(res => {
                if(res?.list?.length) {
                    const _target = this.formData.find(item => item.name == 'mediaId') 
                    _target.optionList = res.list
                }
            })
        }, 
        changeChannelImg(val){
            this.$refs.baseInfoRef.$refs.formRef.validateField('coverUrl')
        },
        handleSave(){
            this.$refs.baseInfoRef.$refs.formRef.validate(valid => {
                if(valid){
                    let params = JSON.parse(JSON.stringify(this.formObj))
                    params.coverUrl = this.formObj.videoCoverArr[0]
                    if(params.applyBrandType == 1){
                        params.brandList = this.brandList.map(item => {
                            return { brandId: item.id , brandName: item.name}
                        })
                    }
                    delete params.videoCoverArr
                    if(!params.id) delete params.id
                    this.$apis.case.addCaseApi(params).then(res => {
                        console.log(res)
                        if(res){
                            this.$message.success('操作成功')
                            this.handleCancel()
                        }
                    })
                }
            })
        },
        handleCancel(){
            this.$router.back()
        },
        confirmDialog(e){
            this.brandList  = e
            this.$refs.baseInfoRef.$refs.formRef.validateField('brand')
            this.openBrandDialog()
        },
        getCaseDetailApi(){
            this.$apis.case.getCaseDetailApi({id: this.$route.query.id}).then(res => {
                console.log(res)
                if(res){
                    const attrList = ['id', 'mediaId', 'caseName', 'introduction', 'applyBrandType']
                    attrList.forEach(key =>{
                        this.formObj[key] = res[key]
                    })
                    this.formObj.videoCoverArr = [res.coverUrl]
                    this.brandList = res.brandList.map(item => ({id: item.brandId, name: item.brandName})) || []
                }
            })
        }
    }
}
</script>
<style scoped lang="scss">
.case-ctnr{
    .glsx-combi-form{
        padding: 20px;
        background-color: #fff;
        margin-bottom: 16px;
        .brand-wrap{
            margin-top: 8px;
            width: 580px;
            height: 80px;
            background: #FFFFFF;
            border: 1px solid #E8E8EA;
            border-radius: 4px;
            padding: 6px 8px;
            overflow-y: auto;
            .brand-item{
                display: inline-block;
                padding: 0 8px;
                height: 24px;
                color: #666666;
                line-height: 24px;
                background: #FAFBFE;
                border: 1px solid #E5E5E5;
                border-radius: 4px;
                margin-right: 4px;
            }
        }
    }
}
</style>