2024-09-29 21:58:03 +08:00
|
|
|
<template>
|
2024-12-11 13:14:47 +08:00
|
|
|
<Dialog v-model="dialogVisible" :title="dialogTitle">
|
2024-09-29 21:58:03 +08:00
|
|
|
<el-form
|
|
|
|
ref="formRef"
|
2024-12-11 13:14:47 +08:00
|
|
|
v-loading="formLoading"
|
2024-09-29 21:58:03 +08:00
|
|
|
:model="formData"
|
|
|
|
:rules="formRules"
|
|
|
|
label-width="100px"
|
|
|
|
>
|
|
|
|
<el-form-item label="功能类型" prop="type">
|
2024-09-30 12:19:55 +08:00
|
|
|
<el-radio-group v-model="formData.type">
|
2024-12-11 13:14:47 +08:00
|
|
|
<el-radio-button :value="1"> 属性</el-radio-button>
|
|
|
|
<el-radio-button :value="2"> 服务</el-radio-button>
|
|
|
|
<el-radio-button :value="3"> 事件</el-radio-button>
|
2024-09-30 12:19:55 +08:00
|
|
|
</el-radio-group>
|
2024-09-29 21:58:03 +08:00
|
|
|
</el-form-item>
|
|
|
|
<el-form-item label="功能名称" prop="name">
|
|
|
|
<el-input v-model="formData.name" placeholder="请输入功能名称" />
|
|
|
|
</el-form-item>
|
|
|
|
<el-form-item label="标识符" prop="identifier">
|
2024-12-13 17:36:20 +08:00
|
|
|
<el-input v-model="formData.identifier" placeholder="请输入标识符" />
|
2024-09-29 21:58:03 +08:00
|
|
|
</el-form-item>
|
2024-12-15 23:15:51 +08:00
|
|
|
<!-- 属性配置 -->
|
2024-12-17 15:39:19 +08:00
|
|
|
<ThinkModelDataSpecs
|
2024-12-15 23:15:51 +08:00
|
|
|
v-if="formData.type === ProductFunctionTypeEnum.PROPERTY"
|
2024-12-16 17:47:02 +08:00
|
|
|
v-model="formData.property"
|
2024-12-15 23:15:51 +08:00
|
|
|
/>
|
2024-09-29 21:58:03 +08:00
|
|
|
</el-form>
|
|
|
|
|
|
|
|
<template #footer>
|
2024-12-11 13:14:47 +08:00
|
|
|
<el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
|
2024-09-29 21:58:03 +08:00
|
|
|
<el-button @click="dialogVisible = false">取 消</el-button>
|
|
|
|
</template>
|
|
|
|
</Dialog>
|
|
|
|
</template>
|
|
|
|
|
2024-12-11 13:14:47 +08:00
|
|
|
<script lang="ts" setup>
|
2024-12-07 18:50:19 +08:00
|
|
|
import { ProductVO } from '@/api/iot/product/product'
|
2024-12-17 15:39:19 +08:00
|
|
|
import ThinkModelDataSpecs from './ThinkModelDataSpecs.vue'
|
|
|
|
import { ProductFunctionTypeEnum, ThinkModelApi, ThinkModelData } from '@/api/iot/thinkmodel'
|
2024-12-11 13:14:47 +08:00
|
|
|
import { IOT_PROVIDE_KEY } from '@/views/iot/utils/constants'
|
2024-12-13 18:08:11 +08:00
|
|
|
import { DataSpecsDataType } from './config'
|
2024-12-16 17:47:02 +08:00
|
|
|
import { cloneDeep } from 'lodash-es'
|
2024-09-29 21:58:03 +08:00
|
|
|
|
2024-12-17 15:39:19 +08:00
|
|
|
defineOptions({ name: 'IoTProductThinkModelForm' })
|
2024-09-29 21:58:03 +08:00
|
|
|
|
2024-12-11 13:14:47 +08:00
|
|
|
const product = inject<Ref<ProductVO>>(IOT_PROVIDE_KEY.PRODUCT) // 注入产品信息
|
2024-09-29 21:58:03 +08:00
|
|
|
|
|
|
|
const { t } = useI18n()
|
|
|
|
const message = useMessage()
|
|
|
|
|
|
|
|
const dialogVisible = ref(false)
|
|
|
|
const dialogTitle = ref('')
|
|
|
|
const formLoading = ref(false)
|
|
|
|
const formType = ref('')
|
2024-12-17 15:39:19 +08:00
|
|
|
const formData = ref<ThinkModelData>({
|
2024-10-10 21:02:55 +08:00
|
|
|
type: ProductFunctionTypeEnum.PROPERTY,
|
2024-12-16 17:47:02 +08:00
|
|
|
dataType: DataSpecsDataType.INT,
|
2024-12-15 23:15:51 +08:00
|
|
|
property: {
|
|
|
|
dataType: DataSpecsDataType.INT,
|
2024-12-16 17:47:02 +08:00
|
|
|
dataSpecs: {
|
|
|
|
dataType: DataSpecsDataType.INT
|
|
|
|
}
|
2024-12-15 23:15:51 +08:00
|
|
|
}
|
2024-09-29 21:58:03 +08:00
|
|
|
})
|
|
|
|
const formRules = reactive({
|
2024-09-30 12:19:55 +08:00
|
|
|
name: [
|
|
|
|
{ required: true, message: '功能名称不能为空', trigger: 'blur' },
|
|
|
|
{
|
|
|
|
pattern: /^[\u4e00-\u9fa5a-zA-Z0-9][\u4e00-\u9fa5a-zA-Z0-9\-_/\.]{0,29}$/,
|
|
|
|
message:
|
|
|
|
'支持中文、大小写字母、日文、数字、短划线、下划线、斜杠和小数点,必须以中文、英文或数字开头,不超过 30 个字符',
|
|
|
|
trigger: 'blur'
|
|
|
|
}
|
|
|
|
],
|
2024-09-29 21:58:03 +08:00
|
|
|
type: [{ required: true, message: '功能类型不能为空', trigger: 'blur' }],
|
2024-09-30 12:19:55 +08:00
|
|
|
identifier: [
|
|
|
|
{ required: true, message: '标识符不能为空', trigger: 'blur' },
|
|
|
|
{
|
|
|
|
pattern: /^[a-zA-Z0-9_]{1,50}$/,
|
|
|
|
message: '支持大小写字母、数字和下划线,不超过 50 个字符',
|
|
|
|
trigger: 'blur'
|
|
|
|
},
|
|
|
|
{
|
2024-12-17 15:03:57 +08:00
|
|
|
validator: (_: any, value: string, callback: any) => {
|
2024-09-30 12:19:55 +08:00
|
|
|
const reservedKeywords = ['set', 'get', 'post', 'property', 'event', 'time', 'value']
|
|
|
|
if (reservedKeywords.includes(value)) {
|
|
|
|
callback(
|
|
|
|
new Error(
|
|
|
|
'set, get, post, property, event, time, value 是系统保留字段,不能用于标识符定义'
|
|
|
|
)
|
|
|
|
)
|
2024-11-03 00:15:44 +08:00
|
|
|
} else if (/^\d+$/.test(value)) {
|
|
|
|
callback(new Error('标识符不能是纯数字'))
|
2024-09-30 12:19:55 +08:00
|
|
|
} else {
|
|
|
|
callback()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
trigger: 'blur'
|
|
|
|
}
|
2024-12-17 15:03:57 +08:00
|
|
|
]
|
2024-09-29 21:58:03 +08:00
|
|
|
})
|
|
|
|
const formRef = ref()
|
|
|
|
|
2024-10-01 19:29:24 +08:00
|
|
|
/** 打开弹窗 */
|
2024-09-29 21:58:03 +08:00
|
|
|
const open = async (type: string, id?: number) => {
|
|
|
|
dialogVisible.value = true
|
|
|
|
dialogTitle.value = t('action.' + type)
|
|
|
|
formType.value = type
|
|
|
|
resetForm()
|
|
|
|
if (id) {
|
|
|
|
formLoading.value = true
|
|
|
|
try {
|
2024-12-17 15:39:19 +08:00
|
|
|
formData.value = await ThinkModelApi.getProductThinkModel(id)
|
2024-09-29 21:58:03 +08:00
|
|
|
} finally {
|
|
|
|
formLoading.value = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defineExpose({ open, close: () => (dialogVisible.value = false) })
|
|
|
|
|
2024-10-01 19:29:24 +08:00
|
|
|
/** 提交表单 */
|
2024-09-29 21:58:03 +08:00
|
|
|
const emit = defineEmits(['success'])
|
|
|
|
const submitForm = async () => {
|
|
|
|
await formRef.value.validate()
|
|
|
|
formLoading.value = true
|
|
|
|
try {
|
2024-12-17 15:39:19 +08:00
|
|
|
const data = cloneDeep(formData.value) as ThinkModelData
|
2024-12-16 17:47:02 +08:00
|
|
|
// 信息补全
|
2024-12-11 13:14:47 +08:00
|
|
|
data.productId = product!.value.id
|
|
|
|
data.productKey = product!.value.productKey
|
2024-12-16 17:47:02 +08:00
|
|
|
data.description = data.property.description
|
|
|
|
data.dataType = data.property.dataType
|
|
|
|
data.property.identifier = data.identifier
|
|
|
|
data.property.name = data.name
|
2024-09-29 21:58:03 +08:00
|
|
|
if (formType.value === 'create') {
|
2024-12-17 15:39:19 +08:00
|
|
|
await ThinkModelApi.createProductThinkModel(data)
|
2024-09-29 21:58:03 +08:00
|
|
|
message.success(t('common.createSuccess'))
|
|
|
|
} else {
|
2024-12-17 15:39:19 +08:00
|
|
|
await ThinkModelApi.updateProductThinkModel(data)
|
2024-09-29 21:58:03 +08:00
|
|
|
message.success(t('common.updateSuccess'))
|
|
|
|
}
|
2024-10-13 23:03:44 +08:00
|
|
|
} finally {
|
2024-09-29 21:58:03 +08:00
|
|
|
dialogVisible.value = false // 确保关闭弹框
|
|
|
|
emit('success')
|
|
|
|
formLoading.value = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-01 19:29:24 +08:00
|
|
|
/** 重置表单 */
|
2024-09-29 21:58:03 +08:00
|
|
|
const resetForm = () => {
|
|
|
|
formData.value = {
|
2024-10-10 21:02:55 +08:00
|
|
|
type: ProductFunctionTypeEnum.PROPERTY,
|
2024-12-16 17:47:02 +08:00
|
|
|
dataType: DataSpecsDataType.INT,
|
2024-12-15 23:15:51 +08:00
|
|
|
property: {
|
|
|
|
dataType: DataSpecsDataType.INT,
|
2024-12-16 17:47:02 +08:00
|
|
|
dataSpecs: {
|
|
|
|
dataType: DataSpecsDataType.INT
|
|
|
|
}
|
2024-12-15 23:15:51 +08:00
|
|
|
}
|
2024-09-29 21:58:03 +08:00
|
|
|
}
|
|
|
|
formRef.value?.resetFields()
|
|
|
|
}
|
2024-11-03 00:15:44 +08:00
|
|
|
</script>
|