GatewayParamExt.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <script setup lang="ts">
  2. import { computed, ref } from 'vue';
  3. import { addUnit } from '@/utils';
  4. import type { CSSProperties } from 'vue';
  5. interface Props {
  6. extInfo: {
  7. deviceName?: string;
  8. deviceParam?: string;
  9. deviceNum?: string;
  10. moduleName?: string;
  11. moduleParam?: string;
  12. moduleNum?: string;
  13. statusName?: string;
  14. statusParam?: string;
  15. };
  16. }
  17. type NumValue = string | number | undefined;
  18. const props = defineProps<Props>();
  19. const emit = defineEmits<{
  20. change: [
  21. param: {
  22. paramName: string;
  23. paramCode: string;
  24. gatewayParamExt: string;
  25. },
  26. ];
  27. }>();
  28. const deviceNum = ref<NumValue>(props.extInfo.deviceNum);
  29. const moduleNum = ref<NumValue>(props.extInfo.moduleNum);
  30. const deviceNumInputStyle = computed<CSSProperties>(() => {
  31. return {
  32. width: addUnit(getInputWidth(deviceNum.value)),
  33. };
  34. });
  35. const moduleNumInputStyle = computed<CSSProperties>(() => {
  36. return {
  37. width: addUnit(getInputWidth(moduleNum.value)),
  38. };
  39. });
  40. const getInputWidth = (num: NumValue): number => {
  41. if (!num || String(num).length < 2) {
  42. return 32;
  43. } else if (String(num).length < 3) {
  44. return 40;
  45. }
  46. return 50;
  47. };
  48. const handleChange = () => {
  49. const { deviceName, deviceParam, moduleName, moduleParam, statusName, statusParam } = props.extInfo;
  50. const deviceNumNameStr = deviceNum.value ? deviceNum.value : '';
  51. const deviceNumCodeStr = deviceNum.value ? `_${deviceNum.value}_` : '';
  52. const moduleNumNameStr = moduleNum.value ? moduleNum.value : '';
  53. const moduleNumCodeStr = moduleNum.value ? `_${moduleNum.value}_` : '';
  54. const paramName = `${deviceName ?? ''}${deviceNumNameStr}${moduleName ?? ''}${moduleNumNameStr}${statusName ?? ''}`;
  55. const paramCode = `${deviceParam ?? ''}${deviceNumCodeStr}${moduleParam ?? ''}${moduleNumCodeStr}${statusParam ?? ''}`;
  56. const gatewayParamExt = {
  57. ...props.extInfo,
  58. };
  59. if (deviceName) {
  60. gatewayParamExt.deviceNum = deviceNum.value ? String(deviceNum.value) : '';
  61. }
  62. if (moduleName) {
  63. gatewayParamExt.moduleNum = moduleNum.value ? String(moduleNum.value) : '';
  64. }
  65. emit('change', {
  66. paramName,
  67. paramCode,
  68. gatewayParamExt: JSON.stringify(gatewayParamExt),
  69. });
  70. };
  71. </script>
  72. <template>
  73. <div class="gateway-param-ext-container">
  74. <template v-if="extInfo.deviceName && extInfo.deviceNum !== undefined">
  75. {{ extInfo.deviceName }}
  76. <AInputNumber
  77. class="gateway-param-ext-input"
  78. :style="deviceNumInputStyle"
  79. v-model:value="deviceNum"
  80. :min="0"
  81. :precision="0"
  82. :controls="false"
  83. @change="handleChange"
  84. />
  85. </template>
  86. <template v-if="extInfo.moduleName && extInfo.moduleNum !== undefined">
  87. {{ extInfo.moduleName }}
  88. <AInputNumber
  89. class="gateway-param-ext-input"
  90. :style="moduleNumInputStyle"
  91. v-model:value="moduleNum"
  92. :min="0"
  93. :precision="0"
  94. :controls="false"
  95. @change="handleChange"
  96. />
  97. </template>
  98. <span>{{ extInfo.statusName }}</span>
  99. </div>
  100. </template>
  101. <style lang="scss" scoped>
  102. .gateway-param-ext-container {
  103. display: flex;
  104. flex-wrap: wrap;
  105. align-items: center;
  106. }
  107. .gateway-param-ext-input {
  108. width: 32px;
  109. height: 32px;
  110. margin-inline: 2px;
  111. }
  112. </style>