SelectCs.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <template>
  2. <div class="select-cs">
  3. <div v-if="config && config.switch1 && config.switch2 && config.switch3" class="select-item">
  4. <div class="select-item-box">
  5. <a-select v-model:value="selectVal" :bordered="false" style="width: 100%">
  6. <a-select-option v-for="item in options" :key="item.value" :value="item.value">{{ item.label }}</a-select-option>
  7. </a-select>
  8. </div>
  9. <div class="select-item-box">
  10. <span>{{ config.switch1.label[0] }}</span>
  11. <a-switch
  12. :disabled="config.switch1.disabled"
  13. :checked="getFormattedText(devicedata, config.switch1.value, config.switch1.trans)"
  14. @update:checked="handlUpdate($event, config.switch1)"
  15. />
  16. <span>{{ config.switch1.label[1] }}</span>
  17. </div>
  18. </div>
  19. <div v-if="config && config.switch2 && config.switch3" class="select-item">
  20. <div class="select-item-box1">
  21. <span>{{ config.switch2.label[0] }}</span>
  22. <a-switch
  23. :disabled="config.switch2.disabled"
  24. :checked="getFormattedText(devicedata, config.switch2.value, config.switch2.trans)"
  25. @update:checked="handlUpdate($event, config.switch2)"
  26. />
  27. </div>
  28. <div class="select-item-box1">
  29. <span>{{ config.switch3.label[0] }}</span>
  30. <a-switch
  31. :disabled="config.switch3.disabled"
  32. :checked="getFormattedText(devicedata, config.switch3.value, config.switch3.trans)"
  33. @update:checked="handlUpdate($event, config.switch3)"
  34. />
  35. </div>
  36. </div>
  37. </div>
  38. </template>
  39. <script lang="ts" setup>
  40. import { computed, ref } from 'vue';
  41. import { getData, getFormattedText } from '../../hooks/helper';
  42. import { deviceControlApi } from '/@/api/vent';
  43. import { message } from 'ant-design-vue';
  44. interface SwitchConfig {
  45. /** 提交控制时 启动 状态对应的key */
  46. keyOn: string;
  47. /** 提交控制时 关闭 状态对应的key */
  48. keyOff: string;
  49. /** 提交控制时 启动 状态对应的value */
  50. valueOn: string;
  51. /** 提交控制时 关闭 状态对应的value */
  52. valueOff: string;
  53. /** 提示文本,按需从数组中依次读取内容 */
  54. label: string[];
  55. disabled?: boolean;
  56. /** 判断该项的默认状态时用到的value,formatter格式 */
  57. value: string;
  58. /** 如果value不是boolean类型,需要给出trans转换 */
  59. trans: any;
  60. }
  61. interface CSConfig {
  62. selector: {
  63. label: string;
  64. value: string;
  65. readFrom: string;
  66. };
  67. switch1: SwitchConfig;
  68. switch2: SwitchConfig;
  69. switch3: SwitchConfig;
  70. }
  71. const props = defineProps<{
  72. config: CSConfig;
  73. devicedata: Record<string, any>;
  74. }>();
  75. const options = computed(() => {
  76. const { readFrom, value, label } = props.config.selector;
  77. const res = getData(props.devicedata.value, readFrom);
  78. return (Array.isArray(res) ? res : [res]).map((item) => {
  79. return {
  80. label: getFormattedText(item, label),
  81. value: getFormattedText(item, value),
  82. };
  83. });
  84. });
  85. const selectVal = ref(undefined);
  86. function handlUpdate(checked: boolean, item: SwitchConfig) {
  87. deviceControlApi({
  88. deviceid: selectVal,
  89. paramcode: checked ? item.keyOn : item.keyOff,
  90. value: checked ? item.valueOn : item.valueOff,
  91. })
  92. .then((r) => {
  93. if (!r.success) throw r.message || '操作失败';
  94. message.success('指令下发成功');
  95. })
  96. .catch((e) => {
  97. message.error(typeof e === 'string' ? e : '指令下发失败');
  98. });
  99. }
  100. </script>
  101. <style lang="less" scoped>
  102. .select-cs {
  103. height: 100%;
  104. padding: 5px 15px;
  105. box-sizing: border-box;
  106. width: 100%;
  107. overflow-y: auto;
  108. .select-item {
  109. display: flex;
  110. width: 100%;
  111. height: calc(50% - 2px);
  112. &:nth-child(1) {
  113. margin-bottom: 2px;
  114. padding: 0px 10px;
  115. background: url('@/assets/images/vent/homeNew/databg/11.png') no-repeat;
  116. background-size: 100% 100%;
  117. }
  118. &:nth-child(2) {
  119. margin-top: 2px;
  120. }
  121. .select-item-box {
  122. display: flex;
  123. justify-content: center;
  124. align-items: center;
  125. width: 50%;
  126. height: 34px;
  127. padding-top: 6px;
  128. box-sizing: border-box;
  129. &:nth-child(1) {
  130. background: url('@/assets/images/vent/homeNew/databg/bg-line.png') no-repeat right;
  131. }
  132. span {
  133. margin: 0px 10px;
  134. }
  135. }
  136. .select-item-box1 {
  137. display: flex;
  138. justify-content: space-between;
  139. width: 100%;
  140. height: 100%;
  141. padding: 10px 20px 0px 20px;
  142. box-sizing: border-box;
  143. background: url('@/assets/images/vent/homeNew/databg/13.png') no-repeat;
  144. background-size: 100% 100%;
  145. }
  146. }
  147. ::v-deep .zxm-select-selector {
  148. background-color: transparent;
  149. height: 28px;
  150. padding: 0 10px;
  151. }
  152. ::v-deep .zxm-switch {
  153. border-top: 1px solid rgba(45, 187, 255);
  154. border-bottom: 1px solid rgba(45, 187, 255);
  155. border-right: 1px solid rgba(45, 187, 255);
  156. }
  157. }
  158. </style>