LightMore.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <template>
  2. <div class="more-box">
  3. <div class="light-more-top">
  4. <div class="left">
  5. <div class="light-box">
  6. <img
  7. :src="lampData.isOpen ? lampOpenIcon : lampCloseIcon"
  8. alt=""
  9. :style="lampData.isOpen ? { width: '58px', height: '62px' } : ''"
  10. />
  11. </div>
  12. <div class="light-name">{{ $t(`lamp.${lampData.lampStatusText}`) }}</div>
  13. </div>
  14. <div class="right">
  15. <div
  16. class="control"
  17. :ref="setRef"
  18. @click="handleSwitch('isOpen', true)"
  19. >{{ $t(`common.全开`) }}</div>
  20. <div
  21. class="control"
  22. :ref="setRef"
  23. @click="handleSwitch('isOpen', false)"
  24. >{{ $t(`common.全关`) }}</div>
  25. </div>
  26. </div>
  27. <div
  28. class="light-middle"
  29. v-if="lampData.isOpen"
  30. >
  31. <div class="bright-slider">
  32. <Slider
  33. :min="min"
  34. :max="max"
  35. v-model="lampData.brightValue"
  36. isFollow
  37. showValue
  38. suffixNormal
  39. suffix="%"
  40. @onEnd="setLampStatus('brightValue')"
  41. />
  42. </div>
  43. <!-- <div class="temp-slider">
  44. <LampSlider v-model="lampData.colorTempValue" @onEnd="setLampStatus('colorTempValue')" />
  45. </div> -->
  46. </div>
  47. <div
  48. class="divider"
  49. v-if="lampData.isOpen"
  50. >
  51. <img
  52. src="@/assets/svg/line.svg"
  53. alt=""
  54. />
  55. </div>
  56. <div class="light-bottom">
  57. <div
  58. class="item-box"
  59. :class="item.valueSwitch ? 'light-box-active ' : ''"
  60. v-for="(item, index) in lampList"
  61. >
  62. <div class="name">{{ item.localName }}</div>
  63. <div style="width: 100rpx;">
  64. <SwitchButton
  65. :loading="allLampStatus[item.id]?.loading"
  66. v-model="item.isOpen"
  67. @change="sigleLampChange('isOpen', item, 'single')"
  68. />
  69. </div>
  70. </div>
  71. </div>
  72. </div>
  73. </template>
  74. <script setup>
  75. import lampCloseIcon from '@/assets/taiguv1/svg/lamp_close_p_icon.svg'
  76. import lampOpenIcon from '@/assets/taiguv1/svg/lamp_open_p_icon.svg'
  77. import Slider from '@/components/slider/Slider.vue'
  78. import SwitchButton from '@/components/switch-button/SwitchButton.vue'
  79. import { computed, nextTick, onMounted, onUnmounted, ref, watch } from 'vue'
  80. import useDeviceControl from '@/hooks/useDeviceControl'
  81. import { useStore } from '@/store'
  82. const store = useStore()
  83. const deviceControl = useDeviceControl()
  84. const min = 0
  85. const max = 100
  86. // 接收父组件传递的初始状态
  87. const props = defineProps({
  88. lampStatus: {
  89. type: Object,
  90. default: () => {
  91. return {
  92. isOpen: false,
  93. brightValue: 0
  94. }
  95. }
  96. },
  97. equipList: {
  98. type: Array,
  99. default: () => {
  100. return []
  101. }
  102. }
  103. })
  104. const controlBtn = ref([])
  105. const setRef = el => {
  106. if (el) {
  107. if (!controlBtn.value.includes(el)) {
  108. controlBtn.value.push(el)
  109. }
  110. }
  111. }
  112. const lampList = ref(props.equipList || [])
  113. const lampData = ref(props.lampStatus)
  114. const allLampStatus = computed(() => store.state.taiguv1.lampSwitchStatus)
  115. // const lampSwitchStatus = computed(() => {
  116. // let statusText = ''
  117. // let switchStatus = false
  118. // let arr = props.equipList.filter(item => item.runStatus == 1)
  119. // if (arr.length == 0) {
  120. // statusText = '全部关闭'
  121. // switchStatus = false
  122. // } else {
  123. // statusText = arr.length > 0 && arr.length < props.equipList.length ? '部分开启' : '全部开启'
  124. // switchStatus = true
  125. // }
  126. // return {
  127. // statusText,
  128. // switchStatus
  129. // }
  130. // })
  131. watch(
  132. () => props.lampStatus,
  133. (newVal, oldVal) => {
  134. if (!newVal) {
  135. return
  136. }
  137. lampData.value = { ...newVal }
  138. },
  139. { deep: true } // 添加深度监听
  140. )
  141. watch(
  142. () => props.equipList,
  143. (newVal, oldVal) => {
  144. compareStatus(newVal)
  145. },
  146. { deep: true }
  147. )
  148. // 对比和store中开关状态
  149. const compareStatus = data => {
  150. lampList.value = data.map(item => {
  151. const currentStatus = allLampStatus.value[item.id]
  152. let isOpen = item.brightValue!==0
  153. // 基础状态对象
  154. const baseStatus = {
  155. ...item,
  156. isOpen: isOpen
  157. }
  158. if (!currentStatus) {
  159. return baseStatus
  160. }
  161. // 如果最后切换状态与当前运行状态相同,重置状态
  162. if (currentStatus.lastSwitchStatus == isOpen) {
  163. store.dispatch('taiguv1/setLampStatus', {
  164. id: item.id,
  165. status: {
  166. loading: false,
  167. lastSwitchStatus: null
  168. }
  169. })
  170. return baseStatus
  171. }
  172. // 如果有待处理的切换状态,使用该状态
  173. return {
  174. ...item,
  175. isOpen: currentStatus.lastSwitchStatus ?? isOpen
  176. }
  177. })
  178. }
  179. // 整体开关
  180. const handleSwitch = (type, value) => {
  181. lampData.value.isOpen = value
  182. setLampStatus(type)
  183. }
  184. // 单个空调开关
  185. const sigleLampChange = (type, source, all) => {
  186. if (type == 'isOpen') {
  187. store.dispatch('taiguv1/setLampStatus', {
  188. id: source.id,
  189. status: {
  190. loading: true,
  191. lastSwitchStatus: source.isOpen
  192. }
  193. })
  194. }
  195. if (all == 'single') {
  196. const params = deviceControl.assemblyLampCommand(source[type], type, source)
  197. deviceControl.sendCommands(params)
  198. }
  199. }
  200. const setLampStatus = type => {
  201. if (type == 'isOpen') {
  202. store.dispatch('taiguv1/setLampStatus', {
  203. id: 'all',
  204. status: {
  205. loading: true,
  206. lastSwitchStatus: lampData.value.isOpen
  207. }
  208. })
  209. lampList.value.forEach(item => {
  210. item.isOpen = lampData.value.isOpen
  211. sigleLampChange(type, item, 'all')
  212. })
  213. }
  214. const params = deviceControl.assemblyLampCommand(lampData.value[type], type, lampList.value)
  215. deviceControl.sendCommands(params)
  216. }
  217. onMounted(() => {
  218. nextTick(() => {
  219. controlBtn.value.forEach(button => {
  220. button.addEventListener('touchstart', handleTouchStart)
  221. })
  222. })
  223. })
  224. // 添加 touchstart 处理函数
  225. const handleTouchStart = event => {
  226. const button = event.currentTarget
  227. button.classList.add('active')
  228. setTimeout(() => {
  229. button.classList.remove('active')
  230. }, 200)
  231. }
  232. // 添加 onUnmounted 钩子
  233. onUnmounted(() => {
  234. controlBtn.value.forEach(button => {
  235. button.removeEventListener('touchstart', handleTouchStart)
  236. })
  237. })
  238. </script>
  239. <style lang="scss" scoped>
  240. .more-box {
  241. .light-more-top {
  242. display: flex;
  243. align-items: center;
  244. margin-bottom: 36px;
  245. .left {
  246. margin-right: 36px;
  247. .light-box {
  248. width: 110px;
  249. height: 110px;
  250. background: rgba(255, 255, 255, 0.4);
  251. border-radius: 50%;
  252. text-align: center;
  253. margin-right: 27px;
  254. img {
  255. width: 36px;
  256. height: 36px;
  257. margin: 0 auto;
  258. margin-top: 37px;
  259. }
  260. }
  261. .light-name {
  262. //styleName: Chi/Body Large;
  263. margin-top: 12px;
  264. font-family: PingFang SC;
  265. width: 110px;
  266. font-size: 16px;
  267. font-weight: 300;
  268. line-height: 22px;
  269. letter-spacing: 0.02em;
  270. text-align: center;
  271. color: rgba(0, 20, 40, 1);
  272. }
  273. }
  274. .right {
  275. height: 92px;
  276. display: flex;
  277. flex-direction: column;
  278. justify-content: space-between;
  279. .control {
  280. width: 100px;
  281. height: 40px;
  282. line-height: 40px;
  283. border-radius: 50px;
  284. border-radius: 50px;
  285. background: #e1e1df;
  286. box-shadow: 0px 10px 20px 0px rgba(0, 0, 0, 0.05);
  287. color: #001428;
  288. //styleName: Chi/Body Small;
  289. font-family: PingFang SC;
  290. font-size: 12px;
  291. font-weight: 400;
  292. letter-spacing: 0.04em;
  293. text-align: center;
  294. }
  295. .control.active {
  296. background: rgba(255, 255, 255, 1);
  297. box-shadow: 0px 10px 20px 0px rgba(0, 20, 40, 0.1);
  298. }
  299. }
  300. }
  301. .light-middle {
  302. // height: 100px;
  303. // background: #fff;
  304. .bright-slider {
  305. width: 100%;
  306. height: 62px;
  307. background: rgba(255, 255, 255, 0.6);
  308. margin-bottom: 12px;
  309. border-radius: 12px;
  310. }
  311. .temp-slider {
  312. width: 100%;
  313. height: 58px;
  314. padding: 2px;
  315. border-radius: 12px;
  316. background: var(--White-White-60, rgba(255, 255, 255, 0.6));
  317. margin-bottom: 12px;
  318. }
  319. }
  320. .divider {
  321. height: 24px;
  322. display: flex;
  323. align-items: center;
  324. margin-bottom: 12px;
  325. }
  326. .light-bottom {
  327. // margin-top: 36px;
  328. .item-box {
  329. display: flex;
  330. justify-content: space-between;
  331. align-items: center;
  332. width: 300px;
  333. height: 68px;
  334. box-sizing: border-box;
  335. padding: 20px 24px;
  336. border-radius: 24px 24px 44px 24px;
  337. background: rgba(255, 255, 255, 0.2);
  338. box-shadow: 0px 10px 20px 0px rgba(0, 20, 40, 0.05);
  339. margin-bottom: 12px;
  340. .name {
  341. //styleName: Chi/Body Regular;
  342. font-family: PingFang SC;
  343. font-size: 14px;
  344. font-weight: 400;
  345. line-height: 19px;
  346. letter-spacing: 0.02em;
  347. color: rgba(0, 20, 40, 1);
  348. }
  349. }
  350. .light-box-active {
  351. background: rgba(255, 255, 255, 0.6);
  352. }
  353. }
  354. .switch-btn {
  355. margin-top: 0;
  356. width: 50px !important;
  357. height: 28px !important;
  358. border: none;
  359. }
  360. }
  361. </style>
  362. <style lang="scss">
  363. .more-box {
  364. .van-loading__spinner {
  365. color: $elActiveColor !important;
  366. }
  367. .van-switch__loading {
  368. top: 0;
  369. left: 0;
  370. width: 100%;
  371. height: 100%;
  372. line-height: 1;
  373. }
  374. .van-switch--disabled {
  375. opacity: 0.5;
  376. }
  377. }
  378. </style>