|
|
@@ -31,20 +31,26 @@
|
|
|
import Konva from 'konva';
|
|
|
import { computed, onMounted, onUnmounted, ref, watch } from 'vue';
|
|
|
import FenceItem from './FenceItem.vue';
|
|
|
- import { createCircleConfigItem, createGroupConfig } from './utils';
|
|
|
- import { FenceGroup } from './types';
|
|
|
+ import { createCircleConfigItem, createGroupConfig, polygonPoint1ToPoint2 } from './utils';
|
|
|
+ import { FenceGroup, FencePolygonPoints, SingleFence } from './types';
|
|
|
import { ElMessage } from 'element-plus';
|
|
|
import { GROUP_NAME } from './constants';
|
|
|
|
|
|
const props = defineProps<{
|
|
|
/** 电子围栏的坐标 */
|
|
|
- linePoints: [number, number][][];
|
|
|
+ linePoints: SingleFence[];
|
|
|
/** 画布的大小 */
|
|
|
canvasSize: { width: number; height: number };
|
|
|
/** dom的真实尺寸 */
|
|
|
domWidth: number;
|
|
|
}>();
|
|
|
|
|
|
+ // 保存修改后的电子围栏, fenceId为空表示新建的,否则表示编辑已存在的
|
|
|
+ interface Save {
|
|
|
+ (e: 'save', data: { fenceId?: number; polygon: FencePolygonPoints }): unknown;
|
|
|
+ }
|
|
|
+ const emits = defineEmits<Save>();
|
|
|
+
|
|
|
const scale = computed(() => {
|
|
|
return props.domWidth / props.canvasSize.width;
|
|
|
});
|
|
|
@@ -64,10 +70,11 @@
|
|
|
(newLinePoints) => {
|
|
|
const configs: FenceGroup[] =
|
|
|
newLinePoints.map((points) => {
|
|
|
- const flattenedPoints = points.reduce((total, next) => {
|
|
|
+ const polygonJSON = points.polygon || [];
|
|
|
+ const flattenedPoints = polygonJSON.reduce((total, next) => {
|
|
|
return [...total, ...next];
|
|
|
}, [] as number[]);
|
|
|
- return createGroupConfig(flattenedPoints, scale.value);
|
|
|
+ return createGroupConfig({ fenceId: points.id, points: flattenedPoints, scale: scale.value });
|
|
|
}) || [];
|
|
|
fenceGroups.value = configs;
|
|
|
},
|
|
|
@@ -120,7 +127,7 @@
|
|
|
|
|
|
/** 如果还没开始画线,那么增加第一个点 */
|
|
|
if (!drawingGroupId.value) {
|
|
|
- const groupConfig = createGroupConfig(point, scale.value);
|
|
|
+ const groupConfig = createGroupConfig({ points: point, scale: scale.value });
|
|
|
drawingGroupId.value = groupConfig.uid;
|
|
|
currentGroupId.value = groupConfig.uid;
|
|
|
groupConfig._temp.points = point;
|
|
|
@@ -147,7 +154,13 @@
|
|
|
groupConfig.circleConfigs = [];
|
|
|
currentGroupId.value = '';
|
|
|
} else {
|
|
|
- groupConfig.lineConfig.points = groupConfig._temp.points;
|
|
|
+ const points = groupConfig._temp.points;
|
|
|
+ groupConfig.lineConfig.points = points;
|
|
|
+ console.log('完成了一个电子围栏的编辑groupConfig', groupConfig);
|
|
|
+ const points2 = polygonPoint1ToPoint2(points);
|
|
|
+ if (points2) {
|
|
|
+ emits('save', { fenceId: groupConfig.fenceId, polygon: points2 });
|
|
|
+ }
|
|
|
}
|
|
|
drawingGroupId.value = '';
|
|
|
groupConfig._temp.points = [];
|
|
|
@@ -165,11 +178,7 @@
|
|
|
groupConfig._temp.points = finalPoints;
|
|
|
currentGroupId.value = groupConfig.uid;
|
|
|
|
|
|
- const circleConfig = createCircleConfigItem(
|
|
|
- point,
|
|
|
- groupConfig.circleConfigs.length,
|
|
|
- scale.value,
|
|
|
- );
|
|
|
+ const circleConfig = createCircleConfigItem(point, groupConfig.circleConfigs.length, scale.value);
|
|
|
groupConfig.circleConfigs.push(circleConfig);
|
|
|
}
|
|
|
};
|
|
|
@@ -182,9 +191,7 @@
|
|
|
if (!mousePosition?.x || !mousePosition?.y) return;
|
|
|
const newPoint = [mousePosition.x, mousePosition.y];
|
|
|
if (drawingGroupId.value) {
|
|
|
- const groupConfig: FenceGroup | undefined = fenceGroups.value.find(
|
|
|
- (x) => x.uid === drawingGroupId.value,
|
|
|
- );
|
|
|
+ const groupConfig: FenceGroup | undefined = fenceGroups.value.find((x) => x.uid === drawingGroupId.value);
|
|
|
if (!groupConfig) {
|
|
|
console.error('drawingGroupId无效', drawingGroupId.value);
|
|
|
return;
|
|
|
@@ -221,23 +228,7 @@
|
|
|
const fenceGroups = stage?.find('.' + GROUP_NAME);
|
|
|
const gropuPoints = fenceGroups
|
|
|
?.map((item) => {
|
|
|
- const groupX = item.x();
|
|
|
- const groupY = item.y();
|
|
|
-
|
|
|
- const line = (item as Konva.Group).findOne(
|
|
|
- (x: any) => x.className === 'Line',
|
|
|
- ) as Konva.Line;
|
|
|
- const points = line?.points();
|
|
|
- /** 有些line对象存在,但是没有点坐标,所以要判断过滤一下 */
|
|
|
- if (points && points.length > 0) {
|
|
|
- const newPoints: number[][] = [];
|
|
|
- /** 存到后端的时候,只给点的坐标信息,不会给group的位置信息,所以要将点的坐标加上group的位移,才是之后点的最终坐标 */
|
|
|
- for (let i = 0; i < points.length; i += 2) {
|
|
|
- newPoints.push([Math.floor(points[i] + groupX), Math.floor(points[i + 1] + groupY)]);
|
|
|
- }
|
|
|
- return newPoints;
|
|
|
- }
|
|
|
- return null;
|
|
|
+ return polygonPoint1ToPoint2(item);
|
|
|
})
|
|
|
.filter(Boolean);
|
|
|
return gropuPoints;
|