baseTopu1.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. axios.post("/labsl/graph/pub/read", obj).then((res) => {
  53. this.getDataSuc(res);
  54. });
  55. },
  56. // 读图成功回调
  57. getDataSuc(res) {
  58. if (res.data.result == "failure") return;
  59. const parse = new PTopoParser();
  60. // 获取数据解析数据再将转化得实例添加到场景中
  61. // 测试数据
  62. parse.parseData(data.content.elements);
  63. parse.markers.forEach((item) => {
  64. this.scene.addItem(item);
  65. });
  66. parse.nodes.forEach((item) => {
  67. this.scene.addItem(item);
  68. });
  69. parse.relations.forEach((t) => {
  70. this.scene.addItem(t);
  71. });
  72. this.view.fitSceneToView();
  73. },
  74. // 清空画布
  75. clearImg() {
  76. this.scene = new SGraphScene();
  77. if (this.view) {
  78. this.view.update();
  79. }
  80. },
  81. },
  82. created() {
  83. this.clearImg();
  84. },
  85. mounted() {
  86. // 获取 canvas 的宽高
  87. this.canvasWidth = 800;
  88. this.canvasHeight = 600;
  89. // 初始化场景类
  90. this.view = new SGraphView("floor_topu");
  91. if (this.scene) {
  92. this.view.scene = this.scene;
  93. }
  94. this.init();
  95. },
  96. };
  97. </script>
  98. <style lang="less" scoped>
  99. .base-topu {
  100. width: 100%;
  101. height: 100%;
  102. position: relative;
  103. }
  104. </style>