wujingjing
2025-01-15 0647fd26414fc8a3ab077d355f50da6e3e36ad63
src/components/chat/chatComponents/summaryCom/components/recordSetTable/map/Map.vue
@@ -1,73 +1,130 @@
<template>
   <div class="h-[60vh] relative">
            <div ref="containerRef" class="h-full"></div>
      <!-- <div v-if="bottomBarIsShow" class="absolute w-full bottom-0 bg-white border-gray-300 border border-solid">
      <!-- 原始地图容器 -->
      <div v-show="!isFullscreen" ref="containerRef" class="h-full"></div>
      <!-- 全屏按钮 -->
      <div class="absolute right-2 top-2 cursor-pointer" @click="toggleFullScreen">
         <el-tooltip content="全屏展开" placement="top">
            <div class="ywifont text-[20px] text-black rounded-lg ywicon-fullscreen"></div>
         </el-tooltip>
      </div>
      <div ref="infoWindowRef" v-show="infoWindowIsShow">
         <div
            class="w-28 h-5 absolute left-1/2 -translate-x-1/2 -translate-y-[100%] cursor-pointer bg-[#4974f3] rounded-t-lg flex-center"
            @click="toggleShowChart"
            class="bg-white rounded-md text-nowrap flex flex-col w-fit border border-solid border-blue-600 relative z-[1]"
            style="padding: 12px"
         >
            <div
               class="ywifont -rotate-90 text-white !text-[13px]"
               :class="{ 'ywicon-zuoyoujiantou': chartIsShow, 'ywicon-zuoyoujiantou1': !chartIsShow }"
            ></div>
            <span
               @click="closeInfoWindow"
               class="guanbi absolute ywifont ywicon-guanbi right-[4px] top-[-2px] font-bold text-[#c3c3c3] cursor-pointer"
            ></span>
            <div class="space-x-4 flex w-full">
               <div class="flex flex-col w-1/2 key-list">
                  <span
                     v-for="(col, index) in colsArray"
                     :key="index"
                     class="flex-items-center justify-start text-gray-600"
                     style="height: 20px"
                     >{{ col.title ?? '' }}</span
                  >
               </div>
               <div class="flex flex-col w-1/2 value-list" v-if="infoWindowMapRow">
                  <span
                     v-for="(col, index) in colsArray"
                     :key="index"
                     class="flex-items-center justify-end text-black"
                     style="height: 20px"
                     >{{ infoWindowMapRow[index] ?? '' }}</span
                  >
               </div>
            </div>
         </div>
      </div> -->
      </div>
      <!-- Teleport 全屏地图 -->
      <Teleport to=".layout-parent">
         <Transition name="fullscreen">
            <div v-if="isFullscreen" class="absolute inset-0 z-50 w-full h-full">
               <div ref="fullscreenContainerRef" class="w-full h-full"></div>
               <div class="absolute right-2 top-2 cursor-pointer" @click="toggleFullScreen">
                  <el-tooltip content="退出全屏(Esc)" placement="top">
                     <div class="ywifont text-[20px] text-black rounded-lg ywicon-tuichuquanping"></div>
                  </el-tooltip>
               </div>
               <div class="absolute bottom-0 w-full">
                  <EquipCurve
                     v-model:isShow="chartDlgIsShow"
                     class="opacity-90"
                     :data="equipCurveMapRow"
                     :quotaChartCol="data?.quota_chart?.col"
                     height="15rem"
                     :tableHeight="240"
                  />
               </div>
            </div>
         </Transition>
      </Teleport>
   </div>
</template>
<script setup lang="ts">
import { onMounted, ref } from 'vue';
import 'ol/ol.css';
import { computed, nextTick, onDeactivated, onMounted, onUnmounted, ref } from 'vue';
import equipSelectPic from './img/equip-select.svg';
import equipPic from './img/equip.svg';
import type { GaoDePosition, LabelMarkerData } from '/@/model/map/GaoDeMap';
import { GaoDeMap } from '/@/model/map/GaoDeMap';
import { OLMap } from '/@/model/map/OLMap';
import type Overlay from 'ol/Overlay';
import EquipCurve from '../components/EquipCurve.vue';
import { GaoDeMap } from '/@/model/map/GaoDeMap';
let gaoDeMap = new GaoDeMap();
const containerRef = ref<HTMLDivElement>(null);
const fullscreenContainerRef = ref<HTMLDivElement>(null);
const isFullscreen = ref(false);
const props = defineProps(['data']);
const emit = defineEmits(['equipClick','closeInfoWindow']);
const createInfoWindow = () => {
   // title
   // <div class="title flex-center bg-[#ca0dab] text-white py-0.5 mb-2 w-full over-ellipsis">
   //          罗汉鱼
   //       </div>
   const dom = `<div class="rounded-md text-nowrap flex flex-col  w-fit border border-solid border-blue-600 " style="padding: 12px">
         <div class="space-x-4 flex w-full">
            <div class="flex flex-col w-1/2 key-list">
            </div>
const emit = defineEmits(['equipClick', 'closeInfoWindow']);
            <div class="flex flex-col w-1/2 value-list">
            </div>
         </div>
      </div>`;
const colsArray = computed(() => {
   return props.data.cols ?? [];
});
   return dom;
const infoWindowRef = ref<HTMLDivElement>(null);
const infoWindowIsShow = ref(false);
const infoWindowMapRow = ref(null);
let olMap: OLMap;
let infoWindowOverlay: Overlay;
let lastOverlay: Overlay;
const setMarkerIcon = (overlay, icon: string) => {
   if (!overlay) return;
   const ele = overlay.getElement() as HTMLImageElement;
   if (!ele) return;
   ele.src = icon;
};
const updateInfoWindow = (item) => {
   const cellHeight = '20px';
   const keyListDom = infoWindow.dom.querySelector('.key-list');
   const valueListDom = infoWindow.dom.querySelector('.value-list');
   const colsArray = props.data.cols ?? [];
   const keyItemHtml = colsArray
      .map((col) => {
         return `<span class="flex-items-center justify-start text-gray-600" style="height:${cellHeight}">${col.title ?? ''}</span>`;
      })
      .join('');
   const valueItemHtml = colsArray
      .map((col, index) => {
         return `<span class="flex-items-center justify-end text-black" style="height:${cellHeight}">${item[index] ?? ''}</span>`;
      })
      .join('');
   keyListDom.innerHTML = keyItemHtml;
   valueListDom.innerHTML = valueItemHtml;
const showInfoWindow = (overlay, row, position) => {
   lastOverlay && setMarkerIcon(lastOverlay, equipPic);
   if (isFullscreen.value) {
      showCurve(row);
   } else {
      emit('equipClick', row);
   }
   infoWindowIsShow.value = true;
   infoWindowOverlay.setPosition(position);
   infoWindowMapRow.value = row;
   setMarkerIcon(overlay, equipSelectPic);
   lastOverlay = overlay;
};
const closeInfoWindow = () => {
   if (isFullscreen.value) {
      closeChartDlg();
   } else {
      emit('closeInfoWindow');
   }
   infoWindowIsShow.value = false;
   infoWindowMapRow.value = null;
   setMarkerIcon(lastOverlay, equipPic);
};
const addMarkerLayer = () => {
@@ -85,23 +142,17 @@
         title: item.title,
      };
   });
   gaoDeMap.addMarkerLayer(dataList, {
   olMap.addMarkerLayer(dataList, {
      markerOpt: {
         icon: {
            url: equipPic,
            size: 30,
            selectUrl: equipSelectPic,
         },
         click(e, label) {
            // if (!bottomBarIsShow.value) {
            //    bottomBarIsShow.value = true;
            // }
            // if (!chartIsShow.value) {
            //    chartIsShow.value = true;
            // }
            infoWindow.open(gaoDeMap.map, label.getPosition() as any);
            const extData = label.getExtData();
            emit('equipClick', extData);
            updateInfoWindow(extData);
         click(e, label, item, position) {
            const extData = item.extData;
            showInfoWindow(label, extData, position);
         },
      },
      layerOpt: {
@@ -110,45 +161,83 @@
   });
};
const bottomBarIsShow = ref(false);
const chartIsShow = ref(true);
const toggleShowChart = () => {
   chartIsShow.value = !chartIsShow.value;
// 切换全屏
const toggleFullScreen = async () => {
   isFullscreen.value = !isFullscreen.value;
   // 等待 DOM 更新完成
   await nextTick();
   // 重新初始化地图
   if (isFullscreen.value) {
      closeInfoWindow();
      await initMap(fullscreenContainerRef.value);
   } else {
      // await initMap(containerRef.value);
   }
};
let infoWindow: AMap.InfoWindow;
// 初始化地图
const initMap = async (container: HTMLDivElement) => {
   olMap = new OLMap({
      container,
   });
   addMarkerLayer();
   infoWindowOverlay = olMap.createEleOverlay(infoWindowRef.value);
   olMap.map.addOverlay(infoWindowOverlay);
};
//#region ====================== 设备曲线 ======================
const chartDlgIsShow = ref(false);
const equipCurveMapRow = ref(null);
const showCurve = (row) => {
   equipCurveMapRow.value = row;
   chartDlgIsShow.value = true;
};
const closeChartDlg = () => {
   chartDlgIsShow.value = false;
   equipCurveMapRow.value = null;
};
//#endregion
// 添加 ESC 键监听函数
const handleEscKey = (event: KeyboardEvent) => {
   if (event.key === 'Escape' && isFullscreen.value) {
      toggleFullScreen();
   }
};
onMounted(async () => {
   await gaoDeMap.init({
      container: containerRef.value,
      aMapOption: {
         resizeEnable: true,
      },
   });
   // const southWest: GaoDePosition = [props.data.minx, props.data.miny];
   // const northEast: GaoDePosition = [props.data.maxx, props.data.maxy];
   // gaoDeMap.zoomToRect(southWest, northEast);
   gaoDeMap.applyBasicPlugins();
   initMap(containerRef.value);
   // 添加 ESC 键监听
   document.addEventListener('keydown', handleEscKey);
});
   addMarkerLayer();
   gaoDeMap.map.setFitView();
onDeactivated(async () => {
   if (isFullscreen.value) {
      await toggleFullScreen();
   }
});
   infoWindow = new AMap.InfoWindow({
      content: createInfoWindow(),
      // offset: [3, -34],
      anchor: 'top-left',
      closeWhenClickMap: true,
   });
   const closeBtnDom = infoWindow.dom.querySelector('.amap-info-close');
   closeBtnDom.addEventListener('click', () => {
      emit('closeInfoWindow');
   });
// 在组件卸载时移除监听
onUnmounted(() => {
   // 移除 ESC 键监听
   document.removeEventListener('keydown', handleEscKey);
});
</script>
<style scoped lang="scss">
:deep(.amap-info-content) {
   padding: 0;
}
// 全屏过渡动画
.fullscreen-enter-active,
.fullscreen-leave-active {
   transition: all 0.3s ease;
}
.fullscreen-enter-from,
.fullscreen-leave-to {
   opacity: 0;
   transform: scale(0.95);
}
</style>