baseTopu1.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. // 以下为测试环境demo;
  34. this.getDataSuc(data);
  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. axios.post("/labsl/graph/pub/read", obj).then((res) => {
  55. // 注入测试数据
  56. this.getDataSuc(data);
  57. });
  58. },
  59. // 读图成功回调
  60. getDataSuc(data) {
  61. // if (data.result == "failure") return;
  62. // 添加图片路径
  63. if (data.content.elements.nodes && data.content.elements.nodes.length) {
  64. data.content.elements.nodes = data.content.elements.nodes.map((obj) => {
  65. if (obj.properties.type == "BaseEquipment") {
  66. if (obj.style.default.url) {
  67. obj.style.default.url = !obj.style.default.url.includes("/image-service/common/") ?
  68. "/image-service/common/image_get?systemId=dataPlatform&key=" +
  69. obj.style.default.url :obj.style.default.url;
  70. } else {
  71. // 默认图标
  72. obj.style.default.url =
  73. !obj.style.default.url.includes("/image-service/common/") ?
  74. "/image-service/common/image_get?systemId=dataPlatform&key=" +
  75. "1607752841478.svg" :obj.style.default.url;
  76. ;
  77. }
  78. }
  79. return obj;
  80. });
  81. }
  82. const parse = new PTopoParser();
  83. // 获取数据解析数据再将转化得实例添加到场景中
  84. // 测试数据
  85. parse.parseData(data.content.elements);
  86. parse.markers.forEach((item) => {
  87. this.scene.addItem(item);
  88. });
  89. parse.nodes.forEach((item) => {
  90. item.moveable = false //设备不可拖动
  91. this.scene.addItem(item);
  92. });
  93. parse.relations.forEach((t) => {
  94. this.scene.addItem(t);
  95. });
  96. setTimeout(()=>{
  97. this.view.fitSceneToView();
  98. })
  99. },
  100. // 清空画布
  101. clearImg() {
  102. this.scene = new SGraphScene();
  103. if (this.view) {
  104. this.view.update();
  105. }
  106. },
  107. },
  108. created() {
  109. this.clearImg();
  110. },
  111. mounted() {
  112. // 获取 canvas 的宽高
  113. this.canvasWidth = 800;
  114. this.canvasHeight = 600;
  115. // 初始化场景类
  116. this.view = new SGraphView("floor_topu");
  117. if (this.scene) {
  118. this.view.scene = this.scene;
  119. }
  120. this.init();
  121. },
  122. };
  123. </script>
  124. <style lang="less" scoped>
  125. .base-topu {
  126. width: 100%;
  127. height: 100%;
  128. position: relative;
  129. }
  130. </style>