LightMore.vue 9.0 KB

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