index.vue
13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
<template>
<div class="map-module">
<div class="tool-style">
<span style="font-size: 14px; color: #409eff"
>{{ $t("main.monitor.legend") }}:</span
>
<el-tag type="success" effect="dark">{{
$t("main.monitor.standby")
}}</el-tag>
<el-divider direction="vertical"></el-divider>
<el-tag effect="dark">{{ $t("main.monitor.normalRunning") }}</el-tag>
<el-divider direction="vertical"></el-divider>
<el-tag type="warning" effect="dark">{{
$t("main.monitor.nonAutomatic")
}}</el-tag>
<el-divider direction="vertical"></el-divider>
<el-tag type="danger" effect="dark">{{
$t("main.monitor.abnormal")
}}</el-tag>
<el-divider direction="vertical"></el-divider>
<el-button
type="primary"
size="mini"
icon="el-icon-refresh"
@click="reset"
>{{ $t("main.monitor.reset") }}</el-button
>
<el-divider direction="vertical"></el-divider>
<div style="width: 350px">
{{ $t("main.monitor.currentScalingFactor") }}:{{ scale.sx }}
</div>
<!-- <el-divider direction="vertical"></el-divider> -->
<!-- <span>当前偏移量:1</span> -->
</div>
<div id="flow" class="canvas-style">
<div id="container" @wheel.prevent="onWheel"></div>
<!-- @mousedown="onMouseDown"
@mousemove="onMouseMove"
@mouseup="onMouseUp" -->
</div>
</div>
</template>
<script>
import FlowGraph from "./flow/graph";
import graphData from "./flow/graph/data/test-data";
// import graphData from './flow/graph/data/data-custom-3';
import { getGUID } from "@/utils/index.js";
import {
conveyor,
conveyor_center,
conveyor_left,
conveyor_right,
} from "./flow/graph/images";
import { $, getContainerSize } from "./flow/index.js";
export default {
name: "MapModule",
data() {
return {
canvas: null,
graph: null,
initialTranslate: { x: 0, y: 0 },
initialScale: 1,
dataJson: graphData,
srmPath: {},
scale: {
sx: 1,
sy: 1,
},
isDragging: false, // 标记是否正在拖动
lastPointer: null, // 记录上次鼠标位置
canvasTop: 0, // 画布的顶部偏移
canvasLeft: 0, // 画布的左部偏移
};
},
props: {
isActive: {
type: Boolean,
default: false,
},
},
mounted() {
this.getFlowJson();
},
destroyed() {
const { graph } = FlowGraph;
// 销毁画布,资源回收
if (graph) graph.dispose();
// 移除监听
window.removeEventListener("resize", this.resizeFn);
},
methods: {
onWheel(event) {
const { graph } = FlowGraph;
if (graph) {
this.scale.sx = parseFloat(graph.scale().sx.toFixed(2));
}
},
onMouseDown(event) {
this.isDragging = true;
// this.lastPointer = this.canvas.getPointer(event);
// 获取相对于视口的坐标
const clientX = event.clientX;
const clientY = event.clientY;
// 获取相对于页面的坐标
const pageX = event.pageX;
const pageY = event.pageY;
// 获取相对于目标元素的坐标
const rect = event.target.getBoundingClientRect();
const offsetX = event.clientX - rect.left;
const offsetY = event.clientY - rect.top;
console.log("相对于视口的坐标:", clientX, clientY);
console.log("相对于页面的坐标:", pageX, pageY);
console.log("相对于 div 的坐标:", offsetX, offsetY);
},
onMouseMove(event) {
if (this.isDragging) {
// const currentPointer = this.canvas.getPointer(event);
// const deltaX = currentPointer.x - this.lastPointer.x;
// const deltaY = currentPointer.y - this.lastPointer.y;
// // 更新 canvasTop 和 canvasLeft
// this.canvasTop += deltaY;
// this.canvasLeft += deltaX;
// this.lastPointer = currentPointer;
}
},
onMouseUp() {
this.isDragging = false;
},
// 去后台拿json
getFlowJson() {
setTimeout(() => {
const graphJson = this.dataJson; //JSON.parse(window.localStorage.getItem('graphJson'))
// console.log(graphJson)
if (graphJson) {
this.srmPath = {};
for (let i = 0; i < 8; i++) {
var x = 150;
var y = 150;
switch (i) {
case 1:
y = 180;
break;
case 2:
y = 270;
break;
case 3:
y = 300;
break;
case 4:
y = 360;
break;
case 5:
y = 390;
break;
case 6:
y = 480;
break;
case 7:
y = 510;
break;
}
for (let j = 0; j < 28; j++) {
const isDuplicate = this.srmPath["A" + (j + 1)];
if (isDuplicate == undefined) {
this.srmPath["A" + (j + 1)] = x;
}
graphJson.cells.push({
position: {
x: x,
y: y,
},
attrs: {
text: {
text: j + 1,
},
},
size: {
width: 30,
height: 30,
},
shape: "Storage",
id: getGUID(),
zIndex: 1,
});
x += 30;
}
}
this.initFlowImage(graphJson);
}
}, 300);
},
// 根据json渲染
initFlowImage(graphJson) {
// 初始化画板
const graph = FlowGraph.init(
$("#container"),
$("#container").getBoundingClientRect().width,
$("#container").getBoundingClientRect().height,
false
);
// 渲染操作
graph.fromJSON(graphJson);
// // 定义新节点的数据
// const newNode = {
// position: { x: 500, y: 500 },
// size: { width: 80, height: 42 },
// attrs: {
// text: { text: '新节点' },
// body: { rx: 24, ry: 24 }
// },
// visible: true,
// shape: 'flow-chart-rect',
// id: 'new-node-id',
// zIndex: 24,
// data: { status: 0, pointCode: '53', fieldName: 'newFieldName' }
// };
// // 添加新节点到图中
// graph.addNode(newNode);
console.log(graph.getNodes());
// 监听数据改变事件
graph.getNodes().forEach((node) => {
if (node.getData()) {
var data = node.getData();
if (node.shape.startsWith("Station") && data.type != undefined) {
if (data.type == "left") {
node.attr("image/xlink:href", conveyor_left);
// node.attr('body/class','flash-shrink-animation')
} else if (data.type == "right") {
node.attr("image/xlink:href", conveyor_right);
// node.attr('body/class','flash-shrink-animation')
// node.attr('body/fill','green')
// node.attr('text/fill', '#080808')
} else if (data.type == "center") {
node.attr("image/xlink:href", conveyor_center);
// node.attr('body/class','flash-shrink-animation')
// node.attr('body/fill','green')
// node.attr('text/fill', '#080808')
} else {
node.attr("image/xlink:href", conveyor);
}
}
node.on("change:data", ({ cell, current }) => {
if (current.status == "0") {
cell.attr("body/class", "flash-shrink-animation-close");
} else {
cell.attr("body/class", "flash-shrink-animation");
}
if (current.pallet == "0") {
cell.attr("imagePallet/class", "hide-pallet");
} else {
cell.attr("imagePallet/class", "show-pallet");
}
});
}
if (node.shape === "Storage") {
node.attr("body/width", 30);
node.attr("body/height", 30);
}
});
window.addEventListener("resize", this.resizeFn);
},
resizeFn() {
setTimeout(() => {
const { graph } = FlowGraph;
const { width, height } = getContainerSize($("#flow"));
graph.resize(width, height);
}, 100);
},
reset() {
const { graph } = FlowGraph;
if (graph) {
// console.log(this.initialScale);
// console.log(this.initialTranslate);
// 恢复初始缩放比例
graph.scale(1, 1);
// 恢复初始位置
graph.translate(0, 0);
}
},
//更新数据
updateData(srmList, rgvList, stationList) {
const { graph } = FlowGraph;
if (graph) {
// 监听数据改变事件
graph.getNodes().forEach((node) => {
if (node.getData()) {
var data = node.getData();
var isExist = false;
if (node.shape == "SRM") {
srmList.forEach((item) => {
// console.log(item)
if (item.code == data.code) {
isExist = true;
data.status = item.totalError == "True" ? "1" : "0";
data.pallet =
item.fork1HasPallet == "True" ||
item.fork2HasPallet == "True"
? "1"
: "0";
data.auto = item.operationModel == "5" ? "1" : "0";
data.text = item.fork1GoodsBarcode;
// 定义要过渡的属性路径
const path = "position/x";
// 定义目标值
var target = 0;
if (
item.fork1XPosition != null &&
item.fork1XPosition != ""
) {
if (item.fork1XPosition < 500) {
target = this.srmPath["A" + item.fork1XPosition];
} else {
target = 1000;
}
} else {
target = -999;
}
if (target != -999) {
// 定义过渡动画的配置选项
const options = {
duration: 1000, // 过渡效果持续 1 秒
easing: "ease-out", // 缓动函数为 ease-out
callback: function () {},
};
node.transition(path, target, options);
}
}
});
} else if (node.shape == "RGV") {
rgvList.forEach((item) => {
if (item.code == data.code) {
isExist = true;
data.status = item.totalError == "True" ? "1" : "0";
data.pallet =
item.fork1HasPallet == "True" ||
item.fork2HasPallet == "True"
? "1"
: "0";
data.auto = item.operationModel == "5" ? "1" : "0";
data.text = item.fork1GoodsBarcode;
}
});
} else if (node.shape.startsWith("Station")) {
stationList.forEach((item) => {
if (item.code.replace("StationMonitor", "") == data.code) {
isExist = true;
data.status =
item.stationError == "" ? "0" : item.stationError;
data.pallet = item.stationMonitorOccupied;
data.auto = item.stationMonitorAutomation;
data.text = item.stationMonitorBarcode;
}
});
}
if (isExist) {
//状态
if (data.status == "0" && data.auto == "1") {
node.attr("body/class", "flash-shrink-animation-close");
} else {
node.attr("body/width", node.getSize().width);
node.attr("body/height", node.getSize().height);
if (data.status != "0") {
node.attr("body/stroke", "#FF0000");
// node.attr('body/stroke',"#f56c6c")
} else {
node.attr("body/stroke", "#FFFF00");
// node.attr('body/stroke',"#e6a23c")
}
node.attr("body/class", "flash-shrink-animation");
}
//托盘
if (data.pallet == "0") {
node.attr("imagePallet/class", "hide-pallet");
} else {
node.attr("imagePallet/class", "show-pallet");
//个性化设置
if (node.shape == "SRM") {
node.attr("imagePallet/x", -5);
node.attr("imagePallet/y", 0);
} else if (node.shape == "Station") {
node.attr("imagePallet/x", 10);
}
//托盘
if (data.text != "") {
node.attr("textCode/text", data.text);
}
}
node.setData(data);
} else {
node.attr("body/width", node.getSize().width);
node.attr("body/height", node.getSize().height);
node.attr("body/stroke", "#FFFF00");
node.attr("body/class", "flash-shrink-animation");
}
}
});
}
},
},
};
</script>
<style scoped>
.tool-style {
position: absolute;
/* left: 0px;
top: 0px; */
display: flex;
align-items: center;
margin: 10px;
z-index: 99999;
}
.canvas-style {
border: 1px solid #ebeef5;
width: 100vw;
height: 100vh;
}
</style>
<style>
.map-module {
.el-main {
padding-top: 0px;
}
}
</style>