gaoluyang
2026-06-24 712aa51536236d43e87273e4ce45ac5691dffad8
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
<!-- Modbus 连接配置弹窗 -->
<script lang="ts" setup>
import type { IotDeviceModbusConfigApi } from '#/api/iot/device/modbus/config';
 
import { computed, ref } from 'vue';
 
import { useVbenModal } from '..\..\..\..\..\..\packages\effects\common-ui\src';
import {
  CommonStatusEnum,
  DICT_TYPE,
  ModbusFrameFormatEnum,
  ModbusModeEnum,
} from '..\..\..\..\..\..\packages\constants\src';
import { getDictOptions } from '..\..\..\..\..\..\packages\effects\hooks\src';
 
import { message } from 'ant-design-vue';
 
import { useVbenForm, z } from '#/adapter/form';
import { saveModbusConfig } from '#/api/iot/device/modbus/config';
import { ProtocolTypeEnum } from '#/api/iot/product/product';
import { $t } from '#/locales';
 
const emit = defineEmits(['success']);
 
const formData = ref<IotDeviceModbusConfigApi.ModbusConfig>();
const deviceId = ref<number>(0);
const protocolType = ref<string>('');
 
const isClient = computed(
  () => protocolType.value === ProtocolTypeEnum.MODBUS_TCP_CLIENT,
); // 是否为 Client 模式
const isServer = computed(
  () => protocolType.value === ProtocolTypeEnum.MODBUS_TCP_SERVER,
); // 是否为 Server 模式
 
const [Form, formApi] = useVbenForm({
  commonConfig: {
    componentProps: {
      class: 'w-full',
    },
    formItemClass: 'col-span-2',
    labelWidth: 120,
  },
  layout: 'horizontal',
  schema: [
    {
      component: 'Input',
      fieldName: 'id',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'ip',
      label: 'IP 地址',
      component: 'Input',
      componentProps: {
        placeholder: '请输入 Modbus 服务器 IP 地址',
      },
      // Client 模式专有字段:必填;Server 模式不显示也不校验
      dependencies: {
        triggerFields: [''],
        show: () => isClient.value,
        rules: () =>
          isClient.value ? z.string().min(1, '请输入 IP 地址') : null,
      },
    },
    {
      fieldName: 'port',
      label: '端口',
      component: 'InputNumber',
      componentProps: {
        class: '!w-full',
        placeholder: '请输入端口',
        min: 1,
        max: 65_535,
      },
      dependencies: {
        triggerFields: [''],
        show: () => isClient.value,
        rules: () =>
          isClient.value
            ? z.number({ message: '请输入端口' }).min(1).max(65_535)
            : null,
      },
      defaultValue: 502,
    },
    {
      fieldName: 'slaveId',
      label: '从站地址',
      component: 'InputNumber',
      componentProps: {
        class: '!w-full',
        placeholder: '请输入从站地址,范围 1-247',
        min: 1,
        max: 247,
      },
      rules: z.number().min(1, '请输入从站地址').max(247),
      defaultValue: 1,
    },
    {
      fieldName: 'timeout',
      label: '连接超时(ms)',
      component: 'InputNumber',
      componentProps: {
        class: '!w-full',
        placeholder: '请输入连接超时时间',
        min: 1000,
        step: 1000,
      },
      dependencies: {
        triggerFields: [''],
        show: () => isClient.value,
        rules: () =>
          isClient.value
            ? z.number({ message: '请输入连接超时时间' }).min(1000)
            : null,
      },
      defaultValue: 3000,
    },
    {
      fieldName: 'retryInterval',
      label: '重试间隔(ms)',
      component: 'InputNumber',
      componentProps: {
        class: '!w-full',
        placeholder: '请输入重试间隔',
        min: 1000,
        step: 1000,
      },
      dependencies: {
        triggerFields: [''],
        show: () => isClient.value,
        rules: () =>
          isClient.value
            ? z.number({ message: '请输入重试间隔' }).min(1000)
            : null,
      },
      defaultValue: 10_000,
    },
    {
      fieldName: 'mode',
      label: '工作模式',
      component: 'RadioGroup',
      componentProps: {
        options: getDictOptions(DICT_TYPE.IOT_MODBUS_MODE, 'number'),
      },
      dependencies: {
        triggerFields: [''],
        show: () => isServer.value, // Server 模式专有字段:工作模式
      },
      rules: 'required',
      defaultValue: ModbusModeEnum.POLLING,
    },
    {
      fieldName: 'frameFormat',
      label: '帧格式',
      component: 'RadioGroup',
      componentProps: {
        options: getDictOptions(DICT_TYPE.IOT_MODBUS_FRAME_FORMAT, 'number'),
      },
      dependencies: {
        triggerFields: [''],
        show: () => isServer.value, // Server 模式专有字段:帧格式
      },
      rules: 'required',
      defaultValue: ModbusFrameFormatEnum.MODBUS_TCP,
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'RadioGroup',
      componentProps: {
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        buttonStyle: 'solid',
        optionType: 'button',
      },
      rules: z.number().default(CommonStatusEnum.ENABLE),
    },
  ],
  showDefaultActions: false,
});
 
const [Modal, modalApi] = useVbenModal({
  async onConfirm() {
    const { valid } = await formApi.validate();
    if (!valid) {
      return;
    }
    modalApi.lock();
    // 提交表单
    const data =
      (await formApi.getValues()) as IotDeviceModbusConfigApi.ModbusConfig;
    try {
      data.deviceId = deviceId.value;
      await saveModbusConfig(data);
      // 关闭并提示
      await modalApi.close();
      emit('success');
      message.success($t('ui.actionMessage.operationSuccess'));
    } finally {
      modalApi.unlock();
    }
  },
  async onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      formData.value = undefined;
      return;
    }
    // 加载数据
    const data = modalApi.getData<{
      config?: IotDeviceModbusConfigApi.ModbusConfig;
      deviceId: number;
      protocolType: string;
    }>();
    if (!data) {
      return;
    }
    deviceId.value = data.deviceId;
    protocolType.value = data.protocolType;
    if (!data.config) {
      return;
    }
    // 设置到 values
    formData.value = { ...data.config };
    await formApi.setValues(formData.value);
  },
});
</script>
 
<template>
  <Modal title="编辑 Modbus 连接配置" class="w-[600px]">
    <Form class="mx-4" />
  </Modal>
</template>