MiniMapConfig.vue 12 KB

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