baseTopu2.vue 4.4 KB

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