MiniMapConfig.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. <template>
  2. <div class="page">
  3. <div class="page-head flex items-center">
  4. <div class="page-head__btn" @click="router.back">
  5. <img :src="rollback" />
  6. <span>返回</span>
  7. </div>
  8. <!-- <el-icon size="20">
  9. <ArrowLeft />
  10. </el-icon> -->
  11. <div class="head-opt flex-1 flex justify-between items-center">
  12. <div class="workshop-name">{{ selectedName }}
  13. <!-- <span>场景:</span>
  14. <el-tree-select v-model="selectedShopCode" :data="scenesTree" accordion :render-after-expand="false"
  15. :default-expand-all="true" :teleported="false" placeholder="请选择相关场景" @change="changeShop"
  16. style="width: 200px" /> -->
  17. </div>
  18. <div v-if="shopCameraList.length === 1" class="upload-option">
  19. <el-tooltip class="box-item" effect="dark" content="选择'是'则表示需要上传背景图片;选择'否'则表示无需上传背景图片,直接点击保存为布局即可"
  20. placement="top">
  21. <div class="upload-title">上传背景图片</div>
  22. </el-tooltip>
  23. <div class="my-2 flex items-center text-sm">
  24. <el-radio-group v-model="isUploadBg" @change="changeUploadBg" class="ml-4">
  25. <el-radio :label="true">是</el-radio>
  26. <el-radio :label="false">否</el-radio>
  27. </el-radio-group>
  28. </div>
  29. </div>
  30. <div class="flex">
  31. <el-upload class="avatar-uploader flex justify-center items-center" :action="actionUrl"
  32. :show-file-list="false" :on-success="handleAvatarSuccess" :with-credentials="true" name="file"
  33. :headers="getHeaders()">
  34. <el-button :icon="Refresh" :disabled="!hasBg"> 替换照片 </el-button>
  35. </el-upload>
  36. <el-button @click="handleSave" style="margin-left: 40px" type="primary"
  37. :disabled="!isSave && isUploadBg">保存为布局
  38. </el-button>
  39. </div>
  40. </div>
  41. </div>
  42. <div class="paint-tool flex">
  43. <div class="camera-list">
  44. <div>
  45. <span class="label-text flex">相机列表:</span>
  46. <ElInput class="search-put" style="margin: 10px 0; width: 230px" placeholder="请输入搜索内容" v-model="searchKey"
  47. :suffix-icon="Search" />
  48. </div>
  49. <span v-if="filterShopCameraList.length == 0" class="ml-1" style="color: #3f3f3f">
  50. 提示:该车间还未配置相机
  51. </span>
  52. <el-scrollbar v-else style="position: relative; height: calc(100% - 90px)">
  53. <div v-for="item in filterShopCameraList" :key="item.code" class="camera-item flex justify-start items-center"
  54. :class="{
  55. isAdded: isAddedCamera(item.code),
  56. isActive: item.code === caremaActiveId,
  57. integrationState: item.integrationState === 1,
  58. }" @click="handleAddCamera(item.code)">
  59. <span class="camera-id">{{ item.name }}</span>
  60. <el-popover placement="bottom-start" trigger="hover" :content="item.name" :teleported="false">
  61. <template #reference>
  62. <span class="space-name">{{ item.workSpaceName }}</span>
  63. </template>
  64. </el-popover>
  65. </div>
  66. </el-scrollbar>
  67. </div>
  68. <div ref="drawContainer" v-show="isUploadBg || shopCameraList.length !== 1" class="draw-container"
  69. :class="{ 'bg-background': hasBg ? true : false }">
  70. <KonvaMap ref="konvaMap" :filter-data="filterShopCameraList" :camera-list="shopCameraList"
  71. @change-default-camera="changeDefault" @send-camera-id="sendCameras" @change="changeMap" v-moveable:1 />
  72. <div id="editContainer" v-moveable:1></div>
  73. <el-upload v-if="!hasBg" class="upload-icon flex justify-center items-center" :action="actionUrl"
  74. :show-file-list="false" :before-upload="handleBeforeUpload" :on-success="handleAvatarSuccess"
  75. :with-credentials="true" name="file" :headers="getHeaders()">
  76. <img src="~@/assets/images/img-upload.png" />
  77. </el-upload>
  78. </div>
  79. <div v-show="!isUploadBg && shopCameraList.length === 1" class="camera-one-tip">仅此一个相机,无需添加背景图片</div>
  80. </div>
  81. </div>
  82. </template>
  83. <script setup lang="ts">
  84. // import useMiniMap from './use-mini-map';
  85. // import { storeToRefs } from 'pinia';
  86. import { ElMessage, ElInput, ElMessageBox } from 'element-plus';
  87. import { onMounted, onUnmounted, ref } from 'vue';
  88. import { updateMinMapViewLayoutApi, getCamerasByWorkShopId, getWorkshopMiniMapLayoutApi } from '@/api/scene/scene';
  89. import { computed } from 'vue';
  90. import { Search, Refresh, ArrowLeft } from '@element-plus/icons-vue';
  91. import KonvaMap from './MapBase/KonvaMap.vue';
  92. import useCameraStatus from '@/views/cameras/preview/store/useCameraStatus';
  93. import { onBeforeRouteLeave, useRoute } from 'vue-router';
  94. import urlJoin from 'url-join';
  95. import { useGlobSetting } from '@/hooks/setting';
  96. import { getHeaders } from '@/utils/http/axios';
  97. import rollback from '@/assets/rollback.png'
  98. import router from '@/router';
  99. import { ShopMapCamera } from '@/types/scene/type'
  100. const cameraStatus = useCameraStatus();
  101. const { openIntervalNew, closeInterval } = cameraStatus;
  102. // const miniMap = useMiniMap();
  103. // const { scenesTree, selectedShopCode, selectedShopDetail } = storeToRefs(miniMap);
  104. // const { getScenesTree, getMapLayout } = miniMap;
  105. // const { scenesTree, shopCameraList, selectedShopCode, selectedShopDetail } = storeToRefs(miniMap);
  106. // const { getScenesTree, getShowCameras, getMapLayout } = miniMap;
  107. const drawContainer = ref<HTMLDivElement>();
  108. const konvaMap = ref();
  109. const caremaActiveId = ref<string>('');
  110. const camerasAdded = ref<string[]>([]);
  111. const imgUrlBg = ref<string>('');
  112. const searchKey = ref('');
  113. // 是否已有背景图
  114. const hasBg = ref(false);
  115. //是否能够保存
  116. const isSave = computed(() => selectedShopId.value && hasBg.value);
  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. const changeUploadBg = () => {
  137. if (isUploadBg.value && !isMap.value) {
  138. konvaMap.value.resetMap();
  139. hasBg.value = false;
  140. }
  141. };
  142. const handleBeforeUpload = () => {
  143. if (!selectedShopId.value) {
  144. ElMessage.error({
  145. message: '请先选择车间',
  146. });
  147. return false;
  148. }
  149. };
  150. const sendCameras = (camerasList) => {
  151. camerasAdded.value = camerasList.map((item) => {
  152. return item.id;
  153. });
  154. };
  155. /** 判断相机是否已经添加 */
  156. const isAddedCamera = (cameraId: string) => {
  157. const index = camerasAdded.value.findIndex((item) => item === cameraId);
  158. return index >= 0;
  159. };
  160. const changeDefault = (defaultCameraId) => {
  161. caremaActiveId.value = defaultCameraId;
  162. };
  163. const handleAvatarSuccess = (e) => {
  164. imgUrlBg.value = e.data;
  165. konvaMap.value.addBg(imgUrlBg.value);
  166. hasBg.value = true;
  167. };
  168. // const changeShop = (code: string) => {
  169. // konvaMap.value.resetMap();
  170. // getShopContent(code);
  171. // hasBg.value = false;
  172. // };
  173. const getMapLayout = async (id: number) => {
  174. const res = await getWorkshopMiniMapLayoutApi(id);
  175. if (!res) return;
  176. const layoutJSON = JSON.parse(res.layout);
  177. return layoutJSON;
  178. }
  179. const getShopContent = async (id: number) => {
  180. await getShowCameras(id);
  181. const idList = filterShopCameraList.value.map((item) => item.id);
  182. const res = await getMapLayout(id);
  183. if (!res) return;
  184. hasBg.value = true;
  185. isMap.value = res.isUploadBg;
  186. if (res.isUploadBg) {
  187. isUploadBg.value = true;
  188. konvaMap.value.createMap(res, selectedShopId.value);
  189. } else {
  190. hasBg.value = false;
  191. isUploadBg.value = res.isUploadBg;
  192. }
  193. openIntervalNew(idList, (targetData) => {
  194. updataState(filterShopCameraList.value, targetData);
  195. });
  196. getMapLayout(id).then((res) => {
  197. if (!res) {
  198. return;
  199. }
  200. hasBg.value = true;
  201. isMap.value = res.isUploadBg;
  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. });
  210. };
  211. const selectedShopId = ref();
  212. const selectedName = ref();
  213. const viewType = ref();
  214. const shopCameraList = ref<ShopMapCamera[]>([]);
  215. const route = useRoute();
  216. const getShowCameras = async (id: number) => {
  217. const res = await getCamerasByWorkShopId({ workshopId: id });
  218. if (!res) return;
  219. res.children.forEach((item) => {
  220. if (!item.children || item.children.length <= 0) return
  221. item.children.forEach(camera => {
  222. shopCameraList.value.push({ ...camera, isSet: 0, workSpaceName: item.name })
  223. })
  224. })
  225. };
  226. onMounted(async () => {
  227. // getScenesTree({ level: 2, valueKey: 'code', labelKey: 'name', disabled: true });
  228. konvaMap.value.resizeContainer(
  229. drawContainer.value!.clientWidth,
  230. drawContainer.value!.clientHeight,
  231. );
  232. selectedShopId.value = Number(route.query.workshopId);
  233. selectedName.value = route.query.workshopName;
  234. viewType.value = route.query.viewType;
  235. await getShopContent(selectedShopId.value)
  236. // if (selectedShopCode.value) {
  237. // getShopContent(selectedShopCode.value);
  238. // }
  239. });
  240. onUnmounted(() => {
  241. closeInterval();
  242. });
  243. const filterShopCameraList = computed(() => {
  244. const k = searchKey.value.trim();
  245. if (!k) return shopCameraList.value;
  246. return shopCameraList.value.filter(
  247. (x) => x.code?.includes(k) || x.name?.includes(k) || x.workSpaceName?.includes(k),
  248. );
  249. });
  250. const handleAddCamera = (cameraId: string) => {
  251. if (isAddedCamera(cameraId)) {
  252. const camera = konvaMap.value.findCamera(cameraId);
  253. konvaMap.value.handleCameraClick(camera);
  254. return;
  255. }
  256. if (!hasBg.value) {
  257. ElMessage.warning({
  258. message: '请先添加背景图片',
  259. });
  260. return;
  261. }
  262. konvaMap.value.addCamera(cameraId);
  263. };
  264. const handleSave = () => {
  265. if (shopCameraList.value.length === 1 && !isUploadBg.value) {
  266. isMap.value = false;
  267. const layout = JSON.stringify({
  268. isUploadBg: isUploadBg.value,
  269. defaultCameraId: shopCameraList.value[0].code,
  270. });
  271. updateMinMapViewLayoutApi({
  272. layout,
  273. targetId: String(selectedShopId.value),
  274. viewType: viewType.value,
  275. }).then((res) => {
  276. console.log('updateMinMapViewLayoutApi', res);
  277. ElMessage.success('保存成功');
  278. });
  279. } else {
  280. isMap.value = true;
  281. const layout = konvaMap.value.saveLayout();
  282. updateMinMapViewLayoutApi({
  283. layout: JSON.stringify({ ...JSON.parse(layout), isUploadBg: true }),
  284. targetId: String(selectedShopId.value),
  285. viewType: viewType.value,
  286. }).then((res) => {
  287. console.log('updateMinMapViewLayoutApi', res);
  288. ElMessage.success('保存成功');
  289. });
  290. }
  291. };
  292. const changeMap = (val) => {
  293. isChange.value = val;
  294. };
  295. onBeforeRouteLeave(async () => {
  296. if (isChange.value) {
  297. await ElMessageBox.confirm('是否保存当前修改?', '提示', {
  298. confirmButtonText: '是',
  299. cancelButtonText: '否',
  300. type: 'warning',
  301. })
  302. .then(() => {
  303. handleSave();
  304. })
  305. .catch(() => {
  306. return true;
  307. });
  308. } else {
  309. return true;
  310. }
  311. });
  312. </script>
  313. <style scoped lang="scss">
  314. .page-head {
  315. height: 54px;
  316. padding-left: 15px;
  317. background-color: #ffffff;
  318. &__btn {
  319. display: flex;
  320. gap: 10px;
  321. align-items: center;
  322. font-size: 14px;
  323. cursor: pointer;
  324. img {
  325. width: 14px;
  326. }
  327. }
  328. }
  329. .head-opt {
  330. margin-left: 20px;
  331. padding-right: 15px;
  332. font-size: 14px;
  333. color: #3f3f3f;
  334. .workshop-name {
  335. font-weight: 600;
  336. }
  337. }
  338. .upload-option {
  339. display: flex;
  340. }
  341. .upload-title {
  342. margin-top: 13px;
  343. }
  344. .camera-one-tip {
  345. color: rgb(197, 97, 20);
  346. font-size: 30px;
  347. margin: 200px auto;
  348. }
  349. .avatar-uploader {
  350. border-radius: 4px;
  351. margin-left: 30px;
  352. }
  353. .upload-icon {
  354. position: absolute;
  355. top: 0;
  356. right: 0;
  357. left: 0;
  358. bottom: 0;
  359. margin: auto;
  360. }
  361. .paint-tool {
  362. position: relative;
  363. height: calc(100vh - 138px);
  364. margin-top: 2px;
  365. }
  366. .camera-list {
  367. width: 250px;
  368. padding: 0 10px;
  369. background-color: #ffffff;
  370. }
  371. .label-text {
  372. font-size: 14px;
  373. font-weight: 600;
  374. margin: 10px 0 5px 10px;
  375. }
  376. .camera-item {
  377. height: 32px;
  378. font-size: 14px;
  379. padding-left: 8px;
  380. font-weight: 400;
  381. color: #404040;
  382. line-height: 14px;
  383. cursor: pointer;
  384. &:hover {
  385. background-color: #e6f7ff;
  386. color: #1890ff;
  387. }
  388. }
  389. .isAdded {
  390. color: #1890ff;
  391. //cursor: not-allowed;
  392. }
  393. .isActive {
  394. background-color: #e6f7ff;
  395. color: #1890ff;
  396. }
  397. .camera-item-disabled {
  398. color: #c6c6c6;
  399. }
  400. .camera-id {
  401. width: 110px;
  402. }
  403. .space-name {
  404. width: 120px;
  405. white-space: nowrap;
  406. overflow: hidden;
  407. text-overflow: ellipsis;
  408. }
  409. .draw-container {
  410. position: relative;
  411. width: calc(100% - 300px);
  412. margin: 20px;
  413. overflow: hidden;
  414. //background-color: rgba(0, 0, 0, 0.6);
  415. }
  416. .bg-background {
  417. background-color: rgba(0, 0, 0, 0.6);
  418. }
  419. .integrationState {
  420. cursor: not-allowed;
  421. color: #ccc;
  422. }
  423. :deep(.search-put .el-input__wrapper) {
  424. background-color: #f0f2f5;
  425. }
  426. :deep(.el-popper__arrow) {
  427. display: none;
  428. }
  429. :deep(.el-tree-node__content:hover) {
  430. background: #e6f4ff;
  431. }
  432. :deep(.el-button--primary) {
  433. --el-button-disabled-bg-color: #bfbfbf;
  434. }
  435. :deep(.el-popover) {
  436. width: unset !important;
  437. min-width: 110px;
  438. text-align: center;
  439. font-weight: 400;
  440. }
  441. </style>