baseTopu8.vue 5.0 KB

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