index.vue
4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<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>