sourceMaterial.vue 10 KB
<template>
  <div class="sourceMaterial-form" id="sourceMaterial-form">
    <div class="title">产品系列-SPU</div>
    <productForm v-if="show"
                 ref="d2GoodsProductForm"
                 :add-or-edit="addOrEdit"
                 form-name="product"
                 :origin-data="editData"
                 :form-list="editForm"
                 :form-ref="proFormRef"
                 :is-product="false"
                 @handleAddBrandSupplyConfirm="handleAddBrandSupplyConfirm"
                 @handleEditBrandSupplyConfirm="handleEditBrandSupplyConfirm">
    </productForm>
  </div>
</template>
<script>
import { deepClone } from '@/common/util'
import { mapActions } from 'vuex'
import { getBrandSupplyMerchant, addBrandSupplyMerchant, editBrandSupplyMerchant, getProductTypeBySceneId, getAllCategory } from '@/api/product'
import productForm from './productForm.vue'
import { baseConfig } from '@/main'
export default {
  name: 'SourceMaterial',
  data() {
    return {
      userInfo: this.$store.state.d2admin.user.info,
      show: false,
      editData: {
        goodsName: '',
        goodsType: '',
        tempGoodsType: [],
        goodsTypeName: '',
        sapCode: '',
        brandMerchantCode: '',
        brandMerchantName: '',
        supplierCode: '',
        supplierName: '',
        thumbUrl: '',
        goodsImgList: [],
        goodsDescList: [],
        videoUrl: '',
        convertedValue: '',
        positionIds: [],
        carTypeList: [1, 2, 3, 4],
        goodsPositionList: [],
        goodsSource: 0
      },
      proFormRef: 'pro',
      editForm: [{
        formBlock: [
          {
            name: 'goodsName',
            label: '产品名称',
            type: 'input',
            maxlength: 20,
            width: '432px',
            isDisabled: false
          },
          {
            name: 'tempGoodsType',
            label: '类目',
            isRequired: true,
            type: 'cascader',
            isClear: false,
            isDisabled: false,
            optionList: [],
            optionId: 'categoryType',
            optionLabel: 'categoryName',
            width: '432px'
          }, {
            name: 'sapCode',
            label: '编号',
            type: 'input',
            maxlength: 32,
            width: '432px'
          }, {
            name: 'brandMerchantCode',
            label: '品牌方',
            isRequired: true,
            type: 'special-select',
            isClear: true,
            optionList: [],
            optionId: 'id',
            optionLabel: 'relatedPartyName',
            partitionFlag: 1,
            width: '432px'
          }, {
            name: 'supplierCode',
            label: '供应方',
            isRequired: true,
            type: 'select',
            isClear: true,
            optionList: [],
            optionId: 'id',
            optionLabel: 'relatedPartyName',
            partitionFlag: 2,
            width: '432px',
            isDisabled: false
          }, {
            name: 'thumbUrl',
            label: '缩略图',
            type: 'photo',
            tips: ['注:用于小程序主页商品列表配图展示', '请上传10M以内的jpg、jpeg、png图片,建议图片尺寸256*216'],
            maxPhotoNum: 1
          }, {
            name: 'goodsImgList',
            label: '产品主图',
            type: 'photo',
            tips: ['注:用于在小程序商品详情页的顶部展示', '请上传10M以内的jpg、jpeg、png图片,建议图片尺寸256*216'],
            maxPhotoNum: 10
          }, {
            name: 'videoUrl',
            label: '产品视频',
            type: 'video',
            tips: ['注:作为商品详情页顶部展示,格式建议mp4,大小不超过20M'],
            maxPhotoNum: 1
          }, {
            name: 'goodsDescList',
            label: '详情描述',
            type: 'descList',
            maxlength: 256
          }]
      }],
      baseData: {
        productType: []
      },
      selfBuildGoodsType: baseConfig.selfBuildGoodsType || '' // 服务商自建类目值
    }
  },

  components: {
    productForm: productForm
  },

  props: {
    addOrEdit: String,
    formData: Object
  },

  mounted() {
    if (this.addOrEdit === 'edit') {
      this.show = false
      this.editData = deepClone(this.formData)
      this.editData.brandMerchantCode = Number(deepClone(this.formData).brandMerchantCode) || ''
      this.editData.supplierCode = Number(deepClone(this.formData).supplierCode) || ''
      if (!this.editData.goodsDescList) this.editData.goodsDescList = []
      // 太阳膜默认适用全部车型 部位回显
      if (this.editData.goodsType == 1) {
        this.editData.carTypeList = deepClone(this.formData).carTypeList ? deepClone(this.formData).carTypeList : [1, 2, 3, 4]
        this.editData.positionIds = deepClone(this.formData)?.goodsPositionList?.map(item => { return item.position })
      }
      this.editData.goodsImgList = this.editData.goodsImgList || []
      this.editData.goodsImgList.forEach(i => {
        delete i.createdDate
        delete i.updatedDate
      })
      this.editData.goodsDescList.forEach(i => {
        delete i.createdDate
        delete i.updatedDate
      })
      this.$nextTick(() => {
        this.show = true
      })
    } else {
      this.show = true
    }
    // 获取品牌方/供应方数据 1品牌商 2供应方
    this.getBrandSupplyList(1)
    this.getBrandSupplyList(2)
    // 获取类目
    this.getGoodTypeList()
  },
  methods: {
    ...mapActions({
      get: 'd2admin/base/get',
      close: 'd2admin/page/close'
    }),
    // 获取全部类目 屏蔽服务商自建类目
    getGoodTypeList() {
      getAllCategory({ categoryTypeList: this.selfBuildGoodsType, pageSize: 9999, currentPage: 1 }).then(res => {
        const obj = this.editForm[0].formBlock[this.editForm[0].formBlock.findIndex(val => val.name == 'tempGoodsType')]
        obj.optionList = res?.list || []
        // SCRM同步过来商品 或者非草稿支持在当前类目下变更1.2.3级类目,不允许更换一级类目
        if (this.addOrEdit === 'edit' && (this.formData?.status != 3 || this.formData?.scrmFlag == 'Y')) {
          obj.optionList.forEach(item => {
            if (item.categoryType != this.editData.goodsType) {
              item.disabled = true
              item?.children?.forEach(i => {
                i.disabled = true
                i?.children?.forEach(j => {
                  j.disabled = true
                })
              })
            }
          })
        } else {
          const goodsTypeIndex = this.editForm[0]?.formBlock.findIndex(val => val.name === 'tempGoodsType')
          const goodsTypeOptionList = this.editForm[0]?.formBlock[goodsTypeIndex]?.optionList
          // 更新延保类目选项的 disabled 状态
          const updateGoodsTypeOptions = (isDisabled) => {
            const updatedGoodsTypeOptionList = goodsTypeOptionList.map(item => {
              const newItem = { ...item, disabled: isDisabled && this.baseConfig?.extendedPosiType?.includes(item.categoryType) };
              if (newItem.children) {
                newItem.children = newItem.children.map(i => ({
                  ...i,
                  disabled: newItem.disabled,
                  children: i.children ? i.children.map(j => ({ ...j, disabled: newItem.disabled })) : undefined
                }))
              }
              return newItem
            })
            this.$set(this.editForm[0].formBlock[goodsTypeIndex], 'optionList', updatedGoodsTypeOptionList)
          }
          // 根据 supplierCode 更新延保类目选项
          if (this.formData?.supplierCode && !this.baseConfig?.supplierCode?.includes(`${this.formData?.supplierCode}`)) {
            updateGoodsTypeOptions(true)
          } else {
            updateGoodsTypeOptions(false)
          }
        }
        // SCRM同步过来商品字段不可编辑
        if (this.formData?.scrmFlag == 'Y') {
          let disabledLabel = ['goodsName', 'supplierCode']
          for (var i = 0; i < disabledLabel.length; i++) {
            this.editForm[0].formBlock[this.editForm[0].formBlock.findIndex(val => val.name == disabledLabel[i])].isDisabled = true
          }
        }
      })
    },
    // 获取品牌/供应方
    getBrandSupplyList(num) {
      getBrandSupplyMerchant({
        partitionFlag: num
      }).then(res => {
        if (num == 1) {
          this.editForm[0].formBlock[this.editForm[0].formBlock.findIndex(val => val.name == 'brandMerchantCode')].optionList = res
        } else {
          const supplierIndex = this.editForm[0]?.formBlock.findIndex(val => val.name === "supplierCode")
          this.editForm[0].formBlock[supplierIndex].optionList = res
          const suppOptionList = this.editForm[0]?.formBlock[supplierIndex]?.optionList
          // 更新供应方选项的 disabled 状态
          const updateSupplierOptions = (isDisabled) => {
            suppOptionList?.forEach(item => {
              item.isDisabled = isDisabled && !this.baseConfig?.supplierCode?.includes(`${item.id}`)
            })
            this.$set(this.editForm[0].formBlock[supplierIndex], 'optionList', suppOptionList)
          }
          // 根据 goodsType 更新供应方选项
          if (this.baseConfig?.extendedPosiType?.includes(this.formData?.goodsType)) {
            updateSupplierOptions(true)
          } else {
            updateSupplierOptions(false)
          }
        }
      })
    },
    // 新增品牌商/供应商
    handleAddBrandSupplyConfirm(data) {
      addBrandSupplyMerchant(data).then(res => {
        this.getBrandSupplyList(data.partitionFlag)
      })
    },
    // 编辑删除 品牌商/供应商
    handleEditBrandSupplyConfirm(data) {
      editBrandSupplyMerchant(data).then(res => {
        this.getBrandSupplyList(data.partitionFlag)
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.sourceMaterial-form {
  background: #ffffff;

  .title {
    height: 50px;
    line-height: 50px;
    padding: 0 20px;
    border-bottom: 1px solid #E5E5E5;
    font-size: 16px;
    font-weight: 600;
    color: #333333;
  }
}
</style>