MiniMapConfig.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. <template>
  2. <div class="page">
  3. <div class="page-head flex items-center">
  4. <el-icon size="20"><ArrowLeft /></el-icon>
  5. <div class="head-opt flex-1 flex justify-between items-center">
  6. <div>
  7. <span>场景:</span>
  8. <el-tree-select
  9. v-model="selectedShopCode"
  10. :data="scenesTree"
  11. accordion
  12. :render-after-expand="false"
  13. :default-expand-all="true"
  14. :teleported="false"
  15. placeholder="请选择相关场景"
  16. @change="changeShop"
  17. />
  18. </div>
  19. <div v-if="shopCameraList.length === 1" class="upload-option">
  20. <el-tooltip
  21. class="box-item"
  22. effect="dark"
  23. content="选择'是'则表示需要上传背景图片;选择'否'则表示无需上传背景图片,直接点击保存为布局即可"
  24. placement="top"
  25. >
  26. <div class="upload-title">上传背景图片</div>
  27. </el-tooltip>
  28. <div class="my-2 flex items-center text-sm">
  29. <el-radio-group v-model="isUploadBg" @change="changeUploadBg" class="ml-4">
  30. <el-radio :label="true">是</el-radio>
  31. <el-radio :label="false">否</el-radio>
  32. </el-radio-group>
  33. </div></div
  34. >
  35. <div class="flex">
  36. <el-upload
  37. class="avatar-uploader flex justify-center items-center"
  38. action="/skyeye-admin-api/layout/uploadPicture"
  39. :show-file-list="false"
  40. :on-success="handleAvatarSuccess"
  41. :with-credentials="true"
  42. name="file"
  43. :data="{ workshopId: selectedShopDetail?.id }"
  44. >
  45. <el-button :icon="Refresh" :disabled="!hasBg"> 替换照片 </el-button>
  46. </el-upload>
  47. <el-button
  48. @click="handleSave"
  49. style="margin-left: 40px"
  50. type="primary"
  51. :disabled="!selectedShopCode"
  52. >保存为布局
  53. </el-button>
  54. </div>
  55. </div>
  56. </div>
  57. <div class="paint-tool flex">
  58. <div class="camera-list">
  59. <div>
  60. <span class="label-text flex">相机列表:</span>
  61. <ElInput
  62. class="search-put"
  63. style="margin: 10px 0; width: 230px"
  64. placeholder="请输入搜索内容"
  65. v-model="searchKey"
  66. :suffix-icon="Search"
  67. />
  68. </div>
  69. <span v-if="filterShopCameraList.length == 0" class="ml-1" style="color: #3f3f3f">
  70. 提示:请先选择相应场景和图片
  71. </span>
  72. <el-scrollbar v-else style="position: relative; height: calc(100% - 90px)">
  73. <div
  74. v-for="item in filterShopCameraList"
  75. :key="item.code"
  76. class="camera-item flex justify-start items-center"
  77. :class="{
  78. isAdded: isAddedCamera(item.code),
  79. isActive: item.code === caremaActiveId,
  80. }"
  81. @click="handleAddCamera(item.code, item.pushstreamIp)"
  82. >
  83. <span class="camera-id">{{ item.name }}</span>
  84. <el-popover
  85. placement="bottom-start"
  86. trigger="hover"
  87. :content="item.name"
  88. :teleported="false"
  89. >
  90. <template #reference>
  91. <span class="space-name">{{ item.workSpaceName }}</span>
  92. </template>
  93. </el-popover>
  94. </div>
  95. </el-scrollbar>
  96. </div>
  97. <div
  98. ref="drawContainer"
  99. v-show="isUploadBg || shopCameraList.length !== 1"
  100. class="draw-container"
  101. >
  102. <KonvaMap
  103. ref="konvaMap"
  104. @change-default-camera="changeDefault"
  105. @send-camera-id="sendCameras"
  106. v-moveable:1
  107. />
  108. <div id="editContainer" v-moveable:1></div>
  109. <el-upload
  110. v-if="!hasBg"
  111. class="upload-icon flex justify-center items-center"
  112. action="/skyeye-admin-api/layout/uploadPicture"
  113. :show-file-list="false"
  114. :before-upload="handleBeforeUpload"
  115. :on-success="handleAvatarSuccess"
  116. :with-credentials="true"
  117. name="file"
  118. :data="{ workshopId: selectedShopDetail?.id }"
  119. >
  120. <img src="~@/assets/images/img-upload.png" />
  121. </el-upload>
  122. </div>
  123. <div v-show="!isUploadBg && shopCameraList.length === 1" class="camera-one-tip"
  124. >仅此一个相机,无需添加背景图片</div
  125. >
  126. </div>
  127. </div>
  128. </template>
  129. <script setup lang="ts">
  130. import useMiniMap from './use-mini-map';
  131. import { storeToRefs } from 'pinia';
  132. import { ElMessage, ElInput } from 'element-plus';
  133. import { onMounted, ref } from 'vue';
  134. import { updateMinMapViewLayoutApi } from '@/api/scene/scene';
  135. import { computed } from 'vue';
  136. import { Search, Refresh, ArrowLeft } from '@element-plus/icons-vue';
  137. import KonvaMap from './MapBase/KonvaMap.vue';
  138. const miniMap = useMiniMap();
  139. const { scenesTree, shopCameraList, selectedShopCode, selectedShopDetail } = storeToRefs(miniMap);
  140. const { getScenesTree, getShowCameras, getMapLayout } = miniMap;
  141. const drawContainer = ref<HTMLDivElement>();
  142. const konvaMap = ref();
  143. const caremaActiveId = ref<string>('');
  144. const camerasAdded = ref<string[]>([]);
  145. const imgUrlBg = ref<string>('');
  146. const searchKey = ref('');
  147. // 是否已有背景图
  148. const hasBg = ref(false);
  149. //单个相机时是否上传图片
  150. const isUploadBg = ref<boolean>(true);
  151. const isMap = ref(false);
  152. const changeUploadBg = () => {
  153. if (isUploadBg.value && !isMap.value) {
  154. konvaMap.value.resetMap();
  155. hasBg.value = false;
  156. }
  157. };
  158. const handleBeforeUpload = () => {
  159. if (!selectedShopCode.value) {
  160. ElMessage.error({
  161. message: '请先选择车间',
  162. });
  163. return false;
  164. }
  165. };
  166. const sendCameras = (camerasList) => {
  167. camerasAdded.value = camerasList.map((item) => {
  168. return item.id;
  169. });
  170. };
  171. /** 判断相机是否已经添加 */
  172. const isAddedCamera = (cameraId: string) => {
  173. const index = camerasAdded.value.findIndex((item) => item === cameraId);
  174. return index >= 0;
  175. };
  176. const changeDefault = (defaultCameraId) => {
  177. caremaActiveId.value = defaultCameraId;
  178. };
  179. const handleAvatarSuccess = (e) => {
  180. imgUrlBg.value = e.data;
  181. konvaMap.value.addBg(imgUrlBg.value);
  182. hasBg.value = true;
  183. };
  184. const changeShop = (code: string) => {
  185. konvaMap.value.resetMap();
  186. getShopContent(code);
  187. hasBg.value = false;
  188. };
  189. const getShopContent = (code: string) => {
  190. getShowCameras(code);
  191. getMapLayout(code).then((res) => {
  192. if (!res) {
  193. return;
  194. }
  195. hasBg.value = true;
  196. isMap.value = res.isUploadBg;
  197. if (res.isUploadBg) {
  198. isUploadBg.value = true;
  199. konvaMap.value.createMap(res);
  200. } else {
  201. hasBg.value = false;
  202. isUploadBg.value = res.isUploadBg;
  203. }
  204. });
  205. };
  206. onMounted(() => {
  207. getScenesTree({ level: 2, valueKey: 'code', labelKey: 'name', disabled: true });
  208. konvaMap.value.resizeContainer(
  209. drawContainer.value!.clientWidth,
  210. drawContainer.value!.clientHeight,
  211. );
  212. if (selectedShopCode.value) {
  213. getShopContent(selectedShopCode.value);
  214. }
  215. });
  216. const filterShopCameraList = computed(() => {
  217. const k = searchKey.value.trim();
  218. if (!k) return shopCameraList.value;
  219. return shopCameraList.value.filter((x) => x.code?.includes(k) || x.workSpaceName?.includes(k));
  220. });
  221. const handleAddCamera = (cameraId: string, streamIp: string) => {
  222. if (!hasBg.value) {
  223. ElMessage.warning({
  224. message: '请先添加背景图片',
  225. });
  226. return;
  227. }
  228. konvaMap.value.addCamera(cameraId, streamIp);
  229. };
  230. const handleSave = () => {
  231. if (shopCameraList.value.length === 1 && !isUploadBg.value) {
  232. isMap.value = false;
  233. const layout = JSON.stringify({ isUploadBg: isUploadBg.value });
  234. updateMinMapViewLayoutApi({ layout, targetId: String(selectedShopDetail.value?.id) }).then(
  235. (res) => {
  236. console.log('updateMinMapViewLayoutApi', res);
  237. ElMessage.success('保存成功');
  238. },
  239. );
  240. } else {
  241. isMap.value = true;
  242. const layout = konvaMap.value.saveLayout();
  243. updateMinMapViewLayoutApi({
  244. layout: JSON.stringify({ ...JSON.parse(layout), isUploadBg: true }),
  245. targetId: String(selectedShopDetail.value?.id),
  246. }).then((res) => {
  247. console.log('updateMinMapViewLayoutApi', res);
  248. ElMessage.success('保存成功');
  249. });
  250. }
  251. };
  252. </script>
  253. <style scoped lang="scss">
  254. .page {
  255. }
  256. .page-head {
  257. height: 54px;
  258. padding-left: 15px;
  259. background-color: #ffffff;
  260. }
  261. .head-opt {
  262. margin-left: 20px;
  263. padding-right: 15px;
  264. font-size: 14px;
  265. color: #3f3f3f;
  266. }
  267. .upload-option {
  268. display: flex;
  269. }
  270. .upload-title {
  271. margin-top: 13px;
  272. }
  273. .camera-one-tip {
  274. color: rgb(197, 97, 20);
  275. font-size: 30px;
  276. margin: 200px auto;
  277. }
  278. .avatar-uploader {
  279. border-radius: 4px;
  280. margin-left: 30px;
  281. }
  282. .upload-icon {
  283. position: absolute;
  284. top: 0;
  285. right: 0;
  286. left: 0;
  287. bottom: 0;
  288. margin: auto;
  289. }
  290. .paint-tool {
  291. position: relative;
  292. height: calc(100vh - 138px);
  293. margin-top: 2px;
  294. }
  295. .camera-list {
  296. width: 250px;
  297. padding: 0 10px;
  298. background-color: #ffffff;
  299. }
  300. .label-text {
  301. font-size: 14px;
  302. font-weight: 600;
  303. margin: 10px 0 5px 10px;
  304. }
  305. .camera-item {
  306. height: 32px;
  307. font-size: 14px;
  308. padding-left: 8px;
  309. font-weight: 400;
  310. color: #404040;
  311. line-height: 14px;
  312. cursor: pointer;
  313. &:hover {
  314. background-color: #e6f7ff;
  315. color: #1890ff;
  316. }
  317. }
  318. .isAdded {
  319. color: #1890ff;
  320. cursor: not-allowed;
  321. }
  322. .isActive {
  323. background-color: #e6f7ff;
  324. color: #1890ff;
  325. }
  326. .camera-item-disabled {
  327. color: #c6c6c6;
  328. }
  329. .camera-id {
  330. width: 110px;
  331. }
  332. .space-name {
  333. width: 120px;
  334. white-space: nowrap;
  335. overflow: hidden;
  336. text-overflow: ellipsis;
  337. }
  338. .draw-container {
  339. position: relative;
  340. width: calc(100% - 300px);
  341. margin: 20px;
  342. overflow: hidden;
  343. }
  344. :deep(.search-put .el-input__wrapper) {
  345. background-color: #f0f2f5;
  346. }
  347. :deep(.el-popper__arrow) {
  348. display: none;
  349. }
  350. :deep(.el-tree-node__content:hover) {
  351. background: #e6f4ff;
  352. }
  353. :deep(.el-button--primary) {
  354. --el-button-disabled-bg-color: #bfbfbf;
  355. }
  356. :deep(.el-popover) {
  357. width: unset !important;
  358. min-width: 110px;
  359. text-align: center;
  360. font-weight: 400;
  361. }
  362. </style>