gaoluyang
2026-06-24 c0cb161bb52ce0fbdce5c66ec391d107c75e2452
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { IotDeviceApi } from '#/api/iot/device/device';
 
import { DICT_TYPE } from '..\..\..\..\packages\constants\src';
import { getDictOptions } from '..\..\..\..\packages\effects\hooks\src';
 
import { z } from '#/adapter/form';
import { getSimpleDeviceGroupList } from '#/api/iot/device/group';
import { getSimpleProductList } from '#/api/iot/product/product';
 
/** 基础表单字段 */
export function useBasicFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'id',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'productId',
      label: '产品',
      component: 'ApiSelect',
      componentProps: {
        api: getSimpleProductList,
        labelField: 'name',
        valueField: 'id',
        placeholder: '请选择产品',
      },
      dependencies: {
        triggerFields: ['id'],
        disabled: (values: any) => !!values?.id,
      },
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'deviceType',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'deviceName',
      label: 'DeviceName',
      component: 'Input',
      componentProps: {
        placeholder: '请输入 DeviceName',
      },
      dependencies: {
        triggerFields: ['id'],
        disabled: (values: any) => !!values?.id,
      },
      rules: z
        .string()
        .min(4, 'DeviceName 长度不能少于 4 个字符')
        .max(32, 'DeviceName 长度不能超过 32 个字符')
        .regex(
          /^[\w.\-:@]{4,32}$/,
          '支持英文字母、数字、下划线(_)、中划线(-)、点号(.)、半角冒号(:)和特殊字符@',
        ),
    },
  ];
}
 
/** 高级设置表单字段(更多设置) */
export function useAdvancedFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'nickname',
      label: '备注名称',
      component: 'Input',
      componentProps: {
        placeholder: '请输入备注名称',
      },
      rules: z
        .string()
        .refine((value) => {
          const length = value.replaceAll(
            /[\u4E00-\u9FA5\u3040-\u30FF]/g,
            'aa',
          ).length;
          return length >= 4 && length <= 64;
        }, '备注名称长度限制为 4~64 个字符,中文及日文算 2 个字符')
        .refine(
          (value) => /^[\u4E00-\u9FA5\u3040-\u30FF\w]+$/.test(value),
          '备注名称只能包含中文、英文字母、日文、数字和下划线(_)',
        )
        .optional()
        .or(z.literal('')),
    },
    {
      fieldName: 'picUrl',
      label: '设备图片',
      component: 'ImageUpload',
    },
    {
      fieldName: 'groupIds',
      label: '设备分组',
      component: 'ApiSelect',
      componentProps: {
        api: getSimpleDeviceGroupList,
        labelField: 'name',
        valueField: 'id',
        mode: 'multiple',
        placeholder: '请选择设备分组',
      },
    },
    {
      fieldName: 'serialNumber',
      label: '设备序列号',
      component: 'Input',
      componentProps: {
        placeholder: '请输入设备序列号',
      },
      rules: z
        .string()
        .regex(/^[\w-]+$/, '序列号只能包含字母、数字、中划线和下划线')
        .optional()
        .or(z.literal('')),
    },
    {
      fieldName: 'longitude',
      label: '设备经度',
      component: 'InputNumber',
      componentProps: {
        class: '!w-full',
        placeholder: '请输入设备经度',
        min: -180,
        max: 180,
        precision: 6,
      },
      rules: z
        .number()
        .min(-180, '经度范围为 -180 到 180')
        .max(180, '经度范围为 -180 到 180')
        .optional()
        .nullable(),
    },
    {
      fieldName: 'latitude',
      label: '设备纬度',
      component: 'InputNumber',
      componentProps: {
        class: '!w-full',
        placeholder: '请输入设备纬度',
        min: -90,
        max: 90,
        precision: 6,
      },
      rules: z
        .number()
        .min(-90, '纬度范围为 -90 到 90')
        .max(90, '纬度范围为 -90 到 90')
        .optional()
        .nullable(),
    },
  ];
}
 
/** 设备分组表单 */
export function useGroupFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'groupIds',
      label: '设备分组',
      component: 'ApiSelect',
      componentProps: {
        api: getSimpleDeviceGroupList,
        labelField: 'name',
        valueField: 'id',
        mode: 'multiple',
        placeholder: '请选择设备分组',
      },
      rules: 'required',
    },
  ];
}
 
/** 设备导入表单 */
export function useImportFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'file',
      label: '设备数据',
      component: 'Upload',
      rules: 'required',
      help: '仅允许导入 xls、xlsx 格式文件',
    },
    {
      fieldName: 'updateSupport',
      label: '是否覆盖',
      component: 'Switch',
      componentProps: {
        checkedChildren: '是',
        unCheckedChildren: '否',
      },
      rules: z.boolean().default(false),
      help: '是否更新已经存在的设备数据',
    },
  ];
}
 
/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'productId',
      label: '产品',
      component: 'ApiSelect',
      componentProps: {
        api: getSimpleProductList,
        labelField: 'name',
        valueField: 'id',
        placeholder: '请选择产品',
        allowClear: true,
      },
    },
    {
      fieldName: 'deviceName',
      label: 'DeviceName',
      component: 'Input',
      componentProps: {
        placeholder: '请输入 DeviceName',
        allowClear: true,
      },
    },
    {
      fieldName: 'nickname',
      label: '备注名称',
      component: 'Input',
      componentProps: {
        placeholder: '请输入备注名称',
        allowClear: true,
      },
    },
    {
      fieldName: 'deviceType',
      label: '设备类型',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.IOT_PRODUCT_DEVICE_TYPE, 'number'),
        placeholder: '请选择设备类型',
        allowClear: true,
      },
    },
    {
      fieldName: 'status',
      label: '设备状态',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.IOT_DEVICE_STATE, 'number'),
        placeholder: '请选择设备状态',
        allowClear: true,
      },
    },
    {
      fieldName: 'groupId',
      label: '设备分组',
      component: 'ApiSelect',
      componentProps: {
        api: getSimpleDeviceGroupList,
        labelField: 'name',
        valueField: 'id',
        placeholder: '请选择设备分组',
        allowClear: true,
      },
    },
  ];
}
 
/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<IotDeviceApi.Device>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'deviceName',
      title: 'DeviceName',
      minWidth: 150,
      slots: { default: 'deviceName' },
    },
    {
      field: 'nickname',
      title: '备注名称',
      minWidth: 120,
    },
    {
      field: 'picUrl',
      title: '设备图片',
      width: 100,
      cellRender: {
        name: 'CellImage',
      },
    },
    {
      field: 'productId',
      title: '所属产品',
      minWidth: 120,
      slots: { default: 'product' },
    },
    {
      field: 'deviceType',
      title: '设备类型',
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.IOT_PRODUCT_DEVICE_TYPE },
      },
    },
    {
      field: 'groupIds',
      title: '所属分组',
      minWidth: 150,
      slots: { default: 'groups' },
    },
    {
      field: 'state',
      title: '设备状态',
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.IOT_DEVICE_STATE },
      },
    },
    {
      field: 'onlineTime',
      title: '最后上线时间',
      minWidth: 180,
      formatter: 'formatDateTime',
    },
    {
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}