Commit ce1d11fc34af2f0d42982039d0fc75b803d83c7b
Merge branch 'fix/attribute-distribution' into 'main_dev'
fix: 修改属性下发数字精度问题 See merge request yunteng/thingskit-front!797
Showing
1 changed file
with
23 additions
and
5 deletions
... | ... | @@ -90,6 +90,14 @@ |
90 | 90 | return array; |
91 | 91 | }; |
92 | 92 | |
93 | + // 获取小数 | |
94 | + const getFloatPart = (number): any => { | |
95 | + number = number.toString(); | |
96 | + const floatPartStartIndex = number.indexOf('.'); | |
97 | + const value = ~floatPartStartIndex ? `0.${number.substring(floatPartStartIndex + 1)}` : '0'; | |
98 | + return value; | |
99 | + }; | |
100 | + | |
93 | 101 | const { createMessage } = useMessage(); |
94 | 102 | const loading = ref(false); |
95 | 103 | const handleSend = async () => { |
... | ... | @@ -110,8 +118,12 @@ |
110 | 118 | const oldValue = getFieldsValue()[unref(formField)]; |
111 | 119 | modBUSForm.value.registerNumber = 1; |
112 | 120 | modBUSForm.value.registerValues = [oldValue]; |
121 | + | |
113 | 122 | if (unref(isShowMultiply) && unref(modBUSForm).method == '06') { |
114 | - if (String(oldValue * unref(zoomFactorValue)).indexOf('.') > -1) { | |
123 | + const newValue = | |
124 | + Math.floor(oldValue) * unref(zoomFactorValue) + | |
125 | + getFloatPart(oldValue) * unref(zoomFactorValue); | |
126 | + if (newValue % 1 != 0) { | |
115 | 127 | createMessage.warning(`属性下发类型必须是整数,缩放因子为${unref(zoomFactorValue)}`); |
116 | 128 | return; |
117 | 129 | } |
... | ... | @@ -121,13 +133,19 @@ |
121 | 133 | return; |
122 | 134 | } |
123 | 135 | //bool类型的就不用去乘缩放因子了 |
124 | - modBUSForm.value.registerValues = [oldValue * unref(zoomFactorValue)]; | |
136 | + modBUSForm.value.registerValues = [newValue]; | |
125 | 137 | } |
126 | 138 | |
127 | 139 | if (unref(modBUSForm).method == '16' || unref(modBUSForm).method == '10') { |
128 | - const newValue = getArray( | |
129 | - SingleToHex(unref(isShowMultiply) ? oldValue * unref(zoomFactorValue) : oldValue) | |
130 | - ); | |
140 | + const regex = /^-?\d+(\.\d{0,2})?$/; | |
141 | + const values = | |
142 | + Math.floor(oldValue) * unref(zoomFactorValue) + | |
143 | + getFloatPart(oldValue) * unref(zoomFactorValue); | |
144 | + if (!regex.test(values as any)) { | |
145 | + createMessage.warning(`属性下发值精确到两位小数,缩放因子是${unref(zoomFactorValue)}`); | |
146 | + return; | |
147 | + } | |
148 | + const newValue = getArray(SingleToHex(unref(isShowMultiply) ? values : oldValue)); | |
131 | 149 | modBUSForm.value.registerValues = newValue; |
132 | 150 | modBUSForm.value.registerNumber = 2; |
133 | 151 | modBUSForm.value.method = '10'; | ... | ... |