MapBox.vue 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352
  1. <template>
  2. <div id="map" style="width: 100%; height: 100%">
  3. <div class="space-main" id="spaceMain">
  4. <div
  5. class="space-box"
  6. v-for="(area, index) in mapData.spaceList"
  7. :style="{
  8. width: area.width + 'px',
  9. height: area.height + 'px',
  10. top: area.top + 'px',
  11. left: area.left + 'px',
  12. }"
  13. :key="'map' + index"
  14. >
  15. <div
  16. class="space"
  17. :id="'space' + area.id"
  18. :class="[
  19. area.canClick ? '' : 'click-disable',
  20. selectArea.id === area.id ? 'select' : '',
  21. isSetSpace ? 'use-select' : '',
  22. ]"
  23. @click.stop="checkSpace(area)"
  24. :style="{
  25. backgroundColor: area.backgroundColor ? area.backgroundColor : '',
  26. }"
  27. >
  28. <div
  29. class="device"
  30. :class="selectArea.id === area.id ? 'select-device' : ''"
  31. v-show="
  32. area.width * displacement.scale > 24 &&
  33. area.height * displacement.scale > 24
  34. "
  35. >
  36. <img
  37. :src="area.icon"
  38. v-if="selectArea.id !== area.id && area.icon"
  39. />
  40. <img :src="area.selectIcon" v-else class="select-icon" alt="" />
  41. <template v-if="area.localName">
  42. <span
  43. v-if="
  44. (area.height > 80 &&
  45. area.localName.length * 12 < area.width) ||
  46. selectArea.id === area.id
  47. "
  48. style="font-size: 12px"
  49. >{{ area.localName }}</span
  50. >
  51. </template>
  52. </div>
  53. <!-- <div class="circle" v-else :style="{backgroundColor: area.circleColor?area.circleColor:''}"></div>-->
  54. </div>
  55. </div>
  56. </div>
  57. <!--地图右侧的操作按钮-->
  58. <div class="map-toolbar">
  59. <img
  60. :src="parseImgUrl('map-icon', 'icon-search-big.svg')"
  61. @click.stop="goSearch"
  62. class="icon-search"
  63. alt=""
  64. />
  65. <div class="toolbar-item" @click.stop="checkFloor">
  66. <span class="text-hidden">{{ floorItem.localName }}</span>
  67. </div>
  68. <div class="toolbar-item" @click.stop="checkBuilding">
  69. <span class="text-hidden">{{ buildingItem.localName }}</span>
  70. </div>
  71. <!-- <div class="toolbar-item">-->
  72. <!-- <img src="../../assets/images/map-icon/icon-floor-arrow.svg" alt="">-->
  73. <!-- </div>-->
  74. </div>
  75. <!-- 选择建筑 -->
  76. <van-popup
  77. v-model:show="showBuilding"
  78. teleport="body"
  79. class="buiding-box"
  80. style="width: 40%; height: 100%; padding: 20px"
  81. position="right"
  82. >
  83. <div
  84. class="buiding-item"
  85. :key="item.id"
  86. :class="buildingItem.buildingId === item.id ? 'active' : ''"
  87. @click.stop="comfirmBuilding(item)"
  88. v-for="item in buildingData"
  89. >
  90. {{ item.localName }}
  91. </div>
  92. </van-popup>
  93. <!-- 选择楼层 -->
  94. <van-popup
  95. v-model:show="showFloor"
  96. teleport="body"
  97. class="buiding-box"
  98. style="width: 40%; height: 100%; padding: 20px"
  99. position="right"
  100. >
  101. <div
  102. class="buiding-item"
  103. :key="item.id"
  104. :class="floorItem.id === item.id ? 'active' : ''"
  105. @click.stop="comfirmFloor(item)"
  106. v-for="item in floorData"
  107. >
  108. {{ item.localName }}
  109. </div>
  110. </van-popup>
  111. <!--搜索-->
  112. <van-popup
  113. v-model:show="showSearch"
  114. teleport="body"
  115. class="buiding-box"
  116. style="width: 100%; height: 100%"
  117. position="right"
  118. >
  119. <div style="width: 100%; height: 100%">
  120. <space-search @closeSearch="closeSearch"></space-search>
  121. </div>
  122. </van-popup>
  123. </div>
  124. </template>
  125. <script lang="ts">
  126. import {
  127. defineComponent,
  128. onMounted,
  129. nextTick,
  130. reactive,
  131. toRefs,
  132. watch,
  133. } from "vue";
  134. import {
  135. getLocalStorageFloor,
  136. getStorageSpaceId,
  137. localStorageFloor,
  138. localStorageSpaceId,
  139. parseImgUrl,
  140. setLocalNewSpaceInfo,
  141. } from "@/utils";
  142. import { Toast, Dialog } from "vant";
  143. import { getBuildingList, getFloorList, getMapInfo } from "@/apis/envmonitor";
  144. import SpaceSearch from "@/views/envmonitor/Search/SpaceSearch.vue";
  145. import { useRouter } from "vue-router";
  146. import { getSpaceType, mapIcon } from "@/utils/mapIcon";
  147. import any = jasmine.any;
  148. export default defineComponent({
  149. props: {
  150. projectId: {
  151. type: String,
  152. default: () => "",
  153. },
  154. spaceInfo: {
  155. type: Object,
  156. default: () => {},
  157. },
  158. isSetSpace: {
  159. type: Boolean,
  160. default: () => false,
  161. },
  162. floorId: {
  163. type: String,
  164. default: () => "",
  165. },
  166. buildingId: {
  167. type: String,
  168. default: () => "",
  169. },
  170. },
  171. components: {
  172. SpaceSearch,
  173. },
  174. setup(props, contx) {
  175. const router = useRouter();
  176. const mapData: any = {};
  177. const document: any = window.document;
  178. const screenInfo: any = {
  179. screenWidth: document.body.clientWidth,
  180. screenHeight: document.body.clientHeight,
  181. };
  182. const comMapScale: any = 0.8;
  183. const displacement: any = {
  184. scale: 1,
  185. pageX: 0,
  186. pageX2: 0,
  187. originScale: 0,
  188. moveable: false,
  189. };
  190. let buildingData: any = [];
  191. let floorData: any = [];
  192. let floorItem: any = {};
  193. let selectArea: any = {};
  194. let transformData: any = {};
  195. const proxyData = reactive({
  196. parseImgUrl: parseImgUrl,
  197. isSetSpace: props.isSetSpace, // isSetSpace:true 代表为设置常驻空间页面展示
  198. widthMapScale: 1,
  199. tempScale: 0.8,
  200. comMapScale: comMapScale,
  201. heightScale: 1,
  202. showSearch: false,
  203. isSetSearchSpace: false,
  204. showBuilding: false,
  205. showFloor: false,
  206. buildingItem: {
  207. buildingId: "",
  208. localName: "",
  209. },
  210. floorData: floorData,
  211. floorItem: floorItem,
  212. buildingData: buildingData,
  213. mapData: mapData,
  214. copyMapDaata: mapData,
  215. selectArea: selectArea,
  216. displacement: displacement,
  217. screenInfo: screenInfo,
  218. transformData: transformData,
  219. // 切换楼层的时候恢缩放和滚动的样式
  220. clearPreStyle() {
  221. let mapDom = document.querySelector("#map");
  222. mapDom.scrollLeft = 0;
  223. mapDom.scrollTop = 0;
  224. let matrix_box: any = document.querySelector("#spaceMain");
  225. // 记住使用的缩放值
  226. proxyData.displacement.scale = 1;
  227. if (matrix_box) {
  228. matrix_box.style.left = 0 + "px";
  229. matrix_box.style.top = 0 + "px";
  230. }
  231. },
  232. swipe(el: any, options: any) {
  233. //设置开关,监听move事件
  234. let isMove: any = false;
  235. // 设置手指触摸开始的坐标
  236. let startX: any = 0;
  237. let startY: any = 0;
  238. // 设置手指移动的坐标
  239. let moveX: any = 0;
  240. let moveY: any = 0;
  241. //设置指针距离元素的边框的距离
  242. let disX: any = 0;
  243. let disY: any = 0;
  244. // 如果用户未传入参数2,自己定义一个
  245. let fun: any = function () {};
  246. let data: any = {
  247. swipeLeft: fun,
  248. swipeRight: fun,
  249. swipeDown: fun,
  250. swipeUp: fun,
  251. drag: fun,
  252. };
  253. // 判断是否传入参数2,有的话覆盖默认值
  254. Object.assign(data, options);
  255. // 给元素绑定三个事件
  256. el.addEventListener(
  257. "touchstart",
  258. function (e: any) {
  259. console.log(e);
  260. //获取手指开始的坐标
  261. startX = e.touches[0].pageX;
  262. startY = e.touches[0].pageY;
  263. //计算指针距离元素边框的位置
  264. disX = startX - el.offsetLeft;
  265. disY = startY - el.offsetTop;
  266. },
  267. { passive: true }
  268. );
  269. el.addEventListener(
  270. "touchmove",
  271. function (e: any) {
  272. if (proxyData.displacement.moveable) {
  273. return;
  274. }
  275. console.log(e);
  276. //如果触发了move事件,打开开关
  277. isMove = true;
  278. // 获取移动时的坐标
  279. moveX = e.touches[0].pageX;
  280. moveY = e.touches[0].pageY;
  281. e.mation = {
  282. startX: startX,
  283. startY: startY,
  284. moveX: moveX,
  285. moveY: moveY,
  286. disX: disX,
  287. disY: disY,
  288. };
  289. data.drag.call(el, e);
  290. // 判断是否触发了move事件
  291. if (isMove) {
  292. // 计算水平边长
  293. let absX: any = Math.abs(moveX - startX);
  294. // 计算垂直边长
  295. let absY: any = Math.abs(moveY - startY);
  296. // 判断垂直还是水平滑动
  297. if (absX > absY) {
  298. // console.log('水平滑动');
  299. //再判断是左滑右滑
  300. if (moveX - startX > 0) {
  301. data.swipeRight.call(el, e);
  302. } else {
  303. data.swipeLeft.call(el, e);
  304. }
  305. } else {
  306. // console.log('垂直滑动');
  307. //判断是上滑还是下滑
  308. if (moveY - startX > 0) {
  309. // 下滑
  310. data.swipeDown.call(el, e);
  311. } else {
  312. data.swipeUp.call(el, e);
  313. }
  314. }
  315. }
  316. },
  317. { passive: true }
  318. );
  319. el.addEventListener("touchend", function (e: any) {
  320. isMove = false;
  321. });
  322. },
  323. /**
  324. * 地图滚动
  325. */
  326. mapScroll() {
  327. let matrix_box: any = document.querySelector("#spaceMain");
  328. let houseDesDom = document.querySelector("#houseDes");
  329. let houseHeight: any = houseDesDom ? houseDesDom.offsetHeight : 0;
  330. proxyData.swipe(matrix_box, {
  331. swipeLeft: function (e: any) {
  332. if (!proxyData.displacement.moveable) {
  333. if (matrix_box.offsetWidth < proxyData.screenInfo.screenWidth) {
  334. } else {
  335. let width: any =
  336. matrix_box.offsetWidth -
  337. Math.abs(e.touches[0].pageX - e.mation.disX);
  338. if (width < proxyData.screenInfo.screenWidth) {
  339. matrix_box.style.left =
  340. -(
  341. matrix_box.offsetWidth - proxyData.screenInfo.screenWidth
  342. ) + "px";
  343. } else {
  344. matrix_box.style.left =
  345. e.touches[0].pageX - e.mation.disX + "px";
  346. }
  347. }
  348. }
  349. },
  350. swipeRight: function (e: any) {
  351. if (!proxyData.displacement.moveable) {
  352. if (matrix_box.offsetWidth < proxyData.screenInfo.screenWidth) {
  353. return;
  354. }
  355. if (e.touches[0].pageX - e.mation.disX > 0) {
  356. matrix_box.style.left = 0 + "px";
  357. } else {
  358. matrix_box.style.left =
  359. e.touches[0].pageX - e.mation.disX + "px";
  360. }
  361. }
  362. },
  363. swipeDown: function (e: any) {
  364. if (!proxyData.displacement.moveable) {
  365. let height: any =
  366. proxyData.screenInfo.screenHeight - houseHeight - 20;
  367. let boxHeight: any = matrix_box.offsetHeight;
  368. if (boxHeight < height) {
  369. matrix_box.style.top = 0 + "px";
  370. } else {
  371. if (e.touches[0].pageY - e.mation.disY > 0) {
  372. matrix_box.style.top = 0 + "px";
  373. } else {
  374. matrix_box.style.top =
  375. e.touches[0].pageY - e.mation.disY + "px";
  376. }
  377. }
  378. }
  379. },
  380. swipeUp: function (e: any) {
  381. if (!proxyData.displacement.moveable) {
  382. let houseDesDom = document.querySelector("#houseDes");
  383. let houseHeight: any = houseDesDom ? houseDesDom.offsetHeight : 0;
  384. let height: any =
  385. proxyData.screenInfo.screenHeight - houseHeight - 20;
  386. let boxHeight: any = matrix_box.offsetHeight;
  387. let top: any = e.touches[0].pageY - e.mation.disY;
  388. if (boxHeight < height) {
  389. matrix_box.style.top = 0 + "px";
  390. } else {
  391. if (boxHeight - Math.abs(top) < height / 2) {
  392. matrix_box.style.top = -(boxHeight - height / 2) + "px";
  393. } else {
  394. matrix_box.style.top =
  395. e.touches[0].pageY - e.mation.disY + "px";
  396. }
  397. }
  398. }
  399. },
  400. drag: function (e: any) {},
  401. });
  402. },
  403. /**
  404. * 关闭搜索页面
  405. * @param item
  406. */
  407. closeSearch(item: any) {
  408. proxyData.showSearch = false;
  409. // 搜索页面过来的
  410. if (item) {
  411. proxyData.isSetSearchSpace = true;
  412. proxyData.loadingStart();
  413. let searchSpace: any = {
  414. buildingId: item.buildingId,
  415. buildingName: "",
  416. floorId: item.floorId,
  417. floorName: "",
  418. spaceId: item.id,
  419. };
  420. proxyData.init(searchSpace);
  421. } else {
  422. proxyData.isSetSearchSpace = false;
  423. }
  424. },
  425. /*
  426. 去搜索页面
  427. */
  428. goSearch() {
  429. proxyData.showSearch = true;
  430. // router.push({ path: '/search' })
  431. },
  432. /**
  433. * 设置选中的空间的位置,
  434. */
  435. setSelectSpacePosition(area: any) {
  436. setTimeout(() => {
  437. proxyData.setScrollLeft(area);
  438. proxyData.scrollTop(area);
  439. });
  440. },
  441. /**
  442. * 判断像左还是像右滚动
  443. */
  444. setScrollLeft(area: any) {
  445. let mapBoxEle = document.querySelector("#spaceMain");
  446. let left: any = 0;
  447. if (area.left + area.width >= proxyData.screenInfo.screenWidth / 2) {
  448. left =
  449. area.left - proxyData.screenInfo.screenWidth / 2 + area.width / 2;
  450. left = ~left;
  451. } else {
  452. left = 0;
  453. }
  454. if (left > 0) {
  455. left = 0;
  456. }
  457. if (
  458. Math.abs(left) >
  459. mapBoxEle.offsetWidth - proxyData.screenInfo.screenWidth
  460. ) {
  461. left = -(mapBoxEle.offsetWidth - proxyData.screenInfo.screenWidth);
  462. }
  463. mapBoxEle.style.left = left + "px";
  464. },
  465. /**
  466. * 获取竖着的方向
  467. */
  468. scrollTop(area: any) {
  469. let mapDom = document.querySelector("#map");
  470. let mapBoxEle = document.querySelector("#spaceMain");
  471. let houseDesDom = document.querySelector("#houseDes");
  472. let houseHeight: any = houseDesDom ? houseDesDom.offsetHeight : 0;
  473. let top: any = 0;
  474. if (
  475. area.top + area.width >
  476. (proxyData.screenInfo.screenHeight - houseHeight - 20) / 2
  477. ) {
  478. top =
  479. area.top -
  480. (proxyData.screenInfo.screenHeight - houseHeight) / 2 +
  481. area.height / 2;
  482. } else {
  483. top = 0;
  484. }
  485. top = ~top;
  486. if (top > 0) {
  487. top = 0;
  488. }
  489. mapBoxEle.style.top = top + "px";
  490. },
  491. /**
  492. * 切换空间
  493. */
  494. checkSpace(area: any) {
  495. proxyData.isSetSearchSpace = false;
  496. if (area && area.canClick) {
  497. proxyData.setSelectSpacePosition(area);
  498. if (proxyData.isSetSpace) {
  499. // 记录当前选中的id
  500. proxyData.comfirSetSpace(area);
  501. } else {
  502. proxyData.selectArea = area;
  503. proxyData.setLocalSpaceInfo();
  504. // 记录当前选中的id
  505. contx.emit("changeSpaceId", proxyData.selectArea.spaceId);
  506. }
  507. } else {
  508. Toast("不可选区域");
  509. }
  510. },
  511. /**
  512. * 确认设置为常驻空间
  513. */
  514. comfirSetSpace(item: any) {
  515. if (item.spaceId === props.spaceInfo.spaceId) {
  516. Dialog.confirm({
  517. title: "取消常驻空间",
  518. confirmButtonColor: "#000000",
  519. cancelButtonColor: "#4D5262",
  520. message: `
  521. 是否取消常驻空间设置 ? `,
  522. })
  523. .then(() => {
  524. contx.emit("delPermanentSpace");
  525. })
  526. .catch(() => {});
  527. } else {
  528. Dialog.confirm({
  529. title: "设置常驻空间",
  530. confirmButtonColor: "#000000",
  531. cancelButtonColor: "#4D5262",
  532. message: `是否将常驻空间设置为${item.localName}?`,
  533. })
  534. .then(() => {
  535. proxyData.selectArea = item;
  536. contx.emit("changeSpaceId", proxyData.selectArea.spaceId);
  537. })
  538. .catch(() => {});
  539. }
  540. },
  541. /**
  542. * 本地缓存最新的空间建筑信息,提供给首次进入页面使用
  543. */
  544. setLocalSpaceInfo() {
  545. localStorageSpaceId(
  546. proxyData.buildingItem.buildingId,
  547. proxyData.floorItem.id,
  548. proxyData.selectArea.spaceId
  549. );
  550. let spaceInfo: any = {
  551. buildingId: proxyData.buildingItem.buildingId,
  552. buildingName: proxyData.buildingItem.localName,
  553. floorId: proxyData.floorItem.id,
  554. floorName: proxyData.floorItem.localName,
  555. spaceId: proxyData.selectArea.spaceId, // 上格云3
  556. };
  557. setLocalNewSpaceInfo(spaceInfo);
  558. },
  559. /**
  560. * 地图放大的距离
  561. * @param start
  562. * @param stop
  563. */
  564. getDistance(start: any, stop: any) {
  565. return Math.hypot(stop.x - start.x, stop.y - start.y);
  566. },
  567. /**
  568. * 格式化地图数据
  569. */
  570. formateMapData() {
  571. let data: any = proxyData.mapData;
  572. if (data) {
  573. proxyData.setSpaceScale();
  574. proxyData.setTempScale();
  575. if (data.width < 3000) {
  576. proxyData.comMapScale = 0.8;
  577. }
  578. data.width = data.width * proxyData.comMapScale;
  579. data.height = data.height * proxyData.comMapScale;
  580. let spaceList: any = data?.spaceList ?? [];
  581. for (let i = 0; i < spaceList.length; i++) {
  582. let item = spaceList[i];
  583. item.width = item.width * proxyData.comMapScale;
  584. item.height = item.height * proxyData.comMapScale;
  585. item.top = item.top * proxyData.comMapScale;
  586. item.left = item.left * proxyData.comMapScale;
  587. // item.canClick = !item.canClick
  588. proxyData.setSpaceIcon(item);
  589. }
  590. // console.log(" proxyData.mapData proxyData.mapData");
  591. // console.log(proxyData.mapData);
  592. // 备份数据
  593. proxyData.copyMapDaata = JSON.parse(JSON.stringify(data));
  594. }
  595. },
  596. // 获取空间最小宽
  597. setSpaceScale() {
  598. let data: any = proxyData.mapData;
  599. if (data) {
  600. let spaceList: any = data?.spaceList ?? [];
  601. let minHeight: any = Math.min.apply(
  602. null,
  603. spaceList.map((item: any) => item.height)
  604. );
  605. let minWidth: any = Math.min.apply(
  606. null,
  607. spaceList.map((item: any) => item.width)
  608. );
  609. let min: any = minHeight < minWidth ? minHeight : minWidth;
  610. min = min ? min : 30;
  611. proxyData.comMapScale = 30 / min;
  612. }
  613. },
  614. // 缩放的临界值修改
  615. setTempScale() {
  616. proxyData.tempScale = 0.4;
  617. let data: any = proxyData.mapData;
  618. if (data) {
  619. let width: any = proxyData.mapData.width;
  620. let height: any = proxyData.mapData.height;
  621. if (width > height) {
  622. let num: any = Math.ceil(width / proxyData.screenInfo.screenWidth);
  623. if (num < 10) {
  624. proxyData.tempScale = 0.4;
  625. } else if (num < 20) {
  626. proxyData.tempScale = 0.3;
  627. } else {
  628. proxyData.tempScale = 0.25;
  629. }
  630. } else {
  631. let num: any = Math.ceil(
  632. height / proxyData.screenInfo.screenHeight
  633. );
  634. if (num < 10) {
  635. proxyData.tempScale = 0.4;
  636. } else if (num < 20) {
  637. proxyData.tempScale = 0.3;
  638. } else {
  639. proxyData.tempScale = 0.25;
  640. }
  641. }
  642. }
  643. },
  644. /**
  645. * 设置地图最外框宽和高
  646. */
  647. setMapBoxStyle() {
  648. let mapBoxEle = document.querySelector("#spaceMain");
  649. if (proxyData.mapData && proxyData.mapData.width) {
  650. mapBoxEle.style.width = proxyData.mapData.width + "px";
  651. mapBoxEle.style.height = proxyData.mapData.height + "px";
  652. }
  653. },
  654. /**
  655. * 设置空间的图标
  656. *
  657. */
  658. setSpaceIcon(item: any) {
  659. // if(item.can)
  660. if (item.canClick) {
  661. let typeName: any = getSpaceType(item.roomFuncType);
  662. item.icon = mapIcon[typeName].icon;
  663. item.selectIcon = mapIcon[typeName].selectIcon;
  664. item.backgroundColor = mapIcon[typeName].backgroundColor;
  665. item.bg = mapIcon[typeName].bg;
  666. item.icon = parseImgUrl("map-new-icon", item.icon);
  667. item.selectIcon = parseImgUrl("map-new-icon", item.selectIcon);
  668. }
  669. },
  670. midpoint(x1: any, y1: any, x2: any, y2: any) {
  671. return [x1 + (x2 - x1) / 2, y1 + (y2 - y1) / 2];
  672. // return [x1 + x2 / 2, y1 + (y2 - y1)]
  673. },
  674. /**
  675. * 更新地图的宽高
  676. */
  677. updateMapStyle() {
  678. let data: any = JSON.parse(JSON.stringify(proxyData.copyMapDaata));
  679. if (data) {
  680. data.width = data.width * proxyData.displacement.scale;
  681. data.height = data.height * proxyData.displacement.scale;
  682. let spaceList: any = data?.spaceList ?? [];
  683. for (let i = 0; i < spaceList.length; i++) {
  684. let item = spaceList[i];
  685. item.width = item.width * proxyData.displacement.scale;
  686. item.height = item.height * proxyData.displacement.scale;
  687. item.top = item.top * proxyData.displacement.scale;
  688. item.left = item.left * proxyData.displacement.scale;
  689. }
  690. proxyData.mapData = data;
  691. proxyData.setMapBoxStyle();
  692. }
  693. },
  694. /**
  695. * 更新地图的位置
  696. */
  697. updateMapPositon(temp: any, matrix_box: any) {
  698. // let left: any = (proxyData.displacement.oldSize[0] - proxyData.displacement.oldSize[0] * proxyData.displacement.scale) * proxyData.displacement.scaleTranslateProportion[0]
  699. // let top: any = (proxyData.displacement.oldSize[1] - proxyData.displacement.oldSize[1] * proxyData.displacement.scale) * proxyData.displacement.scaleTranslateProportion[1]
  700. let left: any =
  701. proxyData.displacement.scaleCenter[0] -
  702. proxyData.displacement.scaleCenter[0] * proxyData.displacement.scale;
  703. let top: any =
  704. proxyData.displacement.scaleCenter[1] -
  705. proxyData.displacement.scaleCenter[1] * proxyData.displacement.scale;
  706. if (temp > 0 && proxyData.displacement.scale < 1) {
  707. if (left > 0) {
  708. left = ~left;
  709. }
  710. if (top > 0) {
  711. top = ~top;
  712. // top = 0
  713. }
  714. }
  715. let newTop: any =
  716. proxyData.displacement.top * proxyData.displacement.scale + top;
  717. let newLeft: any =
  718. proxyData.displacement.left * proxyData.displacement.scale + left;
  719. if (matrix_box.offsetWidth < proxyData.screenInfo.screenWidth) {
  720. newLeft = 0;
  721. }
  722. if (newLeft > 0) {
  723. matrix_box.style.left = 0 + "px";
  724. } else {
  725. matrix_box.style.left = newLeft + "px";
  726. }
  727. if (matrix_box.offsetHeight < proxyData.screenInfo.screenHeight) {
  728. matrix_box.style.top = 0;
  729. } else {
  730. if (newTop > 0) {
  731. matrix_box.style.top = 0;
  732. } else {
  733. matrix_box.style.top = newTop + "px";
  734. }
  735. }
  736. },
  737. /**
  738. * 地图缩放功能
  739. */
  740. mapScale() {
  741. let matrix_box: any = document.querySelector("#spaceMain");
  742. matrix_box.addEventListener(
  743. "touchstart",
  744. function (event: any) {
  745. let touches: any = event.touches;
  746. let events: any = touches[0];
  747. let events2: any = touches[1];
  748. // 第一个触摸点的坐标
  749. proxyData.displacement.pageX = events.pageX;
  750. proxyData.displacement.pageY = events.pageY;
  751. proxyData.displacement.moveable = true;
  752. proxyData.displacement.originScale =
  753. proxyData.displacement.scale || 1;
  754. if (events2) {
  755. proxyData.displacement.pageX2 = events2.pageX;
  756. proxyData.displacement.pageY2 = events2.pageY;
  757. let left: any = isNaN(parseInt(matrix_box.style.left))
  758. ? 0
  759. : parseInt(matrix_box.style.left);
  760. let top: any = isNaN(parseInt(matrix_box.style.top))
  761. ? 0
  762. : parseInt(matrix_box.style.top);
  763. proxyData.displacement.center = proxyData.midpoint(
  764. events.pageX,
  765. events.pageY,
  766. events2.pageX,
  767. events2.pageY
  768. );
  769. proxyData.displacement.left = left / proxyData.displacement.scale;
  770. proxyData.displacement.top = top / proxyData.displacement.scale;
  771. proxyData.displacement.scaleCenter = [
  772. proxyData.displacement.center[0] / proxyData.displacement.scale,
  773. proxyData.displacement.center[1] / proxyData.displacement.scale,
  774. ];
  775. proxyData.displacement.scaleTranslateProportion = [
  776. proxyData.displacement.scaleCenter[0] /
  777. (matrix_box.offsetWidth / proxyData.displacement.scale),
  778. proxyData.displacement.scaleCenter[1] /
  779. (matrix_box.offsetHeight / proxyData.displacement.scale),
  780. ];
  781. proxyData.displacement.oldSize = [
  782. matrix_box.offsetWidth / proxyData.displacement.scale,
  783. matrix_box.offsetHeight / proxyData.displacement.scale,
  784. ];
  785. } else {
  786. proxyData.displacement.moveable = false;
  787. }
  788. },
  789. { passive: true }
  790. );
  791. matrix_box.addEventListener(
  792. "touchmove",
  793. function (event: any) {
  794. if (!proxyData.displacement.moveable) {
  795. return;
  796. }
  797. // event.preventDefault();
  798. let touches: any = event.touches;
  799. let events: any = touches[0];
  800. let events2: any = touches[1];
  801. // 双指移动
  802. if (events2) {
  803. // 第2个指头坐标在touchmove时候获取
  804. if (!proxyData.displacement.pageX2) {
  805. proxyData.displacement.pageX2 = events2.pageX;
  806. }
  807. if (!proxyData.displacement.pageY2) {
  808. proxyData.displacement.pageY2 = events2.pageY;
  809. }
  810. // 双指缩放比例计算
  811. let zoom: any =
  812. proxyData.getDistance(
  813. {
  814. x: events.pageX,
  815. y: events.pageY,
  816. },
  817. {
  818. x: events2.pageX,
  819. y: events2.pageY,
  820. }
  821. ) /
  822. proxyData.getDistance(
  823. {
  824. x: proxyData.displacement.pageX,
  825. y: proxyData.displacement.pageY,
  826. },
  827. {
  828. x: proxyData.displacement.pageX2,
  829. y: proxyData.displacement.pageY2,
  830. }
  831. );
  832. // 应用在元素上的缩放比例
  833. let newScale: any = proxyData.displacement.originScale * zoom;
  834. if (newScale < proxyData.tempScale) {
  835. newScale = proxyData.tempScale;
  836. }
  837. // 最大缩放比例限制
  838. if (newScale > 1.5) {
  839. newScale = 1.5;
  840. }
  841. let temp: any = newScale - proxyData.displacement.scale;
  842. proxyData.displacement.scale = newScale;
  843. if (temp !== 0) {
  844. proxyData.updateMapPositon(temp, matrix_box);
  845. proxyData.updateMapStyle();
  846. }
  847. }
  848. },
  849. { passive: true }
  850. );
  851. matrix_box.addEventListener("touchend", function () {
  852. proxyData.displacement.moveable = true;
  853. });
  854. },
  855. /**
  856. * 获取建筑信息
  857. */
  858. getBuildingList() {
  859. let params: any = {
  860. criteria: {
  861. projectId: props.projectId,
  862. },
  863. orders: [
  864. {
  865. asc: true,
  866. column: "localId",
  867. },
  868. ],
  869. };
  870. getBuildingList(params).then((res) => {
  871. let resData: any = res;
  872. if (resData.result === "success") {
  873. let content: any = resData?.content ?? [];
  874. proxyData.buildingData = content;
  875. // 设置展示的建筑名称
  876. proxyData.setBuildingName();
  877. }
  878. });
  879. },
  880. /**
  881. *
  882. * 选择建筑
  883. */
  884. checkBuilding() {
  885. proxyData.showBuilding = true;
  886. },
  887. /**
  888. * 选择楼层
  889. */
  890. checkFloor() {
  891. proxyData.showFloor = true;
  892. },
  893. /**
  894. * 确定选择的楼层信息
  895. */
  896. comfirmFloor(item: any, flag: boolean = true) {
  897. proxyData.floorItem = item;
  898. proxyData.showFloor = false;
  899. // 获取地图数据
  900. proxyData.loadingStart();
  901. proxyData.getMapInfo(flag);
  902. /**
  903. *缓存建筑对应的楼层
  904. */
  905. localStorageFloor(
  906. proxyData.buildingItem.buildingId,
  907. proxyData.floorItem.id
  908. );
  909. },
  910. /**
  911. * 确定建筑
  912. */
  913. comfirmBuilding(item: any) {
  914. proxyData.showBuilding = false;
  915. proxyData.buildingItem.buildingId = item.id;
  916. proxyData.buildingItem.localName = item.localName;
  917. proxyData.loadingStart();
  918. proxyData.getFloorList();
  919. },
  920. /**
  921. * 加载loading
  922. */
  923. loadingStart() {
  924. Toast.loading({
  925. duration: 0, // 持续展示 toast
  926. forbidClick: true,
  927. message: "加载中...",
  928. });
  929. },
  930. /**
  931. * 结束
  932. */
  933. loadinngEnd() {
  934. Toast.clear();
  935. },
  936. /**
  937. * 查询楼层信息
  938. */
  939. getFloorList(flag: boolean = true) {
  940. if (proxyData.buildingItem.buildingId) {
  941. let params: any = {
  942. criteria: {
  943. projectId: props.projectId,
  944. buildingId: proxyData.buildingItem.buildingId,
  945. },
  946. orders: [
  947. {
  948. asc: true,
  949. column: "localId",
  950. },
  951. ],
  952. };
  953. getFloorList(params)
  954. .then((res) => {
  955. let resData: any = res;
  956. if (res.result === "success") {
  957. proxyData.floorData = resData?.content ?? [];
  958. // 设置楼层的名称
  959. proxyData.setFloorName();
  960. } else {
  961. proxyData.floorData = [];
  962. }
  963. if (flag && proxyData.floorData.length) {
  964. proxyData.getHistorySelectFloor();
  965. proxyData.getMapInfo();
  966. }
  967. })
  968. .catch(() => {
  969. proxyData.loadinngEnd();
  970. });
  971. }
  972. },
  973. /**
  974. * 获取地图信息
  975. */
  976. getMapInfo(flag: boolean = true, searchItem: any = null) {
  977. let params: any = {
  978. projectId: props.projectId,
  979. floorId: proxyData.floorItem.id,
  980. };
  981. getMapInfo(params)
  982. .then((res) => {
  983. let resData: any = res;
  984. // debugger;
  985. if (resData.result === "success") {
  986. proxyData.mapData = resData?.data ?? null;
  987. // 切换数据的时候清楚默认样式
  988. proxyData.clearPreStyle();
  989. proxyData.formateMapData();
  990. /**
  991. * 如果是当前页面切换楼层,展示历史楼层
  992. */
  993. if (flag) {
  994. proxyData.getHistorySpace();
  995. } else {
  996. // 首次进入页面的时候展示空间来源于父div
  997. let area = proxyData.getFirstSelectSpace(searchItem);
  998. if (area) {
  999. proxyData.selectArea = area;
  1000. }
  1001. }
  1002. // 当没有默认空间的时候设置常驻空间页面不可高量
  1003. if (proxyData.isSetSpace && !props.spaceInfo.isPermanent) {
  1004. proxyData.selectArea = {};
  1005. } else {
  1006. // 非常驻空间进入需要点击切换空间
  1007. if (!proxyData.isSetSpace) {
  1008. proxyData.checkSpace(proxyData.selectArea);
  1009. }
  1010. // proxyData.selectArea = proxyData.mapData.spaceList[0];
  1011. // 设置当前空间的位置
  1012. console.log(" proxyData.selectArea===");
  1013. console.log(proxyData.selectArea);
  1014. proxyData.setSelectSpacePosition(proxyData.selectArea);
  1015. }
  1016. // 接口返回后设置地图缩放和滚动
  1017. nextTick(() => {
  1018. // 地图双指缩放
  1019. proxyData.setMapBoxStyle();
  1020. proxyData.mapScale();
  1021. proxyData.mapScroll();
  1022. });
  1023. }
  1024. // setTimeout(() => {
  1025. // proxyData.loadinngEnd()
  1026. // }, 1000)
  1027. })
  1028. .catch(() => {
  1029. proxyData.loadinngEnd();
  1030. });
  1031. },
  1032. /**
  1033. * 获取缓存的楼层
  1034. */
  1035. getHistorySelectFloor() {
  1036. let historyFloor: any = getLocalStorageFloor();
  1037. let flag: any = false;
  1038. let floorId: any = historyFloor[proxyData.buildingItem.buildingId];
  1039. proxyData.floorData.map((floor: any) => {
  1040. if (floor.id === floorId) {
  1041. flag = true;
  1042. proxyData.floorItem = floor;
  1043. }
  1044. });
  1045. if (!flag) {
  1046. proxyData.floorItem = proxyData.floorData[0];
  1047. }
  1048. },
  1049. /**
  1050. * 获取选中的选中空间
  1051. */
  1052. getHistorySpace() {
  1053. let data: any = proxyData.mapData;
  1054. let historySpace: any = getStorageSpaceId();
  1055. if (data) {
  1056. let flag: boolean = false;
  1057. let spaceList = data?.spaceList ?? [];
  1058. let key: any = `${proxyData.buildingItem.buildingId},${proxyData.floorItem.id}`;
  1059. let spaceId: any = historySpace[key];
  1060. if (spaceId) {
  1061. spaceList.map((space: any) => {
  1062. if (spaceId === space.spaceId) {
  1063. proxyData.selectArea = space;
  1064. flag = true;
  1065. }
  1066. });
  1067. }
  1068. if (!flag) {
  1069. for (let i: any = 0; i < spaceList.length; i++) {
  1070. if (spaceList[i].canClick) {
  1071. proxyData.selectArea = spaceList[i];
  1072. break;
  1073. }
  1074. }
  1075. }
  1076. }
  1077. },
  1078. /**
  1079. * 首次进入页面设置选中的空间
  1080. */
  1081. getFirstSelectSpace(item: any) {
  1082. let area: any = null;
  1083. let data: any = proxyData.mapData;
  1084. // debugger;
  1085. if (data && item) {
  1086. let spaceList = data?.spaceList ?? [];
  1087. for (let i = 0; i < spaceList.length; i++) {
  1088. if (spaceList[i].spaceId === item.spaceId) {
  1089. // proxyData.selectArea = spaceList[i]
  1090. area = spaceList[i];
  1091. if (!proxyData.isSetSpace) {
  1092. localStorageSpaceId(
  1093. proxyData.buildingItem.buildingId,
  1094. proxyData.floorItem.id,
  1095. item.spaceId
  1096. );
  1097. }
  1098. break;
  1099. }
  1100. }
  1101. }
  1102. return area;
  1103. },
  1104. setDefaultSpace(item: any) {
  1105. if (item) {
  1106. proxyData.buildingItem.buildingId = item.buildingId;
  1107. proxyData.buildingItem.localName = item.buildingName
  1108. ? item.buildingName
  1109. : "";
  1110. proxyData.floorItem = {
  1111. id: item.floorId,
  1112. localName: item.floorName ? item.floorName : "",
  1113. };
  1114. }
  1115. },
  1116. // 设备模块初始化地图
  1117. init(item: any) {
  1118. // proxyData.setDefaultSpace(item);
  1119. /**
  1120. *缓存建筑对应的楼层
  1121. */
  1122. if (!proxyData.isSetSpace) {
  1123. localStorageFloor(
  1124. proxyData.buildingItem.buildingId,
  1125. proxyData.floorItem.id
  1126. );
  1127. }
  1128. proxyData.getBuildingList();
  1129. proxyData.getFloorList(false);
  1130. proxyData.getMapInfo(false, item);
  1131. },
  1132. /**
  1133. *搜索后设置建筑的localName
  1134. */
  1135. setBuildingName() {
  1136. if (proxyData.buildingItem.buildingId) {
  1137. proxyData.buildingData.map((item: any) => {
  1138. if (item.id === proxyData.buildingItem.buildingId) {
  1139. proxyData.buildingItem.localName = item.localName;
  1140. }
  1141. });
  1142. } else {
  1143. // 设置常驻空间的时候使用
  1144. proxyData.buildingItem = {
  1145. buildingId: proxyData.buildingData[0].id,
  1146. localName: proxyData.buildingData[0].localName,
  1147. };
  1148. }
  1149. },
  1150. /**
  1151. * 搜索页面回来根据id设置楼层的localName
  1152. */
  1153. setFloorName() {
  1154. if (proxyData.floorItem.id) {
  1155. proxyData.floorData.map((item: any) => {
  1156. if (item.id === proxyData.floorItem.id) {
  1157. proxyData.floorItem.localName = item.localName;
  1158. }
  1159. });
  1160. } else {
  1161. // 设置常驻空间的时候使用
  1162. proxyData.floorItem = proxyData.floorData[0];
  1163. }
  1164. },
  1165. });
  1166. watch(
  1167. props,
  1168. (newProps: any) => {
  1169. proxyData.buildingItem.buildingId = newProps.buildingId;
  1170. proxyData.floorItem.id = newProps.floorId;
  1171. // proxyData.selectArea.spaceId = newProps.spaceInfo.spaceId;
  1172. },
  1173. {
  1174. deep: false,
  1175. immediate: true,
  1176. }
  1177. );
  1178. onMounted(() => {
  1179. let item: any = props.spaceInfo;
  1180. console.log("props.spaceInfo===")
  1181. console.log(proxyData.selectArea)
  1182. console.log(props.spaceInfo)
  1183. proxyData.buildingItem.buildingId = props.buildingId;
  1184. proxyData.floorItem.id = props.floorId;
  1185. // proxyData.selectArea.spaceId = item.spaceId;
  1186. proxyData.init(item);
  1187. });
  1188. return {
  1189. ...toRefs(proxyData),
  1190. };
  1191. },
  1192. });
  1193. </script>
  1194. <style lang="scss" scoped>
  1195. .space-main {
  1196. width: 100%;
  1197. height: 100%;
  1198. position: relative;
  1199. background: #f7f8fa;
  1200. transform-origin: 0 0;
  1201. transition: transform 0.3s;
  1202. transform: scale(1);
  1203. //transform: translateX(-100px) !important;
  1204. padding: 0 5px;
  1205. overflow: hidden;
  1206. .space-box {
  1207. position: absolute;
  1208. padding: 5px;
  1209. }
  1210. .space {
  1211. position: relative;
  1212. width: 100%;
  1213. height: 100%;
  1214. box-shadow: 0px 1px 4px rgba(1, 1, 51, 0.01),
  1215. 0px 16px 40px rgba(1, 1, 51, 0.03);
  1216. border-radius: 4px;
  1217. box-sizing: border-box;
  1218. .device {
  1219. position: absolute;
  1220. left: 50%;
  1221. top: 50%;
  1222. transform: translate(-50%, -50%);
  1223. text-align: center;
  1224. z-index: 666;
  1225. img {
  1226. //margin-bottom: 6px;
  1227. }
  1228. span {
  1229. display: block;
  1230. word-break: keep-all;
  1231. white-space: nowrap;
  1232. //font-size: 12px;
  1233. color: #8995ba;
  1234. font-weight: 600;
  1235. }
  1236. }
  1237. .select-device {
  1238. transform: translate(-50%, -70%);
  1239. img {
  1240. margin-bottom: 6px;
  1241. }
  1242. }
  1243. .circle {
  1244. width: 4px;
  1245. height: 4px;
  1246. border-radius: 50%;
  1247. position: absolute;
  1248. top: 50%;
  1249. left: 50%;
  1250. transform: translateX(-50%) translateY(-50%);
  1251. }
  1252. }
  1253. .select {
  1254. border: 2px solid rgba(77, 148, 255, 0.8);
  1255. }
  1256. .use-select {
  1257. border: 5px solid #ffe823;
  1258. }
  1259. &::-webkit-scrollbar {
  1260. display: none;
  1261. }
  1262. }
  1263. .map-toolbar {
  1264. position: fixed;
  1265. width: 44px;
  1266. right: 20px;
  1267. z-index: 999;
  1268. top: 50%;
  1269. transform: translateY(-50%);
  1270. .icon-search {
  1271. width: 44px;
  1272. height: 44px;
  1273. border-radius: 5px;
  1274. background: #fff;
  1275. }
  1276. .toolbar-item {
  1277. width: 44px;
  1278. height: 44px;
  1279. background: #ffffff;
  1280. box-shadow: 0px 0px 1px rgba(0, 0, 0, 0.1), 0px 4px 8px rgba(0, 0, 0, 0.1);
  1281. border-radius: 4px;
  1282. margin-top: 10px;
  1283. text-align: center;
  1284. .toolbar-menu {
  1285. width: 44px;
  1286. }
  1287. img {
  1288. width: 12px;
  1289. height: 14px;
  1290. margin-top: 15px;
  1291. }
  1292. }
  1293. .text-hidden {
  1294. //font-family: Montserrat;
  1295. display: block;
  1296. font-style: normal;
  1297. width: 44px;
  1298. line-height: 44px;
  1299. font-weight: 500;
  1300. font-size: 16px;
  1301. text-align: center;
  1302. color: #4d5262;
  1303. overflow: hidden;
  1304. text-overflow: ellipsis;
  1305. white-space: nowrap;
  1306. }
  1307. }
  1308. .buiding-item {
  1309. //font-family: 'Montserrat';
  1310. width: 100%;
  1311. padding: 10px;
  1312. font-style: normal;
  1313. font-weight: 500;
  1314. font-size: 14px;
  1315. text-align: left;
  1316. color: rgba(13, 13, 61, 0.86);
  1317. border-bottom: 1px solid rgba(2, 2, 82, 0.03);
  1318. }
  1319. .active {
  1320. color: rgba(77, 148, 255, 0.8);
  1321. }
  1322. </style>