MiniMapConfig.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. <template>
  2. <div class="page">
  3. <div class="page-head flex items-center">
  4. <el-icon size="20"><ArrowLeft /></el-icon>
  5. <div class="head-opt flex-1 flex justify-between items-center">
  6. <div>
  7. <span>场景:</span>
  8. <el-tree-select
  9. v-model="selectedShopCode"
  10. :data="scenesTree"
  11. accordion
  12. :render-after-expand="false"
  13. :default-expand-all="true"
  14. :teleported="false"
  15. placeholder="请选择相关场景"
  16. @change="changeShop"
  17. />
  18. </div>
  19. <div class="flex">
  20. <el-upload
  21. class="avatar-uploader flex justify-center items-center"
  22. action="/temp/api/layout/uploadPicture"
  23. :show-file-list="false"
  24. :on-success="handleAvatarSuccess"
  25. :with-credentials="true"
  26. name="file"
  27. :data="{ workshopId: selectedShopCode }"
  28. >
  29. <el-button style="font-size: 12px" :icon="Refresh" :disabled="!hasBg">
  30. 替换照片
  31. </el-button>
  32. </el-upload>
  33. <el-button
  34. @click="handleSave"
  35. style="margin-left: 40px"
  36. type="primary"
  37. :disabled="!selectedShopCode"
  38. >保存为布局
  39. </el-button>
  40. </div>
  41. </div>
  42. </div>
  43. <div class="paint-tool flex">
  44. <div class="camera-list">
  45. <div>
  46. <span class="label-text flex">相机列表:</span>
  47. <ElInput
  48. class="search-put"
  49. style="margin: 10px 0; width: 230px"
  50. placeholder="请输入搜索内容"
  51. v-model="searchKey"
  52. :suffix-icon="Search"
  53. />
  54. </div>
  55. <span v-if="filterShopCameraList.length == 0" class="ml-1" style="color: #3f3f3f">
  56. 提示:请先选择相应场景和图片
  57. </span>
  58. <el-scrollbar v-else style="position: relative; height: calc(100% - 90px)">
  59. <div
  60. v-for="item in filterShopCameraList"
  61. :key="item.code"
  62. class="camera-item flex justify-start"
  63. :class="{
  64. isAdded: isAddedToMap(item.code),
  65. isActive: item.code === selectedCamera?.cameraId,
  66. }"
  67. @click="handleAddCamera(item.code)"
  68. >
  69. <span class="camera-id">{{ item.name }}</span>
  70. <el-popover
  71. placement="bottom-start"
  72. trigger="hover"
  73. :content="item.name"
  74. :teleported="false"
  75. >
  76. <template #reference>
  77. <span class="space-name">{{ item.workSpaceName }}</span>
  78. </template>
  79. </el-popover>
  80. </div>
  81. </el-scrollbar>
  82. </div>
  83. <div class="draw-container">
  84. <div style="overflow: auto">
  85. <canvas width="400" height="400" id="mapEditCanvas"></canvas>
  86. </div>
  87. <el-upload
  88. v-if="!hasBg"
  89. class="upload-icon flex justify-center items-center"
  90. action="/temp/api/layout/uploadPicture"
  91. :show-file-list="false"
  92. :on-success="handleAvatarSuccess"
  93. :with-credentials="true"
  94. name="file"
  95. :data="{ workshopId: selectedShopCode }"
  96. >
  97. <img src="~@/assets/images/img-upload.png" />
  98. </el-upload>
  99. <div>
  100. <div style="height: 20px; margin-bottom: 10px">
  101. <div v-if="selectedCamera">
  102. <SelectedCameraToolbar
  103. @render-map="renderMap"
  104. :selected-camera="selectedCamera!"
  105. :key="selectedPositionHash"
  106. />
  107. </div>
  108. </div>
  109. </div>
  110. <div style="overflow: auto">
  111. <canvas width="400" height="400" id="mapEditCanvas"></canvas>
  112. </div>
  113. <div style="overflow: auto; position: relative">
  114. <canvas
  115. width="400"
  116. height="400"
  117. id="mapEditCanvas"
  118. style="border: 1px solid #ccc"
  119. ></canvas>
  120. <ContextMenu
  121. :visible="menuVisible"
  122. :position="mousePosition"
  123. :set-default="handleSetDefault"
  124. />
  125. <DefaultCameraIcon :position="favPosition" />
  126. </div>
  127. <CameraPreview :json="cameraJSON" />
  128. </div>
  129. </div>
  130. </div>
  131. </template>
  132. <script setup lang="ts">
  133. import useMiniMap from './use-mini-map';
  134. import { storeToRefs } from 'pinia';
  135. import { ElMessage, ElInput } from 'element-plus';
  136. import urlJoin from 'url-join';
  137. import { onMounted, ref } from 'vue';
  138. import useCameraMap from './MapBase/useCameraMap';
  139. import { updateMinMapViewLayoutApi } from '@/api/scene/scene';
  140. import { onUnmounted, watchEffect } from 'vue';
  141. import { useGlobSetting } from '@/hooks/setting';
  142. import { computed } from 'vue';
  143. import ContextMenu from './MapBase/ContextMenu.vue';
  144. import DefaultCameraIcon from './MapBase/DefaultCameraIcon.vue';
  145. import { CameraImage } from './MapBase/types';
  146. import { createSelectedPositionHash, getFavPositionByCamera } from './MapBase/utils';
  147. import SelectedCameraToolbar from './components/SelectedCameraToolbar.vue';
  148. import CameraPreview from './MapBase/CameraPreview.vue';
  149. import { Search, Refresh } from '@element-plus/icons-vue';
  150. const miniMap = useMiniMap();
  151. const globSetting = useGlobSetting();
  152. const { scenesTree, shopCameraList, selectedShopCode, selectedShopDetail } = storeToRefs(miniMap);
  153. const { getScenesTree, getShowCameras, getMapLayout } = miniMap;
  154. const selectedPositionHash = ref('');
  155. const allObjects = ref<CameraImage[]>([]);
  156. const onSelectCamera = () => {
  157. menuVisible.value = false;
  158. };
  159. const handleRightClick = (e) => {
  160. mousePosition.value = { left: e.pointer.x, top: e.pointer.y };
  161. menuVisible.value = true;
  162. rightSelectedCamera.value = e.target;
  163. };
  164. const handleMoving = (e) => {
  165. const target = e.transform.target;
  166. selectedPositionHash.value = createSelectedPositionHash(target);
  167. if (target?.cameraId === defaultCamera.value?.cameraId) {
  168. favPosition.value = getFavPositionByCamera(target);
  169. }
  170. };
  171. const handleRoating = (e) => {
  172. const target = e.transform.target;
  173. selectedPositionHash.value = createSelectedPositionHash(target);
  174. if (target.cameraId === defaultCamera.value?.cameraId) {
  175. favPosition.value = getFavPositionByCamera(target);
  176. }
  177. };
  178. const handleSetSelectedCameara = (target: CameraImage | null) => {
  179. selectedCamera.value = target;
  180. };
  181. const handleObjectsAdded = () => {
  182. allObjects.value = map.getObjects();
  183. };
  184. const map = useCameraMap({
  185. onSelect: onSelectCamera,
  186. onRightClick: handleRightClick,
  187. onMoving: handleMoving,
  188. onRotating: handleRoating,
  189. setSelectedCamera: handleSetSelectedCameara,
  190. onObjectsAdded: handleObjectsAdded,
  191. });
  192. const selectedCamera = ref<CameraImage | null>();
  193. /** 右键选中的摄像机 */
  194. const rightSelectedCamera = ref<CameraImage | null>(null);
  195. const defaultCamera = ref<CameraImage | null>();
  196. const searchKey = ref('');
  197. // 是否已有背景图
  198. const hasBg = ref(false);
  199. const mousePosition = ref<{ left: number; top: number }>({ left: 0, top: 0 });
  200. const menuVisible = ref(false);
  201. const favPosition = ref<{ left: number; top: number } | null>(null);
  202. const handleAvatarSuccess = (e) => {
  203. const imgPath = e.data;
  204. const imgUrl = urlJoin(globSetting.imgUrl!, imgPath);
  205. hasBg.value = true;
  206. map.uploadBg(imgUrl);
  207. };
  208. const renderMap = () => {
  209. map.renderCamera();
  210. favPosition.value = getFavPositionByCamera(defaultCamera.value);
  211. };
  212. const changeShop = (code: string) => {
  213. getShopContent(code);
  214. hasBg.value = false;
  215. };
  216. const cameraJSON = ref();
  217. const getShopContent = (code: string) => {
  218. getShowCameras(code);
  219. getMapLayout(code).then((res) => {
  220. if (!res) {
  221. map.clear();
  222. defaultCamera.value = null;
  223. return;
  224. }
  225. cameraJSON.value = res;
  226. map.loadFromJSON(res).then(() => {
  227. console.log('loadFromJSON', res);
  228. if (res.defaultCameraId) {
  229. defaultCamera.value = map.getCameraById(res.defaultCameraId);
  230. console.log('defaultCamera', defaultCamera.value);
  231. } else {
  232. defaultCamera.value = map.getObjects()?.[0] as CameraImage;
  233. }
  234. renderMap();
  235. });
  236. });
  237. };
  238. onMounted(() => {
  239. getScenesTree({ level: 2, valueKey: 'code', labelKey: 'name', disabled: true });
  240. if (selectedShopCode.value) {
  241. getShopContent(selectedShopCode.value);
  242. }
  243. map.createMap('mapEditCanvas');
  244. });
  245. const keyupListener = (e) => {
  246. const keyCode = e.code;
  247. if (keyCode === 'Delete') {
  248. handleDeleteCamera();
  249. }
  250. };
  251. onMounted(() => {
  252. document.addEventListener('keyup', keyupListener);
  253. });
  254. onUnmounted(() => {
  255. document.removeEventListener('keyup', keyupListener);
  256. });
  257. const filterShopCameraList = computed(() => {
  258. const k = searchKey.value.trim();
  259. if (!k) return shopCameraList.value;
  260. return shopCameraList.value.filter((x) => x.code?.includes(k) || x.workSpaceName?.includes(k));
  261. });
  262. /** 摄像机是否已添加到底图 */
  263. const isAddedToMap = (cameraId: string): boolean => {
  264. return !!allObjects.value.find((item) => item.cameraId === cameraId);
  265. };
  266. const handleAddCamera = (cameraId: string) => {
  267. if (!hasBg.value) return;
  268. if (map.hasCamera(cameraId)) {
  269. ElMessage.warning({ message: '相机已添加' });
  270. return;
  271. }
  272. map.addCamera(cameraId).then((cameraImg) => {
  273. if (!defaultCamera.value?.cameraId) {
  274. defaultCamera.value = cameraImg;
  275. }
  276. });
  277. };
  278. const handleSave = () => {
  279. const json = map.toJSON();
  280. console.log('save json', json);
  281. if (!json?.backgroundImage) {
  282. ElMessage.error('背景图片未添加');
  283. return;
  284. }
  285. const layout = JSON.stringify({ ...json, defaultCameraId: defaultCamera.value?.cameraId });
  286. updateMinMapViewLayoutApi({ layout, targetId: String(selectedShopDetail.value?.id) }).then(
  287. (res) => {
  288. console.log('updateMinMapViewLayoutApi', res);
  289. ElMessage.success('保存成功');
  290. },
  291. );
  292. };
  293. const handleDeleteCamera = () => {
  294. if (!selectedCamera.value) return;
  295. /** 如果删除的是默认选中的摄像头,那么先清空默认的摄像头再 */
  296. if (selectedCamera.value?.cameraId === defaultCamera.value?.cameraId) {
  297. defaultCamera.value = map.getObjects()?.[0];
  298. }
  299. map.removeActiveCamera();
  300. };
  301. const handleSetDefault = () => {
  302. const cameraId = rightSelectedCamera.value?.cameraId;
  303. if (!cameraId) return;
  304. defaultCamera.value = rightSelectedCamera.value;
  305. /** 选择完成后隐藏 */
  306. menuVisible.value = false;
  307. };
  308. watchEffect(() => {
  309. if (!defaultCamera.value) {
  310. favPosition.value = null;
  311. return;
  312. }
  313. favPosition.value = getFavPositionByCamera(defaultCamera.value);
  314. });
  315. </script>
  316. <style scoped>
  317. .page {
  318. }
  319. .page-head {
  320. height: 54px;
  321. padding-left: 15px;
  322. background-color: #ffffff;
  323. }
  324. .head-opt {
  325. margin-left: 20px;
  326. padding-right: 15px;
  327. font-size: 14px;
  328. color: #3f3f3f;
  329. }
  330. .avatar-uploader {
  331. /* width: 120px; */
  332. /* height: 30px; */
  333. /* border: 1px solid #eee; */
  334. border-radius: 4px;
  335. margin-left: 30px;
  336. }
  337. .upload-icon {
  338. position: absolute;
  339. top: 0;
  340. right: 0;
  341. left: 0;
  342. bottom: 0;
  343. margin: auto;
  344. }
  345. .paint-tool {
  346. position: relative;
  347. height: calc(100vh - 138px);
  348. margin-top: 2px;
  349. }
  350. .camera-list {
  351. width: 250px;
  352. padding: 0 10px;
  353. background-color: #ffffff;
  354. }
  355. .label-text {
  356. font-size: 14px;
  357. font-weight: 600;
  358. margin: 10px 0 5px 10px;
  359. }
  360. .camera-item {
  361. height: 32px;
  362. font-size: 14px;
  363. font-weight: 400;
  364. color: #404040;
  365. line-height: 14px;
  366. cursor: pointer;
  367. }
  368. .camera-item-disabled {
  369. color: #c6c6c6;
  370. }
  371. .camera-id {
  372. width: 110px;
  373. }
  374. .space-name {
  375. width: 120px;
  376. white-space: nowrap;
  377. overflow: hidden;
  378. text-overflow: ellipsis;
  379. }
  380. .draw-container {
  381. position: relative;
  382. width: calc(100% - 300px);
  383. margin: 20px;
  384. }
  385. :deep(.search-put .el-input__wrapper) {
  386. background-color: #f0f2f5;
  387. }
  388. :deep(.el-popper__arrow) {
  389. display: none;
  390. }
  391. :deep(.el-tree-node__content:hover) {
  392. background: #e6f4ff;
  393. }
  394. :deep(.el-button--primary) {
  395. --el-button-disabled-bg-color: #bfbfbf;
  396. }
  397. :deep(.el-popover) {
  398. width: unset !important;
  399. min-width: 110px;
  400. text-align: center;
  401. font-weight: 400;
  402. }
  403. .isAdded {
  404. color: #409eff;
  405. }
  406. .isActive {
  407. background-color: #eee;
  408. }
  409. </style>
  410. ./MapBase/useCameraMap ./MapBase/CameraMapBak