index.vue 4.24 KB
<template>
  <div>
    <Tabs @change="handleTabsChange" v-model:activeKey="activeKey">
      <TabPane class="tab-pane" forceRender key="Params" tab="Params">
        <EditCellTable ref="editParamsCellTableRef" :method="method" />
      </TabPane>
      <TabPane
        v-if="method !== '2' && requestTypeAndUrl?.requestHttpType !== 'GET'"
        class="tab-pane"
        forceRender
        key="Body"
        tab="Body"
      >
        <Body
          ref="bodyRef"
          :method="method"
          :paramsType="requestTypeAndUrl?.requestHttpType"
          :data="dataMap.mapObj"
        />
      </TabPane>
      <TabPane v-if="method !== '2'" class="tab-pane" forceRender key="Header" tab="Header">
        <Header ref="editHeaderCellTableRef" :method="method" />
      </TabPane>
    </Tabs>
    <div class="tab-pane">
      <TestRequest
        @testInterface="handleTestInterface"
        ref="testRequestRef"
        :method="method"
        :requestOriginUrl="requestOriginUrl"
        :requestUrl="requestTypeAndUrl?.requestUrl"
        :data="dataMap.mapObj"
      />
    </div>
  </div>
</template>
<script lang="ts" setup name="simpleRequest">
  import { ref, nextTick, reactive } from 'vue';
  import { Tabs, TabPane } from 'ant-design-vue';
  import { EditCellTable } from '../EditCellTable';
  import Body from './body.vue';
  import { TestRequest } from '../TestRequest/index';
  import Header from './header.vue';

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

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

  const activeKey = ref('Params');

  const editParamsCellTableRef = ref<InstanceType<typeof EditCellTable>>();

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

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

  const testRequestRef = ref<InstanceType<typeof TestRequest>>();

  const dataMap: any = reactive({
    mapObj: {},
  });

  const handleTabsChange = () => testRequestRef.value?.setValue();

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

  //测试接口
  const handleTestInterface = () => {
    let value = getValue(false) as any;
    const type = value?.requestParamsBodyType;
    // if (type === 'none') value = [];
    // if (type === 'form-data') value = value['form-data'];
    // if (type === 'x-www-form-urlencoded') value = value['x-www-form-urlencoded'];
    nextTick(
      () =>
        (dataMap.mapObj = {
          list: value?.Params || value?.Header,
          requestOriginUrl: props.requestOriginUrl,
          requestParamsBodyType: type,
          requestTypeAndUrl: props.requestTypeAndUrl,
        })
    );
  };

  //获取数据
  const getValue = (status) => {
    const type = activeKey.value;
    status ? emits('activeKey', type) : null;
    const Body = bodyRef.value?.getValue();
    const Params = editParamsCellTableRef.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](Objects?.Params);
      dataForTypeMap[1][1](Objects?.Body);
      dataForTypeMap[2][1](Objects?.Header);
    });
  };

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

  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>