baseTopoEditer.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. <template>
  2. <div class="baseTopo" id="baseTopo" ref="baseTopo">
  3. <topoTooltip
  4. v-show="showTooltip"
  5. class="topoTooltip-box"
  6. ref="topoTooltip"
  7. @closeTooltip="showTooltip = false"
  8. :havItem="havItem"
  9. ></topoTooltip>
  10. <canvas
  11. id="persagy_topo"
  12. :width="canvasWidth"
  13. :height="canvasHeight"
  14. tabindex="0"
  15. ></canvas>
  16. </div>
  17. </template>
  18. <script>
  19. import {
  20. PTopoScene,
  21. PTopoParser,
  22. PTopoView,
  23. } from "@/components/editClass/persagy-edit";
  24. import { SBaseEquipment } from "@/components/editClass/big-edit";
  25. import topoTooltip from "./topoTooltip.vue";
  26. import { mapState, mapMutations } from "vuex";
  27. import base64ToFile from "@/utils/base64ToFile";
  28. import { v1 as uuidv1 } from "uuid";
  29. import bus from "@/bus/bus";
  30. import axios from "axios";
  31. import {
  32. saveGroup,
  33. readGroup,
  34. uploadGroup,
  35. getImageGroup,
  36. readPubGroup,
  37. } from "@/api/editer";
  38. import { publishGraph } from "@/api/home";
  39. import crypto from "crypto-js/";
  40. export default {
  41. components: { topoTooltip },
  42. data() {
  43. return {
  44. scene: null, //场景
  45. view: null, //视图
  46. canvasWidth: 700, //画布宽
  47. canvasHeight: 700, //画布高
  48. havItem: false, //右击是否选中item
  49. showTooltip: false, //是否显示tooltip
  50. topoContent: {}, // 读图后存储图所有数据
  51. autoSave: null, // 自动保存定时器
  52. };
  53. },
  54. computed: {
  55. ...mapState([
  56. "editCmd",
  57. "legendObj",
  58. "graphId",
  59. "id",
  60. "isPub",
  61. "categoryId",
  62. "projectId",
  63. ]),
  64. },
  65. mounted() {
  66. this.canvasWidth = this.$refs.baseTopo.offsetWidth;
  67. this.canvasHeight = this.$refs.baseTopo.offsetHeight - 10;
  68. this.scene = new PTopoScene();
  69. this.view = new PTopoView("persagy_topo");
  70. this.view.scene = this.scene;
  71. this.scene.clearCmdStatus = this.clearCmdStatus;
  72. // 初始化bus绑定事件
  73. this.initBusEvent();
  74. // 右键事件
  75. this.scene.getItem = this.onContextMenu;
  76. this.scene.emitChoice = this.emitChoice;
  77. //左键事件
  78. this.scene.vueOnMouseDown = this.vueOnMouseDown;
  79. // 屏蔽浏览器右键功能(防止与编辑器右键交互重合)
  80. document.getElementById("baseTopo").oncontextmenu = function (e) {
  81. return false;
  82. };
  83. // 读取底图
  84. this.readtopoMsg();
  85. // 2分钟自动保存
  86. this.autoSave = setInterval(() => {
  87. this.autoSaveTopo();
  88. }, 120000);
  89. },
  90. methods: {
  91. ...mapMutations([
  92. "SETCHOICELEHEND",
  93. "SETLEGENDOBJ",
  94. "SETPROJECT",
  95. "SETCATEGROY",
  96. "SETISPUB",
  97. "ADDEQUIPITEM",
  98. "EDITEQUIPITEM",
  99. ]),
  100. // 恢复命令状态
  101. clearCmdStatus() {
  102. this.SETCHOICELEHEND("");
  103. this.SETLEGENDOBJ(null);
  104. },
  105. // 右键获取item
  106. onContextMenu(item, [event]) {
  107. this.showTooltip = true;
  108. if (item) {
  109. this.havItem = true;
  110. } else {
  111. this.havItem = false;
  112. }
  113. const doms = document.getElementsByClassName("topoTooltip-box")[0];
  114. doms.style.left = event.offsetX + "px";
  115. doms.style.top = event.offsetY + "px";
  116. },
  117. // 左键事键
  118. vueOnMouseDown(e) {
  119. // 关闭tooltip
  120. this.showTooltip = false;
  121. },
  122. // 选中后的回调函数
  123. emitChoice(itemList) {
  124. bus.$emit("emitChoice", itemList);
  125. },
  126. //初始化bus绑定事件
  127. initBusEvent() {
  128. // 改变样式
  129. bus.$off("updateStyle");
  130. bus.$on("updateStyle", (type, val) => {
  131. this.scene.updateStyle(type, val);
  132. });
  133. // 撤销
  134. bus.$off("topoUndo");
  135. bus.$on("topoUndo", (val) => {
  136. this.scene.undo();
  137. });
  138. // 重做
  139. bus.$off("topoRedo");
  140. bus.$on("topoRedo", (val) => {
  141. this.scene.redo();
  142. });
  143. // 删除
  144. bus.$off("deleteItem");
  145. bus.$on("deleteItem", (val) => {
  146. this.scene.deleteItem([val]);
  147. this.EDITEQUIPITEM();
  148. });
  149. // 复制
  150. bus.$off("copy");
  151. bus.$on("copy", (val) => {
  152. this.scene.copy();
  153. });
  154. // 粘贴
  155. bus.$off("paste");
  156. bus.$on("paste", (val) => {
  157. this.scene.paste();
  158. });
  159. // 保存
  160. bus.$off("saveTopo");
  161. bus.$on("saveTopo", (val) => {
  162. this.saveTopoDraft();
  163. });
  164. // 设置实例置顶置底
  165. bus.$off("setOrder");
  166. bus.$on("setOrder", (val) => {
  167. this.scene.setOrder(val);
  168. });
  169. // 设置实例status状态
  170. bus.$off("setItemStatus");
  171. bus.$on("setItemStatus", (val) => {
  172. this.scene.setItemStatus();
  173. });
  174. // 下载图片
  175. bus.$off("saveTopoImg");
  176. bus.$on("saveTopoImg", () => {
  177. // 隐藏选择器
  178. this.scene.selectContainer.clear();
  179. setTimeout(() => {
  180. this.view.saveImage(`${this.topoContent.name}.png`, "png");
  181. }, 80);
  182. });
  183. // 发布图片
  184. bus.$off("issueTopo");
  185. bus.$on("issueTopo", () => {
  186. this.saveTopoDraft().then(() => {
  187. this.issueDraft();
  188. });
  189. });
  190. // 手动添加设备实例
  191. bus.$off("addEquipment");
  192. bus.$on("addEquipment", (val) => {
  193. this.addEquipmentList(val);
  194. });
  195. // 更改设备信息点
  196. bus.$off("changeEquipMsgPoint");
  197. bus.$on("changeEquipMsgPoint", (val) => {
  198. this.scene.changeEquipMsgPoint(val);
  199. });
  200. // 选中item
  201. bus.$off("chioceItem");
  202. bus.$on("chioceItem", (item) => {
  203. this.scene.toggleItem(item);
  204. });
  205. },
  206. // 读取拓扑图
  207. readtopoMsg() {
  208. const obj = {
  209. graphId: this.graphId,
  210. id: this.id,
  211. };
  212. if (this.isPub == 1) {
  213. // 已发布
  214. readPubGroup(obj).then((res) => {
  215. this.getDataSuc(res);
  216. });
  217. } else {
  218. readGroup(obj).then((res) => {
  219. this.getDataSuc(res);
  220. });
  221. }
  222. },
  223. // 读图成功回调
  224. getDataSuc(res) {
  225. if (res.result == "failure") return;
  226. this.SETCATEGROY(res.content);
  227. this.topoContent = res.content;
  228. const parse = new PTopoParser();
  229. parse.parseData(res.content.elements);
  230. parse.markers.forEach((item) => {
  231. item.selectable = true;
  232. item.moveable = true;
  233. item.connect("finishCreated", this.scene, this.scene.finishCreated);
  234. item.connect("onContextMenu", this, this.scene.getItem);
  235. this.scene.addItem(item);
  236. });
  237. parse.nodes.forEach((item) => {
  238. item.connect("finishCreated", this.scene, this.scene.finishCreated);
  239. item.connect("onContextMenu", this, this.scene.getItem);
  240. this.scene.addItem(item);
  241. // 如果为设备则存于vuex中便于联动
  242. if (item instanceof SBaseEquipment) {
  243. this.ADDEQUIPITEM(item);
  244. }
  245. });
  246. },
  247. // 生成快照并保存草稿
  248. saveTopoDraft() {
  249. const uuid = uuidv1();
  250. return Promise.all([this.generateSnap(uuid), this.saveDraft(uuid)]).then(
  251. (vals) => {
  252. this.$message.success(`保存成功${vals[1].version}`);
  253. }
  254. );
  255. },
  256. // 生成快照
  257. generateSnap(uuid) {
  258. // 隐藏选择器
  259. this.scene.selectContainer.clear();
  260. setTimeout(() => {
  261. // base64数据
  262. const data = this.view.imageUrl("png");
  263. // 根据base64生成file
  264. const file = base64ToFile(data);
  265. const reader = new FileReader();
  266. const fileType = file.name.split(".");
  267. const imgType = fileType[fileType.length - 1];
  268. return new Promise((resolve, reject) => {
  269. reader.onloadend = function () {
  270. // 这个事件在读取结束后,无论成功或者失败都会触发
  271. if (reader.error) {
  272. console.log("reader error", reader.error);
  273. reject(reader.error);
  274. } else {
  275. // 构造 XMLHttpRequest 对象,发送文件 Binary 数据
  276. const xhr = new XMLHttpRequest();
  277. xhr.open(
  278. "POST",
  279. `/image-service/common/image_upload?systemId=dataPlatform&secret=9e0891a7a8c8e885&overwrite=true&key=${uuid}.${imgType}`
  280. );
  281. xhr.send(reader.result);
  282. xhr.onreadystatechange = function () {
  283. if (xhr.readyState == 4) {
  284. if (xhr.status == 200) {
  285. resolve(xhr);
  286. }
  287. }
  288. };
  289. }
  290. };
  291. reader.readAsArrayBuffer(file);
  292. });
  293. }, 80);
  294. },
  295. // 保存草稿
  296. saveDraft(uuid) {
  297. const elements = this.scene.save();
  298. console.log("elements", elements);
  299. const obj = {
  300. elements,
  301. name: this.topoContent.name, // 名称
  302. categoryId: this.categoryId, // 图分类ID
  303. projectId: this.projectId, // 项目ID
  304. label: this.topoContent.label,
  305. buildingId: "1", // 建筑ID
  306. floorId: "1", // 楼层id
  307. note: "1", // 图说明
  308. pic: `${uuid}.png`,
  309. graphId: this.graphId,
  310. id: this.id,
  311. log: {
  312. // 图操作日志
  313. mark: "1", // 图的存盘标记
  314. commandList: [
  315. {
  316. command: "1", // 命令
  317. desc: "1", // 描述
  318. detail: "1", // 详情
  319. },
  320. ],
  321. },
  322. };
  323. console.log("elements", obj);
  324. return new Promise((resolve, reject) => {
  325. saveGroup(obj).then((res) => {
  326. // 如果是从已发布跳转过来
  327. if (this.isPub == 1) {
  328. // 设置发布状态为 未发布
  329. this.SETISPUB(0);
  330. const gid = res.entityList[0].graphId;
  331. const id = res.entityList[0].id;
  332. // 重设图id 与 id
  333. this.SETPROJECT({ graphId: gid, id: id });
  334. // 修改url参数
  335. this.$router.push({
  336. name: "Editer",
  337. query: {
  338. graphId: gid,
  339. id: id,
  340. categoryName: encodeURI(this.categoryName),
  341. isPub: 0,
  342. },
  343. });
  344. }
  345. resolve(res.entityList[0]);
  346. });
  347. });
  348. },
  349. // 自动保存
  350. autoSaveTopo() {
  351. console.log(this.scene.undoStack.isChange);
  352. if (this.scene && this.scene.undoStack.isChange) {
  353. this.saveTopoDraft().then(() => {
  354. this.scene.undoStack.isChange = false;
  355. });
  356. }
  357. },
  358. // 发布草稿
  359. issueDraft() {
  360. const pa = {
  361. graphId: this.graphId,
  362. id: this.id,
  363. };
  364. publishGraph(pa).then((res) => {
  365. this.$message.success("发布成功");
  366. });
  367. },
  368. // 手动添加设备
  369. addEquipmentList(list) {
  370. const parse = new PTopoParser();
  371. list.forEach((item, i) => {
  372. const x = (i + 1) * 100 + 300;
  373. const baseUrl =
  374. "/image-service/common/image_get?systemId=dataPlatform&key=";
  375. const url = baseUrl + item.url;
  376. let svg2Base = "";
  377. let EquipHeight = this.canvasHeight - 100;
  378. let data = {
  379. /** 名称 */
  380. name: "基础设备",
  381. /** 返回物理世界对象 ID 列表 */
  382. attachObjectIds: [item.id],
  383. size: { width: 50, height: 50 },
  384. /** 图标 (Image),线类型 (Line) */
  385. type: "Image",
  386. /** 位置 */
  387. pos: { x: x, y: 100 },
  388. /** 由应用自己定义 */
  389. properties: {
  390. type: "BaseEquipment",
  391. classCode: item.classCode, // 设备类型
  392. localId: item.localId, // 本地编码
  393. localName: item.localName, //本地名称
  394. },
  395. style: {
  396. default: {
  397. strokecolor: "#c0ccda",
  398. url: url,
  399. base64Url: "",
  400. },
  401. },
  402. };
  403. parse.addNode(data);
  404. });
  405. // 添加到 scence 中
  406. parse.nodes.forEach((item) => {
  407. item.connect("finishCreated", this.scene, this.scene.finishCreated);
  408. item.connect("onContextMenu", this, this.scene.getItem);
  409. this.scene.addItem(item);
  410. // 如果为设备则存于vuex中便于联动
  411. if (item instanceof SBaseEquipment) {
  412. this.ADDEQUIPITEM(item);
  413. }
  414. });
  415. },
  416. },
  417. watch: {
  418. editCmd(val) {
  419. if (this.scene) {
  420. // 设置当前编辑状态
  421. this.scene.editCmd = val;
  422. }
  423. },
  424. legendObj: {
  425. handler: function (val, oldVal) {
  426. this.scene.legendObj = val;
  427. },
  428. deep: true,
  429. },
  430. },
  431. created() {
  432. this.SETPROJECT(this.$route.query);
  433. this.SETISPUB(this.$route.query.isPub);
  434. this.categoryName = decodeURI(this.$route.query.categoryName);
  435. },
  436. beforeDestroy() {
  437. clearInterval(this.autoSave);
  438. },
  439. };
  440. </script>
  441. <style lang="less" scoped>
  442. .baseTopo {
  443. width: 100%;
  444. height: 100%;
  445. position: relative;
  446. .topoTooltip-box {
  447. position: absolute;
  448. left: 0;
  449. top: 0;
  450. }
  451. }
  452. </style>