transferConfigApi.vue 9.7 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="fileList1"
          name="file"
          :multiple="false"
          @change="handleChange('T', $event)"
          :before-upload="() => false"
        >
          <p class="ant-upload-drag-icon">
            <InboxOutlined />
          </p>
          <p class="ant-upload-text">点击或将文件拖拽到这里上传</p>
          <p class="ant-upload-hint">
            支持扩展名:.jpeg .png .jpg ...
            <br />
            文件大小:最大支持5M
          </p>
        </a-upload-dragger>
      </template>
      <template #uploadAdd2="{ field }">
        <span style="display: none">{{ field }}</span>
        <a-upload-dragger
          v-model:fileList="fileList2"
          name="file"
          :multiple="false"
          @change="handleChange('F', $event)"
          :before-upload="() => false"
        >
          <p class="ant-upload-drag-icon">
            <InboxOutlined />
          </p>
          <p class="ant-upload-text">点击或将文件拖拽到这里上传</p>
          <p class="ant-upload-hint">
            支持扩展名:.jpeg .png .jpg ...
            <br />
            文件大小:最大支持5M
          </p>
        </a-upload-dragger>
      </template>
      <template #uploadAdd3="{ field }">
        <span style="display: none">{{ field }}</span>
        <a-upload-dragger
          v-model:fileList="fileList3"
          name="file"
          :multiple="false"
          @change="handleChange('C', $event)"
          :before-upload="() => false"
        >
          <p class="ant-upload-drag-icon">
            <InboxOutlined />
          </p>
          <p class="ant-upload-text">点击或将文件拖拽到这里上传</p>
          <p class="ant-upload-hint">
            支持扩展名:.jpeg .png .jpg ...
            <br />
            文件大小:最大支持5M
          </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, CredentialsEnum } from '../config';
  import { InboxOutlined } from '@ant-design/icons-vue';
  import { Upload } from 'ant-design-vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { uploadApi } from '/@/api/personal/index';

  interface IKeyAndValue {
    key: string;
    value: string;
  }
  export default defineComponent({
    components: {
      BasicForm,
      InboxOutlined,
      [Upload.Dragger.name]: Upload.Dragger,
    },
    emits: ['next', 'prev', 'register'],
    setup(_, { emit }) {
      const { createMessage } = useMessage();
      let caCertFileName = ref('');
      let privateKeyFileName = ref('');
      let certFileName = ref('');
      let fileList1: any = ref<[]>([]);
      let fileList2: any = ref<[]>([]);
      let fileList3: any = ref<[]>([]);
      const keyAndValueArr = ref<[]>([]);
      const temp = ref({});
      let tempObj = ref({});
      const otherPropertiesValues = reactive({
        headers: {},
      });
      const credentialsV: any = 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: '上一步',
        },
        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,
        });
        setFieldsValue({
          password: v.credentials?.password,
          username: v.credentials?.username,
          type: v.credentials?.type,
        });
        fileList1.value = [{ name: v.credentials?.caCertFileName.slice(39) }];
        fileList2.value = [{ name: v.credentials?.certFileName.slice(39) }];
        fileList3.value = [{ name: v.credentials?.privateKeyFileName.slice(39) }];
      };
      const customClearStepTwoValueFunc = async () => {
        nextTick(() => {
          defineClearFunc();
          defineClearKeyAndValueFunc();
          fileList1.value = [];
          fileList2.value = [];
          fileList3.value = [];
          caCertFileName.value = '';
          privateKeyFileName.value = '';
          certFileName.value = '';
        });
      };
      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 = async (e, { file }) => {
        if (file.status === 'removed') {
          e == 'T'
            ? (fileList1.value = [])
            : e == 'F'
            ? (fileList2.value = [])
            : (fileList3.value = []);
        } else {
          const isLt5M = file.size / 1024 / 1024 < 5;
          if (!isLt5M) {
            createMessage.error('图片大小不能超过5MB!');
          } else {
            e == 'T'
              ? (fileList1.value = [file])
              : e == 'F'
              ? (fileList2.value = [file])
              : (fileList3.value = [file]);
            const formData = new FormData();
            formData.append('file', file);
            const response = await uploadApi(formData);
            if (response.fileStaticUri) {
              e == 'T'
                ? (caCertFileName.value = response.fileStaticUri)
                : e == 'F'
                ? (certFileName.value = response.fileStaticUri)
                : (privateKeyFileName.value = response.fileStaticUri);
            }
          }
        }
      };

      const getSonValueFunc = async () => {
        sonValues.configuration = await validate();
        if (keyAndValueArrTemp.value.length != 0) {
          await getDefaultValue();
        }
        credentialsV.credentials.type = sonValues.configuration.type;
        if (credentialsV.credentials.type == CredentialsEnum.IS_BASIC) {
          credentialsV.credentials.username = sonValues.configuration.username;
          credentialsV.credentials.password = sonValues.configuration.password;
          sonValues.configuration.username = undefined;
          sonValues.configuration.password = undefined;
        } else if (credentialsV.credentials.type == CredentialsEnum.IS_PEM) {
          credentialsV.credentials.caCertFileName = caCertFileName.value;
          credentialsV.credentials.certFileName = certFileName.value;
          credentialsV.credentials.privateKeyFileName = privateKeyFileName.value;
          credentialsV.credentials.password = sonValues.configuration.password;
        }
        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,
        handleChange,
        fileList1,
        fileList2,
        fileList3,
        caCertFileName,
        privateKeyFileName,
        certFileName,
      };
    },
  });
</script>
<style lang="less" scoped>
  :deep(.ant-col-24) {
    margin-bottom: 20px !important;
  }
</style>