baseTopu5.vue 4.6 KB

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