MiniMapConfig.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. <template>
  2. <div class="min-map">
  3. <header class="min-map__header">
  4. <section class="min-map__btn" @click="router.back">
  5. <img :src="rollback" />
  6. <span>返回</span>
  7. </section>
  8. <section class="workshop-name">{{ selectedName }}</section>
  9. <section class="operate-btn">
  10. <a @click="downloadImage(imgUrlBg, selectedName + '.png')" v-if="imgUrlBg">下载底图</a>
  11. <el-upload
  12. class="avatar-uploader"
  13. :action="actionUrl"
  14. :show-file-list="false"
  15. :before-upload="handleBeforeUpload"
  16. :on-success="handleAvatarSuccess"
  17. :with-credentials="true"
  18. name="file"
  19. :headers="getHeaders()"
  20. >
  21. <el-button :icon="UploadFilled" :disabled="!hasBg"> 替换照片 </el-button>
  22. </el-upload>
  23. <el-button
  24. :icon="Refresh"
  25. @click="openMessageBox('提示', '是否重置当前设置', refreshUploadBg, '重置成功!')"
  26. :disabled="!hasBg"
  27. >
  28. 重置布局
  29. </el-button>
  30. <el-button @click="handleSave" type="primary">保存</el-button>
  31. </section>
  32. </header>
  33. <main class="min-map__main">
  34. <section class="camera-list">
  35. <header class="camera-list__title">相机列表:</header>
  36. <ElInput class="camera-list__search" placeholder="请输入搜索内容" v-model="searchKey" :suffix-icon="Search" />
  37. <main class="camera-item">
  38. <span class="camera-item__empty" v-show="filterShopCameraList.length == 0"> 提示:该车间还未配置相机 </span>
  39. <div
  40. v-for="(item, index) in filterShopCameraList"
  41. :key="item.code"
  42. class="camera-item__list"
  43. :class="{
  44. isAdded: isAddedCamera(item.code),
  45. isActive: item.code === caremaActiveId,
  46. integrationState: item.integrationState === IntegrationIsActive.NO,
  47. }"
  48. @click="handleAddCamera(item, index)"
  49. >
  50. <span class="camera-id">{{ item.name }}</span>
  51. <el-popover placement="right-start" trigger="hover" :content="item.workSpaceName" :teleported="false">
  52. <template #reference>
  53. <span class="camera-space">{{ item.workSpaceName }}</span>
  54. </template>
  55. </el-popover>
  56. </div>
  57. </main>
  58. </section>
  59. <section class="workshop-map" ref="drawContainer">
  60. <el-upload
  61. v-if="!hasBg"
  62. :action="actionUrl"
  63. :show-file-list="false"
  64. :before-upload="handleBeforeUpload"
  65. :on-success="handleAvatarSuccess"
  66. :with-credentials="true"
  67. name="file"
  68. :headers="getHeaders()"
  69. >
  70. <img src="~@/assets/images/img-upload.png" />
  71. </el-upload>
  72. <KonvaMap
  73. ref="konvaMap"
  74. :filter-data="filterShopCameraList"
  75. :camera-list="shopCameraList"
  76. :map-config="mapConfig"
  77. :is-knova-destroy="isKnovaDestroy"
  78. @change-default-camera="changeDefault"
  79. @send-camera-id="sendCameras"
  80. @change="changeMap"
  81. v-else
  82. />
  83. </section>
  84. </main>
  85. </div>
  86. </template>
  87. <script setup lang="ts">
  88. import { ElMessage, ElInput, ElMessageBox } from 'element-plus';
  89. import { Search, Refresh, UploadFilled } from '@element-plus/icons-vue';
  90. import rollback from '@/assets/rollback.png';
  91. import { onMounted, onUnmounted, ref, computed, reactive, nextTick } from 'vue';
  92. import {
  93. updateMinMapViewLayoutApi,
  94. getCamerasByWorkShopId,
  95. getWorkshopMiniMapLayoutPCApi,
  96. getWorkshopMiniMapLayoutMobileApi,
  97. } from '@/api/scene/scene';
  98. import KonvaMap from './MapBase/KonvaMap.vue';
  99. import useCameraStatus from '@/views/cameras/preview/store/useCameraStatus';
  100. import { onBeforeRouteLeave, useRoute } from 'vue-router';
  101. import urlJoin from 'url-join';
  102. import { useGlobSetting } from '@/hooks/setting';
  103. import { getHeaders } from '@/utils/http/axios';
  104. import router from '@/router';
  105. import { ShopMapCamera } from '@/types/scene/type';
  106. import { ViewType } from '@/types/page-config/type';
  107. import { openMessageBox } from '@/views/system-config/business-scene/components/MessageBox';
  108. import { IntegrationIsActive } from '@/types/camera/type';
  109. const cameraStatus = useCameraStatus();
  110. const { openIntervalNew, closeInterval } = cameraStatus;
  111. const drawContainer = ref<HTMLDivElement>();
  112. const konvaMap = ref();
  113. const caremaActiveId = ref<string>('');
  114. const camerasAdded = ref<string[]>([]);
  115. const imgUrlBg = ref<string>('');
  116. const searchKey = ref('');
  117. // 是否已有背景图
  118. const hasBg = ref(false);
  119. //是否修改
  120. const isChange = ref<boolean>(false);
  121. //单个相机时是否上传图片
  122. const isUploadBg = ref<boolean>(true);
  123. const isMap = ref(false);
  124. const { urlPrefix, minifyImgUrl } = useGlobSetting();
  125. const actionUrl = computed(() => {
  126. return urlJoin(urlPrefix!, `/admin/minimap/uploadPicture`);
  127. });
  128. function updataState(data, updateData) {
  129. for (let i = 0; i < data.length; i++) {
  130. const camera = data[i];
  131. const matchedCamera = updateData.find((item) => item.code === camera.code);
  132. if (matchedCamera) {
  133. camera.status = matchedCamera.networkingState;
  134. camera.integrationState = matchedCamera.integrationState;
  135. }
  136. }
  137. }
  138. function downloadImage(url: string, filename: string) {
  139. const link = document.createElement('a');
  140. link.href = url;
  141. link.download = filename;
  142. // 触发点击事件
  143. document.body.appendChild(link);
  144. link.click();
  145. document.body.removeChild(link);
  146. }
  147. const refreshUploadBg = () => {
  148. konvaMap.value.resetMap();
  149. hasBg.value = false;
  150. };
  151. const handleBeforeUpload = (rawFile) => {
  152. if (!selectedShopId.value) {
  153. ElMessage.error({
  154. message: '请先选择车间',
  155. });
  156. return Promise.reject();
  157. }
  158. if (rawFile.type !== 'image/jpg' && rawFile.type !== 'image/jpeg' && rawFile.type !== 'image/png') {
  159. ElMessage.error('请上传jpg、jpeg、png格式的图片!');
  160. return Promise.reject();
  161. }
  162. const mSize = 1024 * 1024;
  163. if (rawFile.size > 2 * mSize) {
  164. const realSize = (rawFile.size / mSize).toFixed(2);
  165. ElMessageBox.alert(`<div>当前图片${realSize}M,建议压缩到2M以内。 <div>`, '提示', {
  166. dangerouslyUseHTMLString: true,
  167. confirmButtonText: minifyImgUrl ? '点我去压缩' : '确定',
  168. type: 'warning',
  169. }).then(() => {
  170. if (minifyImgUrl) {
  171. window.open(minifyImgUrl);
  172. }
  173. });
  174. return Promise.reject();
  175. }
  176. };
  177. const sendCameras = (camerasList) => {
  178. camerasAdded.value = camerasList.map((item) => {
  179. return item.id;
  180. });
  181. };
  182. /** 判断相机是否已经添加 */
  183. const isAddedCamera = (cameraId: string) => {
  184. const index = camerasAdded.value.findIndex((item) => item === cameraId);
  185. return index >= 0;
  186. };
  187. const changeDefault = (defaultCameraId) => {
  188. caremaActiveId.value = defaultCameraId;
  189. };
  190. const isKnovaDestroy = ref<boolean>(false); //重新上传图片之后将knova销毁
  191. const handleAvatarSuccess = (e) => {
  192. isKnovaDestroy.value = true;
  193. imgUrlBg.value = e.data;
  194. hasBg.value = true;
  195. nextTick(() => {
  196. konvaMap.value.addBg(imgUrlBg.value).then(() => {
  197. isKnovaDestroy.value = false;
  198. isChange.value = true;
  199. });
  200. });
  201. };
  202. const getMapLayoutAPIMap = {
  203. [ViewType.minimap_PC]: getWorkshopMiniMapLayoutPCApi,
  204. [ViewType.minimap_phone]: getWorkshopMiniMapLayoutMobileApi,
  205. };
  206. const getMapLayout = async (id: number) => {
  207. const api = getMapLayoutAPIMap[viewType.value];
  208. const res = await api(id);
  209. if (!res) return;
  210. const layoutJSON = JSON.parse(res.layout);
  211. return layoutJSON;
  212. };
  213. const getShopContent = async (id: number) => {
  214. await getShowCameras(id);
  215. const idList = filterShopCameraList.value.map((item) => item.id);
  216. const res = await getMapLayout(id);
  217. if (!res) return;
  218. hasBg.value = true;
  219. isMap.value = res.isUploadBg;
  220. imgUrlBg.value = res.bgImgUrl;
  221. nextTick(() => {
  222. if (res.isUploadBg) {
  223. isUploadBg.value = true;
  224. konvaMap.value.createMap(res, selectedShopId.value);
  225. } else {
  226. hasBg.value = false;
  227. isUploadBg.value = res.isUploadBg;
  228. }
  229. openIntervalNew(idList, (targetData) => {
  230. updataState(filterShopCameraList.value, targetData);
  231. });
  232. });
  233. };
  234. const selectedShopId = ref();
  235. const selectedName = ref();
  236. const viewType = ref();
  237. const shopCameraList = ref<ShopMapCamera[]>([]);
  238. const route = useRoute();
  239. const getShowCameras = async (id: number) => {
  240. const res = await getCamerasByWorkShopId({ workshopId: id });
  241. if (!res) return;
  242. res.children.forEach((item) => {
  243. if (!item.children || item.children.length <= 0) return;
  244. item.children.forEach((camera) => {
  245. shopCameraList.value.push({ ...camera, isSet: 0, workSpaceName: item.name });
  246. });
  247. });
  248. };
  249. const mapConfig = reactive({
  250. width: 0,
  251. height: 0,
  252. });
  253. onMounted(async () => {
  254. selectedShopId.value = Number(route.query.workshopId);
  255. selectedName.value = route.query.workshopName;
  256. viewType.value = route.query.viewType;
  257. mapConfig.width = drawContainer.value?.clientWidth || 0;
  258. mapConfig.height = drawContainer.value?.clientHeight || 0;
  259. await getShopContent(selectedShopId.value);
  260. });
  261. onUnmounted(() => {
  262. closeInterval();
  263. });
  264. const filterShopCameraList = computed(() => {
  265. const k = searchKey.value.trim();
  266. if (!k) return shopCameraList.value;
  267. return shopCameraList.value.filter(
  268. (x) => x.code?.includes(k) || x.name?.includes(k) || x.workSpaceName?.includes(k),
  269. );
  270. });
  271. const handleAddCamera = (cameraDetail: ShopMapCamera, index: number) => {
  272. const cameraCode = cameraDetail.code;
  273. if (isAddedCamera(cameraCode)) {
  274. const camera = konvaMap.value.findCamera(cameraCode);
  275. konvaMap.value.handleCameraClick(camera);
  276. return;
  277. }
  278. if (cameraDetail.integrationState === IntegrationIsActive.NO) {
  279. ElMessage.warning({
  280. message: '该相机未接入平台,暂时无法添加到小地图',
  281. });
  282. return;
  283. }
  284. if (!hasBg.value) {
  285. ElMessage.warning({
  286. message: '请先添加车间地图',
  287. });
  288. return;
  289. }
  290. konvaMap.value.addCamera(cameraCode, index);
  291. };
  292. const handleSave = () => {
  293. isMap.value = true;
  294. if (!hasBg.value && isMap.value) {
  295. ElMessage.error('请先添加车间地图');
  296. return false;
  297. }
  298. const layout = konvaMap.value.saveLayout();
  299. const cameraList = JSON.parse(layout).cameraList;
  300. if (cameraList.length === 0 && hasBg.value) {
  301. ElMessage.error('请至少添加1个相机标签后发布');
  302. return false;
  303. }
  304. updateMinMapViewLayoutApi({
  305. layout: JSON.stringify({ ...JSON.parse(layout), isUploadBg: hasBg.value }),
  306. targetId: String(selectedShopId.value),
  307. viewType: viewType.value,
  308. }).then(() => {
  309. ElMessage.success('保存成功');
  310. });
  311. return true;
  312. };
  313. const changeMap = (val) => {
  314. isChange.value = val;
  315. };
  316. onBeforeRouteLeave((to, from, next) => {
  317. if (!isChange.value) {
  318. next();
  319. return;
  320. }
  321. setTimeout(() => {
  322. ElMessageBox.confirm('是否保存当前修改?', '提示', {
  323. confirmButtonText: '是',
  324. cancelButtonText: '否',
  325. customClass: 'elMessageBox__custom--warning',
  326. })
  327. .then(async () => {
  328. const isSaveSuccess = await handleSave();
  329. if (isSaveSuccess) {
  330. next();
  331. }
  332. next(false);
  333. })
  334. .catch(() => {
  335. next();
  336. });
  337. }, 200);
  338. });
  339. </script>
  340. <style scoped lang="scss">
  341. .min-map {
  342. display: flex;
  343. flex-direction: column;
  344. gap: 4px;
  345. width: 100%;
  346. height: calc(100vh - 64px - 14px);
  347. background: #f5f7f9;
  348. border-radius: 6px;
  349. box-shadow: 0px 0px 6px 0px rgba(0, 0, 0, 0.12);
  350. &__header {
  351. display: flex;
  352. align-items: center;
  353. gap: 20px;
  354. width: inherit;
  355. height: 54px;
  356. padding: 0 15px 0 15px;
  357. background: #ffffff;
  358. border-radius: 6px 6px 0 0;
  359. }
  360. &__btn {
  361. display: flex;
  362. gap: 10px;
  363. align-items: center;
  364. font-size: 14px;
  365. cursor: pointer;
  366. img {
  367. width: 14px;
  368. }
  369. }
  370. &__main {
  371. display: flex;
  372. gap: 5px;
  373. width: 100%;
  374. height: calc(100% - 60px);
  375. border-radius: 6px;
  376. }
  377. }
  378. .workshop-name {
  379. font-size: 14px;
  380. color: #3f3f3f;
  381. font-weight: 600;
  382. }
  383. .operate-btn {
  384. display: flex;
  385. justify-content: flex-end;
  386. align-items: center;
  387. flex: 1;
  388. height: inherit;
  389. gap: 20px;
  390. .el-button {
  391. margin: 0;
  392. img {
  393. width: 14px;
  394. }
  395. }
  396. }
  397. .camera-list {
  398. width: 250px;
  399. height: 100%;
  400. border-radius: 0 0 0 6px;
  401. padding: 10px 10px;
  402. background-color: #ffffff;
  403. &__title {
  404. margin-left: 5px;
  405. font-size: 14px;
  406. font-weight: 600;
  407. }
  408. &__search {
  409. margin-top: 10px;
  410. :deep(.el-input__wrapper) {
  411. background-color: #f0f2f5;
  412. }
  413. }
  414. }
  415. .workshop-map {
  416. display: flex;
  417. justify-content: center;
  418. align-items: center;
  419. flex: 1;
  420. height: 100%;
  421. overflow: hidden;
  422. }
  423. .camera-item {
  424. width: 100%;
  425. max-height: calc(100% - 65px);
  426. overflow-y: auto;
  427. margin-top: 10px;
  428. margin-left: 5px;
  429. &__empty {
  430. color: #3f3f3f;
  431. }
  432. &__list {
  433. align-items: center;
  434. margin: 10px 0;
  435. font-size: 14px;
  436. font-weight: 400;
  437. color: #404040;
  438. cursor: pointer;
  439. &:hover {
  440. background-color: #e6f7ff;
  441. color: #1890ff;
  442. }
  443. .camera-space {
  444. margin-left: 12px;
  445. }
  446. }
  447. }
  448. :deep(.el-popover) {
  449. width: unset !important;
  450. min-width: 110px;
  451. text-align: center;
  452. font-weight: 400;
  453. }
  454. :deep(.el-popper__arrow) {
  455. display: none;
  456. }
  457. .isAdded {
  458. color: #1890ff;
  459. }
  460. .isActive {
  461. background-color: #e6f7ff;
  462. color: #1890ff;
  463. }
  464. .integrationState {
  465. cursor: not-allowed;
  466. color: #ccc;
  467. }
  468. </style>