KonvaMap.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. <template>
  2. <div>
  3. <v-stage ref="stageAll" :config="stageConfig" @click="handleStageClick">
  4. <v-layer ref="layer">
  5. <v-image :config="bgConfig" v-show="bgImgUrl" />
  6. <v-group
  7. v-for="camera in cameras"
  8. :key="camera.id"
  9. :id="camera.id"
  10. :config="camera.groupConfig"
  11. @click="handleCameraClick(camera)"
  12. @mouseover="(e) => handleMouseOver(e)"
  13. @mouseleave="handleMouseLeave()"
  14. @dragstart="handleDragStart()"
  15. >
  16. <v-image :config="camera.config" />
  17. <v-image v-if="camera.isDefault" ref="defaultIcon" :config="defaultIconConfig" />
  18. </v-group>
  19. <v-transformer :config="transformerConfig" ref="transformer" />
  20. </v-layer>
  21. </v-stage>
  22. <div
  23. @click="setDefaultCamera"
  24. v-show="defaultShow"
  25. class="opt-container"
  26. :style="{ position: 'absolute', left: posX + 'px', top: posY + 'px' }"
  27. ><div class="opt-item" :class="{ disabled: disabledSet }">设为默认相机</div></div
  28. >
  29. <DefaultTip
  30. v-show="tipShow"
  31. :position="pos"
  32. :style="{ position: 'absolute', left: posTipX + 'px', top: posTipY + 'px' }"
  33. />
  34. </div>
  35. </template>
  36. <script setup lang="ts">
  37. import { ref, onMounted, onBeforeUnmount, watch } from 'vue';
  38. import { ElMessage, ElMessageBox } from 'element-plus';
  39. import { useGlobSetting } from '@/hooks/setting';
  40. import DefaultTip from '../components/DefaultTip.vue';
  41. import urlJoin from 'url-join';
  42. import cameraImgSrc from '@/assets/camera/cameraImg.png';
  43. import favoritesImgSrc from '@/assets/camera/favorites.png';
  44. import { TipPositionEnum, camerasGroupType } from '../type';
  45. import { cloneDeep } from 'lodash-es';
  46. import useMiniMap from '../use-mini-map';
  47. import { storeToRefs } from 'pinia';
  48. const globSetting = useGlobSetting();
  49. const miniMap = useMiniMap();
  50. const { shopCameraList } = storeToRefs(miniMap);
  51. const emit = defineEmits(['changeDefaultCamera', 'sendCameraId']);
  52. const camImg = new Image();
  53. const stageConfig = ref({
  54. width: 800,
  55. height: 600,
  56. });
  57. const bgImg = new Image();
  58. const favoritesImg = new Image();
  59. favoritesImg.src = favoritesImgSrc;
  60. //右键点击是否出现
  61. const defaultShow = ref<boolean>(false);
  62. const posX = ref<number>();
  63. const posY = ref<number>();
  64. const posTipX = ref<number>();
  65. const posTipY = ref<number>();
  66. const pos = ref(TipPositionEnum.TOP);
  67. //上一次点击的相机
  68. const lastClickedGroupId = ref<string | null>(null);
  69. const bgImgUrl = ref<string | null>('');
  70. const cameras = ref<camerasGroupType[]>([]);
  71. //默认相机id
  72. const defaultCameraId = ref('');
  73. const tipShow = ref(false);
  74. const disabledSet = ref(false);
  75. //标签
  76. const layer = ref();
  77. const transformer = ref();
  78. const defaultIcon = ref();
  79. const bgConfig = ref({
  80. x: 0,
  81. y: 0,
  82. id: 'bgImg',
  83. width: bgImg.width,
  84. height: bgImg.height,
  85. image: bgImg,
  86. name: 'bgImg',
  87. backgroundSize: 'cover',
  88. });
  89. //待修改
  90. const transformerConfig = ref({
  91. keepRatio: true,
  92. rotateAnchorOffset: 30,
  93. enabledAnchors: ['top-left', 'top-right', 'bottom-left', 'bottom-right'],
  94. });
  95. //背景大小变换
  96. const resizeContainer = (width, height) => {
  97. stageConfig.value.width = width;
  98. stageConfig.value.height = height;
  99. };
  100. const defaultIconConfig = ref({
  101. x: 18,
  102. y: -16,
  103. width: 16,
  104. height: 16,
  105. id: 'defaultIcon',
  106. image: favoritesImg,
  107. });
  108. //取消
  109. const handleMouseOver = (e) => {
  110. document.oncontextmenu = () => {
  111. return false;
  112. };
  113. const group = e.target.parent;
  114. if (group.id() === defaultCameraId.value) {
  115. tipShow.value = true;
  116. const stage = transformer.value.getNode().getStage();
  117. const defaultNode = stage.findOne('#defaultIcon');
  118. const tipPosition = defaultNode.absolutePosition();
  119. posTipX.value = Number(tipPosition?.x.toFixed(2)) || 0;
  120. posTipY.value = Number(tipPosition?.y.toFixed(2)) || 0;
  121. const angle = group.rotation() >= 0 ? group.rotation() : group.rotation() + 360;
  122. if (angle >= 30) {
  123. if (angle <= 150) {
  124. pos.value = TipPositionEnum.RIGHT;
  125. posTipX.value += 26;
  126. posTipY.value -= 17;
  127. } else if (angle <= 210) {
  128. pos.value = TipPositionEnum.BOTTOM;
  129. posTipY.value += 26;
  130. posTipX.value -= 50;
  131. } else {
  132. pos.value = TipPositionEnum.LEFT;
  133. posTipX.value -= 121;
  134. posTipY.value -= 25;
  135. }
  136. } else {
  137. posTipY.value -= 61;
  138. posTipX.value -= 43;
  139. }
  140. }
  141. };
  142. // 还原浏览器默认鼠标事件
  143. const handleMouseLeave = () => {
  144. document.oncontextmenu = () => {
  145. return true;
  146. };
  147. tipShow.value = false;
  148. };
  149. const handleDragStart = () => {
  150. tipShow.value = false;
  151. };
  152. const handleStageClick = (e: any) => {
  153. defaultShow.value = false;
  154. disabledSet.value = false;
  155. document.oncontextmenu = () => {
  156. return false;
  157. };
  158. const parent = e.target.parent;
  159. //判断是否点击相机组
  160. if (parent.hasName('group')) {
  161. lastClickedGroupId.value = parent.id();
  162. // 判断是否为右键点击
  163. if (e.evt.button === 2) {
  164. lastClickedGroupId.value = parent.id();
  165. posX.value = e.evt.offsetX + 20;
  166. posY.value = e.evt.offsetY;
  167. disabledSet.value = defaultCameraId.value === parent.id();
  168. defaultShow.value = true;
  169. }
  170. } else {
  171. lastClickedGroupId.value = null;
  172. //取消transformer选择
  173. const transformerNode = transformer.value.getNode();
  174. transformerNode.nodes([]);
  175. }
  176. };
  177. const handleCameraClick = (camera) => {
  178. tipShow.value = false;
  179. const transformerNode = transformer.value.getNode();
  180. const stage = transformerNode.getStage();
  181. const cameraNode = stage.findOne('#' + camera.id);
  182. // 将变换器附加到点击的相机
  183. transformerNode.nodes([cameraNode]);
  184. transformerNode.moveToTop();
  185. };
  186. //添加相机
  187. const addCamera = (id: string) => {
  188. const existingCamera = cameras.value.find((camera) => camera.id === id);
  189. if (existingCamera) return;
  190. const config = {
  191. width: 52,
  192. height: 37,
  193. image: camImg,
  194. name: 'image',
  195. id: id,
  196. };
  197. const groupConfig = {
  198. x: 50,
  199. y: 50,
  200. draggable: true,
  201. name: 'group',
  202. };
  203. const cameraDetail = {
  204. id,
  205. groupConfig,
  206. config,
  207. isDefault: false,
  208. };
  209. cameras.value.push(cameraDetail);
  210. //当只有一个相机时,自动设置默认相机
  211. if (cameras.value.length === 1) {
  212. cameras.value[0].isDefault = true;
  213. defaultCameraId.value = id;
  214. }
  215. };
  216. //设置默认相机
  217. const setDefaultCamera = () => {
  218. //选中的相机id号
  219. defaultCameraId.value = lastClickedGroupId.value!;
  220. cameras.value.forEach((item) => {
  221. if (item.id === defaultCameraId.value) {
  222. item.isDefault = true;
  223. } else {
  224. item.isDefault = false;
  225. }
  226. });
  227. const transformerNode = transformer.value.getNode();
  228. const stage = transformerNode.getStage();
  229. transformerNode.nodes([]);
  230. const cameraNode = stage.findOne('#' + defaultCameraId.value);
  231. // 将变换器附加到点击的相机
  232. transformerNode.nodes([cameraNode]);
  233. defaultShow.value = false;
  234. };
  235. watch(
  236. lastClickedGroupId,
  237. () => {
  238. emit('changeDefaultCamera', lastClickedGroupId.value);
  239. },
  240. { immediate: true },
  241. );
  242. watch(
  243. () => cameras.value,
  244. () => {
  245. emit('sendCameraId', cameras.value);
  246. },
  247. { immediate: true, deep: true },
  248. );
  249. const clearBg = () => {
  250. bgImg.src = null as any as string;
  251. };
  252. //添加背景
  253. const addBg = (imgBg) => {
  254. return new Promise((resolve) => {
  255. bgImgUrl.value = imgBg;
  256. bgImg.src = urlJoin(globSetting.imgUrl!, imgBg) as string;
  257. bgImg.onload = () => {
  258. bgConfig.value.width = bgImg.width;
  259. bgConfig.value.height = bgImg.height;
  260. resizeContainer(bgImg.width, bgImg.height);
  261. resolve(null);
  262. };
  263. });
  264. };
  265. //保存布局
  266. const saveLayout = () => {
  267. const stage = transformer.value.getNode().getStage();
  268. const groups = stage.find('.group');
  269. const tempList = cloneDeep(cameras.value);
  270. // console.log('cameras.value', cameras.value);
  271. const camerasLists = tempList.map((item, index) => {
  272. item.groupConfig.x = groups[index].attrs.x;
  273. item.groupConfig.y = groups[index].attrs.y;
  274. item.groupConfig.rotation = groups[index].attrs.rotation || 0;
  275. item.groupConfig.scaleX = groups[index].attrs.scaleX || 1;
  276. item.groupConfig.scaleY = groups[index].attrs.scaleY || 1;
  277. // item.config.url = cameraImgSrc;
  278. return item;
  279. });
  280. const layout = {
  281. stageConfig: stageConfig.value,
  282. bgConfig: bgConfig.value,
  283. bgImgUrl: bgImgUrl.value,
  284. defaultCameraId: defaultCameraId.value,
  285. cameraList: camerasLists,
  286. };
  287. return JSON.stringify(layout);
  288. };
  289. //删除相机
  290. const handleKeyDown = (e) => {
  291. if (e.keyCode === 46 || e.code === 'Delete') {
  292. if (lastClickedGroupId.value === defaultCameraId.value) {
  293. // ElMessage.error({
  294. // message: '无法删除默认相机',
  295. // });
  296. // return;
  297. ElMessageBox.confirm('此相机为默认相机,您确认要删除此相机?', 'Warning', {
  298. confirmButtonText: '确认',
  299. cancelButtonText: '取消',
  300. type: 'warning',
  301. })
  302. .then(() => {
  303. const index = cameras.value.findIndex((item) => item.id === lastClickedGroupId.value);
  304. index >= 0 && cameras.value.splice(index, 1);
  305. lastClickedGroupId.value = '';
  306. //取消transformer
  307. const transformerNode = transformer.value.getNode();
  308. transformerNode.nodes([]);
  309. })
  310. .catch(() => {});
  311. } else {
  312. const index = cameras.value.findIndex((item) => item.id === lastClickedGroupId.value);
  313. index >= 0 && cameras.value.splice(index, 1);
  314. lastClickedGroupId.value = '';
  315. //取消transformer
  316. const transformerNode = transformer.value.getNode();
  317. transformerNode.nodes([]);
  318. }
  319. }
  320. };
  321. //重置
  322. const resetMap = () => {
  323. bgImgUrl.value = null;
  324. clearBg();
  325. cameras.value = [];
  326. lastClickedGroupId.value = null;
  327. defaultCameraId.value = '';
  328. };
  329. /** 导入布局json */
  330. const createMap = (layout) => {
  331. addBg(layout.bgImgUrl).then((_res) => {
  332. const unExitList = [] as any[];
  333. stageConfig.value = layout.stageConfig;
  334. defaultCameraId.value = layout.defaultCameraId;
  335. layout.cameraList = layout.cameraList
  336. ?.map((item) => {
  337. item.config.image = camImg;
  338. return item;
  339. })
  340. ?.filter((cam) => {
  341. if (shopCameraList.value.findIndex((x) => x.code === cam.code) >= 0) {
  342. return true;
  343. } else {
  344. unExitList.push(cam.code);
  345. return false;
  346. }
  347. });
  348. if (unExitList.length > 0) {
  349. ElMessage.warning('部分相机不存在,已为您删除!');
  350. }
  351. cameras.value = layout.cameraList;
  352. });
  353. };
  354. defineExpose({ addBg, createMap, resetMap, addCamera, resizeContainer, saveLayout });
  355. onMounted(() => {
  356. window.addEventListener('keydown', handleKeyDown);
  357. camImg.src = cameraImgSrc;
  358. });
  359. onBeforeUnmount(() => {
  360. window.removeEventListener('keydown', handleKeyDown);
  361. });
  362. </script>
  363. <style scoped lang="scss">
  364. .opt-container {
  365. width: 160px;
  366. padding: 10px;
  367. border-radius: 5px;
  368. background-color: #ffffff;
  369. box-shadow: 5px 5px 5px #a3a5a5;
  370. }
  371. .opt-item {
  372. height: 30px;
  373. font-size: 14px;
  374. color: #404040;
  375. display: flex;
  376. justify-content: flex-start;
  377. align-items: center;
  378. padding-left: 8px;
  379. border-radius: 3px;
  380. cursor: pointer;
  381. &:hover {
  382. background-color: #f1f2f5;
  383. }
  384. }
  385. .disabled {
  386. background-color: #f1f2f5;
  387. color: #bcbdc0;
  388. cursor: not-allowed;
  389. }
  390. </style>