123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453 |
- <template>
- <div class="baseTopo" id="baseTopo" ref="baseTopo">
- <topoTooltip
- v-show="showTooltip"
- class="topoTooltip-box"
- ref="topoTooltip"
- @closeTooltip="showTooltip = false"
- :havItem="havItem"
- ></topoTooltip>
- <canvas
- id="persagy_topo"
- :width="canvasWidth"
- :height="canvasHeight"
- tabindex="0"
- ></canvas>
- </div>
- </template>
- <script>
- import {
- PTopoScene,
- PTopoParser,
- PTopoView,
- } from "@/components/editClass/persagy-edit";
- import { SBaseEquipment } from "@/components/editClass/big-edit";
- import topoTooltip from "./topoTooltip.vue";
- import { mapState, mapMutations } from "vuex";
- import base64ToFile from "@/utils/base64ToFile";
- import { v1 as uuidv1 } from "uuid";
- import bus from "@/bus/bus";
- import axios from "axios";
- import {
- saveGroup,
- readGroup,
- uploadGroup,
- getImageGroup,
- readPubGroup,
- } from "@/api/editer";
- import { publishGraph } from "@/api/home";
- import crypto from "crypto-js/";
- export default {
- components: { topoTooltip },
- data() {
- return {
- scene: null,
- view: null,
- canvasWidth: 700,
- canvasHeight: 700,
- havItem: false,
- showTooltip: false,
- topoContent: {},
- autoSave: null,
- };
- },
- computed: {
- ...mapState([
- "editCmd",
- "legendObj",
- "graphId",
- "id",
- "isPub",
- "categoryId",
- "projectId",
- ]),
- },
- mounted() {
- this.canvasWidth = this.$refs.baseTopo.offsetWidth;
- this.canvasHeight = this.$refs.baseTopo.offsetHeight - 10;
- this.scene = new PTopoScene();
- this.view = new PTopoView("persagy_topo");
- this.view.scene = this.scene;
- this.scene.clearCmdStatus = this.clearCmdStatus;
-
- this.initBusEvent();
-
- this.scene.getItem = this.onContextMenu;
- this.scene.emitChoice = this.emitChoice;
-
- this.scene.vueOnMouseDown = this.vueOnMouseDown;
-
- document.getElementById("baseTopo").oncontextmenu = function (e) {
- return false;
- };
-
- this.readtopoMsg();
-
- this.autoSave = setInterval(() => {
- this.autoSaveTopo();
- }, 120000);
- },
- methods: {
- ...mapMutations([
- "SETCHOICELEHEND",
- "SETLEGENDOBJ",
- "SETPROJECT",
- "SETCATEGROY",
- "SETISPUB",
- "ADDEQUIPITEM",
- "EDITEQUIPITEM",
- ]),
-
- clearCmdStatus() {
- this.SETCHOICELEHEND("");
- this.SETLEGENDOBJ(null);
- },
-
- onContextMenu(item, [event]) {
- this.showTooltip = true;
- if (item) {
- this.havItem = true;
- } else {
- this.havItem = false;
- }
- const doms = document.getElementsByClassName("topoTooltip-box")[0];
- doms.style.left = event.offsetX + "px";
- doms.style.top = event.offsetY + "px";
- },
-
- vueOnMouseDown(e) {
-
- this.showTooltip = false;
- },
-
- emitChoice(itemList) {
- bus.$emit("emitChoice", itemList);
- },
-
- initBusEvent() {
-
- bus.$off("updateStyle");
- bus.$on("updateStyle", (type, val) => {
- this.scene.updateStyle(type, val);
- });
-
- bus.$off("topoUndo");
- bus.$on("topoUndo", (val) => {
- this.scene.undo();
- });
-
- bus.$off("topoRedo");
- bus.$on("topoRedo", (val) => {
- this.scene.redo();
- });
-
- bus.$off("deleteItem");
- bus.$on("deleteItem", (val) => {
- this.scene.deleteItem([val]);
- this.EDITEQUIPITEM();
- });
-
- bus.$off("copy");
- bus.$on("copy", (val) => {
- this.scene.copy();
- });
-
- bus.$off("paste");
- bus.$on("paste", (val) => {
- this.scene.paste();
- });
-
- bus.$off("saveTopo");
- bus.$on("saveTopo", (val) => {
- this.saveTopoDraft();
- });
-
- bus.$off("setOrder");
- bus.$on("setOrder", (val) => {
- this.scene.setOrder(val);
- });
-
- bus.$off("setItemStatus");
- bus.$on("setItemStatus", (val) => {
- this.scene.setItemStatus();
- });
-
- bus.$off("saveTopoImg");
- bus.$on("saveTopoImg", () => {
-
- this.scene.selectContainer.clear();
- setTimeout(() => {
- this.view.saveImage(`${this.topoContent.name}.png`, "png");
- }, 80);
- });
-
- bus.$off("issueTopo");
- bus.$on("issueTopo", () => {
- this.saveTopoDraft().then(() => {
- this.issueDraft();
- });
- });
-
- bus.$off("addEquipment");
- bus.$on("addEquipment", (val) => {
- this.addEquipmentList(val);
- });
-
- bus.$off("changeEquipMsgPoint");
- bus.$on("changeEquipMsgPoint", (val) => {
- this.scene.changeEquipMsgPoint(val);
- });
-
- bus.$off("chioceItem");
- bus.$on("chioceItem", (item) => {
- this.scene.toggleItem(item);
- });
- },
-
- readtopoMsg() {
- const obj = {
- graphId: this.graphId,
- id: this.id,
- };
- if (this.isPub == 1) {
-
- readPubGroup(obj).then((res) => {
- this.getDataSuc(res);
- });
- } else {
- readGroup(obj).then((res) => {
- this.getDataSuc(res);
- });
- }
- },
-
- getDataSuc(res) {
- if (res.result == "failure") return;
- this.SETCATEGROY(res.content);
- this.topoContent = res.content;
- const parse = new PTopoParser();
- parse.parseData(res.content.elements);
- parse.markers.forEach((item) => {
- item.selectable = true;
- item.moveable = true;
- item.connect("finishCreated", this.scene, this.scene.finishCreated);
- item.connect("onContextMenu", this, this.scene.getItem);
- this.scene.addItem(item);
- });
- parse.nodes.forEach((item) => {
- item.connect("finishCreated", this.scene, this.scene.finishCreated);
- item.connect("onContextMenu", this, this.scene.getItem);
- this.scene.addItem(item);
-
- if (item instanceof SBaseEquipment) {
- this.ADDEQUIPITEM(item);
- }
- });
- },
-
- saveTopoDraft() {
- const uuid = uuidv1();
- return Promise.all([this.generateSnap(uuid), this.saveDraft(uuid)]).then(
- (vals) => {
- this.$message.success(`保存成功${vals[1].version}`);
- }
- );
- },
-
- generateSnap(uuid) {
-
- this.scene.selectContainer.clear();
- setTimeout(() => {
-
- const data = this.view.imageUrl("png");
-
- const file = base64ToFile(data);
- const reader = new FileReader();
- const fileType = file.name.split(".");
- const imgType = fileType[fileType.length - 1];
- return new Promise((resolve, reject) => {
- reader.onloadend = function () {
-
- if (reader.error) {
- console.log("reader error", reader.error);
- reject(reader.error);
- } else {
-
- const xhr = new XMLHttpRequest();
- xhr.open(
- "POST",
- `/image-service/common/image_upload?systemId=dataPlatform&secret=9e0891a7a8c8e885&overwrite=true&key=${uuid}.${imgType}`
- );
- xhr.send(reader.result);
- xhr.onreadystatechange = function () {
- if (xhr.readyState == 4) {
- if (xhr.status == 200) {
- resolve(xhr);
- }
- }
- };
- }
- };
- reader.readAsArrayBuffer(file);
- });
- }, 80);
- },
-
- saveDraft(uuid) {
- const elements = this.scene.save();
- console.log("elements", elements);
- const obj = {
- elements,
- name: this.topoContent.name,
- categoryId: this.categoryId,
- projectId: this.projectId,
- label: this.topoContent.label,
- buildingId: "1",
- floorId: "1",
- note: "1",
- pic: `${uuid}.png`,
- graphId: this.graphId,
- id: this.id,
- log: {
-
- mark: "1",
- commandList: [
- {
- command: "1",
- desc: "1",
- detail: "1",
- },
- ],
- },
- };
- console.log("elements", obj);
- return new Promise((resolve, reject) => {
- saveGroup(obj).then((res) => {
-
- if (this.isPub == 1) {
-
- this.SETISPUB(0);
- const gid = res.entityList[0].graphId;
- const id = res.entityList[0].id;
-
- this.SETPROJECT({ graphId: gid, id: id });
-
- this.$router.push({
- name: "Editer",
- query: {
- graphId: gid,
- id: id,
- categoryName: encodeURI(this.categoryName),
- isPub: 0,
- },
- });
- }
- resolve(res.entityList[0]);
- });
- });
- },
-
- autoSaveTopo() {
- console.log(this.scene.undoStack.isChange);
- if (this.scene && this.scene.undoStack.isChange) {
- this.saveTopoDraft().then(() => {
- this.scene.undoStack.isChange = false;
- });
- }
- },
-
- issueDraft() {
- const pa = {
- graphId: this.graphId,
- id: this.id,
- };
- publishGraph(pa).then((res) => {
- this.$message.success("发布成功");
- });
- },
-
- addEquipmentList(list) {
- const parse = new PTopoParser();
- list.forEach((item, i) => {
- const x = (i + 1) * 100 + 300;
- const baseUrl =
- "/image-service/common/image_get?systemId=dataPlatform&key=";
- const url = baseUrl + item.url;
- let svg2Base = "";
- let EquipHeight = this.canvasHeight - 100;
- let data = {
-
- name: "基础设备",
-
- attachObjectIds: [item.id],
- size: { width: 50, height: 50 },
-
- type: "Image",
-
- pos: { x: x, y: 100 },
-
- properties: {
- type: "BaseEquipment",
- classCode: item.classCode,
- localId: item.localId,
- localName: item.localName,
- },
- style: {
- default: {
- strokecolor: "#c0ccda",
- url: url,
- base64Url: "",
- },
- },
- };
- parse.addNode(data);
- });
-
- parse.nodes.forEach((item) => {
- item.connect("finishCreated", this.scene, this.scene.finishCreated);
- item.connect("onContextMenu", this, this.scene.getItem);
- this.scene.addItem(item);
-
- if (item instanceof SBaseEquipment) {
- this.ADDEQUIPITEM(item);
- }
- });
- },
- },
- watch: {
- editCmd(val) {
- if (this.scene) {
-
- this.scene.editCmd = val;
- }
- },
- legendObj: {
- handler: function (val, oldVal) {
- this.scene.legendObj = val;
- },
- deep: true,
- },
- },
- created() {
- this.SETPROJECT(this.$route.query);
- this.SETISPUB(this.$route.query.isPub);
- this.categoryName = decodeURI(this.$route.query.categoryName);
- },
- beforeDestroy() {
- clearInterval(this.autoSave);
- },
- };
- </script>
- <style lang="less" scoped>
- .baseTopo {
- width: 100%;
- height: 100%;
- position: relative;
- .topoTooltip-box {
- position: absolute;
- left: 0;
- top: 0;
- }
- }
- </style>
|