Skip to content

Commit c8ab57f

Browse files
author
puhui999
committed
【功能完善】IOT: 产品物模型-dataSpecs 校验规则完善
1 parent 6b9aaf3 commit c8ab57f

File tree

2 files changed

+98
-17
lines changed

2 files changed

+98
-17
lines changed

src/views/iot/product/product/detail/ThingModel/ThingModelDataSpecs.vue

Lines changed: 96 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<template>
2-
<el-form-item label="数据类型" prop="dataType">
2+
<el-form-item
3+
:rules="[{ required: true, message: '请选择数据类型', trigger: 'change' }]"
4+
label="数据类型"
5+
prop="property.dataType"
6+
>
37
<el-select v-model="property.dataType" placeholder="请选择数据类型" @change="handleChange">
48
<el-option
59
v-for="option in dataTypeOptions"
@@ -24,21 +28,43 @@
2428
v-model="property.dataSpecsList"
2529
/>
2630
<!-- 布尔型配置 -->
27-
<el-form-item v-if="property.dataType === DataSpecsDataType.BOOL" label="布尔值" prop="bool">
28-
<template v-for="item in property.dataSpecsList" :key="item.value">
31+
<el-form-item
32+
v-if="property.dataType === DataSpecsDataType.BOOL"
33+
:rules="[{ required: true, message: '请输入布尔值名称', trigger: 'blur' }]"
34+
label="布尔值"
35+
prop="property.dataSpecsList"
36+
>
37+
<template v-for="(item, index) in property.dataSpecsList" :key="item.value">
2938
<div class="flex items-center justify-start w-1/1 mb-5px">
3039
<span>{{ item.value }}</span>
3140
<span class="mx-2">-</span>
32-
<el-input
33-
v-model="item.name"
34-
:placeholder="`如:${item.value === 0 ? '关' : '开'}`"
35-
class="w-255px!"
36-
/>
41+
<el-form-item
42+
:prop="`property.dataSpecsList[${index}].name`"
43+
:rules="[
44+
{ required: true, message: '枚举描述不能为空' },
45+
{ validator: validateBoolName, trigger: 'blur' }
46+
]"
47+
class="flex-1 mb-0"
48+
>
49+
<el-input
50+
v-model="item.name"
51+
:placeholder="`如:${item.value === 0 ? '关' : '开'}`"
52+
class="w-255px!"
53+
/>
54+
</el-form-item>
3755
</div>
3856
</template>
3957
</el-form-item>
4058
<!-- 文本型配置 -->
41-
<el-form-item v-if="property.dataType === DataSpecsDataType.TEXT" label="数据长度" prop="text">
59+
<el-form-item
60+
v-if="property.dataType === DataSpecsDataType.TEXT"
61+
:rules="[
62+
{ required: true, message: '请输入文本字节长度', trigger: 'blur' },
63+
{ validator: validateTextLength, trigger: 'blur' }
64+
]"
65+
label="数据长度"
66+
prop="property.dataSpecs.length"
67+
>
4268
<el-input v-model="property.dataSpecs.length" class="w-255px!" placeholder="请输入文本字节长度">
4369
<template #append>字节</template>
4470
</el-input>
@@ -53,14 +79,24 @@
5379
v-model="property.dataSpecs"
5480
/>
5581
<!-- TODO puhui999: Struct 属性待完善 -->
56-
<el-form-item label="读写类型" prop="accessMode">
82+
<el-form-item
83+
:rules="[{ required: true, message: '请选择读写类型', trigger: 'change' }]"
84+
label="读写类型"
85+
prop="property.accessMode"
86+
>
5787
<el-radio-group v-model="property.accessMode">
5888
<el-radio label="rw">读写</el-radio>
5989
<el-radio label="r">只读</el-radio>
6090
</el-radio-group>
6191
</el-form-item>
6292
<el-form-item label="属性描述" prop="description">
63-
<el-input v-model="property.description" placeholder="请输入属性描述" type="textarea" />
93+
<el-input
94+
v-model="property.description"
95+
:maxlength="200"
96+
:rows="3"
97+
placeholder="请输入属性描述"
98+
type="textarea"
99+
/>
64100
</el-form-item>
65101
</template>
66102

@@ -73,6 +109,7 @@ import {
73109
ThingModelNumberTypeDataSpecs
74110
} from './dataSpecs'
75111
import { ThingModelProperty } from '@/api/iot/thinkmodelfunction'
112+
import { isEmpty } from '@/utils/is'
76113
77114
/** 物模型数据 */
78115
defineOptions({ name: 'ThingModelDataSpecs' })
@@ -105,6 +142,53 @@ const handleChange = (dataType: any) => {
105142
break
106143
}
107144
}
145+
146+
/** 校验布尔值名称 */
147+
const validateBoolName = (_: any, value: string, callback: any) => {
148+
if (isEmpty(value)) {
149+
callback(new Error('布尔值名称不能为空'))
150+
return
151+
}
152+
153+
// 检查开头字符
154+
if (!/^[\u4e00-\u9fa5a-zA-Z0-9]/.test(value)) {
155+
callback(new Error('布尔值名称必须以中文、英文字母或数字开头'))
156+
return
157+
}
158+
159+
// 检查整体格式
160+
if (!/^[\u4e00-\u9fa5a-zA-Z0-9][a-zA-Z0-9\u4e00-\u9fa5_-]*$/.test(value)) {
161+
callback(new Error('布尔值名称只能包含中文、英文字母、数字、下划线和短划线'))
162+
return
163+
}
164+
165+
// 检查长度(一个中文算一个字符)
166+
if (value.length > 20) {
167+
callback(new Error('布尔值名称长度不能超过20个字符'))
168+
return
169+
}
170+
171+
callback()
172+
}
173+
174+
/** 校验文本长度 */
175+
const validateTextLength = (_: any, value: any, callback: any) => {
176+
if (isEmpty(value)) {
177+
callback(new Error('文本长度不能为空'))
178+
return
179+
}
180+
if (isNaN(Number(value))) {
181+
callback(new Error('文本长度必须是数字'))
182+
return
183+
}
184+
callback()
185+
}
108186
</script>
109187

110-
<style lang="scss" scoped></style>
188+
<style lang="scss" scoped>
189+
:deep(.el-form-item) {
190+
.el-form-item {
191+
margin-bottom: 0;
192+
}
193+
}
194+
</style>

src/views/iot/product/product/detail/ThingModel/ThingModelForm.vue

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ const formData = ref<ThingModelData>({
6767
}
6868
}
6969
})
70-
// TODO puhui999: 表单校验待完善
7170
const formRules = reactive({
7271
name: [
7372
{ required: true, message: '功能名称不能为空', trigger: 'blur' },
@@ -87,7 +86,7 @@ const formRules = reactive({
8786
trigger: 'blur'
8887
},
8988
{
90-
validator: (rule, value, callback) => {
89+
validator: (_: any, value: string, callback: any) => {
9190
const reservedKeywords = ['set', 'get', 'post', 'property', 'event', 'time', 'value']
9291
if (reservedKeywords.includes(value)) {
9392
callback(
@@ -103,9 +102,7 @@ const formRules = reactive({
103102
},
104103
trigger: 'blur'
105104
}
106-
],
107-
'property.dataType.type': [{ required: true, message: '数据类型不能为空', trigger: 'blur' }],
108-
'property.accessMode': [{ required: true, message: '读写类型不能为空', trigger: 'blur' }]
105+
]
109106
})
110107
const formRef = ref()
111108

0 commit comments

Comments
 (0)