baseTopu7.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <!-- 派生 设备类 -->
  2. <template>
  3. <div ref="basetopu6" class="base-topu">
  4. <canvas
  5. id="floor_topu7"
  6. :width="canvasWidth"
  7. :height="canvasHeight"
  8. tabindex="0"
  9. ></canvas>
  10. </div>
  11. </template>
  12. <script>
  13. import { SGraphScene } from "@persagy-web/graph";
  14. import { FloorView } from "./FloorView";
  15. import { PTopoParser } from "./PTopoParser";
  16. import { topuFactory } from "./items/topuFactory1";
  17. import { SColor } from "@persagy-web/draw/";
  18. import axios from "axios";
  19. import { data } from "./data/data1.js"; //模拟接口返回参数
  20. export default {
  21. data() {
  22. return {
  23. canvasWidth: 0, // 画布的宽
  24. canvasHeight: 0, // 画布的高
  25. view: null, // 视图 view
  26. scene: null, // 场景类
  27. };
  28. },
  29. methods: {
  30. // 初始化
  31. init() {
  32. this.clearImg();
  33. this.view ? (this.view.scene = this.scene) : null;
  34. // 获取压缩包数据并解压
  35. this.getMapBlob();
  36. },
  37. // 请求获取地图的压缩包
  38. getMapBlob() {
  39. const obj = {
  40. graphId: "2dd925178d164a96941c34326ad340e8",
  41. id: "376f578716fb48febe8fb291b527169f",
  42. };
  43. // 请求头上加 projectId
  44. axios.interceptors.request.use(
  45. (config) => {
  46. config.headers = {
  47. projectId: "Pj1101050029", //项目id
  48. };
  49. return config;
  50. },
  51. (error) => {
  52. return Promise.reject(error);
  53. }
  54. );
  55. // 生产环境放开此代码获取真实数据
  56. // axios.post("/labsl/graph/pub/read", obj).then((res) => {
  57. // this.getDataSuc(data);
  58. // });
  59. // 以下为测试环境demo;
  60. this.getDataSuc(data);
  61. },
  62. // 读图成功回调
  63. getDataSuc(res) {
  64. // if (res.data.result == "failure") return;
  65. // 添加图片路径
  66. if (data.content.elements.nodes && data.content.elements.nodes.length) {
  67. data.content.elements.nodes = data.content.elements.nodes.map((obj) => {
  68. if (obj.properties.type == "BaseEquipment") {
  69. if (obj.style.default.url) {
  70. obj.style.default.url = !obj.style.default.url.includes(
  71. "/image-service/common/"
  72. )
  73. ? "/image-service/common/image_get?systemId=dataPlatform&key=" +
  74. obj.style.default.url
  75. : obj.style.default.url;
  76. } else {
  77. // 默认图标
  78. obj.style.default.url = !obj.style.default.url.includes(
  79. "/image-service/common/"
  80. )
  81. ? "/image-service/common/image_get?systemId=dataPlatform&key=" +
  82. "1607752841478.svg"
  83. : obj.style.default.url;
  84. }
  85. }
  86. return obj;
  87. });
  88. }
  89. const parse = new PTopoParser();
  90. parse.factory = new topuFactory();
  91. // 获取数据解析数据再将转化得实例添加到场景中
  92. parse.parseData(data.content.elements);
  93. parse.markers.forEach((item) => {
  94. item.moveable = false;
  95. this.scene.addItem(item);
  96. });
  97. parse.nodes.forEach((item) => {
  98. item.moveable = false;
  99. if ("BaseEquipment" == item.data.properties.type) {
  100. item.setEquipName();
  101. item.setStatusPointColor("#7ed321");
  102. }
  103. // 相关事件触发
  104. item.connect("onMouseDown", this, this.onMousedown);
  105. item.connect("onMouseUp", this, this.onMouseup);
  106. item.connect("onMouseLeave", this, this.onMouseleave);
  107. item.connect("onMouseEnter", this, this.onMouseenter);
  108. this.scene.addItem(item);
  109. });
  110. parse.relations.forEach((t) => {
  111. t.moveable = false;
  112. this.scene.addItem(t);
  113. });
  114. setTimeout(() => {
  115. this.view.fitSceneToView();
  116. });
  117. },
  118. // 鼠标点击事件
  119. onMousedown(item, e) {
  120. console.log("鼠标按下!", item, e);
  121. this.$emit("onMousedown", item, e);
  122. },
  123. // 鼠标抬起事件
  124. onMouseup(item, e) {
  125. console.log("鼠标抬起!", item, e);
  126. },
  127. // 鼠标事件移入
  128. onMouseenter(item, e) {
  129. console.log("鼠标移入!", item, e);
  130. },
  131. // 鼠标事件移出
  132. onMouseleave(item, e) {
  133. console.log("鼠标移出!", item, e);
  134. },
  135. // 清空画布
  136. clearImg() {
  137. this.scene = new SGraphScene();
  138. if (this.view) {
  139. this.view.update();
  140. }
  141. },
  142. },
  143. created() {
  144. this.clearImg();
  145. },
  146. mounted() {
  147. // 获取 canvas 的宽高
  148. this.canvasWidth = 800;
  149. this.canvasHeight = 600;
  150. // 初始化场景类
  151. this.view = new FloorView("floor_topu7");
  152. if (this.scene) {
  153. this.view.scene = this.scene;
  154. }
  155. this.init();
  156. },
  157. };
  158. </script>
  159. <style lang="less" scoped>
  160. .base-topu {
  161. width: 100%;
  162. height: 100%;
  163. position: relative;
  164. }
  165. </style>