ConfigEdit.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. <template>
  2. <div class="page">
  3. <div class="page-head flex items-center">
  4. <div class="head-opt flex-1 flex justify-between items-center" style="margin-left: 0px">
  5. <div>
  6. <!-- <span>场景:</span> -->
  7. </div>
  8. <div class="top-bar">
  9. <div class="back-and-company">
  10. <div class="back" @click="handelClickBack">
  11. <img src="@/assets/rollback.png" />
  12. <span>返回</span>
  13. </div>
  14. <div class="company-name">{{ companyName }}</div>
  15. </div>
  16. <div class="operation-btns">
  17. <el-upload
  18. class="avatar-uploader"
  19. :action="actionUrl"
  20. :show-file-list="false"
  21. :on-success="handleAvatarSuccess"
  22. :before-upload="handleBeforeUpload"
  23. :with-credentials="true"
  24. name="file"
  25. :data="{ companyId, deleteFileName: bgImg }"
  26. :headers="getHeaders()"
  27. >
  28. <el-button :icon="Refresh" :disabled="!hasBg"> 替换照片 </el-button>
  29. </el-upload>
  30. <el-button :icon="Refresh" :disabled="!hasBg" @click="clearLayout">
  31. 重置布局
  32. </el-button>
  33. <el-button @click="handleSave" type="primary" :disabled="!companyId"> 保存 </el-button>
  34. </div>
  35. </div>
  36. </div>
  37. </div>
  38. <div class="paint-tool">
  39. <div class="camera-list">
  40. <div>
  41. <span class="label-text flex">车间列表:</span>
  42. <ElInput
  43. class="search-put"
  44. style="margin: 10px 0; width: 230px"
  45. placeholder="请输入搜索内容"
  46. v-model="searchKey"
  47. :suffix-icon="Search"
  48. />
  49. </div>
  50. <el-scrollbar style="position: relative; height: calc(100% - 90px)">
  51. <div
  52. v-for="item in filterShopList"
  53. :key="item.code"
  54. class="camera-item flex justify-start items-center"
  55. :class="{
  56. isAdded: isAddedShop(item.id),
  57. isActive: item.id === Number(activeShopId),
  58. }"
  59. @click="handleAddShop(item)"
  60. >
  61. <span class="camera-id">{{ item.name }}</span>
  62. </div>
  63. </el-scrollbar>
  64. </div>
  65. <div ref="drawContainer" id="drawContainer" class="draw-container">
  66. <div id="shopEditContainer" v-moveable:1 class="shop-edit-container">
  67. <MapContainer ref="mapContainerRef" :is-mobile-view="isMobileView" />
  68. </div>
  69. <el-upload
  70. v-if="!hasBg"
  71. class="upload-icon flex justify-center items-center"
  72. :action="actionUrl"
  73. :show-file-list="false"
  74. :before-upload="handleBeforeUpload"
  75. :on-success="handleAvatarSuccess"
  76. :with-credentials="true"
  77. name="file"
  78. :data="{ companyId }"
  79. :headers="getHeaders()"
  80. >
  81. <img src="~@/assets/images/img-upload.png" />
  82. <div class="upload-tips-text">
  83. 请上传1920*1080尺寸的布局背景图,其他尺寸会影响布局准确性!
  84. </div>
  85. </el-upload>
  86. </div>
  87. <div class="shop-tag-edit-area" v-if="hasBg">
  88. <ShopTagEditArea @transformer-change="transformChange" />
  89. </div>
  90. </div>
  91. <ConfigFinish
  92. :visible="visibleResult"
  93. :status="configStatus"
  94. @on-close="closeResult"
  95. class="feedback-position"
  96. />
  97. </div>
  98. </template>
  99. <script setup lang="ts">
  100. import ConfigFinish from './component/ConfigFinish.vue';
  101. import { storeToRefs } from 'pinia';
  102. import { ElMessage, ElInput, ElMessageBox } from 'element-plus';
  103. import { onBeforeUnmount, onMounted, reactive, ref, watch } from 'vue';
  104. import { getWorkshopListApi } from '@/api/scene/scene';
  105. import { computed } from 'vue';
  106. import { Search, Refresh, ArrowLeftBold } from '@element-plus/icons-vue';
  107. import usePageConfig from './usePageConfig';
  108. import MapContainer from './component/mapContainer/MapContainer.vue';
  109. import useMapEditor, { LabelPositionEnum } from './stores/useMapEditor';
  110. import {
  111. uploadCompanyLayout,
  112. updateCompanyLayout,
  113. getCompanyLayoutApi,
  114. getMobileCompanyLayoutApi,
  115. } from '@/api/scene/scene';
  116. import safeParse from '@/utils/safeParse';
  117. import { useRouter } from 'vue-router';
  118. import urlJoin from 'url-join';
  119. import { useGlobSetting } from '@/hooks/setting';
  120. import { getHeaders } from '@/utils/http/axios';
  121. import { ShopType } from '@/types/page-config/type';
  122. import ShopTagEditArea from './component/ShopTagEditArea.vue';
  123. import { ViewType } from '@/types/page-config/type';
  124. const mapEditor = useMapEditor();
  125. const { bgImg, addedShops, activeShopId, showShops, mapHeight, mapWidth } =
  126. storeToRefs(mapEditor);
  127. const { addShop, addBg, calcLayout, resetMap, createMap, deleteShop } = mapEditor;
  128. const router = useRouter();
  129. const pageConfig = usePageConfig();
  130. const { label, layoutId } = pageConfig;
  131. const { urlPrefix } = useGlobSetting();
  132. const drawContainer = ref<HTMLDivElement>();
  133. const mapContainerRef = ref();
  134. const searchKey = ref('');
  135. // 是否已有背景图
  136. const hasBg = ref(false);
  137. // 是否开启移动端视图
  138. const isMobileView = ref(false);
  139. const companyId = ref<number>();
  140. const companyName = ref<string>();
  141. const viewType = ref<number>();
  142. const shopList = ref<ShopType[]>([]); // 车间列表
  143. // mapContainer宽高
  144. const mapContainerStageSize = reactive({ height: 0, width: 0 });
  145. // // mapContainer缩放比例
  146. const scale = computed(() =>
  147. Math.min(
  148. mapContainerStageSize.width / mapWidth.value,
  149. mapContainerStageSize.height / mapHeight.value,
  150. ),
  151. );
  152. const defaultLayoutString = ref(''); // 默认布局的string,用于点击返回时判断是否未保存布局就离开
  153. const handleBeforeUpload = (rawFile) => {
  154. if (
  155. rawFile.type !== 'image/jpg' &&
  156. rawFile.type !== 'image/jpeg' &&
  157. rawFile.type !== 'image/png'
  158. ) {
  159. ElMessage.error('请上传jpg、jpeg、png格式的图片!');
  160. return false;
  161. }
  162. };
  163. /** 判断相机是否已经添加 */
  164. const isAddedShop = (shopId: number) => {
  165. const index = addedShops.value.findIndex((item) => item.id === shopId);
  166. return index >= 0;
  167. };
  168. const transformChange = () => {
  169. mapContainerRef.value.updateLayoutTransformer();
  170. };
  171. const actionUrl = computed(() => {
  172. return urlJoin(urlPrefix, '/admin/homepageConfig/uploadCompanyPicture');
  173. });
  174. watch(
  175. () => showShops,
  176. () => {
  177. transformChange();
  178. },
  179. { deep: true },
  180. );
  181. //总体的保存,将整个数据传过去
  182. const visibleResult = ref(false);
  183. const configStatus = ref(true);
  184. const closeResult = () => {
  185. visibleResult.value = false;
  186. };
  187. /** ------------- */
  188. const handleAvatarSuccess = (e) => {
  189. if (e.code !== 200) {
  190. ElMessage.error(e.data);
  191. return;
  192. }
  193. bgImg.value = e.data;
  194. hasBg.value = true;
  195. addBg();
  196. };
  197. const getShopContent = () => {
  198. const companyLayoutApiMap = {
  199. [ViewType.companyHomepage_PC]: getCompanyLayoutApi,
  200. [ViewType.companyHomepage_phone]: getMobileCompanyLayoutApi,
  201. };
  202. getWorkshopListApi({ companyId: companyId.value! }).then((res) => {
  203. shopList.value = res;
  204. });
  205. companyLayoutApiMap[viewType.value!]({
  206. companyId: companyId.value!,
  207. }).then((res) => {
  208. if (!res) {
  209. layoutId.value = undefined;
  210. return;
  211. }
  212. layoutId.value = res.id;
  213. const layoutJSON = res.layout ? safeParse(res.layout) : null;
  214. if (!layoutJSON) {
  215. return;
  216. }
  217. hasBg.value = true;
  218. createMap(layoutJSON);
  219. defaultLayoutString.value = res.layout;
  220. });
  221. };
  222. const clearLayout = () => {
  223. ElMessageBox.confirm('是否重置当前设置?', '提示', {
  224. confirmButtonText: '确认',
  225. cancelButtonText: '取消',
  226. type: 'warning',
  227. }).then(() => {
  228. resetMap();
  229. hasBg.value = false;
  230. });
  231. };
  232. const handleKeyDown = (e) => {
  233. // 删除键
  234. if (e.keyCode === 46 || e.code === 'Delete') {
  235. deleteShop();
  236. }
  237. };
  238. const filterShopList = computed(() => {
  239. const k = searchKey.value.trim();
  240. if (!k) return shopList.value;
  241. return shopList.value?.filter((x) => x.name?.includes(k));
  242. });
  243. const setNodePosition = (positionX = 50, positionY = 50) => {
  244. if (addedShops.value.find((item) => item.x === positionX && item.y === positionY)) {
  245. return setNodePosition(positionX, positionY + 50);
  246. } else return { positionX, positionY };
  247. };
  248. const handleAddShop = (shop: ShopType) => {
  249. //如果已经存在车间,则禁止加入
  250. const existingCamera = addedShops.value.find((item) => item.id === shop.id);
  251. if (existingCamera) return;
  252. if (!hasBg.value) {
  253. ElMessage.warning({
  254. message: '请先添加背景图片',
  255. });
  256. return;
  257. }
  258. const nodePosition = setNodePosition();
  259. const shopNode = {
  260. ...shop,
  261. x: nodePosition.positionX,
  262. y: nodePosition.positionY,
  263. scaleX: 1,
  264. scaleY: 1,
  265. bgColor: 'rgba(58, 170, 209, 1)',
  266. fontSize: 14,
  267. fontColor: '#ffffff',
  268. posType: LabelPositionEnum.LEFT,
  269. };
  270. addShop(shopNode);
  271. };
  272. const handleSave = () => {
  273. const { json } = mapContainerRef.value?.getLayout();
  274. const layout = hasBg.value === false ? '' : calcLayout(json);
  275. const param = {
  276. layout,
  277. targetId: companyId.value || 2,
  278. labelId: label.value || 1,
  279. };
  280. if (!layoutId.value) {
  281. uploadCompanyLayout(param).then((res) => {
  282. layoutId.value = res;
  283. ElMessage.success('保存成功');
  284. });
  285. } else {
  286. updateCompanyLayout({ ...param, id: layoutId.value, viewType: viewType.value! }).then(
  287. (_res) => {
  288. ElMessage.success('更新成功');
  289. },
  290. );
  291. }
  292. defaultLayoutString.value = layout;
  293. };
  294. function handelClickBack() {
  295. const json = calcLayout(mapContainerRef.value?.getLayout().json);
  296. if (json !== defaultLayoutString.value) {
  297. ElMessageBox.confirm('未保存当前设置,是否离开?', '提示', {
  298. confirmButtonText: '确认',
  299. cancelButtonText: '取消',
  300. type: 'warning',
  301. }).then(() => {
  302. router.back();
  303. });
  304. } else router.back();
  305. }
  306. watch(
  307. () => mapHeight.value,
  308. () => {
  309. mapContainerStageSize.height = document.getElementById('drawContainer')?.clientHeight!;
  310. mapContainerStageSize.width = document.getElementById('drawContainer')?.clientWidth!;
  311. },
  312. );
  313. onBeforeUnmount(() => {
  314. window.removeEventListener('keydown', handleKeyDown);
  315. window.onresize = null;
  316. resetMap();
  317. });
  318. onMounted(() => {
  319. const routerParams = router.currentRoute.value.query;
  320. if (routerParams.companyId) {
  321. companyId.value = Number(routerParams.companyId);
  322. companyName.value = String(routerParams.companyName);
  323. viewType.value = Number(routerParams.viewType);
  324. }
  325. window.addEventListener('keydown', handleKeyDown);
  326. window.onresize = () => {
  327. mapContainerStageSize.height = document.getElementById('drawContainer')?.clientHeight!;
  328. mapContainerStageSize.width = document.getElementById('drawContainer')?.clientWidth!;
  329. };
  330. if (companyId.value) {
  331. getShopContent();
  332. }
  333. });
  334. </script>
  335. <style scoped lang="scss">
  336. .page {
  337. }
  338. .page-head {
  339. height: 54px;
  340. padding-left: 15px;
  341. background-color: #ffffff;
  342. }
  343. .head-opt {
  344. margin-left: 20px;
  345. padding-right: 15px;
  346. font-size: 14px;
  347. color: #3f3f3f;
  348. }
  349. .avatar-uploader {
  350. border-radius: 4px;
  351. margin-left: 30px;
  352. }
  353. .label-workshop {
  354. font-size: 14px;
  355. font-weight: 400;
  356. margin-left: 36px;
  357. margin-top: 6px;
  358. margin-right: 16px;
  359. }
  360. .upload-icon {
  361. position: absolute;
  362. top: 0;
  363. right: 0;
  364. left: 0;
  365. bottom: 0;
  366. margin: auto;
  367. }
  368. .upload-tips-text {
  369. margin-top: 15px;
  370. }
  371. .paint-tool {
  372. position: relative;
  373. height: calc(100vh - 138px);
  374. margin-top: 2px;
  375. display: flex;
  376. justify-content: space-between;
  377. }
  378. .camera-list {
  379. width: 250px;
  380. padding: 0 10px;
  381. background-color: #ffffff;
  382. }
  383. .label-text {
  384. font-size: 14px;
  385. font-weight: 600;
  386. margin: 10px 0 5px 10px;
  387. }
  388. .camera-item {
  389. height: 32px;
  390. font-size: 14px;
  391. padding-left: 8px;
  392. font-weight: 400;
  393. color: #404040;
  394. line-height: 14px;
  395. cursor: pointer;
  396. &:hover {
  397. background-color: #e6f7ff;
  398. color: #1890ff;
  399. }
  400. }
  401. .isAdded {
  402. color: #1890ff;
  403. cursor: not-allowed;
  404. }
  405. .isActive {
  406. background-color: #e6f7ff;
  407. color: #1890ff;
  408. }
  409. .camera-item-disabled {
  410. color: #c6c6c6;
  411. }
  412. .camera-id {
  413. width: 110px;
  414. }
  415. .space-name {
  416. width: 120px;
  417. white-space: nowrap;
  418. overflow: hidden;
  419. text-overflow: ellipsis;
  420. }
  421. .draw-container {
  422. height: calc(100vh - 160px);
  423. position: relative;
  424. margin: 10px;
  425. overflow: hidden;
  426. display: flex;
  427. justify-content: center;
  428. align-items: center;
  429. }
  430. .shop-edit-container {
  431. scale: v-bind(scale);
  432. }
  433. .drawer-position {
  434. position: absolute;
  435. right: 0px;
  436. // left: 1150px;
  437. top: 74px;
  438. z-index: 99;
  439. opacity: 1;
  440. background: #ffffff;
  441. }
  442. .dialog-btn {
  443. position: absolute;
  444. right: 0px;
  445. top: 66px;
  446. }
  447. .position-tooltip {
  448. margin-right: -10px;
  449. }
  450. .circle-rectangle {
  451. width: 40px;
  452. height: 30px;
  453. border-radius: 50% 0% 0% 50%;
  454. position: absolute;
  455. right: 0px;
  456. top: 66px;
  457. background-color: white;
  458. display: flex;
  459. }
  460. .shape-shadow {
  461. filter: drop-shadow(5px 5px 10px rgb(102, 100, 100));
  462. }
  463. .left-icon {
  464. margin-top: 7px;
  465. margin-left: 8px;
  466. margin-right: -7px;
  467. }
  468. .feedback-position {
  469. position: absolute;
  470. left: 760px;
  471. top: 250px;
  472. z-index: 9999;
  473. }
  474. .top-bar {
  475. width: 100%;
  476. display: flex;
  477. justify-content: space-between;
  478. align-items: center;
  479. .back-and-company {
  480. display: flex;
  481. align-items: center;
  482. gap: 10px;
  483. .back {
  484. display: flex;
  485. align-items: center;
  486. gap: 10px;
  487. cursor: pointer;
  488. img {
  489. width: 14px;
  490. height: 14px;
  491. }
  492. span {
  493. line-height: 14px;
  494. }
  495. }
  496. .company-name {
  497. font-weight: 600;
  498. font-size: 14px;
  499. color: rgba(0, 0, 0, 0.85);
  500. }
  501. }
  502. .operation-btns {
  503. width: 380px;
  504. display: flex;
  505. justify-content: space-around;
  506. align-items: center;
  507. .el-button {
  508. // margin-left: 20px;
  509. }
  510. }
  511. }
  512. .shop-tag-edit-area {
  513. width: 300px;
  514. background-color: #ffffff;
  515. }
  516. :deep(.el-button) {
  517. margin: 0px;
  518. }
  519. :deep(.el-upload) {
  520. flex-direction: column;
  521. }
  522. :deep(.search-put .el-input__wrapper) {
  523. background-color: #f0f2f5;
  524. }
  525. :deep(.el-popper__arrow) {
  526. display: none;
  527. }
  528. :deep(.el-tree-node__content:hover) {
  529. background: #e6f4ff;
  530. }
  531. :deep(.el-button--primary) {
  532. --el-button-disabled-bg-color: #bfbfbf;
  533. }
  534. :deep(.el-popover) {
  535. width: unset !important;
  536. min-width: 110px;
  537. text-align: center;
  538. font-weight: 400;
  539. }
  540. ::v-deep.el-radio-button {
  541. margin-right: 8px;
  542. box-shadow: none;
  543. border-radius: 4px !important;
  544. border: 1px solid #d9d9d9 !important;
  545. }
  546. </style>
  547. ./MapBase/useCameraMap ./MapBase/CameraMapBak ./usePageConfig1