wangting
2024-04-26 c4d9e7a20dac267c5496ad3586c5053be279a17a
Source/ProjectWeb/src/components/dynamic-components/dynamic-form.vue
@@ -1,52 +1,22 @@
<template>
  <div :id="'UI-dynamic-'+areasName+componentVO.oid" class="UI-dynamic">
    <avue-form v-model="form" :option="option">
      <template v-for="item in slotData" :slot="item.prop + 'Label'">
        <span>
          <span>{{ item.label }} </span>
          <el-tooltip
            v-if="item.keyAttr"
            class="item"
            content="该属性为关键属性"
            effect="dark"
            placement="top-start"
          >
            <i class="el-icon-star-on" style="font-size: 17px !important; color: red;vertical-align: baseline;"></i>
          </el-tooltip>
        </span>
      </template>
      <template slot="menuForm">
        <dynamic-button :componentVO="componentVO" :butttonList="componentVO.buttons" :dataStore="[form]" :sourceData="sourceData" type="form" @buttonClick="buttonClick"></dynamic-button>
      </template>
      <template v-for="item in slotData" :slot="item.prop + ''">
        <vciWebRefer v-if="item.type == 'refer'"
                     :key="item.prop"
                     :ref="'referFormRef'+item.prop"
                     :data-key="item.prop"
                     :disabled="item.disabled || false"
                     :display="item.display || true"
                     :referConfig="item.referConfig || {}"
                     :reloadFormKey="(item.referConfig.useFormKey && item.referConfig.formValues && item.referConfig.formValues[item.referConfig.useFormKey]) || ''"
                     :text="item.referConfig.showProp"
                     :value="item.prop || item.value"
                     referType="master"
                     @setReferValue="setReferValue">
        </vciWebRefer>
      </template>
    </avue-form>
    <dynamic-button v-if="componentVO && componentVO.buttons" :key="areasName+'buttons-'+componentVO.oid"
                    :componentVO="componentVO" :butttonList="componentVO.buttons" :dataStore="[form]" :sourceData="sourceData" type="form"></dynamic-button>
    <basic-form :key="areasName+'basicForm-'+componentVO.oid"
                :span="this.componentVO.formDefineVO.columnOneRow?(24/this.componentVO.formDefineVO.columnOneRow) : 12"
                :formItems="this.componentVO.formDefineVO && this.componentVO.formDefineVO.items"
                :disabled="!inDialog"
                :isEdit="inDialog"
                :formData="form">
    </basic-form>
  </div>
</template>
<script>
import VciWebRefer from "@/components/refer/vciWebRefer";
import {verifyNull, verifySlash} from "@/util/validate";
import {dataForm} from "@/api/base/ui";
export default {
  name: "dynamic-form",
  components: {VciWebRefer},
  props: {
    //ui上下文的业务类型(或链接类型)
    uiBtmType: {
@@ -73,6 +43,10 @@
      type: Object,
      default: {}
    },
    //上一区域业务类型
    sourceBtmType:{
      type: String
    },
    dataStore: {
      //弹窗时按钮所属区域选中数据
      type: Array,
@@ -91,206 +65,97 @@
  data() {
    return {
      form: {},
      params:{},
      sourceDataMapParams:{},
      currentDefineVO:this.componentVO.formDefineVO,
    }
  },
  mounted() {
  },
  computed: {
    option() {
      return {
        submitBtn: false,
        emptyBtn: false,
        height: 300,
        column: this.getColumnData()
      }
    },
    slotData() {
      return this.getColumnData()
    }
  },
  watch: {
    slotData: {
      handler(newV) {
        this.getDictList(newV)
      },
      immediate: true,
    },
    form: {
      handler(val) {
        if (val) {
          if (Array.isArray(val)) { // 检查 val 是否为数组
            for (let code of val) {
              if (
                code.type == "refer" &&
                code.referConfig &&
                code.referConfig.useFormKey
              ) {
                code.referConfig.formValues = val;
                // code.referConfigTemp.options = code.referConfig;
              }
            }
          } else if (typeof val === 'object') { // 检查 val 是否为对象
            // 迭代对象的逻辑
          }
          this.$emit("input", val);
    form:{
      handler(newval) {
        if(newval) {
          this.$emit("setDataStore", {
            area: this.areasName,
            type:this.componentVO.uiComponentType,
            btmType:this.componentVO.treeDefineVO.btmType,
            dataStore:[newval]
          });
        }
      },
      deep: true,
      immediate: true,
      }
    },
    sourceData: {
      handler(newval) {
        //源数据有变化时变更当前区域数据
        console.log(this.areasName);
        console.log(newval);
        this.sourceDataMapParams=this.sourceDataMap();
        this.getParams();
        this.handleRefresh();
      }
    }
  },
  created() {
    this.getParams();
  },
  methods: {
    //转化数据
    formColumn(formList) {
      return formList.map(item => {
        const typeValue = item.type === 'text' ? 'input' : item.type === 'combox' ? 'select' : item.type;
        const focusFunc = item.type === 'refer' ? (i) => {
        } : undefined;
        return {
          label: item.text,
          prop: item.field,
          type: typeValue,
          labelWidth: item.text.length >= 6 ? 115 : 90,
          value: item.defaultValue,
          dicData: item.type === 'combox' ? item.dicData : item.dicUrl,
          readonly: item.readOnly,
          disabled: item.disabled,
          display: !item.hidden,
          labelSuffix: item.suffix,
          suffixIcon: item.prefix,
          placeholder: item.placeholder,
          clearable: item.clearable,
          tip: item.tooltips,
          keyAttr: item.keyAttr,
          focus: focusFunc,
          referConfig: item.referConfig,
          rules: [{
            required: item.required,
            message: `请输入${item.text}!`,
            trigger: "blur"
          }]
        };
      });
    },
    //数据判空
    getColumnData() {
      if (this.componentVO && this.componentVO.formDefineVO && this.componentVO.formDefineVO.items && this.componentVO.formDefineVO.items.length > 0) {
        return this.formColumn(this.componentVO.formDefineVO.items)
      } else {
        return null;
      }
    },
    buttonClick(item) {
      console.log(item.id)
    },
    async getDictList(val) {
      for (let code of val) {
        if (!verifyNull(code.dictData) && code.type == "select") {
          if (
            verifySlash(code.dictCode) &&
            Object.prototype.hasOwnProperty.call(code, "dictCode")
          ) {
            const res = await getlistByCode(code.dictCode);
            if (res.success) {
              const dic = res.data;
              code.dictData = dic.map((d) => {
                return {
                  label: d.name,
                  key: d.code,
                  value: d.id,
                };
              });
            }
          } else {
            this.getDicts(code.dictCode)
              .then((res) => {
                if (res.success) {
                  const dic = res.obj.datas;
                  code.dictData = dic.map((d) => {
                    return {
                      label: d.name,
                      key: d.code,
                      value: d.code,
                    };
                  });
                }
              })
              .catch(() => {
                this.$message.error(` 数据字典${code.dictCode}错误`);
              });
    sourceDataMap: function () {
      const sourceDataMap = {};
      if (Object.keys(this.sourceData).length>0) {
        if(this.sourceData.oid ) {
          if (this.sourceData.oid.indexOf('@vcitreesep@') > -1) {
            this.sourceData.oid = this.sourceData.oid.split('@vcitreesep@')[1];
          }
          sourceDataMap.sourceBtmName = this.sourceBtmType;;
          sourceDataMap.sourceOid = this.sourceData.oid;
        }
        if (code.type == "refer") {
          if (code.referConfig && code.referConfig.useFormKey) {
            if (verifyNull(code.referConfig.formValuesKey)) {
              code.referConfig.formValuesKey = "form";
            }
            code.referConfig.formValues = this[code.referConfig.formValuesKey];
          }
          code.referConfigTemp = {
            title: code.label,
            showProp:
              code.showProp || code.referConfig.showProp || code.prop + "Name",
            prop: code.prop,
            propMap: code.propMap || {},
            placeholder: code.placeholder
              ? code.placeholder
              : ` 请选择` + code.label,
            options: code.referConfig,
          };
        for (let i in this.sourceData) {
          let item = this.sourceData[i]
          if (item && item.constructor === Object) return;
          if (i == 'type' || i == 'context' || i == 'content') return;
          sourceDataMap['sourceData["' + i + '"]'] = item
        }
      }
      this.formTemplateData = val;
    },
    setReferValue(data) {
      if (data && data.prop) {
        this.form[data.prop] = data.value || "";
        this.form[data.showProp] = data.text || "";
        if (data.propMap) {
          //说明需要映射
          for (let key in data.propMap) {
            let mapFields = data.propMap[key].split(",");
            let value = [];
            data.rawData.forEach((_item) => {
              var temp;
              if (!_item.extendData) {
                _item.extendData = {};
              }
              if (mapFields.length == 1) {
                var mapField = mapFields[0];
                temp = _item[mapField] || _item["extendData"][mapField];
              } else {
                //有多个
                var mutiTemp = [];
                mapFields.forEach((_itemField) => {
                  mutiTemp.push(
                    _item[_itemField] || _item["extendData"][_itemField]
                  );
                });
                temp = mutiTemp.join(" ");
              }
              if (temp != null && temp != "") {
                value.push(temp);
              }
            });
            this.form[key] = value.join(",");
          }
      if (Object.keys(this.paramVOS).length>0) {
        for (let i in this.paramVOS) {
          let item = this.paramVOS[i]
          if (item && item.constructor === Object) return;
          if (i == 'type' || i == 'context' || i == 'content') return;
          sourceDataMap['sourceData["' + i + '"]'] = item
        }
      }
      return sourceDataMap;
    },
    getParams: function () {
      const formParams = {
        btmname: this.currentDefineVO.btmType,
        btmType:this.currentDefineVO.btmType,
        formDefineId: this.currentDefineVO.id,
        oid:this.currentDefineVO.oid
      };
      if(this.dataStore[0] && this.dataStore[0].oid) {
        formParams.oid = this.dataStore[0].oid;
      }
      const sourceDataMapList = this.sourceDataMapParams;
      this.params = Object.assign({},formParams, sourceDataMapList);
      console.log(this.params)
    },
    onLoad:function () {
      if (Object.keys(this.sourceData).length>0 && this.isShow) {
        this.loading = true;
        dataForm(this.params).then(res => {
          this.form = res.data.obj;
          this.loading = false;
        }).catch(error => {
          this.$message.error(error);
          this.loading = false;
        });
      }
    },
    handleRefresh(){
    }
  }
}
</script>