index.vue
4.93 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
151
152
153
154
155
<script lang="ts" setup>
import { RequestDataTypeEnum, RequestDataTypeNameEnum } from '@/enums/external/httpEnum';
import { useDesign } from '@/hooks/external/useDesign';
import { CreateComponentType } from '@/packages/index.d';
import { CreateComponentGroupType } from '@/packages/index.d';
import { RequestConfigType } from '@/store/modules/chartEditStore/chartEditStore.d';
import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore';
import { NButton, NDivider, NModal, NSpace, NTag } from 'naive-ui';
import { ref, unref, computed, nextTick } from 'vue';
import { PublicInterfaceForm } from '../PublicInterfaceForm';
import ComponentConfiguration from './ComponentConfiguration.vue';
import GlobalPublicConfiguration from './GlobalPublicConfiguration.vue';
import { createRequestModalContext } from './useRequestModalContext';
import { useTargetData } from '../../../../hooks/useTargetData.hook';
import { useFetchTargetData } from '@/hooks/external/useFetchTargetData';
import { useFilterFn } from '@/hooks/external/useFilterFn';
const requestDataType = ref<RequestDataTypeEnum>(RequestDataTypeEnum.AJAX)
const showModal = ref(false)
const chartEditStore = useChartEditStore()
const componentConfigurationEl = ref<InstanceType<typeof ComponentConfiguration>>()
const publicInterfaceFormEl = ref<InstanceType<typeof PublicInterfaceForm>>()
const getRequestTypeName = computed(() => {
return RequestDataTypeNameEnum[RequestDataTypeEnum[unref(requestDataType)] as keyof typeof RequestDataTypeEnum]
})
const { prefixCls } = useDesign('chart-data-request')
const openModal = async (flag = true, type: RequestDataTypeEnum) => {
requestDataType.value = type
showModal.value = flag
await nextTick()
unref(componentConfigurationEl)?.setConfigurationData(unref(selectTarget)!.request || {})
unref(publicInterfaceFormEl)?.setConfigurationData(unref(selectTarget)!.request || {})
}
const handleSaveAndRequest = () => {
handleSaveAction()
}
const selectTarget = computed<CreateComponentType | CreateComponentGroupType | undefined>(() => {
const selectId = chartEditStore.getTargetChart.selectId
// 排除多个
if (selectId.length !== 1) return undefined
const target = chartEditStore.componentList[chartEditStore.fetchTargetIndex()]
return target as CreateComponentType | CreateComponentGroupType
})
const validate = async () => {
if (unref(requestDataType) === RequestDataTypeEnum.Pond) {
return await unref(publicInterfaceFormEl)?.validate()
}
return true
}
const getResult = () => {
if (unref(requestDataType) === RequestDataTypeEnum.AJAX) {
return unref(componentConfigurationEl)?.getConfigurationData()
} else if (unref(requestDataType) === RequestDataTypeEnum.Pond) {
return unref(publicInterfaceFormEl)?.getConfigurationData()
}
return {} as unknown as RequestConfigType
}
const { targetData } = useTargetData()
const { fetchTargetData } = useFetchTargetData()
// 发送请求
const sendHandle = async () => {
if (!targetData.value?.request || !targetData.value.request.requestUrl) {
window['$message'].warning('请先配置请求')
return
}
const res = await fetchTargetData()
if (res) {
const { value } = useFilterFn(targetData.value.filter, res)
targetData.value.option.dataset = value
return
}
}
const handleSaveAction = async () => {
if (!(await validate())) return
const value = getResult()
if (unref(selectTarget)) {
chartEditStore.updateComponentList(chartEditStore.fetchTargetIndex(), {
...unref(selectTarget)!,
request: value as RequestConfigType
})
}
showModal.value = false
sendHandle()
}
createRequestModalContext({
requestDataType
})
defineExpose({
openModal,
})
</script>
<template>
<NModal :class="prefixCls" v-model:show="showModal" preset="card" style="width: 1000px;" title="请求配置">
<GlobalPublicConfiguration v-if="requestDataType === RequestDataTypeEnum.AJAX" />
<NDivider v-if="requestDataType === RequestDataTypeEnum.AJAX" />
<ComponentConfiguration v-if="requestDataType === RequestDataTypeEnum.AJAX" ref="componentConfigurationEl" />
<PublicInterfaceForm v-if="requestDataType === RequestDataTypeEnum.Pond" ref="publicInterfaceFormEl" />
<template #action>
<NSpace justify="space-between">
<div style="display: flex; align-items: center; ">
<span>「 更多 」</span>
<span style="margin-right: 5px;">——</span>
<NTag type="info">{{ getRequestTypeName }}</NTag>
</div>
<NButton type="primary" @click="handleSaveAndRequest">保存 & 发送请求</NButton>
</NSpace>
</template>
</NModal>
</template>
<style lang="scss" >
@include thingsKit('chart-data-request') {
&.n-card.n-modal,
.n-card {
@extend .go-background-filter;
}
.n-card-shallow {
background-color: rgba(0, 0, 0, 0) !important;
}
@include deep() {
&>.n-card__content {
padding-right: 0;
}
.n-card__content {
padding-bottom: 5px;
}
}
}
</style>