add.vue 6.14 KB
<template>
    <div class="category-ctnr">
        <div class="main-wrap">
            <div class="global-title">新增二级分类</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="mainCategorySlot">
                    {{routeInfo.parentCategoryName}}
                </div>
                <div slot="secondCategorySlot" class="category-slot">
                    <div class="category-item" v-for="(item,index) in categoryList" :key="index">
                        <el-input placeholder="请输入" v-model="item.name" class="input"  maxlength="20" show-word-limit></el-input>
                        <i v-if="categoryList.length>1" class="el-icon-delete del-icon" @click="delSecondCategory(index)"></i>
                    </div>
                    <div v-if="!routeInfo.id" class="add-btn" @click="addCategory()">
                        <i class="el-icon-circle-plus-outline"></i>
                        新增二级分类
                    </div>
                </div>
            </glsx-combi-form>
        </div>
        <GLFooter>
            <el-button @click="handleCancel">取消</el-button>
            <el-button type="primary" @click="handleSave">确定</el-button>
        </GLFooter>
    </div>
</template>
<script>
import glsxCombiForm from '@/components/glsx-combi-form'
export default {
    name: 'add',
    components: {
        glsxCombiForm
    },
    data() {
        return {
            formObj:{
                brandType: 1,
                videoCoverArr: []
            },
            myRule:{
                mainCategory: [{ required: true,validator: (rule, value, callback) => {
                    if(!this.routeInfo.parentCategoryId) {
                        callback(new Error(`缺失一级分类ID`))
                    }else if(!this.routeInfo.categoryType){
                        callback(new Error(`缺失频道ID`))
                    }else{
                        callback()
                    }
                }, trigger: 'change' }],
                secondCategory: [{ required: true, validator: (rule, value, callback) => {
                    const _target = this.categoryList.find(item => !item.name)
                    if(_target) {
                        callback(new Error(`请输入二级分类`))
                    }else{
                        callback()
                    }
                }, trigger: 'change' }],
            },
            formData:[
                {
                    label: '一级分类',
                    name: 'mainCategory',
                    addHtml: 'mainCategorySlot'
                },
                {
                    label: '二级分类名称',
                    name: 'secondCategory',
                    addHtml: 'secondCategorySlot'
                }
            ],
            categoryList: []
        }
    },
    created(){
        const urlParam = this.$route.params
        this.routeInfo = JSON.parse(decodeURIComponent(urlParam.id))
        const { id, name } = this.routeInfo
        this.categoryList = [{id: id || '', name: name || ''}]
    },
    methods:{
        addCategory(){
            const _target = this.categoryList.find(item => !item.name)
            if(_target) return this.$message.error('请输入二级分类')
            this.categoryList.push({id: '', name: ''})
        },
        delSecondCategory(index){
            this.categoryList.splice(index, 1)
        },
        handleSave(){
            this.$refs.baseInfoRef.$refs.formRef.validate(valid => {
                if(valid){
                    if(this.routeInfo.id){
                        this.editCategory()
                    }else{
                        this.batchAddCategory()
                    }
                }
            })
        },
        batchAddCategory(){
            const { categoryType, parentCategoryId } = this.routeInfo
            const params = {
                categoryType, 
                parentCategoryId,
                categoryNameList: this.categoryList.map(item => item.name)
            }
            this.$apis.category.batchAddCategoryApi(params).then(res => {
                if(res) {
                    this.$message.success('操作成功')
                    this.handleCancel()
                }
            })
        },
        editCategory(){
            const { categoryType, parentCategoryId, id } = this.routeInfo
            const params = {
                id,
                categoryType,
                parentCategoryId,
                categoryLevel: 2,
                categoryName: this.categoryList[0]?.name
            }
            this.$apis.category.addCategoryApi(params).then(res => {
                if(res){
                    this.$message.success('编辑成功')
                    this.handleCancel()
                }
            })
        },
        handleCancel(){
            this.$router.back()
        },
    }
}
</script>
<style scoped lang="scss">
.category-ctnr{
    min-height: 500px;
    height: calc(100vh - 140px);
    .main-wrap{
        height: 100%;
        background-color: #fff;
        margin-bottom: 16px;
        .glsx-combi-form{
            padding: 20px;
            background-color: #fff;
            margin-bottom: 16px;
            .category-slot{
                .category-item{
                    margin-top: 10px;
                    &:first-child{
                        margin-top: 0;
                    }
                    .el-input{
                        width: 432px;
                    }
                    .del-icon{
                        font-size: 16px;
                        color: #999999;
                        margin-left: 8px;
                        vertical-align: middle;
                    }
                }
                .add-btn{
                    display: inline-block;
                    color: #5850FF;
                    cursor: pointer;
                    i{
                        font-size: 20px;
                        vertical-align: middle;
                    }
                }
            }
        }
    }
}
</style>