MiniMapConfig.vue 13 KB

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