add.vue 6.14 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;">建议尺寸:120*120</div>
        </div>
        <div slot="selectBrandSlot" class="mode-slot">
          <div class="mode-item" v-for="item in modeList" :key="item.id">
            <el-radio v-model="formObj.categoryStyle" :label="item.id">{{item.name}}</el-radio>
            <div>
              <el-popover
                popper-class="tool-gltip"
                placement="top-start"
                width="200"
                trigger="hover">
              <img :src="require(`@/assets/category/${item.url}.png`)" style="width:300px;height:300px;"/>
                <img slot="reference" :src="require(`@/assets/category/${item.url}.png`)">
              </el-popover>
            </div>
          </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'
const MODE_LIST = [
  {
    id: 0,
    url: 'vertical',
    name: '商品-竖向排列'
  },
  {
    id: 1,
    url: 'transverse',
    name: '商品-横向排列'
  },
  {
    id: 2,
    url: 'recommend',
    name: '种草样式A'
  },
  {
    id: 3,
    url: 'recommend2',
    name: '种草样式B'
  }
]
export default {
  name: 'add',
  components: {
    glsxCombiForm,
  },
  data() {
    return {
      dialogVisible: false,
      modeList: MODE_LIST,
      formObj: {
        id: '',
        categoryType: 1,
        categoryName: '',
        categoryStyle: 0,
        videoCoverArr: []
      },
      brandList: [],
      posterOptions: {
        width: '120px',
        height: '120px',
        key: 'posterImg',
        isClose: true
      },
      myRule: {
        categoryName: [{required: true, message: '请输入分类名称', trigger: 'change'}],
        categoryImage: [{
          required: true, validator: (rule, value, callback) => {
            if (this.formObj.videoCoverArr?.length != 1) {
              callback(new Error(`请上传分类图标`))
            } else {
              callback()
            }
          }, trigger: 'change'
        }],
        categoryStyle: [{
          required: true, validator: (rule, value, callback) => {
            if (this.formObj.brandType == 'null') {
              callback(new Error(`请选择布局样式`))
            } else {
              callback()
            }
          }, trigger: 'blur'
        }],
      },
      formData: [
        {
          label: '分类名称',
          name: 'categoryName',
          maxlength: 20,
          showLimit: true,
          type: 'input',
          isDisabled: false
        },
        {
          label: '分类归属',
          name: 'categoryType', 
          type: 'radio',
          optionList: [
            {
              label: '配置升级',
              id: 1
            },
            {
              label: '车主优选',
              id: 2
            },
          ],
          optionId: 'id',
          optionLabel: 'label'
        },
        {
          label: '分类图标',
          name: 'categoryImage',
          addHtml: 'videoCoverSlot'
        },
        {
          label: '布局样式',
          name: 'categoryStyle',
          addHtml: 'selectBrandSlot'
        }
      ],
    }
  },
  created() {
    this.formObj.id = this.$route.query.id || ''
    if (this.formObj.id) {
      this.getCaseDetailApi()
    }
  },
  methods: {
    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.categoryLevel = 1 //分类等级(1:一级分类,2:二级分类)
          params.categoryImage = this.formObj.videoCoverArr[0]
          delete params.videoCoverArr
          if (!params.id) delete params.id
          this.$apis.category.addCategoryApi(params).then(res => {
            if (res) {
              this.$message.success('操作成功')
              this.handleCancel()
            }
          })
        }
      })
    },
    handleCancel() {
      this.$route.query.name = '2';
      this.$router.back()
    },

    getCaseDetailApi() {
      this.$apis.category.batchDetailApi({id: this.$route.query.id}).then(res => {
        if (res) {
          const attrList = ['id', 'categoryName', 'categoryStyle', 'categoryType']
          attrList.forEach(key => {
            this.formObj[key] = res[key]
          })
          this.formObj.videoCoverArr = [res.categoryImage]
        }
      })
    }
  }
}
</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;
      }
    }
  }
}
.mode-slot{
  display: flex;
  .mode-item{
    width: 145px;
    img{
      width: 103.97px;
      height: 86.5px;
    }
  }
}
.category-style {
  img {
    width: 104px;
    height: 102.6px;

    margin-right: 65px;
  }
}
</style>
<style>
.tool-gltip {
  max-width: 340px!important;
  width: 340px!important;
}
</style>