OperationCard.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <template>
  2. <view class="operation">
  3. <view class="operation__header">
  4. <image class="operation__icon" :src="iconSrc" mode="aspectFit" />
  5. <view class="operation__name">{{
  6. dourceData.display_name || dourceData.name || dourceData.code
  7. }}</view>
  8. </view>
  9. <view class="operation__body">
  10. <!-- 三状态:按钮形式(0 停止 1 正向 2 反向) -->
  11. <block v-if="hasPos">
  12. <view
  13. class="operation__btn"
  14. :class="btnClass(opt.value)"
  15. v-for="opt in options"
  16. :key="opt.value"
  17. @click="handleClick(opt.value)"
  18. >{{ opt.label }}</view
  19. >
  20. </block>
  21. <!-- 两状态:单个开关(1 开 0 关) -->
  22. <view v-else class="operation__switch-row">
  23. <view
  24. class="operation__switch-label"
  25. :class="{ on: statusValue == 1 }"
  26. >{{ statusValue == 1 ? '开' : '关' }}</view
  27. >
  28. <u-switch
  29. :value="statusValue == 1"
  30. active-color="#0bbc58"
  31. inactive-color="#C3CAD8"
  32. size="36"
  33. @input="handleSwitch"
  34. ></u-switch>
  35. </view>
  36. </view>
  37. </view>
  38. </template>
  39. <script>
  40. const ICONS = {
  41. inner_shade_on: 'https://s3.hnyfwlw.com/webstaticimg/bigdata_app/greenHouseIcon/inner_shade_on.png',
  42. inner_shade_off: 'https://s3.hnyfwlw.com/webstaticimg/bigdata_app/greenHouseIcon/inner_shade_off.png',
  43. outer_shade_on: 'https://s3.hnyfwlw.com/webstaticimg/bigdata_app/greenHouseIcon/outer_shade_on.png',
  44. outer_shade_off: 'https://s3.hnyfwlw.com/webstaticimg/bigdata_app/greenHouseIcon/outer_shade_off.png',
  45. roof_window_on: 'https://s3.hnyfwlw.com/webstaticimg/bigdata_app/greenHouseIcon/roof_window_on.png',
  46. roof_window_off: 'https://s3.hnyfwlw.com/webstaticimg/bigdata_app/greenHouseIcon/roof_window_off.png',
  47. side_window_on: 'https://s3.hnyfwlw.com/webstaticimg/bigdata_app/greenHouseIcon/side_window_on.png',
  48. side_window_off: 'https://s3.hnyfwlw.com/webstaticimg/bigdata_app/greenHouseIcon/side_window_off.png',
  49. fan_on: 'https://s3.hnyfwlw.com/webstaticimg/bigdata_app/greenHouseIcon/fan_on.png',
  50. fan_off: 'https://s3.hnyfwlw.com/webstaticimg/bigdata_app/greenHouseIcon/fan_off.png',
  51. };
  52. export default {
  53. name: 'OperationCard',
  54. props: {
  55. dourceData: {
  56. type: Object,
  57. default: () => ({}),
  58. },
  59. },
  60. computed: {
  61. // 存在 CurrentPos 字段即为三状态(0 停止 1 正向 2 反向),否则两状态(0 停止 1 开启)
  62. // CurrentPos 可能是明文字段或 ChannelParamSetXX:CurrentPos 平铺属性,只用于判定形态
  63. hasPos() {
  64. const d = this.dourceData;
  65. if ('CurrentPos' in d) {
  66. return true;
  67. }
  68. return Object.keys(d).some((k) =>
  69. /^ChannelParamSet\d+:CurrentPos$/.test(k)
  70. );
  71. },
  72. options() {
  73. return [
  74. { label: '正向', value: 1 },
  75. { label: '反向', value: 2 },
  76. { label: '停止', value: 0 },
  77. ];
  78. },
  79. // 通道遥测 key:形如 ChannelParamSet00:Status
  80. statusKey() {
  81. const d = this.dourceData;
  82. const code = d.code || d.sfCode;
  83. if (code && /^ChannelParamSet\d+:Status$/.test(String(code))) {
  84. return String(code);
  85. }
  86. return (
  87. Object.keys(d).find((k) => /^ChannelParamSet\d+:Status$/.test(k)) || ''
  88. );
  89. },
  90. // Status 值存放的属性名:同名属性或 value
  91. statusField() {
  92. const d = this.dourceData;
  93. return this.statusKey && this.statusKey in d ? this.statusKey : 'value';
  94. },
  95. // 高亮始终取 Status 值:三状态 0 停止 1 正向 2 反向,两状态 0 停止 1 开启
  96. statusValue() {
  97. const d = this.dourceData;
  98. const val = this.statusKey ? d[this.statusKey] : d.value;
  99. return val == null ? 0 : Number(val);
  100. },
  101. // 按通道名匹配图标,随运行状态切换 on/off 变体
  102. iconSrc() {
  103. const d = this.dourceData;
  104. const name = String(d.display_name || d.name || '');
  105. let base = '';
  106. if (name.includes('内遮阳')) {
  107. base = 'inner_shade';
  108. } else if (name.includes('外遮阳')) {
  109. base = 'outer_shade';
  110. } else if (name.includes('顶开窗') || name.includes('顶通风')) {
  111. base = 'roof_window';
  112. } else if (name.includes('侧开窗') || name.includes('侧通风')) {
  113. base = 'side_window';
  114. } else if (name.includes('风机')) {
  115. base = 'fan';
  116. } else {
  117. base = this.hasPos ? 'inner_shade' : 'roof_window';
  118. }
  119. const state = this.statusValue == 0 ? 'off' : 'on';
  120. return ICONS[`${base}_${state}`];
  121. },
  122. },
  123. methods: {
  124. // 三状态按钮高亮:正/反向绿色,停止橙色
  125. btnClass(value) {
  126. if (Number(value) !== this.statusValue) {
  127. return '';
  128. }
  129. return Number(value) === 0
  130. ? 'operation__btn--stop'
  131. : 'operation__btn--run';
  132. },
  133. handleClick(value) {
  134. this.$emit('change', {
  135. dourceData: this.dourceData,
  136. value,
  137. code: this.statusKey || this.dourceData.code || '',
  138. field: this.statusField,
  139. });
  140. },
  141. // 两状态开关:切换后 开=1 关=0
  142. handleSwitch(newValue) {
  143. this.handleClick(newValue ? 1 : 0);
  144. },
  145. },
  146. };
  147. </script>
  148. <style scoped lang="scss">
  149. .operation {
  150. // 卡片浅灰底,与头部渐变底色衔接;白色按钮直接落在灰底上
  151. background: #eff2fa;
  152. border-radius: 16rpx;
  153. margin-bottom: 24rpx;
  154. overflow: hidden;
  155. &__header {
  156. display: flex;
  157. align-items: center;
  158. padding: 20rpx 24rpx;
  159. background: linear-gradient(180deg, #eff4ff 20.24%, #eff2fa 100%);
  160. }
  161. &__icon {
  162. flex-shrink: 0;
  163. width: 44rpx;
  164. height: 44rpx;
  165. margin-right: 12rpx;
  166. }
  167. &__name {
  168. color: #333333;
  169. font-family: 'Source Han Sans CN VF';
  170. font-size: 28rpx;
  171. font-weight: 500;
  172. overflow: hidden;
  173. white-space: nowrap;
  174. text-overflow: ellipsis;
  175. }
  176. &__body {
  177. display: flex;
  178. align-items: center;
  179. padding: 20rpx 24rpx 24rpx;
  180. }
  181. &__btn {
  182. flex: 1;
  183. height: 72rpx;
  184. display: flex;
  185. align-items: center;
  186. justify-content: center;
  187. border-radius: 12rpx;
  188. background: #ffffff;
  189. border: 2rpx solid #eef0f5;
  190. box-sizing: border-box;
  191. color: #999999;
  192. font-family: 'Source Han Sans CN VF';
  193. font-size: 26rpx;
  194. font-weight: 400;
  195. margin-right: 16rpx;
  196. &:last-child {
  197. margin-right: 0;
  198. }
  199. &--run {
  200. background: #0bbc58;
  201. border-color: #0bbc58;
  202. color: #ffffff;
  203. font-weight: 500;
  204. }
  205. &--stop {
  206. background: #ffa200;
  207. border-color: #ffa200;
  208. color: #ffffff;
  209. font-weight: 500;
  210. }
  211. }
  212. &__switch-row {
  213. flex: 1;
  214. display: flex;
  215. align-items: center;
  216. justify-content: space-between;
  217. }
  218. &__switch-label {
  219. color: #999999;
  220. font-family: 'Source Han Sans CN VF';
  221. font-size: 26rpx;
  222. font-weight: 400;
  223. &.on {
  224. color: #333333;
  225. }
  226. }
  227. }
  228. </style>