Skip to content

Commit 12abcdd

Browse files
author
puhui999
committed
【功能完善】IOT: 产品物模型-枚举型的 dataSpecs 校验规则完善
1 parent d65a10c commit 12abcdd

File tree

1 file changed

+115
-5
lines changed

1 file changed

+115
-5
lines changed

src/views/iot/product/product/detail/ThingModel/dataSpecs/ThingModelEnumTypeDataSpecs.vue

Lines changed: 115 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<template>
2-
<el-form-item label="枚举项" prop="enum">
2+
<el-form-item
3+
:rules="[{ required: true, validator: validateEnumList, trigger: 'change' }]"
4+
label="枚举项"
5+
prop="property.dataSpecsList"
6+
>
37
<div class="flex flex-col">
48
<div class="flex items-center">
59
<span class="flex-1"> 参数值 </span>
@@ -10,10 +14,28 @@
1014
:key="index"
1115
class="flex items-center justify-between mb-5px"
1216
>
13-
<el-input v-model="item.value" placeholder="请输入枚举值,如'0'" />
17+
<el-form-item
18+
:prop="`property.dataSpecsList[${index}].value`"
19+
:rules="[
20+
{ required: true, message: '枚举值不能为空' },
21+
{ validator: validateEnumValue, trigger: 'blur' }
22+
]"
23+
class="flex-1 mb-0"
24+
>
25+
<el-input v-model="item.value" placeholder="请输入枚举值,如'0'" />
26+
</el-form-item>
1427
<span class="mx-2">~</span>
15-
<el-input v-model="item.name" placeholder="对该枚举项的描述" />
16-
<el-button link type="primary" class="ml-10px" @click="deleteEnum(index)">删除</el-button>
28+
<el-form-item
29+
:prop="`property.dataSpecsList[${index}].name`"
30+
:rules="[
31+
{ required: true, message: '枚举描述不能为空' },
32+
{ validator: validateEnumName, trigger: 'blur' }
33+
]"
34+
class="flex-1 mb-0"
35+
>
36+
<el-input v-model="item.name" placeholder="对该枚举项的描述" />
37+
</el-form-item>
38+
<el-button class="ml-10px" link type="primary" @click="deleteEnum(index)">删除</el-button>
1739
</div>
1840
<el-button link type="primary" @click="addEnum">+添加枚举项</el-button>
1941
</div>
@@ -26,6 +48,7 @@ import {
2648
DataSpecsDataType,
2749
DataSpecsEnumOrBoolDataVO
2850
} from '@/views/iot/product/product/detail/ThingModel/config'
51+
import { isEmpty } from '@/utils/is'
2952
3053
/** 枚举型的 dataSpecs 配置组件 */
3154
defineOptions({ name: 'ThingModelEnumTypeDataSpecs' })
@@ -51,6 +74,93 @@ const deleteEnum = (index: number) => {
5174
}
5275
dataSpecsList.value.splice(index, 1)
5376
}
77+
78+
/** 校验枚举值 */
79+
const validateEnumValue = (_: any, value: any, callback: any) => {
80+
if (isEmpty(value)) {
81+
callback(new Error('枚举值不能为空'))
82+
return
83+
}
84+
if (isNaN(Number(value))) {
85+
callback(new Error('枚举值必须是数字'))
86+
return
87+
}
88+
// 检查枚举值是否重复
89+
const values = dataSpecsList.value.map((item) => item.value)
90+
if (values.filter((v) => v === value).length > 1) {
91+
callback(new Error('枚举值不能重复'))
92+
return
93+
}
94+
callback()
95+
}
96+
97+
/** 校验枚举描述 */
98+
const validateEnumName = (_: any, value: string, callback: any) => {
99+
if (isEmpty(value)) {
100+
callback(new Error('枚举描述不能为空'))
101+
return
102+
}
103+
104+
// 检查开头字符
105+
if (!/^[\u4e00-\u9fa5a-zA-Z0-9]/.test(value)) {
106+
callback(new Error('枚举描述必须以中文、英文字母或数字开头'))
107+
return
108+
}
109+
110+
// 检查整体格式
111+
if (!/^[\u4e00-\u9fa5a-zA-Z0-9][a-zA-Z0-9\u4e00-\u9fa5_-]*$/.test(value)) {
112+
callback(new Error('枚举描述只能包含中文、英文字母、数字、下划线和短划线'))
113+
return
114+
}
115+
116+
// 检查长度(一个中文算一个字符)
117+
if (value.length > 20) {
118+
callback(new Error('枚举描述长度不能超过20个字符'))
119+
return
120+
}
121+
122+
callback()
123+
}
124+
125+
/** 校验整个枚举列表 */
126+
const validateEnumList = (_: any, __: any, callback: any) => {
127+
if (isEmpty(dataSpecsList.value)) {
128+
callback(new Error('请至少添加一个枚举项'))
129+
return
130+
}
131+
132+
// 检查是否存在空值
133+
const hasEmptyValue = dataSpecsList.value.some(
134+
(item) => isEmpty(item.value) || isEmpty(item.name)
135+
)
136+
if (hasEmptyValue) {
137+
callback(new Error('存在未填写的枚举值或描述'))
138+
return
139+
}
140+
141+
// 检查枚举值是否都是数字
142+
const hasInvalidNumber = dataSpecsList.value.some((item) => isNaN(Number(item.value)))
143+
if (hasInvalidNumber) {
144+
callback(new Error('存在非数字的枚举值'))
145+
return
146+
}
147+
148+
// 检查是否有重复的枚举值
149+
const values = dataSpecsList.value.map((item) => item.value)
150+
const uniqueValues = new Set(values)
151+
if (values.length !== uniqueValues.size) {
152+
callback(new Error('存在重复的枚举值'))
153+
return
154+
}
155+
156+
callback()
157+
}
54158
</script>
55159

56-
<style lang="scss" scoped></style>
160+
<style lang="scss" scoped>
161+
:deep(.el-form-item) {
162+
.el-form-item {
163+
margin-bottom: 0;
164+
}
165+
}
166+
</style>

0 commit comments

Comments
 (0)