gaoluyang
2026-06-24 bfdc0e0e6d5e47aa501f9b6fadd143d9c97cf00a
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
<!-- dataType:number 数组类型 -->
<script lang="ts" setup>
import type { Ref } from 'vue';
 
import type { ThingModelApi } from '#/api/iot/thingmodel';
 
import { computed } from 'vue';
 
import { DICT_TYPE } from '../../../../../packages/constants/src';
import { getDictOptions } from '../../../../../packages/effects/hooks/src';
 
import { useVModel } from '@vueuse/core';
import { Form, Input, Select } from 'ant-design-vue';
 
const props = defineProps<{ modelValue: any }>();
const emits = defineEmits(['update:modelValue']);
const dataSpecs = useVModel(
  props,
  'modelValue',
  emits,
) as Ref<ThingModelApi.DataSpecsNumberData>;
 
/** 单位字典选项 */
const unitOptions = computed(() =>
  getDictOptions(DICT_TYPE.IOT_THING_MODEL_UNIT, 'string'),
);
 
/** 单位下拉变化时,拆出 unitName 与 unit 回写 */
function unitChange(unitSpecs: any) {
  if (!unitSpecs) {
    return;
  }
  const [unitName, unit] = String(unitSpecs).split('-');
  dataSpecs.value.unitName = unitName;
  dataSpecs.value.unit = unit;
}
 
/** 校验最小值 */
function validateMin(_rule: any, _value: any, callback: any) {
  const min = Number(dataSpecs.value.min);
  const max = Number(dataSpecs.value.max);
  if (Number.isNaN(min)) {
    callback(new Error('请输入有效的数值'));
    return;
  }
  if (!Number.isNaN(max) && min >= max) {
    callback(new Error('最小值必须小于最大值'));
    return;
  }
  callback();
}
 
/** 校验最大值 */
function validateMax(_rule: any, _value: any, callback: any) {
  const min = Number(dataSpecs.value.min);
  const max = Number(dataSpecs.value.max);
  if (Number.isNaN(max)) {
    callback(new Error('请输入有效的数值'));
    return;
  }
  if (!Number.isNaN(min) && max <= min) {
    callback(new Error('最大值必须大于最小值'));
    return;
  }
  callback();
}
 
/** 校验步长 */
function validateStep(_rule: any, _value: any, callback: any) {
  const step = Number(dataSpecs.value.step);
  if (Number.isNaN(step)) {
    callback(new Error('请输入有效的数值'));
    return;
  }
  if (step <= 0) {
    callback(new Error('步长必须大于 0'));
    return;
  }
  const min = Number(dataSpecs.value.min);
  const max = Number(dataSpecs.value.max);
  if (!Number.isNaN(min) && !Number.isNaN(max) && step > max - min) {
    callback(new Error('步长不能大于最大值与最小值的差值'));
    return;
  }
  callback();
}
</script>
 
<template>
  <Form.Item label="取值范围">
    <div class="flex items-center justify-between">
      <Form.Item
        :name="['property', 'dataSpecs', 'min']"
        :rules="[
          { required: true, message: '最小值不能为空', trigger: 'blur' },
          { validator: validateMin, trigger: 'blur' },
        ]"
        class="mb-0 flex-1"
      >
        <Input v-model:value="dataSpecs.min" placeholder="请输入最小值" />
      </Form.Item>
      <span class="mx-2">~</span>
      <Form.Item
        :name="['property', 'dataSpecs', 'max']"
        :rules="[
          { required: true, message: '最大值不能为空', trigger: 'blur' },
          { validator: validateMax, trigger: 'blur' },
        ]"
        class="mb-0 flex-1"
      >
        <Input v-model:value="dataSpecs.max" placeholder="请输入最大值" />
      </Form.Item>
    </div>
  </Form.Item>
  <Form.Item
    :name="['property', 'dataSpecs', 'step']"
    :rules="[
      { required: true, message: '步长不能为空', trigger: 'blur' },
      { validator: validateStep, trigger: 'blur' },
    ]"
    label="步长"
  >
    <Input v-model:value="dataSpecs.step" placeholder="请输入步长" />
  </Form.Item>
  <Form.Item
    :name="['property', 'dataSpecs', 'unit']"
    :rules="[{ required: true, message: '请选择单位', trigger: 'change' }]"
    label="单位"
  >
    <Select
      :value="dataSpecs.unit ? `${dataSpecs.unitName}-${dataSpecs.unit}` : ''"
      show-search
      placeholder="请选择单位"
      class="w-full"
      @change="unitChange"
    >
      <Select.Option
        v-for="(item, index) in unitOptions"
        :key="index"
        :value="`${item.label}-${item.value}`"
      >
        {{ `${item.label}-${item.value}` }}
      </Select.Option>
    </Select>
  </Form.Item>
</template>
 
<style lang="scss" scoped>
:deep(.ant-form-item) {
  .ant-form-item {
    margin-bottom: 0;
  }
}
</style>