MiniMapConfig.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. <template>
  2. <div class="min-map">
  3. <header class="min-map__header">
  4. <section class="min-map__btn" @click="handleBack">
  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 { 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. });
  134. })
  135. };
  136. const getMapLayoutAPIMap = {
  137. [ViewType.minimap_PC]: getWorkshopMiniMapLayoutPCApi,
  138. [ViewType.minimap_phone]: getWorkshopMiniMapLayoutMobileApi,
  139. }
  140. const getMapLayout = async (id: number) => {
  141. const api = getMapLayoutAPIMap[viewType.value];
  142. const res = await api(id);
  143. if (!res) return;
  144. const layoutJSON = JSON.parse(res.layout);
  145. return layoutJSON;
  146. }
  147. const getShopContent = async (id: number) => {
  148. await getShowCameras(id);
  149. const idList = filterShopCameraList.value.map((item) => item.id);
  150. const res = await getMapLayout(id);
  151. if (!res) return;
  152. hasBg.value = true;
  153. isMap.value = res.isUploadBg;
  154. nextTick(() => {
  155. if (res.isUploadBg) {
  156. isUploadBg.value = true;
  157. konvaMap.value.createMap(res, selectedShopId.value);
  158. } else {
  159. hasBg.value = false;
  160. isUploadBg.value = res.isUploadBg;
  161. }
  162. openIntervalNew(idList, (targetData) => {
  163. updataState(filterShopCameraList.value, targetData);
  164. });
  165. })
  166. };
  167. const selectedShopId = ref();
  168. const selectedName = ref();
  169. const viewType = ref();
  170. const shopCameraList = ref<ShopMapCamera[]>([]);
  171. const route = useRoute();
  172. const getShowCameras = async (id: number) => {
  173. const res = await getCamerasByWorkShopId({ workshopId: id });
  174. if (!res) return;
  175. res.children.forEach((item) => {
  176. if (!item.children || item.children.length <= 0) return
  177. item.children.forEach(camera => {
  178. shopCameraList.value.push({ ...camera, isSet: 0, workSpaceName: item.name })
  179. })
  180. })
  181. };
  182. const mapConfig = reactive({
  183. width: 0,
  184. height: 0
  185. });
  186. onMounted(async () => {
  187. selectedShopId.value = Number(route.query.workshopId);
  188. selectedName.value = route.query.workshopName;
  189. viewType.value = route.query.viewType;
  190. mapConfig.width = drawContainer.value?.clientWidth || 0;
  191. mapConfig.height = drawContainer.value?.clientHeight || 0;
  192. await getShopContent(selectedShopId.value);
  193. });
  194. onUnmounted(() => {
  195. closeInterval();
  196. });
  197. const filterShopCameraList = computed(() => {
  198. const k = searchKey.value.trim();
  199. if (!k) return shopCameraList.value;
  200. return shopCameraList.value.filter(
  201. (x) => x.code?.includes(k) || x.name?.includes(k) || x.workSpaceName?.includes(k),
  202. );
  203. });
  204. const handleAddCamera = (cameraId: string, index: number) => {
  205. if (isAddedCamera(cameraId)) {
  206. const camera = konvaMap.value.findCamera(cameraId);
  207. konvaMap.value.handleCameraClick(camera);
  208. return;
  209. }
  210. if (!hasBg.value) {
  211. ElMessage.warning({
  212. message: '请先添加车间地图',
  213. });
  214. return;
  215. }
  216. konvaMap.value.addCamera(cameraId, index);
  217. };
  218. const handleSave = () => {
  219. isMap.value = true;
  220. if(!hasBg.value && isMap.value){
  221. ElMessage.error('请先添加车间地图');
  222. return;
  223. }
  224. const layout = konvaMap.value.saveLayout();
  225. const cameraList = JSON.parse(layout).cameraList;
  226. if (cameraList.length === 0 && hasBg.value) {
  227. ElMessage.error('请至少添加1个相机标签后发布');
  228. return;
  229. }
  230. updateMinMapViewLayoutApi({
  231. layout: JSON.stringify({ ...JSON.parse(layout), isUploadBg: hasBg.value }),
  232. targetId: String(selectedShopId.value),
  233. viewType: viewType.value,
  234. }).then(() => {
  235. ElMessage.success('保存成功');
  236. });
  237. };
  238. const changeMap = (val) => {
  239. isChange.value = val;
  240. };
  241. const handleBack = async() =>{
  242. if (isChange.value) {
  243. await ElMessageBox.confirm('是否保存当前修改?', '提示', {
  244. confirmButtonText: '是',
  245. cancelButtonText: '否',
  246. type: 'warning',
  247. })
  248. .then(() => {
  249. handleSave();
  250. })
  251. .catch(() => {
  252. router.back();
  253. });
  254. } else {
  255. router.back();
  256. }
  257. }
  258. </script>
  259. <style scoped lang="scss">
  260. .min-map {
  261. display: flex;
  262. flex-direction: column;
  263. gap: 4px;
  264. width: 100%;
  265. height: calc(100vh - 64px - 14px);
  266. background: #f5f7f9;
  267. border-radius: 6px;
  268. box-shadow: 0px 0px 6px 0px rgba(0, 0, 0, 0.12);
  269. &__header {
  270. display: flex;
  271. align-items: center;
  272. gap: 20px;
  273. width: inherit;
  274. height: 54px;
  275. padding: 0 15px 0 15px;
  276. background: #ffffff;
  277. border-radius: 6px 6px 0 0;
  278. }
  279. &__btn {
  280. display: flex;
  281. gap: 10px;
  282. align-items: center;
  283. font-size: 14px;
  284. cursor: pointer;
  285. img {
  286. width: 14px;
  287. }
  288. }
  289. &__main {
  290. display: flex;
  291. gap: 5px;
  292. width: 100%;
  293. height: calc(100% - 60px);
  294. border-radius: 6px;
  295. }
  296. }
  297. .workshop-name {
  298. font-size: 14px;
  299. color: #3f3f3f;
  300. font-weight: 600;
  301. }
  302. .operate-btn {
  303. display: flex;
  304. justify-content: flex-end;
  305. align-items: center;
  306. flex: 1;
  307. height: inherit;
  308. gap: 20px;
  309. .el-button {
  310. margin: 0;
  311. img {
  312. width: 14px;
  313. }
  314. }
  315. }
  316. .camera-list {
  317. width: 250px;
  318. height: 100%;
  319. border-radius: 0 0 0 6px;
  320. padding: 10px 10px;
  321. background-color: #ffffff;
  322. &__title {
  323. margin-left: 5px;
  324. font-size: 14px;
  325. font-weight: 600;
  326. }
  327. &__search {
  328. margin-top: 10px;
  329. :deep(.el-input__wrapper) {
  330. background-color: #f0f2f5;
  331. }
  332. }
  333. }
  334. .workshop-map {
  335. display: flex;
  336. justify-content: center;
  337. align-items: center;
  338. flex: 1;
  339. height: 100%;
  340. overflow: hidden;
  341. }
  342. .camera-item {
  343. width: 100%;
  344. max-height: calc(100% - 65px);
  345. overflow-y: auto;
  346. margin-top: 10px;
  347. margin-left: 5px;
  348. &__empty {
  349. color: #3f3f3f;
  350. }
  351. &__list {
  352. display: flex;
  353. justify-content: space-between;
  354. align-items: center;
  355. width: 100%;
  356. height: 32px;
  357. font-size: 14px;
  358. font-weight: 400;
  359. color: #404040;
  360. line-height: 14px;
  361. cursor: pointer;
  362. &:hover {
  363. background-color: #e6f7ff;
  364. color: #1890ff;
  365. }
  366. .camera-id {
  367. width: 110px;
  368. }
  369. .camera-space {
  370. width: 120px;
  371. white-space: nowrap;
  372. overflow: hidden;
  373. text-overflow: ellipsis;
  374. }
  375. }
  376. }
  377. :deep(.el-popover) {
  378. width: unset !important;
  379. min-width: 110px;
  380. text-align: center;
  381. font-weight: 400;
  382. }
  383. :deep(.el-popper__arrow) {
  384. display: none;
  385. }
  386. .isAdded {
  387. color: #1890ff;
  388. }
  389. .isActive {
  390. background-color: #e6f7ff;
  391. color: #1890ff;
  392. }
  393. .integrationState {
  394. cursor: not-allowed;
  395. color: #ccc;
  396. }
  397. </style>