LightTemp.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. <template>
  2. <div class="light-temp">
  3. <div class="light-tep-top">
  4. <span>氛围灯</span>
  5. <!-- <Switch
  6. v-model="lampSw"
  7. inactive-color="rgba(196, 196, 196, 0.4)"
  8. class="switch-btn"
  9. /> -->
  10. </div>
  11. <div class="bright-box" v-if="lightData.bright">
  12. <div class="bight-text">
  13. <span>亮度</span>
  14. <span>{{ lightData.brightValueRate }}%</span>
  15. </div>
  16. <div class="light-control">
  17. <div class="bight-slider" id="sliderId1">
  18. <div class="item-now" :style="{ width: itemWidth }"></div>
  19. <div class="slider-bar" id="handId1"></div>
  20. </div>
  21. </div>
  22. </div>
  23. <div class="bright-box" v-if="lightData.colorTemperature">
  24. <div class="bight-text">
  25. <span>色温</span>
  26. <span>{{ lightData.colorTempValueRate }}%</span>
  27. </div>
  28. <div class="light-control">
  29. <div class="bight-slider temp-slider" id="sliderId2">
  30. <div class="slider-bar" id="handId2"></div>
  31. </div>
  32. </div>
  33. </div>
  34. </div>
  35. </template>
  36. <script lang="ts">
  37. import {
  38. defineComponent,
  39. onMounted,
  40. reactive,
  41. toRefs,
  42. computed,
  43. watch,
  44. onUnmounted,
  45. onBeforeMount,
  46. onBeforeUnmount,
  47. nextTick,
  48. } from "vue";
  49. import { Switch, Dialog, Toast } from "vant";
  50. import { swiper } from "@/utils/swiper";
  51. import { querySpaceConditioners, setSpaceCondtioners } from "@/apis/envmonitor";
  52. import { parseImgUrl, setQueryConfig } from "@/utils";
  53. import any = jasmine.any;
  54. export default defineComponent({
  55. props: {
  56. lightData: {
  57. type: Object,
  58. default: () => {},
  59. },
  60. },
  61. components: { Switch },
  62. setup(props, contx) {
  63. const propsVal = props;
  64. let timeTemp: any = null;
  65. let timeColor: any = null;
  66. const proxyData = reactive({
  67. lampSw: false,
  68. itemWidth: "0%",
  69. lightData: props.lightData,
  70. swiperIinit: false,
  71. // 状态控制调整
  72. setSpaceContrl(data: any) {
  73. setSpaceCondtioners(data).then((res: any) => {});
  74. },
  75. timeTemp: timeTemp,
  76. timeTempNum: 1,
  77. timeColor: timeColor,
  78. timeColorNum: 1,
  79. getTimeNum(newTime: any = "") {
  80. let time: any = (new Date().getTime() - newTime) / 1000;
  81. let num: any = parseInt(time);
  82. if (num == 1) {
  83. return true;
  84. } else {
  85. return false;
  86. }
  87. },
  88. // 改变当前空间设置的值
  89. changContrlVal(type: any, tmerType: any = 2) {
  90. let data: any = [];
  91. if (type == 1) {
  92. if (
  93. proxyData.lightData.brightValue < proxyData.lightData.brightMinValue
  94. ) {
  95. proxyData.lightData.brightValue =
  96. proxyData.lightData.brightMinValue;
  97. }
  98. proxyData.lightData.brightValue = Math.floor(
  99. proxyData.lightData.brightValue
  100. );
  101. data = [
  102. {
  103. id: proxyData.lightData.id,
  104. code: "brightSet",
  105. value: proxyData.lightData.brightValue,
  106. },
  107. ];
  108. if (tmerType == 2) {
  109. if (proxyData.getTimeNum(proxyData.timeTemp)) {
  110. proxyData.setSpaceContrl(data);
  111. proxyData.timeTemp = new Date().getTime();
  112. }
  113. } else {
  114. proxyData.setSpaceContrl(data);
  115. }
  116. } else if (type == 2) {
  117. if (
  118. proxyData.lightData.colorTempValue <
  119. proxyData.lightData.colorTempMinValue
  120. ) {
  121. proxyData.lightData.colorTempValue =
  122. proxyData.lightData.colorTempMinValue;
  123. }
  124. proxyData.lightData.colorTempValue = Math.floor(
  125. proxyData.lightData.colorTempValue
  126. );
  127. data = [
  128. {
  129. id: proxyData.lightData.id,
  130. code: "colorTemperatureSet",
  131. value: proxyData.lightData.colorTempValue,
  132. },
  133. ];
  134. if (tmerType == 2) {
  135. if (proxyData.getTimeNum(proxyData.timeColor)) {
  136. proxyData.setSpaceContrl(data);
  137. proxyData.timeColor = new Date().getTime();
  138. }
  139. } else {
  140. proxyData.setSpaceContrl(data);
  141. }
  142. }
  143. },
  144. // 控制结束处理
  145. changContrlValEnd(type: any) {
  146. if (type == 1) {
  147. proxyData.changContrlVal(1, 1);
  148. } else if (type == 2) {
  149. proxyData.changContrlVal(2, 1);
  150. }
  151. },
  152. // 定时控制
  153. setTimeContrl(type: any) {
  154. if (type == 1) {
  155. proxyData.timeTemp = new Date().getTime();
  156. } else if (type == 2) {
  157. proxyData.timeColor = new Date().getTime();
  158. }
  159. },
  160. // 设置调节滚动条的最大值和最小值
  161. setLightDataMax() {
  162. proxyData.lightData.brightMaxValue = proxyData.lightData.brightMaxValue
  163. ? proxyData.lightData.brightMaxValue
  164. : 100;
  165. proxyData.lightData.colorTempMaxValue = proxyData.lightData
  166. .colorTempMaxValue
  167. ? proxyData.lightData.colorTempMaxValue
  168. : 100;
  169. },
  170. // 计算滚动条样式的值
  171. setBarValue(type: any, sliderWidth: any, left: any, barWidth: any) {
  172. proxyData.setLightDataMax();
  173. if (type == 1) {
  174. // 亮度
  175. let value: any =
  176. proxyData.lightData.brightMaxValue -
  177. proxyData.lightData.brightMinValue;
  178. let bright: any = value / (sliderWidth - barWidth);
  179. let rate: any = 100 / value;
  180. proxyData.lightData.brightValueRate = left * bright * rate;
  181. proxyData.lightData.brightValueRate = Math.floor(
  182. proxyData.lightData.brightValueRate
  183. );
  184. let width: any =
  185. proxyData.lightData.brightValueRate >= 98
  186. ? 98
  187. : proxyData.lightData.brightValueRate;
  188. proxyData.lightData.brightValue =
  189. proxyData.lightData.brightValueRate * rate;
  190. proxyData.itemWidth = left + barWidth / 2 + "px";
  191. } else if (type == 2) {
  192. let value: any =
  193. proxyData.lightData.colorTempMaxValue -
  194. proxyData.lightData.colorTempMinValue;
  195. // 色温
  196. let colorVal: any = value / (sliderWidth - barWidth);
  197. let rate: any = 100 / value;
  198. proxyData.lightData.colorTempValueRate = left * colorVal * rate;
  199. proxyData.lightData.colorTempValueRate = Math.floor(
  200. proxyData.lightData.colorTempValueRate
  201. );
  202. proxyData.lightData.colorTempValue =
  203. proxyData.lightData.colorTempValueRate * rate;
  204. }
  205. },
  206. // 根据当前值设置页面的位置
  207. getDefaultVal(type: any, sliderWidth: any, barWidth: any, handBox: any) {
  208. // console.log("调用了----");
  209. let left: any = "";
  210. if (type == 1) {
  211. proxyData.lightData.brightValue =
  212. proxyData.lightData.brightValue <=
  213. proxyData.lightData.brightMinValue
  214. ? 0
  215. : proxyData.lightData.brightValue;
  216. if (
  217. proxyData.lightData.brightValue > proxyData.lightData.brightMaxValue
  218. ) {
  219. proxyData.lightData.brightValue =
  220. proxyData.lightData.brightMaxValue;
  221. }
  222. let bright: any =
  223. proxyData.lightData.brightMaxValue / (sliderWidth - barWidth);
  224. let rate: any = 100 / proxyData.lightData.brightMaxValue;
  225. // debugger;
  226. left = proxyData.lightData.brightValue / rate / bright / rate;
  227. proxyData.lightData.brightValueRate = Math.floor(
  228. proxyData.lightData.brightValue / rate
  229. );
  230. proxyData.itemWidth = left + barWidth / 2 + "px";
  231. } else if (type == 2) {
  232. proxyData.lightData.colorTempValue =
  233. proxyData.lightData.colorTempValue <=
  234. proxyData.lightData.colorTempMinValue
  235. ? 0
  236. : proxyData.lightData.colorTempValue;
  237. if (
  238. proxyData.lightData.colorTempValue >
  239. proxyData.lightData.colorTempMaxValue
  240. ) {
  241. proxyData.lightData.colorTempValue =
  242. proxyData.lightData.colorTempMaxValue;
  243. }
  244. let colorVal: any =
  245. proxyData.lightData.colorTempMaxValue / (sliderWidth - barWidth);
  246. let rate: any = 100 / proxyData.lightData.colorTempMaxValue;
  247. left = proxyData.lightData.colorTempValue / rate / colorVal / rate;
  248. proxyData.lightData.colorTempValueRate = Math.floor(
  249. proxyData.lightData.colorTempValue / rate
  250. );
  251. }
  252. handBox.style.left = left + "px";
  253. },
  254. //亮度滑块滑动
  255. endBoxSwiper() {
  256. proxyData.swiperIinit = true;
  257. let handBox: any = document.querySelector("#handId1");
  258. let sliderBox: any = document.querySelector("#sliderId1");
  259. let isMove: any = false;
  260. let barLeft: any = 0;
  261. let sliderWidth: any = sliderBox ? sliderBox.offsetWidth : 1;
  262. if (!handBox) {
  263. return;
  264. }
  265. let barWidth: any = handBox.getBoundingClientRect().width;
  266. proxyData.getDefaultVal(1, sliderWidth, barWidth, handBox);
  267. handBox.addEventListener("touchstart", function (e: any) {
  268. barLeft = isNaN(parseInt(handBox.style.left))
  269. ? 0
  270. : parseInt(handBox.style.left);
  271. isMove = true;
  272. proxyData.setTimeContrl(1);
  273. });
  274. handBox.addEventListener("touchend", function (e: any) {
  275. isMove = false;
  276. proxyData.changContrlValEnd(1);
  277. });
  278. swiper(handBox, {
  279. swipeLeft: function (e: any) {
  280. if (isMove) {
  281. barLeft = Math.abs(barLeft);
  282. let moveRealX: any = Math.abs(e.mation.moveX - e.mation.startX);
  283. let left: any = barLeft - moveRealX;
  284. left = left < 0 ? 0 : left;
  285. // debugger
  286. handBox.style.left = left + "px";
  287. proxyData.setBarValue(1, sliderWidth, left, barWidth);
  288. proxyData.changContrlVal(1);
  289. }
  290. },
  291. swipeRight: function (e: any) {
  292. // debugger;
  293. if (isMove) {
  294. barLeft = Math.abs(barLeft);
  295. let moveRealX: any = Math.abs(e.mation.moveX - e.mation.startX);
  296. let left: any = barLeft + moveRealX;
  297. left =
  298. left >= sliderWidth - barWidth ? sliderWidth - barWidth : left;
  299. handBox.style.left = left + "px";
  300. proxyData.setBarValue(1, sliderWidth, left, barWidth);
  301. proxyData.changContrlVal(1);
  302. }
  303. },
  304. });
  305. },
  306. // 色温滑块滑动
  307. tempBoxSwiper() {
  308. proxyData.swiperIinit = true;
  309. let handBox: any = document.querySelector("#handId2");
  310. let sliderBox: any = document.querySelector("#sliderId2");
  311. let isMove: any = false;
  312. let barLeft: any = 0;
  313. let sliderWidth: any = sliderBox ? sliderBox.offsetWidth : 1;
  314. if (!handBox) {
  315. return;
  316. }
  317. let barWidth: any = handBox.getBoundingClientRect().width;
  318. proxyData.getDefaultVal(2, sliderWidth, barWidth, handBox);
  319. handBox.addEventListener("touchstart", function (e: any) {
  320. barLeft = isNaN(parseInt(handBox.style.left))
  321. ? 0
  322. : parseInt(handBox.style.left);
  323. isMove = true;
  324. proxyData.setTimeContrl(2);
  325. });
  326. handBox.addEventListener("touchend", function (e: any) {
  327. isMove = false;
  328. proxyData.changContrlValEnd(2);
  329. });
  330. swiper(handBox, {
  331. swipeLeft: function (e: any) {
  332. if (isMove) {
  333. barLeft = Math.abs(barLeft);
  334. let moveRealX: any = Math.abs(e.mation.moveX - e.mation.startX);
  335. let left: any = barLeft - moveRealX;
  336. left = left < 0 ? 0 : left;
  337. // debugger
  338. handBox.style.left = left + "px";
  339. proxyData.setBarValue(2, sliderWidth, left, barWidth);
  340. proxyData.changContrlVal(2);
  341. }
  342. },
  343. swipeRight: function (e: any) {
  344. // debugger;
  345. if (isMove) {
  346. barLeft = Math.abs(barLeft);
  347. let moveRealX: any = Math.abs(e.mation.moveX - e.mation.startX);
  348. let left: any = barLeft + moveRealX;
  349. left =
  350. left >= sliderWidth - barWidth ? sliderWidth - barWidth : left;
  351. handBox.style.left = left + "px";
  352. proxyData.setBarValue(2, sliderWidth, left, barWidth);
  353. proxyData.changContrlVal(2);
  354. }
  355. },
  356. });
  357. },
  358. // 初始化滑动
  359. async barSwiperInit() {
  360. nextTick(() => {
  361. proxyData.endBoxSwiper();
  362. proxyData.tempBoxSwiper();
  363. });
  364. },
  365. });
  366. watch(props, (newProps: any) => {});
  367. onBeforeUnmount(() => {});
  368. onMounted(() => {
  369. proxyData.barSwiperInit();
  370. });
  371. return {
  372. ...toRefs(proxyData),
  373. };
  374. },
  375. });
  376. </script>
  377. <style lang="scss" scoped>
  378. .light-temp {
  379. padding: 10px;
  380. .light-tep-top {
  381. display: flex;
  382. padding-bottom: 60px;
  383. line-height: 31px;
  384. justify-content: space-between;
  385. span {
  386. font-family: "PingFang SC";
  387. font-style: normal;
  388. font-weight: 500;
  389. font-size: 22px;
  390. color: #000000;
  391. }
  392. }
  393. .bright-box {
  394. margin-bottom: 60px;
  395. .bight-text {
  396. display: flex;
  397. justify-content: space-between;
  398. padding-bottom: 15px;
  399. span {
  400. font-family: "PingFang SC";
  401. font-style: normal;
  402. font-weight: 400;
  403. font-size: 18px;
  404. line-height: 25px;
  405. color: #1b2129;
  406. }
  407. }
  408. .light-control {
  409. .bight-slider {
  410. position: relative;
  411. display: flex;
  412. height: 32px;
  413. flex: 1;
  414. background: #e8ecf0;
  415. border-radius: 25px;
  416. .item-now {
  417. background: #ffe399;
  418. border-radius: 25px 0 0 25px;
  419. }
  420. .slider-bar {
  421. position: absolute;
  422. width: 38px;
  423. height: 38px;
  424. background: #f6f9fe;
  425. left: 0;
  426. bottom: -2px;
  427. z-index: 333;
  428. border-radius: 50%;
  429. border: 2px solid #fff;
  430. box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.06),
  431. 0px 0px 8px rgba(0, 0, 0, 0.1);
  432. }
  433. }
  434. .temp-slider {
  435. background: linear-gradient(
  436. 90deg,
  437. #ffc079 0%,
  438. #ffe7d1 43.1%,
  439. #f1efff 74.36%,
  440. #c8d6ff 100%
  441. );
  442. }
  443. }
  444. .light-control-padding {
  445. padding-top: 35px;
  446. padding-bottom: 15px;
  447. }
  448. }
  449. }
  450. </style>