index.vue 5.98 KB
<template>
  <div>
    <!--TODO: 待优化三者共存下的测试和是否能复用表格 -->
    <Tabs @change="handleChange" v-model:activeKey="activeKey">
      <TabPane class="tab-pane" forceRender key="Params" tab="Params">
        <ParamsTable ref="paramsCellTableRef" :method="method" />
        <ParamsTest
          @testParamsInterface="handleTestParamsInterface"
          ref="testParamsRequestRef"
          :data="dataMap.mapParamsObj"
        />
      </TabPane>
      <TabPane
        v-if="method !== '2' && requestTypeAndUrl?.requestHttpType !== 'GET'"
        class="tab-pane"
        forceRender
        key="Body"
        tab="Body"
        disabled
      >
        <Body ref="bodyRef" />
        <BodyTest
          @testBodyInterface="handleTestBodyInterface"
          ref="testBodyRequestRef"
          :data="dataMap.mapBodyObj"
        />
      </TabPane>
      <TabPane v-if="method !== '2'" class="tab-pane" forceRender key="Header" tab="Header">
        <HeaderTable ref="editHeaderCellTableRef" :method="method" />
        <HeaderTest
          @testHeaderInterface="handleTestHeaderInterface"
          ref="testHeaderRequestRef"
          :data="dataMap.mapHeaderObj"
        />
      </TabPane>
    </Tabs>
    <ExcuteTest ref="excuteTestRef" @emitExcute="handleEmitExcute" :data="excuteData" />
  </div>
</template>
<script lang="ts" setup name="simpleRequest">
  import { ref, nextTick, reactive } from 'vue';
  import { Tabs, TabPane } from 'ant-design-vue';
  import Body from './components/body.vue';
  import { ParamsTest, HeaderTest, BodyTest } from '../TestInterface/index';
  import HeaderTable from './components/headerTable.vue';
  import ParamsTable from './components/paramsTable.vue';
  import { isEmpty } from '/@/utils/is';
  import { useUtils } from '../../hooks/useUtils';
  import ExcuteTest from '../TestInterface/components/excuteTest.vue';

  const props = defineProps({
    method: {
      type: String,
    },
    requestTypeAndUrl: {
      type: Object,
    },
    requestOriginUrl: {
      type: String,
    },
  });

  const emits = defineEmits(['activeKey']);

  const { pushObj } = useUtils();

  const activeKey = ref('Params');

  const excuteData = ref({});

  const paramsCellTableRef = ref<InstanceType<typeof ParamsTable>>();

  const editHeaderCellTableRef = ref<InstanceType<typeof ParamsTable>>();

  const excuteTestRef = ref<InstanceType<typeof ExcuteTest>>();

  const bodyRef = ref<InstanceType<typeof Body>>();

  const testParamsRequestRef = ref<InstanceType<typeof ParamsTest>>();

  const testBodyRequestRef = ref<InstanceType<typeof ParamsTest>>();

  const testHeaderRequestRef = ref<InstanceType<typeof HeaderTest>>();

  const dataMap: any = reactive({
    mapParamsObj: {},
    mapBodyObj: {},
    mapHeaderObj: {},
  });

  const handleChange = () => {
    // excuteTestRef.value?.resetValue();
  };

  const handleClickReset = () => {
    testParamsRequestRef.value?.setValue();
    testHeaderRequestRef.value?.setValue();
    testBodyRequestRef.value?.setValue();
    excuteTestRef.value?.resetValue();
  };

  //if-else-if-else分支优化
  const dataForTypeMap = [
    [(type) => type === 'Params', (data) => paramsCellTableRef.value?.setValue(data)],
    [(type) => type === 'Body', (data) => bodyRef.value?.setValue(data)],
    [(type) => type === 'Header', (data) => editHeaderCellTableRef.value?.setValue(data)],
  ];

  const handleEmitExcute = () => {
    excuteData.value = {
      Params: testParamsRequestRef.value?.getTestValue(),
      Header: testHeaderRequestRef.value?.getTestValue(),
      Body: testBodyRequestRef.value?.getTestValue(),
    };
  };

  const getCompose = () => {
    return {
      requestOriginUrl: props.requestOriginUrl,
      requestTypeAndUrl: props.requestTypeAndUrl,
      method: props.method,
      activeKey: activeKey.value,
    };
  };

  const handleTestParamsInterface = () => {
    excuteTestRef.value?.showTest();
    let value = getValue(false) as any;
    dataMap.mapParamsObj = {
      list: value?.Params,
      ...getCompose(),
    };
  };

  const handleTestBodyInterface = () => {
    excuteTestRef.value?.showTest();
    let value = getValue(false) as any;
    const type = value?.Body?.requestParamsBodyType;
    console.log(type);
    console.log(value?.Body);
    //取出对应的类型

    dataMap.mapBodyObj = {
      list: value?.Body,
      ...getCompose(),
    };
  };

  const handleTestHeaderInterface = () => {
    excuteTestRef.value?.showTest();
    let value = getValue(false) as any;
    dataMap.mapHeaderObj = {
      list: value?.Header,
      ...getCompose(),
    };
  };

  //获取数据
  const getValue = (status) => {
    const type = activeKey.value;
    status ? emits('activeKey', type) : null;
    const Body = bodyRef.value?.getValue();
    const Params = paramsCellTableRef.value?.getValue();
    const Header = editHeaderCellTableRef.value?.getValue();
    return {
      Params,
      Header,
      Body,
    };
  };

  //设置数据
  const setValue = (data) => {
    nextTick(() => {
      const Objects = JSON.parse(data?.requestParams);
      if (!Objects) return;
      dataForTypeMap[0][1](isEmpty(Objects?.Params) ? [pushObj] : Objects?.Params);
      dataForTypeMap[1][1](isEmpty(Objects?.Body) ? {} : Objects?.Body);
      dataForTypeMap[2][1](isEmpty(Objects?.Header) ? [pushObj] : Objects?.Header);
    });
  };

  //重置数据
  const resetValue = () => {
    activeKey.value = 'Params';
    nextTick(() => {
      paramsCellTableRef.value?.resetValue();
      editHeaderCellTableRef.value?.resetValue();
      bodyRef.value?.resetValue();
      handleClickReset();
    });
  };

  defineExpose({
    getValue,
    setValue,
    resetValue,
  });
</script>

<style lang="less" scoped>
  .tab-pane {
    display: flex;
    justify-content: flex-start;
    flex-direction: column;
    align-items: flex-start;
  }
</style>