ScriptSelectItem.vue
4.76 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
156
157
158
159
160
161
162
163
164
165
<template>
<div class="flex items-center">
<Select
v-bind="$attrs"
placeholder="请选择脚本"
@change="handleChange"
v-model:value="scriptId"
class="flex-1"
show-search
:options="selectOptions"
:filter-option="handleSearch"
allowClear
@focus="handleFocus"
/>
<div
@click="handleBusinessModal"
class="ml-2 flex-shrink-0 cursor-pointer text-blue-400"
type="link"
>
新建脚本
</div>
</div>
<a-button @click="handleOpenTestModal" class="mt-4" type="primary"> 测试脚本 </a-button>
<ConvertScriptModal @register="registerModal" @complete="handleSuccess" />
<JavaScriptTestModal
@register="registerTestModal"
onlyTest
:language="testScriptLanguage"
:messageValue="{
params: '010304026C00883BF0',
}"
:javaScriptEditorProps="{
functionName: 'Transform',
paramsName: ['msg', 'metadata', 'msgType'],
scriptType: 'update',
}"
:beforeTest="beforeTest"
/>
</template>
<script lang="ts" setup name="ScriptSelectItem">
import { ref, Ref, watch, unref } from 'vue';
import { SelectTypes } from 'ant-design-vue/es/select';
import { Select } from 'ant-design-vue';
import { ScriptTypeEnum } from '/@/views/rule/script/TcpConversionScript/config';
import { getScriptManageMeList } from '/@/api/scriptmanage/scriptManager';
import { useModal } from '/@/components/Modal';
import ConvertScriptModal from '/@/views/rule/script/TcpConversionScript/components/ConvertScriptModal.vue';
import { DataActionModeEnum } from '/@/enums/toolEnum';
import JavaScriptTestModal from '/@/views/rule/designer/src/components/JavaScriptFilterModal/JavaScriptTestModal.vue';
import { ScriptLanguageEnum } from '/@/enums/scriptEnum';
import { TransportScriptRecordType } from '/@/api/scriptmanage/model/scriptModel';
const props = defineProps({
scriptType: {
type: String,
default: ScriptTypeEnum.TRANSPORT_TCP_AUTH,
},
modelValue: {
type: String,
default: '',
},
serviceType: {
type: String,
default: '',
},
});
const emits = defineEmits(['update:modelValue', 'change']);
const selectOptions: Ref<SelectTypes['options']> = ref([]);
//如果是空字符串,placeholder没提示文字
const scriptId = ref<any>(null);
// onMounted(() => {
// getSelectOptions();
// });
const getSelectOptions = async () => {
selectOptions.value = await getAllScriptType(props.scriptType, props.serviceType);
};
const getAllScriptType = async (type, serviceType) => {
const rest = await getScriptManageMeList({ scriptType: type, serviceType });
return rest.map((m) => ({ ...m, label: m.name, value: m.id }));
};
const handleSearch = (inputValue: string, option: Record<'label' | 'value', string>) => {
return option.label.includes(inputValue);
};
const [registerModal, { openModal }] = useModal();
const [registerTestModal, { openModal: openTestModal }] = useModal();
const testScriptLanguage = ref<string>(ScriptLanguageEnum.JavaScript);
// 业务弹窗
const handleBusinessModal = () => {
openModal(true, { mode: DataActionModeEnum.CREATE });
};
const handleOpenTestModal = () => {
const option = unref(selectOptions)?.find((item) => item.value === props.modelValue);
const {
scriptLanguage = ScriptLanguageEnum.JavaScript,
convertJs,
convertTbel,
} = (option || {}) as TransportScriptRecordType;
testScriptLanguage.value = scriptLanguage;
openTestModal(true, {
mode: DataActionModeEnum.CREATE,
record: scriptLanguage === ScriptLanguageEnum.JavaScript ? convertJs : convertTbel,
} as ModalParamsType<string>);
};
const handleSuccess = () => {
getSelectOptions();
// 默认禁用
// const { id } = rest;
// scriptId.value = id;
};
const handleChange = (value: string) => {
emits('update:modelValue', value);
emits('change', value);
};
const handleFocus = () => {
getSelectOptions();
};
const getValue = () => {
return scriptId.value;
};
const setValue = async (value) => {
const scriptOption = await getAllScriptType(props.scriptType, props.serviceType);
selectOptions.value = scriptOption;
if (props.modelValue && !unref(selectOptions)?.some((item) => item.id === props.modelValue)) {
emits('update:modelValue', null);
}
if (!scriptOption.map((item) => item.value).includes(value)) {
scriptId.value = null;
} else scriptId.value = value;
};
const beforeTest = (params: Recordable) => {
return { ...params, needMetadataAndMsgType: true };
};
watch(
() => props.modelValue,
(value) => {
setValue(value);
},
{
immediate: true,
}
);
defineExpose({
setValue,
getValue,
});
</script>