ConfigEdit.vue 16 KB

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