MiniMapConfig.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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. console.log(res)
  205. konvaMap.value.createMap(res, selectedShopId.value);
  206. } else {
  207. hasBg.value = false;
  208. isUploadBg.value = res.isUploadBg;
  209. }
  210. });
  211. };
  212. const selectedShopId = ref();
  213. const selectedName = ref();
  214. const viewType = ref();
  215. const shopCameraList = ref<ShopMapCamera[]>([]);
  216. const route = useRoute();
  217. const getShowCameras = async (id: number) => {
  218. const res = await getCamerasByWorkShopId({ workshopId: id });
  219. if (!res) return;
  220. res.children.forEach((item) => {
  221. if (!item.children || item.children.length <= 0) return
  222. item.children.forEach(camera => {
  223. shopCameraList.value.push({ ...camera, isSet: 0, workSpaceName: item.name })
  224. })
  225. })
  226. };
  227. onMounted(async () => {
  228. // getScenesTree({ level: 2, valueKey: 'code', labelKey: 'name', disabled: true });
  229. konvaMap.value.resizeContainer(
  230. drawContainer.value!.clientWidth,
  231. drawContainer.value!.clientHeight,
  232. );
  233. selectedShopId.value = Number(route.query.workshopId);
  234. selectedName.value = route.query.workshopName;
  235. viewType.value = route.query.viewType;
  236. await getShopContent(selectedShopId.value)
  237. // if (selectedShopCode.value) {
  238. // getShopContent(selectedShopCode.value);
  239. // }
  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) => {
  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);
  264. };
  265. const handleSave = () => {
  266. if (shopCameraList.value.length === 1 && !isUploadBg.value) {
  267. isMap.value = false;
  268. const layout = JSON.stringify({
  269. isUploadBg: isUploadBg.value,
  270. defaultCameraId: shopCameraList.value[0].code,
  271. });
  272. updateMinMapViewLayoutApi({
  273. layout,
  274. targetId: String(selectedShopId.value),
  275. viewType: viewType.value,
  276. }).then((res) => {
  277. console.log('updateMinMapViewLayoutApi', res);
  278. ElMessage.success('保存成功');
  279. });
  280. } else {
  281. isMap.value = true;
  282. const layout = konvaMap.value.saveLayout();
  283. updateMinMapViewLayoutApi({
  284. layout: JSON.stringify({ ...JSON.parse(layout), isUploadBg: true }),
  285. targetId: String(selectedShopId.value),
  286. viewType: viewType.value,
  287. }).then((res) => {
  288. console.log('updateMinMapViewLayoutApi', res);
  289. ElMessage.success('保存成功');
  290. });
  291. }
  292. };
  293. const changeMap = (val) => {
  294. isChange.value = val;
  295. };
  296. onBeforeRouteLeave(async () => {
  297. if (isChange.value) {
  298. await ElMessageBox.confirm('是否保存当前修改?', '提示', {
  299. confirmButtonText: '是',
  300. cancelButtonText: '否',
  301. type: 'warning',
  302. })
  303. .then(() => {
  304. handleSave();
  305. })
  306. .catch(() => {
  307. return true;
  308. });
  309. } else {
  310. return true;
  311. }
  312. });
  313. </script>
  314. <style scoped lang="scss">
  315. .page-head {
  316. height: 54px;
  317. padding-left: 15px;
  318. background-color: #ffffff;
  319. &__btn {
  320. display: flex;
  321. gap: 10px;
  322. align-items: center;
  323. font-size: 14px;
  324. cursor: pointer;
  325. img {
  326. width: 14px;
  327. }
  328. }
  329. }
  330. .head-opt {
  331. margin-left: 20px;
  332. padding-right: 15px;
  333. font-size: 14px;
  334. color: #3f3f3f;
  335. .workshop-name {
  336. font-weight: 600;
  337. }
  338. }
  339. .upload-option {
  340. display: flex;
  341. }
  342. .upload-title {
  343. margin-top: 13px;
  344. }
  345. .camera-one-tip {
  346. color: rgb(197, 97, 20);
  347. font-size: 30px;
  348. margin: 200px auto;
  349. }
  350. .avatar-uploader {
  351. border-radius: 4px;
  352. margin-left: 30px;
  353. }
  354. .upload-icon {
  355. position: absolute;
  356. top: 0;
  357. right: 0;
  358. left: 0;
  359. bottom: 0;
  360. margin: auto;
  361. }
  362. .paint-tool {
  363. position: relative;
  364. height: calc(100vh - 138px);
  365. margin-top: 2px;
  366. }
  367. .camera-list {
  368. width: 250px;
  369. padding: 0 10px;
  370. background-color: #ffffff;
  371. }
  372. .label-text {
  373. font-size: 14px;
  374. font-weight: 600;
  375. margin: 10px 0 5px 10px;
  376. }
  377. .camera-item {
  378. height: 32px;
  379. font-size: 14px;
  380. padding-left: 8px;
  381. font-weight: 400;
  382. color: #404040;
  383. line-height: 14px;
  384. cursor: pointer;
  385. &:hover {
  386. background-color: #e6f7ff;
  387. color: #1890ff;
  388. }
  389. }
  390. .isAdded {
  391. color: #1890ff;
  392. //cursor: not-allowed;
  393. }
  394. .isActive {
  395. background-color: #e6f7ff;
  396. color: #1890ff;
  397. }
  398. .camera-item-disabled {
  399. color: #c6c6c6;
  400. }
  401. .camera-id {
  402. width: 110px;
  403. }
  404. .space-name {
  405. width: 120px;
  406. white-space: nowrap;
  407. overflow: hidden;
  408. text-overflow: ellipsis;
  409. }
  410. .draw-container {
  411. position: relative;
  412. width: calc(100% - 300px);
  413. margin: 20px;
  414. overflow: hidden;
  415. //background-color: rgba(0, 0, 0, 0.6);
  416. }
  417. .bg-background {
  418. background-color: rgba(0, 0, 0, 0.6);
  419. }
  420. .integrationState {
  421. cursor: not-allowed;
  422. color: #ccc;
  423. }
  424. :deep(.search-put .el-input__wrapper) {
  425. background-color: #f0f2f5;
  426. }
  427. :deep(.el-popper__arrow) {
  428. display: none;
  429. }
  430. :deep(.el-tree-node__content:hover) {
  431. background: #e6f4ff;
  432. }
  433. :deep(.el-button--primary) {
  434. --el-button-disabled-bg-color: #bfbfbf;
  435. }
  436. :deep(.el-popover) {
  437. width: unset !important;
  438. min-width: 110px;
  439. text-align: center;
  440. font-weight: 400;
  441. }
  442. </style>