transferConfigApi.vue 6.87 KB
<template>
  <div class="transfer-config-mode">
    <BasicForm :showSubmitButton="false" @register="register">
      <template #addKeyAndValue="{ field }">
        <span style="display: none">{{ field }}</span>
        <div>
          <div>
            <template v-for="(item, index) in keyAndValueArr" :key="index">
              <span style="display: none">{{ item + index }}</span>
              <BasicForm
                :showResetButton="false"
                :showSubmitButton="false"
                @register="registerKeyAndValue"
              />
            </template>
            <div>
              <a-button type="primary" class="mr-4" @click="addKeyAndValueFunc">添加</a-button>
              <a-button color="error" @click="removeKeyAndValueFunc">删除</a-button>
            </div>
          </div>
        </div>
      </template>
      <template #uploadAdd1="{ field }">
        <span style="display: none">{{ field }}</span>
        <a-upload-dragger
          v-model:fileList="fileList"
          name="file"
          :multiple="true"
          action="https://www.mocky.io/v2/5cc8019d300000980a055e76"
          @change="handleChange"
        >
          <p class="ant-upload-drag-icon">
            <InboxOutlined />
          </p>
          <p class="ant-upload-text">Click or drag file to this area to upload</p>
          <p class="ant-upload-hint">
            Support for a single or bulk upload. Strictly prohibit from uploading company data or
            other band files
          </p>
        </a-upload-dragger>
      </template>
      <template #uploadAdd2="{ field }">
        <span style="display: none">{{ field }}</span>
        <a-upload-dragger
          v-model:fileList="fileList"
          name="file"
          :multiple="true"
          action="https://www.mocky.io/v2/5cc8019d300000980a055e76"
          @change="handleChange"
        >
          <p class="ant-upload-drag-icon">
            <InboxOutlined />
          </p>
          <p class="ant-upload-text">Click or drag file to this area to upload</p>
          <p class="ant-upload-hint">
            Support for a single or bulk upload. Strictly prohibit from uploading company data or
            other band files
          </p>
        </a-upload-dragger>
      </template>
      <template #uploadAdd3="{ field }">
        <span style="display: none">{{ field }}</span>
        <a-upload-dragger
          v-model:fileList="fileList"
          name="file"
          :multiple="true"
          action="https://www.mocky.io/v2/5cc8019d300000980a055e76"
          @change="handleChange"
        >
          <p class="ant-upload-drag-icon">
            <InboxOutlined />
          </p>
          <p class="ant-upload-text">Click or drag file to this area to upload</p>
          <p class="ant-upload-hint">
            Support for a single or bulk upload. Strictly prohibit from uploading company data or
            other band files
          </p>
        </a-upload-dragger>
      </template>
    </BasicForm>
  </div>
</template>
<script lang="ts">
  import { defineComponent, ref, reactive, nextTick } from 'vue';
  import { BasicForm, useForm } from '/@/components/Form';
  import { modeApiForm, modeApiInseretKeyAndValueForm } from '../config';
  import { InboxOutlined } from '@ant-design/icons-vue';
  import { Upload } from 'ant-design-vue';
  interface IKeyAndValue {
    key: string;
    value: string;
  }
  export default defineComponent({
    components: {
      BasicForm,
      InboxOutlined,
      [Upload.Dragger.name]: Upload.Dragger,
    },
    emits: ['next', 'prev', 'register'],
    setup(_, { emit }) {
      const fileList = ref<[]>([]);
      const keyAndValueArr = ref<[]>([]);
      const temp = ref({});
      let tempObj = ref({});
      const otherPropertiesValues = reactive({
        headers: {},
      });
      const credentialsV = reactive({
        credentials: {
          type: '',
        },
      });
      const keyAndValueArrTemp = ref<[]>([]);
      const keyAndValueObj = reactive<IKeyAndValue>({
        key: '',
        value: '',
      });
      const sonValues: any = reactive({
        configuration: {},
      });
      const [register, { validate, setFieldsValue, resetFields: defineClearFunc }] = useForm({
        labelWidth: 80,
        schemas: modeApiForm,
        actionColOptions: {
          span: 14,
        },
        resetButtonOptions: {
          text: '上一步',
          type: 'primary',
        },

        resetFunc: customResetFunc,
        submitFunc: customSubmitFunc,
      });

      const [
        registerKeyAndValue,
        { validate: validateKeyAndValue, resetFields: defineClearKeyAndValueFunc },
      ] = useForm({
        labelWidth: 80,
        schemas: modeApiInseretKeyAndValueForm,
        actionColOptions: {
          span: 14,
        },
      });
      const setStepTwoFieldsValueFunc = (v, v1) => {
        setFieldsValue(v);
        setFieldsValue({
          name: v1,
        });
      };
      const customClearStepTwoValueFunc = async () => {
        nextTick(() => {
          defineClearFunc();
          defineClearKeyAndValueFunc();
        });
      };
      async function customResetFunc() {
        emit('prev');
      }
      async function customSubmitFunc() {
        try {
          const values = await validate();
          emit('next', values);
        } catch (error) {
        } finally {
        }
      }
      const tempGetKeyAndVal = async () => {
        temp.value = await validateKeyAndValue();
      };

      const getDefaultValue = async () => {
        await tempGetKeyAndVal();
        keyAndValueArrTemp.value.push(temp.value as never);
      };

      const addKeyAndValueFunc = async () => {
        keyAndValueArr.value.push(keyAndValueObj as never);
        await tempGetKeyAndVal();
        tempObj.value = temp.value;
        keyAndValueArrTemp.value.push(tempObj.value as never);
      };
      const removeKeyAndValueFunc = () => {
        keyAndValueArr.value.splice(0, 1);
      };
      const handleChange = () => {};

      const getSonValueFunc = async () => {
        sonValues.configuration = await validate();
        if (keyAndValueArrTemp.value.length != 0) {
          await getDefaultValue();
        }
        credentialsV.credentials.type = sonValues.configuration.type;
        const kong = {};
        let kongTemp = {};
        keyAndValueArrTemp.value.map((item: any) => {
          kong[item.key] = item.value;
        });
        kongTemp = JSON.parse(JSON.stringify(kong));
        otherPropertiesValues.headers = kongTemp;
        Object.assign(sonValues.configuration, otherPropertiesValues, credentialsV);
        return sonValues;
      };
      return {
        register,
        setStepTwoFieldsValueFunc,
        customClearStepTwoValueFunc,
        addKeyAndValueFunc,
        removeKeyAndValueFunc,
        getSonValueFunc,
        keyAndValueArr,
        registerKeyAndValue,
        fileList,
        handleChange,
      };
    },
  });
</script>