body.vue 3.69 KB
<template>
  <div>
    <a-radio-group
      v-model:value="getRequestBody.content.requestParamsBodyType"
      @change="handleChange"
    >
      <a-radio v-for="item in RequestBodyTypeEnum" :key="item" :value="item">{{ item }}</a-radio>
    </a-radio-group>
    <div class="mt-3">
      <a-textarea
        v-show="getRequestBody.content.requestParamsBodyType === 'none'"
        disabled
        placeholder="该接口没有 Body 体"
        :rows="2"
      />
      <BodyTable
        ref="bodyCellFormDataTableRef"
        v-show="getRequestBody.content.requestParamsBodyType === 'form-data'"
      />
      <BodyTable
        ref="bodyCellXwwwTableRef"
        v-show="getRequestBody.content.requestParamsBodyType === 'x-www-form-urlencoded'"
      />
      <JsonEditor
        v-show="getRequestBody.content.requestParamsBodyType === 'json'"
        ref="jsonEditorRef"
      />
      <a-textarea
        v-model:value="getRequestBody.content.xml"
        v-show="getRequestBody.content.requestParamsBodyType === 'xml'"
        placeholder="请输入xml"
        :rows="6"
      />
    </div>
  </div>
</template>
<script lang="ts" setup name="body">
  import { reactive, ref, nextTick } from 'vue';
  import { RequestBodyTypeEnum } from '../../../enum/index';
  import BodyTable from './bodyTable.vue';
  import { isEmpty } from '/@/utils/is';
  import { useUtils } from '../../../hooks/useUtils';
  import JsonEditor from './jsonEditor.vue';

  const getRequestBody = reactive({
    content: {
      requestParamsBodyType: 'none',
      json: '',
      xml: '',
      'form-data': {},
      'x-www-form-urlencoded': {},
    },
  });

  const { pushObj } = useUtils();

  const bodyCellFormDataTableRef = ref<InstanceType<typeof BodyTable>>();

  const jsonEditorRef = ref<InstanceType<typeof JsonEditor>>();

  const bodyCellXwwwTableRef = ref<InstanceType<typeof BodyTable>>();

  const handleChange = ({ target }) => {
    const { value } = target;
    getRequestBody.content.requestParamsBodyType = value;
  };

  //获取数据
  const getValue = () => {
    const type = Reflect.get(getRequestBody.content, 'requestParamsBodyType');
    const valuesFormData = bodyCellFormDataTableRef.value?.getValue();
    const valuesXWww = bodyCellXwwwTableRef.value?.getValue();
    const jsonEditorValue = jsonEditorRef.value?.getJsonValue();
    getRequestBody.content['form-data'] = valuesFormData as any;
    getRequestBody.content['x-www-form-urlencoded'] = valuesXWww as any;
    getRequestBody.content['json'] = jsonEditorValue;
    if (type === 'none') getRequestBody.content = {} as any;
    return getRequestBody.content;
  };

  //设置数据
  const setValue = async (data) => {
    if (!data) return;
    const type = data?.requestParamsBodyType;
    getRequestBody.content.requestParamsBodyType = type;
    await nextTick();
    await nextTick();
    bodyCellFormDataTableRef.value?.setValue(isEmpty(data) ? [pushObj] : data['form-data']);
    bodyCellXwwwTableRef.value?.setValue(isEmpty(data) ? [pushObj] : data['x-www-form-urlencoded']);
    jsonEditorRef.value?.setJsonValue(data['json']);
    getRequestBody.content.xml = data['xml'];
  };

  //重置数据
  const resetValue = () => {
    for (let i in getRequestBody.content) Reflect.set(getRequestBody.content, i, '');
    getRequestBody.content['form-data'] = {};
    getRequestBody.content['x-www-form-urlencoded'] = {};
    getRequestBody.content.requestParamsBodyType = 'none';
    nextTick(() => {
      bodyCellFormDataTableRef?.value?.resetValue();
      bodyCellXwwwTableRef?.value?.resetValue();
    });
  };
  defineExpose({
    getValue,
    setValue,
    resetValue,
  });
</script>