Commit 5189c225b5ba9151e48dfe968bd88535fb2d3566
1 parent
9177697d
fix(dynamic request): 修复动态请求header回显
Showing
7 changed files
with
137 additions
and
86 deletions
src/views/chart/ContentConfigurations/components/ChartData/external/components/DynamicForm/index.ts
0 → 100644
1 | +export { default as DynamicForm } from './index.vue' | ... | ... |
src/views/chart/ContentConfigurations/components/ChartData/external/components/DynamicForm/index.vue
0 → 100644
1 | +<script lang="ts" setup> | |
2 | +import { PublicInterfaceRecord } from '@/api/external/dynamicRequest/model'; | |
3 | +import { RequestParams } from '@/enums/httpEnum'; | |
4 | +import { ExtraRequestConfigType } from '@/store/external/modules/extraComponentInfo.d'; | |
5 | +import { FormItemInst, NDatePicker, NForm, NFormItem, NInput, NSelect, NTreeSelect } from 'naive-ui' | |
6 | +import { computed, ref, unref, watch } from 'vue'; | |
7 | +import { ComponentType, useDynamicPublicForm } from './useDynamicPublicForm'; | |
8 | +import { transferData } from './utils'; | |
9 | + | |
10 | +const props = defineProps<{ | |
11 | + publicInterfaceRecord: PublicInterfaceRecord | |
12 | +}>() | |
13 | + | |
14 | +const componentMap: { [key in ComponentType]?: any } = { | |
15 | + [ComponentType.SELECT_TREE]: NTreeSelect, | |
16 | + [ComponentType.SELECT]: NSelect, | |
17 | + [ComponentType.INPUT]: NInput, | |
18 | + [ComponentType.DATE_PICKER]: NDatePicker | |
19 | +} | |
20 | + | |
21 | +const getPublicInterfaceRecord = computed(() => { | |
22 | + return props.publicInterfaceRecord | |
23 | +}) | |
24 | +const dynamicFormItemEl = ref<FormItemInst[]>() | |
25 | + | |
26 | +const { getDynamicFormSchemas, validFlag, getValue, createForm, clearParams, setDynamicFormValue } = useDynamicPublicForm(getPublicInterfaceRecord) | |
27 | + | |
28 | +const getConfigurationData = () => { | |
29 | + return transferData(unref(getPublicInterfaceRecord), unref(getValue)) | |
30 | +} | |
31 | + | |
32 | +const validate = async () => { | |
33 | + for (const item of unref(dynamicFormItemEl) || []) { | |
34 | + await item.validate() | |
35 | + } | |
36 | + return unref(validFlag) | |
37 | +} | |
38 | +const setConfigurationData = async (requestParams: RequestParams) => { | |
39 | + setDynamicFormValue(requestParams) | |
40 | +} | |
41 | + | |
42 | +watch( | |
43 | + () => props.publicInterfaceRecord, | |
44 | + (newValue) => { | |
45 | + if (newValue) { | |
46 | + clearParams() | |
47 | + createForm() | |
48 | + } | |
49 | + } | |
50 | +) | |
51 | + | |
52 | +defineExpose({ | |
53 | + validate, | |
54 | + getConfigurationData, | |
55 | + setConfigurationData, | |
56 | +}) | |
57 | + | |
58 | +</script> | |
59 | + | |
60 | +<template> | |
61 | + <NForm> | |
62 | + <template v-for="item in getDynamicFormSchemas" :key="item.key"> | |
63 | + <NFormItem ref="dynamicFormItemEl" :required="item.required" :label="item.name" :rule="item.rules"> | |
64 | + <component :is="componentMap[item.component]" v-bind="item.props" clearable /> | |
65 | + </NFormItem> | |
66 | + </template> | |
67 | + </NForm> | |
68 | +</template> | ... | ... |
src/views/chart/ContentConfigurations/components/ChartData/external/components/DynamicForm/useDynamicPublicForm.ts
renamed from
src/views/chart/ContentConfigurations/components/ChartData/external/components/PublicInterfaceForm/useDynamicPublicForm.ts
... | ... | @@ -46,7 +46,6 @@ export const useDynamicPublicForm = (publicInterfaceRef: Ref<PublicInterfaceReco |
46 | 46 | const getParams = computed(() => { |
47 | 47 | let params = (publicInterfaceRef.value.requestParams as unknown as RequestParams).Params || [] |
48 | 48 | if (!Array.isArray(params)) params = [] |
49 | - console.log(params) | |
50 | 49 | return params |
51 | 50 | }) |
52 | 51 | |
... | ... | @@ -131,7 +130,7 @@ export const useDynamicPublicForm = (publicInterfaceRef: Ref<PublicInterfaceReco |
131 | 130 | if (Reflect.has(value, BuiltInVariable.DATE_RANGE)) { |
132 | 131 | const fieldMapping = unref(getParams).find(item => item.key === BuiltInVariable.DATE_RANGE) |
133 | 132 | const [start, end] = ((fieldMapping || {}).value || '').split(GROUP_SEPARATOR) |
134 | - const [startValue, endValue] = value[BuiltInVariable.DATE_RANGE] | |
133 | + const [startValue, endValue] = value[BuiltInVariable.DATE_RANGE] || [] | |
135 | 134 | value[start] = startValue |
136 | 135 | value[end] = endValue |
137 | 136 | |
... | ... | @@ -333,17 +332,23 @@ export const useDynamicPublicForm = (publicInterfaceRef: Ref<PublicInterfaceReco |
333 | 332 | const splitKeys = value ? value.split(GROUP_SEPARATOR) : key.split(GROUP_SEPARATOR) |
334 | 333 | if (isDateComponent(key as BuiltInVariable)) { |
335 | 334 | if (key as BuiltInVariable === BuiltInVariable.DATE_FIXED) { |
336 | - params[key] = Params[splitKeys[0]] | |
335 | + params[key] = Params[splitKeys[0]] || null | |
337 | 336 | continue |
338 | 337 | } |
339 | - params[key] = [Params[splitKeys[0]], Params[splitKeys[1]]] | |
338 | + const value = [Params[splitKeys[0]], Params[splitKeys[1]]] | |
339 | + params[key] = value.every(Boolean) ? value : null | |
340 | 340 | continue |
341 | 341 | } |
342 | 342 | for (const temp of splitKeys) { |
343 | 343 | params[temp] = Params[temp] |
344 | 344 | } |
345 | - } | |
345 | + } | |
346 | + } | |
346 | 347 | |
348 | + const clearParams = () => { | |
349 | + Object.keys(params).forEach(key => { | |
350 | + Reflect.deleteProperty(params, key) | |
351 | + }) | |
347 | 352 | } |
348 | 353 | |
349 | 354 | const setDynamicFormValue = (requestParams: RequestConfigType['requestParams']) => { |
... | ... | @@ -364,7 +369,8 @@ export const useDynamicPublicForm = (publicInterfaceRef: Ref<PublicInterfaceReco |
364 | 369 | getValue, |
365 | 370 | getDynamicFormSchemas, |
366 | 371 | setDynamicFormValue, |
367 | - createForm | |
372 | + createForm, | |
373 | + clearParams | |
368 | 374 | } |
369 | 375 | } |
370 | 376 | ... | ... |
src/views/chart/ContentConfigurations/components/ChartData/external/components/DynamicForm/utils.ts
renamed from
src/views/chart/ContentConfigurations/components/ChartData/external/components/PublicInterfaceForm/utils.ts
... | ... | @@ -3,9 +3,10 @@ import { RequestDataTypeEnum, RequestParams } from "@/enums/httpEnum" |
3 | 3 | import { ExternalRequestType } from "@/store/external/modules/extraComponentInfo.d" |
4 | 4 | import { unref } from "vue" |
5 | 5 | |
6 | -export const transferData = (publicInterfaceRecord: PublicInterfaceRecord, requestParams: RequestParams, fillParams: Recordable) => { | |
7 | - const { requestContentType, requestHttpType, id, requestUrl, requestParamsBodyType, requestOriginUrl } = publicInterfaceRecord | |
8 | - const { Header, Body } = requestParams | |
6 | +export const transferData = (publicInterfaceRecord: PublicInterfaceRecord, fillParams: Recordable, fillBody: Recordable = {}) => { | |
7 | + const { requestContentType, requestHttpType, id, requestUrl, requestOriginUrl, requestParams } = publicInterfaceRecord | |
8 | + const { Header, Body } = requestParams as RequestParams | |
9 | + const { requestParamsBodyType } = Body as Recordable | |
9 | 10 | return { |
10 | 11 | requestDataPondId: id, |
11 | 12 | // requestInterval |
... | ... | @@ -28,7 +29,7 @@ export const transferData = (publicInterfaceRecord: PublicInterfaceRecord, reque |
28 | 29 | // requestIntervalUnit |
29 | 30 | requestParams: { |
30 | 31 | Header, |
31 | - Body, | |
32 | + Body: unref(fillBody), | |
32 | 33 | Params: unref(fillParams) |
33 | 34 | } |
34 | 35 | ... | ... |
... | ... | @@ -4,21 +4,14 @@ import { PublicInterfaceRecord, PublicInterfaceStateEnum } from '@/api/external/ |
4 | 4 | import { SettingItem, SettingItemBox } from '@/components/Pages/ChartItemSetting'; |
5 | 5 | import { RequestContentTypeEnum, RequestContentTypeNameEnum } from '@/enums/external/httpEnum'; |
6 | 6 | import { RequestBodyEnum, RequestHttpEnum, RequestHttpIntervalEnum, RequestParams, RequestParamsTypeEnum } from '@/enums/httpEnum'; |
7 | -import { NForm, NCard, NDatePicker, NEmpty, NFormItem, NInput, NInputGroup, NInputNumber, NScrollbar, NSelect, NTabPane, NTabs, NTreeSelect, SelectOption, FormItemInst } from 'naive-ui'; | |
8 | -import { ref, computed, unref } from 'vue' | |
7 | +import { NCard, NEmpty, NInputGroup, NInputNumber, NScrollbar, NSelect, NTabPane, NTabs, SelectOption } from 'naive-ui'; | |
8 | +import { ref, computed, unref, nextTick } from 'vue' | |
9 | 9 | import { selectTimeOptions, selectTypeOptions } from '../../../index.d'; |
10 | 10 | import ParamsTable from '../RequestModal/ParamsTable.vue'; |
11 | 11 | import RequestBody from '../RequestModal/RequestBody.vue'; |
12 | -import { ComponentType, useDynamicPublicForm } from './useDynamicPublicForm'; | |
13 | -import { transferData } from './utils'; | |
14 | 12 | import { ExtraRequestConfigType } from '@/store/external/modules/extraComponentInfo.d'; |
13 | +import { DynamicForm } from '../DynamicForm'; | |
15 | 14 | |
16 | -const componentMap: { [key in ComponentType]?: any } = { | |
17 | - [ComponentType.SELECT_TREE]: NTreeSelect, | |
18 | - [ComponentType.SELECT]: NSelect, | |
19 | - [ComponentType.INPUT]: NInput, | |
20 | - [ComponentType.DATE_PICKER]: NDatePicker | |
21 | -} | |
22 | 15 | const publicInterfaceList = ref<PublicInterfaceRecord[]>([]) |
23 | 16 | |
24 | 17 | const selectedPublicInterface = ref<string>() |
... | ... | @@ -31,7 +24,7 @@ const toJSON = (string: string) => { |
31 | 24 | } |
32 | 25 | } |
33 | 26 | |
34 | -const getSelectedInterface = computed(() => { | |
27 | +const getSelectedInterface = computed<PublicInterfaceRecord>(() => { | |
35 | 28 | const record = unref(publicInterfaceList).find(item => item.id === unref(selectedPublicInterface))! || {} |
36 | 29 | const _record = JSON.parse(JSON.stringify(record)) |
37 | 30 | _record.requestParams = toJSON(_record.requestParams) |
... | ... | @@ -48,12 +41,6 @@ const handleFilter = (query: string, option: SelectOption) => { |
48 | 41 | return ((option as Recordable).interfaceName).includes(query) |
49 | 42 | } |
50 | 43 | |
51 | -const { getDynamicFormSchemas, getValue, createForm, validFlag, setDynamicFormValue } = useDynamicPublicForm(getSelectedInterface) | |
52 | - | |
53 | -const getConfigurationData = () => { | |
54 | - const value = transferData(unref(getSelectedInterface), unref(getSelectedInterface).requestParams as unknown as RequestParams, unref(getValue)) | |
55 | - return value | |
56 | -} | |
57 | 44 | |
58 | 45 | const requestHttpTypeRef = ref<RequestHttpEnum>() |
59 | 46 | |
... | ... | @@ -65,16 +52,17 @@ const requestContentTypeRef = ref<RequestContentTypeEnum>() |
65 | 52 | |
66 | 53 | const requestParamsTypeRef = ref<RequestParamsTypeEnum>(RequestParamsTypeEnum.PARAMS) |
67 | 54 | |
68 | -const requestParamsRef = ref<RequestParams>({ Body: { [RequestBodyEnum.FORM_DATA]: {}, [RequestBodyEnum.JSON]: '', [RequestBodyEnum.XML]: '', [RequestBodyEnum.X_WWW_FORM_URLENCODED]: {} }, Header: {}, Params: {} }) | |
55 | +const requestBodyRef = ref<RequestParams>({ Body: { [RequestBodyEnum.FORM_DATA]: {}, [RequestBodyEnum.JSON]: '', [RequestBodyEnum.XML]: '', [RequestBodyEnum.X_WWW_FORM_URLENCODED]: {} }, Header: {}, Params: {} }) | |
69 | 56 | |
70 | 57 | const requestParamsBodyTypeRef = ref<RequestBodyEnum>(RequestBodyEnum.X_WWW_FORM_URLENCODED) |
71 | 58 | |
72 | 59 | const headerRef = ref() |
73 | 60 | |
74 | -const dynamicFormItemEl = ref<FormItemInst[]>() | |
61 | + | |
62 | +const paramsDynamicFormEl = ref<InstanceType<typeof DynamicForm>>() | |
63 | +const socketDynamicFormEl = ref<InstanceType<typeof DynamicForm>>() | |
75 | 64 | |
76 | 65 | const handleSelectedInterfaceChange = (value: string, option: PublicInterfaceRecord) => { |
77 | - createForm() | |
78 | 66 | requestContentTypeRef.value = option.requestContentType as RequestContentTypeEnum |
79 | 67 | requestHttpTypeRef.value = option.requestHttpType as RequestHttpEnum |
80 | 68 | } |
... | ... | @@ -83,6 +71,22 @@ const getGetRequestTypeName = (key: RequestContentTypeEnum) => { |
83 | 71 | return RequestContentTypeNameEnum[RequestContentTypeEnum[key] as keyof typeof RequestContentTypeEnum] |
84 | 72 | } |
85 | 73 | |
74 | +const validate = async () => { | |
75 | + if (unref(paramsDynamicFormEl)) return await unref(paramsDynamicFormEl)?.validate() | |
76 | + if (unref(socketDynamicFormEl)) return await unref(socketDynamicFormEl)?.validate() | |
77 | +} | |
78 | + | |
79 | +const setDynamicFormValue = (requestParams: RequestParams) => { | |
80 | + if (unref(paramsDynamicFormEl)) unref(paramsDynamicFormEl)?.setConfigurationData(requestParams) | |
81 | + if (unref(socketDynamicFormEl)) unref(socketDynamicFormEl)?.setConfigurationData(requestParams) | |
82 | +} | |
83 | + | |
84 | + | |
85 | +const getConfigurationData = () => { | |
86 | + if (unref(paramsDynamicFormEl)) return unref(paramsDynamicFormEl)?.getConfigurationData() | |
87 | + if (unref(socketDynamicFormEl)) return unref(socketDynamicFormEl)?.getConfigurationData() | |
88 | +} | |
89 | + | |
86 | 90 | const setConfigurationData = async (request: ExtraRequestConfigType) => { |
87 | 91 | await getPublicInterfaceList() |
88 | 92 | const { requestDataPondId, requestParams, requestParamsBodyType, requestContentType, requestHttpType } = request |
... | ... | @@ -92,19 +96,11 @@ const setConfigurationData = async (request: ExtraRequestConfigType) => { |
92 | 96 | requestHttpTypeRef.value = requestHttpType |
93 | 97 | headerRef.value = Header |
94 | 98 | requestParamsBodyTypeRef.value = requestParamsBodyType |
95 | - requestParamsRef.value = requestParams | |
99 | + requestBodyRef.value = requestParams | |
100 | + await nextTick() | |
96 | 101 | setDynamicFormValue(requestParams) |
97 | 102 | } |
98 | 103 | |
99 | - | |
100 | -const validate = async () => { | |
101 | - for (const item of unref(dynamicFormItemEl) || []) { | |
102 | - await item.validate() | |
103 | - } | |
104 | - return unref(validFlag) | |
105 | -} | |
106 | - | |
107 | - | |
108 | 104 | defineExpose({ |
109 | 105 | getConfigurationData, |
110 | 106 | setConfigurationData, |
... | ... | @@ -159,55 +155,26 @@ defineExpose({ |
159 | 155 | |
160 | 156 | <SettingItemBox v-if="requestContentTypeRef === RequestContentTypeEnum.DEFAULT" |
161 | 157 | :item-right-style="{ gridTemplateColumns: '7fr 1fr' }"> |
162 | - <NCard v-if="requestParamsTypeRef === RequestParamsTypeEnum.PARAMS" class="dynamic-form"> | |
158 | + <NCard v-show="requestParamsTypeRef === RequestParamsTypeEnum.PARAMS" class="dynamic-form"> | |
163 | 159 | <NScrollbar style="max-height: 400px; box-sizing: border-box;"> |
164 | - <NForm> | |
165 | - <template v-for="item in getDynamicFormSchemas" :key="item.key"> | |
166 | - <NFormItem ref="dynamicFormItemEl" :required="item.required" :label="item.name" :rule="item.rules"> | |
167 | - <component :is="componentMap[item.component]" v-bind="item.props" clearable /> | |
168 | - </NFormItem> | |
169 | - </template> | |
170 | - </NForm> | |
171 | - <NEmpty v-if="!selectedPublicInterface || !getDynamicFormSchemas.length" description="请选择公共接口" /> | |
160 | + <DynamicForm ref="paramsDynamicFormEl" :public-interface-record="getSelectedInterface" /> | |
161 | + <NEmpty v-if="!selectedPublicInterface" description="请选择公共接口" /> | |
172 | 162 | </NScrollbar> |
173 | 163 | </NCard> |
174 | 164 | |
175 | - <!-- <section v-if="requestParamsTypeRef === RequestParamsTypeEnum.PARAMS"> | |
176 | - <NCard> | |
177 | - <NSpace vertical> | |
178 | - <NTag type="info"> | |
179 | - <span class="func-keyword">function</span> filter(res) { | |
180 | - </NTag> | |
181 | - <MonacoEditor v-model:modelValue="filter" width="380px" height="300px" language="javascript" /> | |
182 | - <NTag type="info">}</NTag> | |
183 | - </NSpace> | |
184 | - </NCard> | |
185 | - <NCard style="margin-top: 20px;"> | |
186 | - <NText>转换结果:</NText> | |
187 | - <section> | |
188 | - <NText>{{ params }}</NText> | |
189 | - </section> | |
190 | - </NCard> | |
191 | - </section> --> | |
192 | - | |
193 | - <RequestBody v-if="requestParamsTypeRef === RequestParamsTypeEnum.BODY" | |
194 | - v-model:request-params-body-type="requestParamsBodyTypeRef" v-model:value="requestParamsRef" /> | |
195 | - | |
196 | - <ParamsTable v-if="requestParamsTypeRef === RequestParamsTypeEnum.HEADER" v-model:value="headerRef" /> | |
165 | + <RequestBody v-show="requestParamsTypeRef === RequestParamsTypeEnum.BODY" | |
166 | + v-model:request-params-body-type="requestParamsBodyTypeRef" v-model:value="requestBodyRef" /> | |
167 | + | |
168 | + <ParamsTable v-show="requestParamsTypeRef === RequestParamsTypeEnum.HEADER" v-model:value="headerRef" | |
169 | + :disabled="true" /> | |
197 | 170 | |
198 | 171 | </SettingItemBox> |
199 | 172 | |
200 | 173 | <SettingItemBox v-if="requestContentTypeRef === RequestContentTypeEnum.WEB_SOCKET"> |
201 | 174 | <NCard v-if="requestParamsTypeRef === RequestParamsTypeEnum.PARAMS" class="dynamic-form"> |
202 | 175 | <NScrollbar style="max-height: 400px; box-sizing: border-box;"> |
203 | - <NForm> | |
204 | - <template v-for="item in getDynamicFormSchemas" :key="item.key"> | |
205 | - <NFormItem ref="dynamicFormItemEl" :required="item.required" :label="item.name" :rule="item.rules"> | |
206 | - <component :is="componentMap[item.component]" v-bind="item.props" clearable /> | |
207 | - </NFormItem> | |
208 | - </template> | |
209 | - </NForm> | |
210 | - <NEmpty v-if="!selectedPublicInterface || !getDynamicFormSchemas.length" description="请选择公共接口" /> | |
176 | + <DynamicForm ref="socketDynamicFormEl" :public-interface-record="getSelectedInterface" /> | |
177 | + <NEmpty v-if="!selectedPublicInterface" description="请选择公共接口" /> | |
211 | 178 | </NScrollbar> |
212 | 179 | </NCard> |
213 | 180 | </SettingItemBox> | ... | ... |
1 | 1 | <script lang="ts" setup> |
2 | 2 | import { MonacoEditor } from '@/components/Pages/MonacoEditor'; |
3 | 3 | import { RequestBodyEnum, RequestBodyEnumList, RequestParams } from '@/enums/httpEnum'; |
4 | -import { NCard } from 'naive-ui'; | |
4 | +import { NCard } from 'naive-ui'; | |
5 | 5 | import ParamsTable from './ParamsTable.vue'; |
6 | 6 | |
7 | 7 | defineProps<{ |
... | ... | @@ -12,14 +12,15 @@ defineProps<{ |
12 | 12 | const emit = defineEmits(['update:requestParamsBodyType']) |
13 | 13 | |
14 | 14 | const handleSyncRequestParamsBodyType = (value: RequestBodyEnum) => { |
15 | - emit('update:requestParamsBodyType', value) | |
15 | + emit('update:requestParamsBodyType', value || RequestBodyEnum.X_WWW_FORM_URLENCODED) | |
16 | 16 | } |
17 | 17 | |
18 | 18 | </script> |
19 | 19 | |
20 | 20 | <template> |
21 | 21 | <section> |
22 | - <NRadioGroup v-model:value="requestParamsBodyType" @update:value="handleSyncRequestParamsBodyType"> | |
22 | + <NRadioGroup :value="requestParamsBodyType || RequestBodyEnum.X_WWW_FORM_URLENCODED" | |
23 | + @update:value="handleSyncRequestParamsBodyType"> | |
23 | 24 | <NSpace> |
24 | 25 | <NRadio v-for="item in RequestBodyEnumList" :key="item" :value="item"> |
25 | 26 | {{ item }} |
... | ... | @@ -36,7 +37,7 @@ const handleSyncRequestParamsBodyType = (value: RequestBodyEnum) => { |
36 | 37 | <ParamsTable v-if="requestParamsBodyType === RequestBodyEnum.FORM_DATA" |
37 | 38 | v-model:value="value.Body[RequestBodyEnum.FORM_DATA]" /> |
38 | 39 | |
39 | - <ParamsTable v-if="requestParamsBodyType === RequestBodyEnum.X_WWW_FORM_URLENCODED" | |
40 | + <ParamsTable v-if="requestParamsBodyType === RequestBodyEnum.X_WWW_FORM_URLENCODED || !requestParamsBodyType" | |
40 | 41 | v-model:value="value.Body[RequestBodyEnum.X_WWW_FORM_URLENCODED]" /> |
41 | 42 | |
42 | 43 | <MonacoEditor v-if="requestParamsBodyType === RequestBodyEnum.JSON" |
... | ... | @@ -49,7 +50,7 @@ const handleSyncRequestParamsBodyType = (value: RequestBodyEnum) => { |
49 | 50 | </template> |
50 | 51 | |
51 | 52 | <style lang="scss" scoped> |
52 | - .body-container { | |
53 | - margin-top: 20px; | |
54 | - } | |
53 | +.body-container { | |
54 | + margin-top: 20px; | |
55 | +} | |
55 | 56 | </style> | ... | ... |