index.vue 3.93 KB
<template>
  <div>
    <div class="mt-8">
      <div>
        <Button @click="handleTest" type="primary"> 测试接口 </Button>
      </div>
      <div v-if="showTestEditCell" class="mt-8">
        <a-row type="flex" justify="center">
          <a-col :span="3"> 参数设置 </a-col>
          <a-col :span="21">
            <TestParamsCellTable ref="testEditCellTableRef" />
          </a-col>
        </a-row>
      </div>
    </div>
    <div class="mt-8"> </div>
  </div>
</template>
<script lang="ts" setup name="testRequest">
  import { ref, nextTick } from 'vue';
  import { Button } from 'ant-design-vue';
  import TestParamsCellTable from './testEditParamsCellTable.vue';
  import moment from 'moment';
  import { useUtils } from '../../../hooks/useUtils';
  import { useMessage } from '/@/hooks/web/useMessage';

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

  const props = defineProps({
    data: {
      type: Object,
    },
  });

  const { createMessage } = useMessage();

  const showTestEditCell = ref(false);

  const excuteData = ref({});

  const testEditCellTableRef = ref<InstanceType<typeof TestParamsCellTable>>();

  const testResult = ref('');

  const { getMultipleKeys } = useUtils();

  const handleTest = () => {
    showTestEditCell.value = true;
    emits('testParamsInterface');
    getValue();
  };

  const getValue = async () => {
    await nextTick();
    await nextTick(() => {
      //拆分"xx,xx,xx"多个
      const getSingleKey = props.data?.list;
      const getMuteKey = props.data?.list?.filter((it: any) => it.key?.split(',').length > 1);
      const getMuteKeys = getMultipleKeys(getMuteKey);
      const mergeKeys = [...getSingleKey, ...getMuteKeys]?.filter(
        (it: any) => it.key?.split(',').length === 1
      );
      testEditCellTableRef.value?.setTableArray(mergeKeys);
    });
  };

  //测试表格里的数据转化为 key value 数组对象形式
  //TODO:待优化这里的TS类型
  const getTestTableKeyValue = () => {
    //把日期拆分成多个
    const keyValueList: any = [];
    testEditCellTableRef.value?.getValue()?.forEach((item: any) => {
      const splitDateKey = item?.key === 'date_range' ? item?.value?.split(',') : [];
      const splitDateValue = item?.key === 'date_range' ? item?.dateValue : [];
      for (let i in splitDateKey) {
        const obj = {
          key: splitDateKey[i],
          value: moment(splitDateValue[i]).valueOf(),
        };
        keyValueList.push(obj);
      }
    });
    return testEditCellTableRef.value
      ?.getValue()
      .concat(keyValueList)
      .filter((it) => it.key !== 'date_range')
      .map((it: any) => {
        const value =
          it?.key === 'scope'
            ? it?.keyValue
            : it?.key === 'fixed_date'
            ? moment(it?.fixed_date_value).valueOf()
            : it?.value;
        const key = it?.key === 'scope' || it?.key === 'fixed_date' ? it?.value : it?.key;
        return {
          key,
          value,
          required: it.required,
        };
      });
  };

  //获取数据
  const getTestValue = () => {
    const getTable = getTestTableKeyValue();
    const hasRequired = getTable?.some((it) => it.required === true && !it.value);
    if (hasRequired) {
      createMessage.error('选择项为必须的,参数不能为空');
      throw new Error('选择项为必须的,参数不能为空');
    }
    const params: any = {};
    getTable?.map((it) => (params[it.key!] = it.value!));
    if (params['keys']) {
      Reflect.set(params, 'keys', params['keys'].join(','));
    }
    excuteData.value = {
      params,
    };
    return excuteData.value;
  };

  //设置数据
  const setValue = () => {
    showTestEditCell.value = false;
    testResult.value = '';
  };

  defineExpose({
    setValue,
    handleTest,
    getTestValue,
  });
</script>

<style scoped lang="less"></style>