index.vue
7.05 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<template>
<div>
<a-form
ref="formRef"
:model="scriptForm"
name="basic"
:label-col="{ span: 4 }"
:wrapper-col="{ span: 16 }"
style="margin-left: 2.4rem"
autocomplete="off"
>
<a-form-item
v-if="deviceTypeStr !== 'SENSOR'"
label="鉴权脚本"
name="'params'"
:rules="[{ required: true, message: '请选择鉴权脚本' }]"
>
<div style="display: flex; align-items: center">
<div>
<Select
@change="handleAuthChange"
placeholder="请选择"
v-model:value="scriptForm.authScriptId"
style="width: 305px"
show-search
:options="selectAuthOptions"
:filter-option="handleAuthSearch"
allowClear
/>
</div>
<div>
<span
@click="handleCreateOrEditAuth('add')"
class="ml-2"
style="color: #409eff; cursor: pointer"
size="small"
>新建转换脚本</span
>
</div>
</div>
<a-button @click="handleCreateOrEditAuth('test')" class="mt-4" type="primary"
>测试脚本</a-button
>
</a-form-item>
<a-form-item
label="上行脚本"
name="'params'"
:rules="[{ required: true, message: '请选择上行脚本' }]"
>
<div style="display: flex; align-items: center">
<div>
<Select
@change="handleUpChange"
placeholder="请选择"
v-model:value="scriptForm.upScriptId"
style="width: 305px"
show-search
:options="selectUpOptions"
:filter-option="handleSearch"
allowClear
/>
</div>
<div>
<span
@click="handleCreateOrEdit('add')"
class="ml-2"
style="color: #409eff; cursor: pointer"
size="small"
>新建转换脚本</span
>
</div>
</div>
<a-button @click="handleCreateOrEdit('test')" class="mt-4" type="primary"
>测试脚本</a-button
>
</a-form-item>
</a-form>
<ConverScriptModal @register="registerModal" @success="handleSuccess" />
<ConverScriptModal @register="registerAuthModal" @success="handleAuthSuccess" />
</div>
</template>
<script lang="ts" setup name="index">
import { ref, Ref, onMounted, reactive } from 'vue';
import { SelectTypes } from 'ant-design-vue/es/select';
import { Select } from 'ant-design-vue';
import { useModal } from '/@/components/Modal';
import { getScriptManageMeList } from '/@/api/scriptmanage/scriptManager';
import ConverScriptModal from '/@/views/scriptmanage/converscript/ConverScriptModal.vue';
import { useMessage } from '/@/hooks/web/useMessage';
const props = defineProps({
deviceTypeStr: { type: String, default: '' },
});
const scriptForm = reactive({
authScriptId: '',
upScriptId: '',
});
const selectUpOptions: Ref<SelectTypes['options']> = ref([]);
const selectAuthOptions: Ref<SelectTypes['options']> = ref([]);
const { createMessage } = useMessage();
const upScriptIdStr = ref('');
const authScriptIdStr = ref('');
onMounted(async () => {
selectUpOptions.value = await getAllScriptType('TRANSPORT_TCP_UP');
selectAuthOptions.value = await getAllScriptType('TRANSPORT_TCP_AUTH');
});
const getAllScriptType = async (type) => {
const rest = await getScriptManageMeList({ scriptType: type });
return rest.map((m) => ({ label: m.name, value: m.id }));
};
const handleSuccess = async ({ res, text }) => {
if (text !== 'test') {
const rest = await getAllScriptType('TRANSPORT_TCP_UP');
selectUpOptions.value = rest;
scriptForm.upScriptId = res?.id;
upScriptIdStr.value = res?.id;
}
};
const handleAuthSuccess = async ({ res, text }) => {
if (text !== 'test') {
const rest = await getAllScriptType('TRANSPORT_TCP_AUTH');
selectAuthOptions.value = rest;
scriptForm.authScriptId = res?.id;
authScriptIdStr.value = res?.id;
}
};
const handleUpChange = (v) => {
upScriptIdStr.value = v;
scriptForm.upScriptId = v;
};
const handleAuthChange = (v) => {
authScriptIdStr.value = v;
scriptForm.authScriptId = v;
};
const [registerModal, { openModal }] = useModal();
const [registerAuthModal, { openModal: openAuthModel }] = useModal();
//TODO: 待优化
const handleCreateOrEditAuth = (c) => {
if (c === 'add') {
openAuthModel(true, {
isUpdate: true,
isView: false,
isTest: false,
isText: 'confirm',
isTitle: 'add',
});
} else {
if (!authScriptIdStr.value) return createMessage.error('请先选择对应脚本');
openAuthModel(true, {
isUpdate: false,
isTest: true,
record: authScriptIdStr.value,
isText: 'test',
isTitle: 'test',
});
}
};
const handleCreateOrEdit = (c) => {
if (c === 'add') {
openModal(true, {
isUpdate: true,
isView: false,
isTest: false,
isText: 'confirm',
isTitle: 'add',
});
} else {
if (!upScriptIdStr.value) return createMessage.error('请先选择对应脚本');
openModal(true, {
isUpdate: false,
isTest: true,
record: upScriptIdStr.value,
isText: 'test',
isTitle: 'test',
});
}
};
const getFormData = async () => {
if (props.deviceTypeStr === 'SENSOR') {
if (!scriptForm.upScriptId) {
createMessage.error('请先选择对应脚本');
throw new Error('请先选择对应脚本');
}
} else {
if (!scriptForm.authScriptId) {
createMessage.error('请先选择对应脚本');
throw new Error('请先选择对应脚本');
}
if (!scriptForm.upScriptId) {
createMessage.error('请先选择对应脚本');
throw new Error('请先选择对应脚本');
}
}
return {
...scriptForm,
type: 'TCP',
};
};
const resetFormData = () => {
// resetFields();
};
const setFormData = (v) => {
scriptForm.authScriptId = v?.authScriptId;
scriptForm.upScriptId = v?.upScriptId;
// setFieldsValue(v);
upScriptIdStr.value = v?.upScriptId;
authScriptIdStr.value = v?.authScriptId;
};
const handleSearch = (inputValue: string, option: Record<'label' | 'value', string>) => {
return option.label.includes(inputValue);
};
const handleAuthSearch = (inputValue: string, option: Record<'label' | 'value', string>) => {
return option.label.includes(inputValue);
};
defineExpose({
getFormData,
resetFormData,
setFormData,
});
</script>
<style lang="less" scoped></style>