baseEditer.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. <template>
  2. <div id="baseEditer" ref="graphy">
  3. <div id="fengMap"></div>
  4. <div class="canvas-container">
  5. <canvas id="canvas" :width="canvasWidth" :height="canvasHeight" ref="canvas" tabindex="0"></canvas>
  6. </div>
  7. </div>
  8. </template>
  9. <script>
  10. import { SFengParser } from "@saga-web/feng-map";
  11. import { SFloorParser } from "@saga-web/big";
  12. import { FloorView } from "./../lib/FloorView";
  13. import { EditScence } from "./mapClass/EditScence";
  14. import bus from "@/bus";
  15. import {
  16. saveGroup,
  17. readGroup,
  18. queryTypeGraph,
  19. publishGraph
  20. } from "@/api/editer.js";
  21. import { STopologyParser } from "./../lib/parsers/STopologyParser";
  22. import { uuid } from "@/components/mapClass/until";
  23. import { SImageItem } from "@saga-web/graph/lib";
  24. import { SPainter, SColor, SFont, SPoint } from "@saga-web/draw";
  25. import store from "../store";
  26. import { Loading } from "element-ui";
  27. import { Message } from "element-ui";
  28. let fengmap = null;
  29. //// 底图空间增加字段 isExtracted:boolean true 已被提取过
  30. export default {
  31. props: {
  32. cmdType: {
  33. type: String,
  34. default: "choice",
  35. required: false
  36. },
  37. changeTextMsg: {
  38. type: String,
  39. default: "",
  40. required: false
  41. }
  42. },
  43. data() {
  44. return {
  45. appName: "万达可视化系统",
  46. key: "23f30a832a862c58637a4aadbf50a566",
  47. mapServerURL: "http://map.wanda.cn/editor",
  48. canvasWidth: 700,
  49. canvasHeight: 800,
  50. fParser: null,
  51. scene: null,
  52. view: null,
  53. floorList: {},
  54. urlMsg: {},
  55. chiceItemList: [], //选中itemlist
  56. hasTypeList: [], // 当前类型下包含的typeid(提取)
  57. graphId: "",
  58. initScale: 1, //加载好底图之后的,初始缩放比例
  59. changeScaleByClick: false //区分 滚轮,点击 事件改变的缩放比例
  60. };
  61. },
  62. mounted() {
  63. this.canvasWidth = this.$refs.graphy.offsetWidth;
  64. this.canvasHeight = this.$refs.graphy.offsetHeight - 10;
  65. this.init();
  66. // 挂在bus
  67. this.getBus();
  68. store.dispatch("getElementType", { PageSize: 1000 });
  69. window.vm = this;
  70. const that = this;
  71. document.onkeydown = function(event) {
  72. const e = event || window.event || arguments.callee.caller.arguments[0];
  73. if (e && e.key == "Control") {
  74. // 按 ctrl
  75. that.scene.isDownCtrl = true;
  76. }
  77. };
  78. document.onkeyup = function(event) {
  79. const e = event || window.event || arguments.callee.caller.arguments[0];
  80. if (e && e.key == "Control") {
  81. // 按 ctrl
  82. that.scene.isDownCtrl = false;
  83. this.scene.setCmd = 'choice'
  84. }
  85. };
  86. },
  87. methods: {
  88. init() {
  89. const loadings = Loading.service({
  90. lock: true,
  91. text: "Loading",
  92. spinner: "el-icon-loading",
  93. background: "rgba(0, 0, 0, 0.7)"
  94. });
  95. document.getElementById(`canvas`).focus();
  96. this.clearGraphy();
  97. this.scene = new EditScence();
  98. fengmap = new SFengParser(
  99. "fengMap",
  100. this.mapServerURL + "/fmap/" + this.urlMsg.fmapID,
  101. this.key,
  102. this.appName,
  103. null
  104. );
  105. const floorid = this.urlMsg.FloorID;
  106. fengmap.loadMap(this.urlMsg.fmapID, resp => {
  107. this.floorList = resp;
  108. fengmap
  109. .loadTheme(
  110. `${this.mapServerURL}/webtheme/${this.urlMsg.fmapID}/${this.urlMsg.fmapID}.theme`
  111. )
  112. .then(response => {
  113. console.log("获取rf成功", response);
  114. this.parserData(floorid);
  115. this.readGraph();
  116. loadings.close();
  117. });
  118. this.view.fitSceneToView();
  119. });
  120. // 获取typeid
  121. this.getTypeId();
  122. this.scene.emitChange = this.emitChange;
  123. this.scene.scenceUpdate = this.scenceUpdate;
  124. },
  125. parserData(floor) {
  126. if (floor == "g80") {
  127. // 屋顶
  128. if (fengmap.frImg) {
  129. const imgItem = new SImageItem(
  130. null,
  131. `${this.mapServerURL}/webtheme/${this.urlMsg.fmapID}/${fengmap.frImg}`
  132. );
  133. this.scene.addItem(imgItem);
  134. this.view.scene = this.scene;
  135. this.view.fitSceneToView();
  136. this.loading = false;
  137. this.isQuerying = false;
  138. }
  139. } else {
  140. if (this.floorList[floor]) {
  141. fengmap.parseData(this.floorList[floor], res => {
  142. if (res.err) {
  143. console.log(res.err);
  144. return;
  145. }
  146. this.fParser = new SFloorParser(null);
  147. this.fParser.parseData(res);
  148. this.scene.fidToItem = {};
  149. this.fParser.spaceList.forEach(t => {
  150. t.zOrder = t.zOrder + t.data.Height;
  151. t.selectable = true;
  152. this.scene.fidToItem[t.data.SourceId] = t;
  153. this.scene.addItem(t);
  154. });
  155. this.scene.spaceList = this.fParser.spaceList;
  156. this.fParser.wallList.forEach(t => this.scene.addItem(t));
  157. this.fParser.virtualWallList.forEach(t => this.scene.addItem(t));
  158. this.fParser.doorList.forEach(t => this.scene.addItem(t));
  159. this.fParser.columnList.forEach(t => this.scene.addItem(t));
  160. this.fParser.casementList.forEach(t => this.scene.addItem(t));
  161. this.view.scene = this.scene;
  162. this.view.fitSceneToView();
  163. this.loading = false;
  164. this.isQuerying = false;
  165. console.log("success");
  166. // 设置初始化缩放比例
  167. this.initScale = this.view.scale;
  168. bus.$emit("initScale", this.view.scale);
  169. });
  170. } else {
  171. console.log("楼层不正确");
  172. }
  173. }
  174. },
  175. // 读取绘制数据
  176. readGraph() {
  177. this.readGroup().then(data => {
  178. this.graphId = data.Data[0].ID;
  179. bus.$emit("setGraphId", this.graphId);
  180. if (data.Data) {
  181. const parserData = new STopologyParser(null);
  182. const itemMap = {}
  183. parserData.parseData(data.Data[0].Elements);
  184. // 多边形(此item需在直线item添加之前添加)
  185. parserData.zoneLegendList.forEach(t => {
  186. this.scene.addItem(t);
  187. // 记录提取
  188. if (t.data.Properties && t.data.Properties.FID) {
  189. this.scene.fidToItem[t.data.Properties.FID].isExtracted = true;
  190. }
  191. this.scene.Nodes.push(t);
  192. itemMap[t.id] = t;
  193. });
  194. // 增加文字(此item需在直线item添加之前添加)
  195. parserData.textMarkerList.forEach(t => {
  196. this.scene.addItem(t);
  197. this.scene.Markers.push(t);
  198. itemMap[t.id] = t;
  199. });
  200. // 增加图片(此item需在直线item添加之前添加)
  201. parserData.imageMarkerList.forEach(t => {
  202. this.scene.addItem(t);
  203. this.scene.Markers.push(t);
  204. itemMap[t.id] = t;
  205. });
  206. // 增加图标类图例(此item需在管线item添加之前添加)
  207. parserData.imageLegendList.forEach(t => {
  208. this.scene.addItem(t);
  209. this.scene.Nodes.push(t);
  210. if (t.anchorList && t.anchorList.length) {
  211. t.anchorList.forEach(anc => {
  212. itemMap[anc.id] = anc;
  213. })
  214. }
  215. });
  216. // 增加直线
  217. parserData.lineMarkerList.forEach(t => {
  218. this.scene.addItem(t);
  219. this.scene.Markers.push(t);
  220. // 设置关联Item
  221. if (t.data.Properties && t.data.Properties.StartItemId) {
  222. const startItem = itemMap[t.data.Properties.StartItemId];
  223. startItem?.connect('onMove', t, t.changePos);
  224. t.startItem = startItem || null
  225. }
  226. if (t.data.Properties && t.data.Properties.EndItemId) {
  227. const endItem = itemMap[t.data.Properties.EndItemId];
  228. endItem?.connect('onMove', t, t.changePos);
  229. t.endItem = endItem || null
  230. }
  231. });
  232. // 增加管线类(需在图标类图例添加后添加)
  233. parserData.relationList.forEach(t => {
  234. this.scene.addItem(t);
  235. this.scene.Relations.push(t);
  236. // 设置锚点
  237. if (t.anchor1ID) {
  238. const startAnc = itemMap[t.anchor1ID]
  239. startAnc.isConnected = true
  240. startAnc.parent?.connect('changePos', t, t.changePos)
  241. t.startAnchor = startAnc || null
  242. }
  243. if (t.anchor2ID) {
  244. const endAnc = itemMap[t.anchor2ID]
  245. endAnc.isConnected = true
  246. endAnc.parent?.connect('changePos', t, t.changePos)
  247. t.endAnchor = endAnc || null
  248. }
  249. });
  250. this.view.fitSceneToView();
  251. bus.$emit("elementDataChange", this.scene);
  252. }
  253. });
  254. },
  255. // 监听变化
  256. emitChange(itemMsg) {
  257. this.chiceItemList = itemMsg.itemList;
  258. this.$emit("changeFocusItem", itemMsg);
  259. bus.$emit("FocusItemChanged", itemMsg);
  260. },
  261. // 监听场景元素数据变化
  262. scenceUpdate(scence) {
  263. bus.$emit("elementDataChange", scence);
  264. },
  265. clearGraphy() {
  266. if (this.view) {
  267. this.view.scene = null;
  268. return;
  269. }
  270. this.view = new FloorView("canvas");
  271. document.getElementById("canvas").focus();
  272. },
  273. getBus() {
  274. bus.$on("changeText", val => {
  275. this.scene.updatedText(val);
  276. });
  277. bus.$on("changeFont", val => {
  278. this.scene.updatedFontSize(val);
  279. });
  280. bus.$on("changeLineWidth", val => {
  281. this.scene.updatedLineWidth(val);
  282. });
  283. bus.$on("changeBorderColor", val => {
  284. this.scene.updatedBorderColor(val);
  285. });
  286. bus.$on("changeFontColor", val => {
  287. this.scene.updatedFontColor(val);
  288. });
  289. bus.$on("itemWidth", val => {
  290. this.scene.updatedWidth(Number(val));
  291. });
  292. bus.$on("itemHeight", val => {
  293. this.scene.updatedHeight(Number(val));
  294. });
  295. bus.$on("itemPositon", (x, y) => {
  296. this.scene.updatedPosition(Number(x), Number(y));
  297. });
  298. bus.$on("changebackColor", val => {
  299. this.scene.updatedbackColor(val);
  300. });
  301. bus.$on("deleiteItem", () => {
  302. this.scene.deleiteItem();
  303. });
  304. bus.$on("changeAlignItem", val => {
  305. this.scene.changeAlignItem(val);
  306. });
  307. bus.$on("extractItem", () => {
  308. const map = {};
  309. console.log(this.hasTypeList);
  310. this.fParser.spaceList.forEach(t => {
  311. if (this.hasTypeList.indexOf(t.data.Type) > -1) {
  312. if (map[t.data.Type]) {
  313. map[t.data.Type]++;
  314. } else {
  315. map[t.data.Type] = 1;
  316. }
  317. }
  318. });
  319. const data = [];
  320. for (const key in map) {
  321. data.push({
  322. key: key,
  323. name: key,
  324. age: "",
  325. number: map[key],
  326. address: "提取"
  327. });
  328. }
  329. bus.$emit("exportItem", data);
  330. });
  331. bus.$on("saveMsgItem", () => {
  332. const Elements = this.scene.saveMsgItem();
  333. const data = {
  334. Elements,
  335. Name: this.appName, // 名称
  336. categoryId: this.urlMsg.categoryId,
  337. ProjectID: this.urlMsg.projectId, // 项目ID
  338. BuildingID: this.urlMsg.BuildingID, // 建筑ID
  339. FloorID: this.urlMsg.FloorID // 楼层id
  340. };
  341. Message({
  342. message: "保存中,切勿关闭窗口!",
  343. type: "warning"
  344. });
  345. saveGroup(data)
  346. .then(res => {
  347. Message.close();
  348. this.graphId = res.Data;
  349. })
  350. .catch(err => {
  351. Message({
  352. message: "保存失败!",
  353. type: "error"
  354. });
  355. });
  356. });
  357. bus.$on("exportByKey", val => {
  358. const list = this.fParser.spaceList
  359. .map(t => {
  360. if (t.data.Type == val.key) {
  361. if (!t.isExtracted) {
  362. t.isExtracted = true;
  363. return {
  364. ID: uuid(),
  365. Name: val.name,
  366. GraphElementType: val.properties.Type,
  367. GraphElementId: "273d633cc5c54a4882794b34843d1a00",
  368. AttachObjectIds: [],
  369. Pos: { x: t.x, y: t.y },
  370. OutLine: t.pointArr[0],
  371. SubType: "",
  372. Properties: {
  373. StrokeColor: "#3d73c0",
  374. FillColor: "#72f5f980",
  375. font: 0,
  376. color: "",
  377. TextPos: { X: 0, Y: 0 }
  378. },
  379. Num: 1
  380. };
  381. }
  382. }
  383. })
  384. .filter(item => item);
  385. const parserData = new STopologyParser(null);
  386. parserData.parseData({ Nodes: list });
  387. parserData.zoneLegendList.forEach(t => {
  388. this.scene.addItem(t);
  389. this.scene.Nodes.push(t);
  390. });
  391. });
  392. // 设备图例样式对象
  393. bus.$on("setLenged", obj => {
  394. this.scene.setlegend = obj;
  395. });
  396. // 修改图片url
  397. bus.$on("upadataImageUrl", val => {
  398. this.scene.upadataImageUrl(val);
  399. });
  400. // 改变边框样式
  401. bus.$on("changeBorder", val => {
  402. this.scene.upadataBorder(val);
  403. });
  404. // 改变图例名称
  405. bus.$on("changeLengedName", val => {
  406. this.scene.upadataLengedName(val);
  407. });
  408. // 改变图例名称
  409. bus.$on("changeImageNum", val => {
  410. this.scene.upadatImageNum(val);
  411. });
  412. // 修改填充色
  413. bus.$on("changefillColor", val => {
  414. this.scene.upadatfillColor(val);
  415. });
  416. // 修改当前得状态是否为编辑状态
  417. bus.$on("OpenEditStatus", () => {
  418. ` `; // 获取焦点item (必须选中且仅选中一个)
  419. if (
  420. this.chiceItemList &&
  421. this.chiceItemList.length &&
  422. this.chiceItemList.length == 1
  423. ) {
  424. if (this.scene.grabItem) {
  425. this.view.tryDbclick();
  426. } else {
  427. this.scene.grabItem = this.chiceItemList[0];
  428. this.view.tryDbclick();
  429. }
  430. }
  431. });
  432. //修改图例说明
  433. bus.$on("changeitemExplain", val => {
  434. this.scene.upadatitemExplain(val);
  435. });
  436. //发布图
  437. bus.$on("publishGraph", val => {
  438. publishGraph({ graphId: this.graphId, pubUser: "" }).then(res => {
  439. if (res.Result == "success") {
  440. this.$message.success(res.Message);
  441. } else {
  442. this.$message.error(res.Message);
  443. }
  444. });
  445. });
  446. //创建区域是否点选
  447. bus.$on("changeDrawType", val => {
  448. this.scene.isSelecting = val == "select";
  449. }),
  450. //发布图
  451. bus.$on("publishMap", () => {
  452. if (this.graphId == "") {
  453. this.$message.error("请先保存");
  454. return false;
  455. }
  456. this.publishBtn();
  457. });
  458. /**
  459. * @name changeScale缩放底图
  460. * @param { Number } zoom 缩放比例
  461. *
  462. */
  463. // TODO: changeScale缩放底图
  464. bus.$on("changeScale", zoom => {
  465. const { scale } = this.view;
  466. this.changeScaleByClick = true;
  467. this.view.scaleByPoint(
  468. zoom,
  469. this.canvasWidth / 2,
  470. this.canvasHeight / 2
  471. );
  472. this.changeScaleByClick = false;
  473. setTimeout(() => {}, 100);
  474. });
  475. // 更改图例数据工程化数据
  476. bus.$on("changeAttachObjectIds", arr => {
  477. this.scene.upadatAttachObjectIds(arr);
  478. });
  479. // redo
  480. bus.$on('changeRedo',()=>{
  481. this.scene.redo()
  482. })
  483. // uodo/
  484. bus.$on('changeUndo',()=>{
  485. this.scene.undo()
  486. })
  487. },
  488. // 读取数据
  489. readGroup() {
  490. const data = {
  491. categoryId: this.urlMsg.categoryId,
  492. projectId: this.urlMsg.projectId,
  493. BuildingID: this.urlMsg.BuildingID, // 建筑ID
  494. FloorID: this.urlMsg.FloorID // 楼层id
  495. };
  496. return readGroup(data);
  497. },
  498. //发布
  499. publishBtn() {
  500. console.log(this.graphId);
  501. const loadings = Loading.service({
  502. lock: true,
  503. text: "Loading",
  504. spinner: "el-icon-loading",
  505. background: "rgba(0, 0, 0, 0.7)"
  506. });
  507. const data = {
  508. BuildingID: this.urlMsg.BuildingID,
  509. CategoryID: this.urlMsg.categoryId,
  510. FloorID: this.urlMsg.FloorID,
  511. GraphId: this.graphId,
  512. ProjectID: this.urlMsg.projectId,
  513. PubUser: ""
  514. };
  515. publishGraph(data).then(res => {
  516. loadings.close();
  517. if (res.Result == "success") {
  518. this.$message.success("发布成功");
  519. setTimeout(() => {
  520. const data = `categoryId=${this.urlMsg.categoryId}&projectId=${this.urlMsg.projectId}&BuildingID=${this.urlMsg.BuildingID}&FloorID=${this.urlMsg.FloorID}&fmapID=${this.urlMsg.fmapID}`;
  521. const url =
  522. window.location.origin +
  523. "/wandaEditer/drafts?" +
  524. encodeURIComponent(data);
  525. window.open(url, true);
  526. }, 1000);
  527. } else {
  528. this.$message.error(res.Message);
  529. }
  530. });
  531. },
  532. // 获取typeid
  533. getTypeId() {
  534. const data = {
  535. categoryId: this.urlMsg.categoryId
  536. };
  537. queryTypeGraph(data).then(res => {
  538. this.hasTypeList = res.Data.map(t => Number(t));
  539. });
  540. }
  541. },
  542. watch: {
  543. cmdType: {
  544. handler(cmd) {
  545. if (cmd == null || cmd == "") {
  546. cmd = "choice";
  547. }
  548. this.scene.setCmd = cmd;
  549. },
  550. deep: true
  551. },
  552. "scene.cmd": {
  553. handler(cmd) {
  554. this.$emit("setCmdType", cmd);
  555. },
  556. deep: true
  557. },
  558. // 监听scale的变化
  559. "view.scale": {
  560. handler(scale) {
  561. // 滚轮触发的缩放
  562. if (!this.changeScaleByClick) {
  563. bus.$emit("mouseScale", scale / this.initScale);
  564. }
  565. }
  566. }
  567. },
  568. created() {
  569. const href = window.location.href;
  570. // 路由
  571. // const route = href.split("?")[0];
  572. // 参数处理
  573. let params = href.split("?")[1];
  574. if (!params) {
  575. // 参数有问题
  576. return false;
  577. }
  578. params = decodeURIComponent(params);
  579. // params = "categoryId=NTXT&ProjectID=5&BuildingID=1&FloorID=1"; // mock 参数
  580. const paramsArr = params.split("&");
  581. console.log("paramsArr", paramsArr);
  582. const obj = {};
  583. paramsArr.map(item => {
  584. const arr = item.split("=");
  585. obj[arr[0]] = arr[1];
  586. });
  587. this.urlMsg = obj;
  588. }
  589. };
  590. </script>
  591. <style lang="less" scoped>
  592. #baseEditer {
  593. background: #f7f9fa;
  594. width: 100%;
  595. height: 100%;
  596. // overflow: hidden;
  597. // position: relative;
  598. #fengMap {
  599. position: absolute;
  600. width: 100px;
  601. height: 100px;
  602. z-index: -1;
  603. }
  604. .canvas-container {
  605. width: 100%;
  606. height: 100%;
  607. }
  608. }
  609. </style>