baseTopu9.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <!-- 派生 设备类 -->
  2. <template>
  3. <div ref="basetopu6" class="base-topu">
  4. <canvas
  5. id="floor_topu9"
  6. :width="canvasWidth"
  7. :height="canvasHeight"
  8. tabindex="0"
  9. ></canvas>
  10. <!-- 鹰眼窗口 -->
  11. <canvas
  12. id="enlarge"
  13. width="200"
  14. height="150"
  15. tabindex="0"
  16. style="border: 1px solid red; position: absolute; bottom: 0; left: 0"
  17. />
  18. </div>
  19. </template>
  20. <script>
  21. import { SGraphScene } from "@persagy-web/graph";
  22. import { FloorView } from "./FloorView";
  23. import { PTopoParser } from "./PTopoParser";
  24. import { topuFactory } from "./items/topuFactory1";
  25. import { EagleView } from "./items/EagleView";
  26. import { SColor, SPoint } from "@persagy-web/draw/";
  27. import axios from "axios";
  28. import { data } from "./data/data1.js"; //模拟接口返回参数
  29. export default {
  30. data() {
  31. return {
  32. canvasWidth: 0, // 画布的宽
  33. canvasHeight: 0, // 画布的高
  34. view: null, // 视图 view
  35. enlarge_view: null, //鹰眼图
  36. scene: null, // 场景类
  37. scene_enlarge: null, //鹰眼场景
  38. };
  39. },
  40. methods: {
  41. // 初始化
  42. init() {
  43. this.clearImg();
  44. this.view ? (this.view.scene = this.scene) : null;
  45. this.enlarge_view ? (this.enlarge_view.scene = this.scene_enlarge) : null;
  46. // 获取压缩包数据并解压
  47. this.getMapBlob();
  48. },
  49. // 请求获取地图的压缩包
  50. getMapBlob() {
  51. const obj = {
  52. graphId: "2dd925178d164a96941c34326ad340e8",
  53. id: "376f578716fb48febe8fb291b527169f",
  54. };
  55. // 请求头上加 projectId
  56. axios.interceptors.request.use(
  57. (config) => {
  58. config.headers = {
  59. projectId: "Pj1101050029", //项目id
  60. };
  61. return config;
  62. },
  63. (error) => {
  64. return Promise.reject(error);
  65. }
  66. );
  67. // 生产环境放开此代码获取真实数据
  68. // axios.post("/labsl/graph/pub/read", obj).then((res) => {
  69. // this.getDataSuc(data);
  70. // });
  71. // 以下为测试环境demo;
  72. this.getDataSuc(data);
  73. },
  74. // 读图成功回调
  75. getDataSuc(res) {
  76. // if (res.data.result == "failure") return;
  77. // 添加图片路径
  78. if (data.content.elements.nodes && data.content.elements.nodes.length) {
  79. data.content.elements.nodes = data.content.elements.nodes.map((obj) => {
  80. if (obj.properties.type == "BaseEquipment") {
  81. if (obj.style.default.url) {
  82. obj.style.default.url = !obj.style.default.url.includes(
  83. "/image-service/common/"
  84. )
  85. ? "/image-service/common/image_get?systemId=dataPlatform&key=" +
  86. obj.style.default.url
  87. : obj.style.default.url;
  88. } else {
  89. // 默认图标
  90. obj.style.default.url = !obj.style.default.url.includes(
  91. "/image-service/common/"
  92. )
  93. ? "/image-service/common/image_get?systemId=dataPlatform&key=" +
  94. "1607752841478.svg"
  95. : obj.style.default.url;
  96. }
  97. }
  98. return obj;
  99. });
  100. }
  101. // 生成大图解析器
  102. const parse = new PTopoParser();
  103. parse.factory = new topuFactory();
  104. // 生成鹰眼图解析器
  105. const parse_enlargee = new PTopoParser();
  106. parse_enlargee.factory = new topuFactory();
  107. // 获取数据解析数据再将转化得实例添加到场景中
  108. parse.parseData(data.content.elements);
  109. parse_enlargee.parseData(data.content.elements);
  110. parse.markers.forEach((item) => {
  111. item.moveable = false;
  112. this.scene.addItem(item);
  113. });
  114. parse.nodes.forEach((item) => {
  115. item.moveable = false;
  116. if ("BaseEquipment" == item.data.properties.type) {
  117. item.setEquipName();
  118. item.setStatusPointColor("#7ed321");
  119. }
  120. // 相关事件触发
  121. item.connect("onMouseDown", this, this.onMousedown);
  122. item.connect("onMouseUp", this, this.onMouseup);
  123. item.connect("onMouseLeave", this, this.onMouseleave);
  124. item.connect("onMouseEnter", this, this.onMouseenter);
  125. this.scene.addItem(item);
  126. });
  127. parse.relations.forEach((t) => {
  128. t.moveable = false;
  129. this.scene.addItem(t);
  130. });
  131. // 鹰眼图 获取数据解析数据再将转化得实例添加到场景中
  132. parse_enlargee.markers.forEach((item) => {
  133. item.moveable = false;
  134. this.scene_enlarge.addItem(item);
  135. });
  136. parse_enlargee.nodes.forEach((item) => {
  137. item.moveable = false;
  138. // 隐藏信息点名称
  139. let msgList = item.getMsgList().forEach((t) => {
  140. t.visible = false;
  141. });
  142. this.scene_enlarge.addItem(item);
  143. });
  144. parse_enlargee.relations.forEach((t) => {
  145. t.moveable = false;
  146. this.scene_enlarge.addItem(t);
  147. });
  148. // 监听放大视图中滚轮放缩事件, wheelScale 是回调
  149. this.view.connect("onMouseWheel", this, this.wheelScale);
  150. // 监听放大视图中滚轮按下拖动事件, midMouseMove 是回调
  151. this.view.connect("leftMove", this, this.midMouseMove);
  152. // 监听标准视图的事件, 回调函数是 mouseMove
  153. this.enlarge_view.connect("viewMouseMove", this, this.mouseMove);
  154. setTimeout(() => {
  155. // 设置放大视图为此时标准视图的 6 倍, 注意 6 = 1200 / 200; 1200是放大视图的宽,200 是标准视图中 可视区域的宽
  156. const scale = this.canvasHeight / 150;
  157. this.view.scale = scale * this.enlarge_view.scale;
  158. this.view.fitSceneToView();
  159. this.enlarge_view.fitSceneToView(1);
  160. // 设置标准视图不可放缩
  161. this.enlarge_view.scalable = false;
  162. });
  163. },
  164. // 鼠标点击事件
  165. onMousedown(item, e) {
  166. this.$emit("onMousedown", item, e);
  167. },
  168. // 放大视图 滚轮放缩事件回调
  169. wheelScale(item, ev) {
  170. // ev[0] view 的放缩比例
  171. // ev[1] true 上滚 放大
  172. if (this.view && this.enlarge_view) {
  173. if (ev[1]) {
  174. this.enlarge_view.rectW /= this.view.wheelZoom;
  175. this.enlarge_view.rectH /= this.view.wheelZoom;
  176. } else {
  177. this.enlarge_view.rectW *= this.view.wheelZoom;
  178. this.enlarge_view.rectH *= this.view.wheelZoom;
  179. }
  180. }
  181. },
  182. // 步骤5
  183. mouseMove(item, ev) {
  184. const p = ev[0];
  185. this.locationGraphy(p);
  186. },
  187. // 定位点到放大视图的中心点
  188. locationGraphy(point) {
  189. let centerX = this.view.width / 2 - point.x * this.view.scale;
  190. let centerY = this.view.height / 2 - point.y * this.view.scale;
  191. this.view.origin = new SPoint(centerX, centerY);
  192. },
  193. midMouseMove(item, ev) {
  194. if (this.enlarge_view && this.view) {
  195. const p = this.view.mapToScene(
  196. this.canvasWidth / 2,
  197. this.canvasHeight / 2
  198. );
  199. const p2 = this.enlarge_view.mapFromScene(p.x, p.y);
  200. this.enlarge_view.rectC.x = p2.x;
  201. this.enlarge_view.rectC.y = p2.y;
  202. this.enlarge_view.update();
  203. }
  204. },
  205. // 鼠标抬起事件
  206. onMouseup(item, e) {
  207. console.log("鼠标抬起!", item, e);
  208. },
  209. // 鼠标事件移入
  210. onMouseenter(item, e) {
  211. console.log("鼠标移入!", item, e);
  212. },
  213. // 鼠标事件移出
  214. onMouseleave(item, e) {
  215. console.log("鼠标移出!", item, e);
  216. },
  217. // 清空画布
  218. clearImg() {
  219. this.scene = new SGraphScene();
  220. this.scene_enlarge = new SGraphScene();
  221. if (this.view) {
  222. this.view.update();
  223. }
  224. if (this.enlarge_view) {
  225. this.enlarge_view.update();
  226. }
  227. },
  228. },
  229. created() {
  230. this.clearImg();
  231. },
  232. mounted() {
  233. // 获取 canvas 的宽高
  234. this.canvasWidth = 800;
  235. this.canvasHeight = 600;
  236. // 初始化场景类
  237. this.view = new FloorView("floor_topu9");
  238. // 初始化鹰眼场景类
  239. this.enlarge_view = new EagleView("enlarge"); // 小图
  240. if (this.scene) {
  241. this.view.scene = this.scene;
  242. this.enlarge_view.scene = this.scene_enlarge;
  243. }
  244. this.init();
  245. },
  246. };
  247. </script>
  248. <style lang="less" scoped>
  249. .base-topu {
  250. width: 100%;
  251. height: 100%;
  252. position: relative;
  253. }
  254. </style>