indexCopy.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. <template>
  2. <div class="light" v-if="lampList && lampList.length">
  3. <div class="light-top">
  4. <div class="light-desc">
  5. <p class="light-title">
  6. {{ isShowChildLight ? "光照" : lampList[0].localName }}
  7. </p>
  8. <p :class="isShowChildLight ? 'light-status' : 'light-status'">
  9. {{ lampSw ? "照明已开启" : "照明已关闭" }}
  10. </p>
  11. <div class="show-all" @click.stop="showAll" v-if="isShowChildLight">
  12. <van-icon :name="lightIcon" class="light-icon" />
  13. <span>显示全部</span>
  14. </div>
  15. </div>
  16. <div class="light-right">
  17. <img :src="lightImg" alt="" />
  18. <Switch
  19. v-if="!showFlag"
  20. :disabled="!userIsControl"
  21. v-model="lampSw"
  22. @click="eqChange('allLamp', '', 0)"
  23. inactive-color="rgba(196, 196, 196, 0.4)"
  24. class="switch-btn"
  25. />
  26. </div>
  27. </div>
  28. <!--灯控制按钮-->
  29. <template v-if="showFlag && isShowChildLight">
  30. <div
  31. class="light-control"
  32. v-for="(item, index) in lampList"
  33. :key="item.id"
  34. >
  35. <div class="control-title">
  36. {{ item.localName }}
  37. </div>
  38. <Switch
  39. active-color="$elActiveColor"
  40. :disabled="!userIsControl"
  41. v-model="item.switch"
  42. @click="eqChange('main', item, index)"
  43. inactive-color="rgba(196, 196, 196, 0.2)"
  44. class="switch-btn"
  45. />
  46. </div>
  47. </template>
  48. </div>
  49. </template>
  50. <script lang="ts">
  51. import {
  52. defineComponent,
  53. computed,
  54. onMounted,
  55. reactive,
  56. toRefs,
  57. getCurrentInstance,
  58. watch,
  59. onBeforeMount,
  60. onUnmounted,
  61. onBeforeUnmount,
  62. } from "vue";
  63. import { Switch, Toast } from "vant";
  64. import { getLampHttp, getStatusHttp, setallLampHttp } from "@/apis/envmonitor";
  65. import { parseImgUrl } from "@/utils";
  66. export default defineComponent({
  67. props: {
  68. projectId: {
  69. type: String,
  70. default: () => "",
  71. },
  72. controlMode: {
  73. type: Number,
  74. default: () => 0,
  75. },
  76. spaceId: {
  77. type: String,
  78. default: () => "",
  79. },
  80. userIsControl: {
  81. type: Boolean,
  82. default: () => false,
  83. },
  84. forceOverTimeFlag: {
  85. type: Boolean,
  86. default: () => false,
  87. },
  88. seviceEquipmentList: {
  89. // 是否走服务定制的设备
  90. type: Array,
  91. default: () => [],
  92. },
  93. },
  94. components: { Switch },
  95. setup(props, contx) {
  96. const lampList: any = [];
  97. let lightsStatusTimer: any = null;
  98. const timeOut: any = null;
  99. const initData: any = [];
  100. let lightParams: any = [];
  101. const proxyData = reactive({
  102. seviceEquipmentList: props.seviceEquipmentList,
  103. controlMode: props.controlMode,
  104. userIsControl: props.userIsControl,
  105. forceOverTimeFlag: props.forceOverTimeFlag,
  106. setStatus: false,
  107. lightParams: lightParams,
  108. allowSvg: "down_Arrow.svg",
  109. timeOut: timeOut,
  110. lightIcon: "arrow-down",
  111. showFlag: false,
  112. loadingLight: false,
  113. initData: initData,
  114. lampList: lampList,
  115. lightsStatusTimer: lightsStatusTimer,
  116. startCheckLightsTime: 0,
  117. lightImg: parseImgUrl("page-officehome", "lamp_close_v2.png"),
  118. lampSw: false, // 主灯开关
  119. // 点击展示所有的登录
  120. showAll() {
  121. proxyData.showFlag = !proxyData.showFlag;
  122. proxyData.lightIcon = "arrow-down";
  123. },
  124. // 检查设备是否执行空间服务时间
  125. checkDeviceIsExeSpaceTime(deviceAll: any = []) {
  126. if (proxyData.controlMode !== 1) {
  127. return true;
  128. }
  129. let seviceEquipmentList: any = proxyData.seviceEquipmentList;
  130. let flag: any = false;
  131. for (let i = 0; i < seviceEquipmentList.length; i++) {
  132. for (let j = 0; j < deviceAll.length; j++) {
  133. if (
  134. seviceEquipmentList[i].id == deviceAll[j].id &&
  135. seviceEquipmentList[i].isExeSpaceTime
  136. ) {
  137. flag = true;
  138. break;
  139. }
  140. }
  141. if (flag) {
  142. break;
  143. }
  144. }
  145. return flag;
  146. },
  147. // 获取灯的状态
  148. getLampList() {
  149. getLampHttp({ spaceId: props.spaceId })
  150. .then((res) => {
  151. const resData: any = res;
  152. if (!proxyData.setStatus) {
  153. if (resData && resData.result == "success") {
  154. let content = resData?.content ?? [];
  155. let lampOpen = false; // 如果有一个开 则总灯开
  156. for (let i = 0; i < content.length; i++) {
  157. content[i].type = "lamp";
  158. content[i].imgOpen = parseImgUrl(
  159. "page-officehome",
  160. "lampch_open.png"
  161. );
  162. content[i].imgClose = parseImgUrl(
  163. "page-officehome",
  164. "lampch_close.png"
  165. );
  166. content[i].switch = content[i].runStatus ? true : false;
  167. if (content[i].runStatus) {
  168. lampOpen = true;
  169. }
  170. content[i].loading = false;
  171. }
  172. proxyData.initData = JSON.parse(JSON.stringify(content)); // 灯的数据
  173. proxyData.lampSw = lampOpen; // 主灯开关
  174. proxyData.lampList = content;
  175. proxyData.lightImg = proxyData.lampSw
  176. ? parseImgUrl("page-officehome", "lamp_open_v2.png")
  177. : parseImgUrl("page-officehome", "lamp_close_v2.png");
  178. }
  179. }
  180. console.log("执行了----====");
  181. proxyData.startLightsStatusTimer();
  182. })
  183. .catch(() => {
  184. proxyData.startLightsStatusTimer();
  185. });
  186. },
  187. // 调整灯
  188. eqChange(type: any, item: any, index: any) {
  189. if (proxyData.userIsControl) {
  190. // debugger
  191. if (type === "allLamp") {
  192. let isExeSpaceTime: Boolean = proxyData.checkDeviceIsExeSpaceTime(
  193. proxyData.lampList
  194. );
  195. if (
  196. proxyData.forceOverTimeFlag &&
  197. proxyData.lampSw &&
  198. isExeSpaceTime
  199. ) {
  200. // 强制加班开灯
  201. contx.emit("triggerWork", 3);
  202. return;
  203. }
  204. // 总开关按钮所有灯
  205. // 当前要执行的灯的操作
  206. proxyData.loadingLight = true;
  207. // const statusFlag = !proxyData.lampSw
  208. // console.log('proxyData.lampSw==')
  209. const statusFlag = proxyData.lampSw;
  210. // 瞬间修改状态
  211. proxyData.setStatus = true;
  212. proxyData.updateAllLampStatus(statusFlag);
  213. proxyData.setLamp(type, "", statusFlag, 0); // 调接口
  214. } else {
  215. let isExeSpaceTime: Boolean = proxyData.checkDeviceIsExeSpaceTime([
  216. item,
  217. ]);
  218. if (proxyData.forceOverTimeFlag && item.switch && isExeSpaceTime) {
  219. // 强制加班开灯
  220. contx.emit("triggerWork", 3, item);
  221. return;
  222. }
  223. //当前要执行的灯的操作
  224. // 按钮loading
  225. item.loading = true;
  226. const statusFlag: any = item.switch;
  227. let id = item.id;
  228. proxyData.setStatus = true;
  229. proxyData.updateLampStatus(item, statusFlag);
  230. proxyData.setLamp(type, id, statusFlag, index); // 调接口
  231. }
  232. } else {
  233. Toast("您没有当前空间的控制权限!");
  234. }
  235. },
  236. // 控制灯的接口
  237. setLamp(type: string, id: any, statusFlag: any, index: any) {
  238. let params: any = [];
  239. if (type === "allLamp") {
  240. for (let i = 0; i < proxyData.initData.length; i++) {
  241. proxyData.initData[i].switch = statusFlag;
  242. }
  243. params = proxyData.initData;
  244. } else {
  245. params = [
  246. {
  247. id: id,
  248. switch: statusFlag,
  249. },
  250. ];
  251. }
  252. proxyData.lightParams = params;
  253. // 调服务端的接口像后台发送灯的操作的指令
  254. setallLampHttp(params)
  255. .then((res) => {
  256. const resData: any = res;
  257. // set成功后1s关闭定时器
  258. setTimeout(() => {
  259. proxyData.setStatus = false;
  260. }, 1000);
  261. if (resData.result === "success") {
  262. // const checkData: any = proxyData.judgeChangeResponeseSuccess(resData)
  263. // proxyData.getTimeLampStatus(checkData, type, id, statusFlag, index)
  264. } else {
  265. proxyData.initLampLoading();
  266. }
  267. })
  268. .catch(() => {
  269. setTimeout(() => {
  270. proxyData.setStatus = false;
  271. }, 1000);
  272. });
  273. },
  274. // 报错后关闭灯的loading
  275. initLampLoading() {
  276. proxyData.loadingLight = false;
  277. proxyData.lampList.map((item: any) => {
  278. item.loading = false;
  279. });
  280. },
  281. //手动修改灯的状态
  282. updateAllLampStatus(statusFlag: any) {
  283. proxyData.lampSw = statusFlag;
  284. for (let i = 0; i < proxyData.lampList.length; i++) {
  285. proxyData.lampList[i].switch = statusFlag;
  286. // if (proxyData.lampList[i].type == 'lamp') {
  287. // }
  288. }
  289. },
  290. // 单个灯的状态修改
  291. updateLampStatus(item: any, statusFlag: any) {
  292. item.switch = statusFlag;
  293. // let flag: any = true
  294. // for (let i = 0; i < proxyData.lampList.length; i++) {
  295. // if (proxyData.lampList[i].type == 'lamp') {
  296. // if (proxyData.lampList[i].switch !== statusFlag) {
  297. // flag = false
  298. // break
  299. // }
  300. // }
  301. // }
  302. },
  303. // 轮询查看状态
  304. getTimeLampStatus(
  305. checkData: any,
  306. type: any,
  307. id: any,
  308. statusFlag: any,
  309. index: any
  310. ) {
  311. if (checkData["success"] && checkData["success"].length) {
  312. if (type === "allLamp") {
  313. // 点击主灯按钮
  314. proxyData.updateAllLampLoading(checkData["success"]); // 所有灯
  315. } else {
  316. // 当个的时候修改loading的状态
  317. proxyData.updateLampLoading(index);
  318. }
  319. proxyData.getLampList();
  320. }
  321. if (checkData["processing"] && checkData["processing"].length) {
  322. let checkParams: any = proxyData.lightParams;
  323. let processArr: any = checkData["processing"];
  324. let params: any = [];
  325. processArr.map((item: any) => {
  326. let obj: any = {
  327. id: item.id,
  328. orderSeqNum: item.orderSeqNum,
  329. };
  330. for (let i = 0; i < checkParams.length; i++) {
  331. if (item.id === checkParams[i].id) {
  332. obj.switch = checkParams[i].switch;
  333. break;
  334. }
  335. }
  336. params.push(obj);
  337. });
  338. proxyData.checkChangeLightStatusSuccess(
  339. params,
  340. type,
  341. id,
  342. statusFlag,
  343. index
  344. );
  345. }
  346. if (checkData["error"] && checkData["error"].length) {
  347. if (type === "allLamp") {
  348. // 点击主灯按钮
  349. proxyData.updateAllLampLoading(checkData["error"]); // 所有灯
  350. } else {
  351. // 当个的时候修改loading的状态
  352. proxyData.updateLampLoading(index);
  353. }
  354. }
  355. },
  356. // 查看指令是否成功完成
  357. judgeChangeResponeseSuccess(resData: any) {
  358. let data: any = resData?.content ?? [];
  359. let processingArr: any = [];
  360. let successArr: any = [];
  361. let errorArr: any = [];
  362. for (let i = 0; i < data.length; i++) {
  363. let item: any = data[i];
  364. if ((item.result = "success")) {
  365. if (item.state === 200 && item.exeResult === "success") {
  366. successArr.push(item);
  367. } else if (
  368. (item.state === 200 && item.exeResult === "processing:rcvd") ||
  369. (item.state === 202 && !item.exeResult)
  370. ) {
  371. processingArr.push(item);
  372. } else {
  373. errorArr.push(item);
  374. }
  375. } else {
  376. errorArr.push(item);
  377. }
  378. }
  379. let obj: any = {
  380. success: successArr,
  381. processing: processingArr,
  382. error: errorArr,
  383. };
  384. return obj;
  385. },
  386. // 操作成功后更新所有灯的真实状态
  387. updateAllLampLoading(arr: any) {
  388. for (let i = 0; i < proxyData.lampList.length; i++) {
  389. if (proxyData.lampList[i].type == "lamp") {
  390. arr.map((item: any) => {
  391. if (proxyData.lampList[i].id === item.id) {
  392. proxyData.lampList[i].loading = false;
  393. }
  394. });
  395. }
  396. }
  397. let loadingLen = proxyData.lampList.filter((item: any) => {
  398. return item.loading;
  399. });
  400. if (loadingLen.length === 0) {
  401. proxyData.loadingLight = false;
  402. }
  403. },
  404. // 动态切换主灯的状态
  405. updateLampLoading(index: any) {
  406. proxyData.lampList[index].loading = false;
  407. },
  408. // 灯的指令完成后更新状态
  409. checkChangeLightStatusSuccess(
  410. checkParams: any,
  411. type: any,
  412. id: any,
  413. value: any,
  414. index: any
  415. ) {
  416. // 轮询查询灯的状态
  417. getStatusHttp(checkParams).then((res) => {
  418. const resData: any = res;
  419. const checkData: any = proxyData.judgeChangeResponeseSuccess(resData);
  420. proxyData.getTimeLampStatus(checkData, type, id, value, index);
  421. });
  422. },
  423. // 灯灯接口清除定时器
  424. clearLightStatusTimer() {
  425. clearTimeout(proxyData.lightsStatusTimer);
  426. proxyData.lightsStatusTimer = null;
  427. },
  428. // 定时刷新接口
  429. startLightsStatusTimer(timerLen: any = 2000) {
  430. proxyData.lightsStatusTimer = setTimeout(() => {
  431. proxyData.getLampList();
  432. proxyData.clearLightStatusTimer();
  433. }, timerLen);
  434. },
  435. });
  436. const isShowChildLight = computed(() => {
  437. return proxyData.lampList.length && proxyData.lampList.length > 1;
  438. });
  439. watch(
  440. props,
  441. (newProps: any) => {
  442. // 定时调空间信息
  443. proxyData.userIsControl = newProps.userIsControl;
  444. proxyData.controlMode = newProps.controlMode;
  445. proxyData.forceOverTimeFlag = newProps.forceOverTimeFlag;
  446. proxyData.seviceEquipmentList = newProps.seviceEquipmentList;
  447. if (newProps.spaceId) {
  448. // 空间id改变的重新获取值调用接口
  449. proxyData.clearLightStatusTimer();
  450. proxyData.getLampList();
  451. console.log("调用了====");
  452. }
  453. },
  454. {
  455. deep: false,
  456. immediate: true,
  457. }
  458. );
  459. onBeforeUnmount(() => {
  460. proxyData.clearLightStatusTimer();
  461. });
  462. onMounted(() => {});
  463. return {
  464. isShowChildLight,
  465. ...toRefs(proxyData),
  466. };
  467. },
  468. });
  469. </script>
  470. <style lang="scss" scoped>
  471. .light {
  472. width: 100%;
  473. background: #ffffff;
  474. box-shadow: 0px 0px 2px rgba(0, 0, 0, 0.07), 0px 4px 10px rgba(0, 0, 0, 0.05);
  475. border-radius: 25px;
  476. margin: 15px 0px;
  477. }
  478. .light-top {
  479. padding-left: 20px;
  480. padding-bottom: 0px;
  481. display: flex;
  482. justify-content: space-between;
  483. }
  484. .light-desc {
  485. flex: 1 98px;
  486. padding-top: 10px;
  487. font-family: PingFang SC;
  488. font-size: 16px;
  489. line-height: 19px;
  490. color: #4d5262;
  491. flex: none;
  492. order: 0;
  493. flex-grow: 0;
  494. margin: 5px 0px;
  495. }
  496. .light-right {
  497. position: relative;
  498. width: 98px;
  499. margin-right: 45px;
  500. text-align: center;
  501. img {
  502. //width: 98px;
  503. height: 120px;
  504. }
  505. .switch-btn {
  506. position: absolute;
  507. right: -20px;
  508. bottom: 20px;
  509. }
  510. }
  511. .light-title {
  512. font-family: PingFang SC;
  513. padding-left: 5px;
  514. font-size: 16px;
  515. line-height: 19px;
  516. color: #4d5262;
  517. flex: none;
  518. order: 0;
  519. flex-grow: 0;
  520. margin: 5px 0px;
  521. }
  522. .light-status {
  523. font-family: PingFang SC;
  524. padding-left: 5px;
  525. font-size: 14px;
  526. line-height: 16px;
  527. color: #c4c4c4;
  528. flex: none;
  529. order: 1;
  530. flex-grow: 0;
  531. margin: 10px 0px;
  532. }
  533. .show-all {
  534. font-family: PingFang SC;
  535. //display: none;
  536. .light-icon {
  537. font-size: 12px;
  538. padding-right: 10px;
  539. }
  540. padding-top: 28px;
  541. padding-bottom: 22px;
  542. font-size: 14px;
  543. line-height: 16px;
  544. color: #c4c4c4;
  545. margin: 0px 5px;
  546. }
  547. .light-control {
  548. display: flex;
  549. justify-content: space-between;
  550. padding: 10px 15px 10px 28px;
  551. border-top: 1px solid rgba(196, 196, 196, 0.4);
  552. .control-title {
  553. flex: 1 110px;
  554. font-weight: 500;
  555. font-family: PingFang SC;
  556. font-size: 16px;
  557. line-height: 30px;
  558. color: #4d5262;
  559. }
  560. .switch-btn {
  561. margin-top: 5px;
  562. }
  563. }
  564. </style>
  565. <style class="style" lang="scss">
  566. .light {
  567. .van-loading__spinner {
  568. color: $elActiveColor !important;
  569. }
  570. .van-switch__loading {
  571. top: 0;
  572. left: 0;
  573. width: 100%;
  574. height: 100%;
  575. line-height: 1;
  576. }
  577. .van-switch--disabled {
  578. opacity: 0.5;
  579. }
  580. }
  581. </style>