Commit b3689ee166ed5b150c7226d3e4edc359e4cfc039
1 parent
b2f7584e
fix: baidu map dynamic import script slow
Showing
4 changed files
with
67 additions
and
5 deletions
| ... | ... | @@ -101,6 +101,7 @@ | 
| 101 | 101 | import { QueryWay, SchemaFiled, AggregateDataEnum } from './cpns/TimePeriodForm/config'; | 
| 102 | 102 | import { dateUtil } from '/@/utils/dateUtil'; | 
| 103 | 103 | import { Spin } from 'ant-design-vue'; | 
| 104 | + import { useAsyncQueue } from './useAsyncQueue'; | |
| 104 | 105 | |
| 105 | 106 | interface DeviceInfo { | 
| 106 | 107 | alarmStatus: 0 | 1; | 
| ... | ... | @@ -168,14 +169,30 @@ | 
| 168 | 169 | showSizeChanger: true, | 
| 169 | 170 | }, | 
| 170 | 171 | afterFetch: async (data: DeviceInfo[]) => { | 
| 172 | + if (!(window as any).BMap) { | |
| 173 | + clearTask(); | |
| 174 | + setTask(createMarket.bind(null, data)); | |
| 175 | + return data; | |
| 176 | + } | |
| 171 | 177 | createMarket(data); | 
| 172 | 178 | return data; | 
| 173 | 179 | }, | 
| 174 | 180 | }); | 
| 181 | + const { setTask, clearTask, executeFlag } = useAsyncQueue(); | |
| 182 | + | |
| 183 | + let interval: Nullable<NodeJS.Timer> = setInterval(() => { | |
| 184 | + if ((window as any).BMap) { | |
| 185 | + executeFlag.value = true; | |
| 186 | + clearInterval(interval!); | |
| 187 | + interval = null; | |
| 188 | + } | |
| 189 | + }, 1000); | |
| 175 | 190 | |
| 176 | 191 | async function createMarket(data: DeviceInfo[]) { | 
| 177 | 192 | const BMap = (window as any).BMap; | 
| 178 | - if (!BMap) return; | |
| 193 | + if (!BMap) { | |
| 194 | + return; | |
| 195 | + } | |
| 179 | 196 | unref(BMapInstance)?.clearOverlays(); | 
| 180 | 197 | const markerList: MarkerList[] = []; | 
| 181 | 198 | data.forEach((item) => { | 
| ... | ... | @@ -252,7 +269,6 @@ | 
| 252 | 269 | const { name, organizationDTO, deviceState, deviceProfile, deviceType } = record; | 
| 253 | 270 | const { address, longitude, latitude } = record.deviceInfo; | 
| 254 | 271 | |
| 255 | - console.log(record); | |
| 256 | 272 | // 创建信息窗口对象 | 
| 257 | 273 | const res = await getDeviceActiveTime(entityId); | 
| 258 | 274 | ... | ... | 
src/views/device/localtion/useAsyncQueue.ts
0 → 100644
| 1 | +import { ref, watchEffect } from 'vue'; | |
| 2 | + | |
| 3 | +export function useAsyncQueue() { | |
| 4 | + const queue: Fn<any, Promise<any>>[] = []; | |
| 5 | + | |
| 6 | + const executeFlag = ref(false); | |
| 7 | + | |
| 8 | + const setTask = (fn: Fn<any, Promise<any>>) => { | |
| 9 | + queue.push(fn); | |
| 10 | + }; | |
| 11 | + | |
| 12 | + const removeTask = (fn: Fn<any, Promise<any>>) => { | |
| 13 | + const index = queue.findIndex((item) => item === fn); | |
| 14 | + ~index && queue.splice(index, 1); | |
| 15 | + }; | |
| 16 | + | |
| 17 | + const clearTask = () => { | |
| 18 | + queue.length = 0; | |
| 19 | + }; | |
| 20 | + | |
| 21 | + watchEffect(() => { | |
| 22 | + if (executeFlag.value) { | |
| 23 | + queue.forEach((item) => item()); | |
| 24 | + executeFlag.value = false; | |
| 25 | + } | |
| 26 | + }); | |
| 27 | + | |
| 28 | + return { setTask, removeTask, clearTask, executeFlag }; | |
| 29 | +} | ... | ... | 
| 1 | 1 | <script lang="ts"> | 
| 2 | 2 | export default { | 
| 3 | + components: { Spin }, | |
| 3 | 4 | inheritAttrs: false, | 
| 4 | 5 | }; | 
| 5 | 6 | </script> | 
| ... | ... | @@ -12,7 +13,7 @@ | 
| 12 | 13 | PlayCircleOutlined, | 
| 13 | 14 | PauseCircleOutlined, | 
| 14 | 15 | } from '@ant-design/icons-vue'; | 
| 15 | - import { Button, Tooltip } from 'ant-design-vue'; | |
| 16 | + import { Button, Spin, Tooltip } from 'ant-design-vue'; | |
| 16 | 17 | import { FrontComponent } from '../../const/const'; | 
| 17 | 18 | import { buildUUID } from '/@/utils/uuid'; | 
| 18 | 19 | import { useModal } from '/@/components/Modal'; | 
| ... | ... | @@ -20,6 +21,7 @@ | 
| 20 | 21 | import { HistoryModalOkEmitParams, HistoryModalParams } from './type'; | 
| 21 | 22 | import { formatToDateTime } from '/@/utils/dateUtil'; | 
| 22 | 23 | import { isEqual } from 'lodash-es'; | 
| 24 | + import { useAsyncQueue } from '/@/views/device/localtion/useAsyncQueue'; | |
| 23 | 25 | |
| 24 | 26 | // useVisualBoardContext(); | 
| 25 | 27 | type TrackRecord = Record<'lng' | 'lat' | 'ts', number>; | 
| ... | ... | @@ -80,6 +82,7 @@ | 
| 80 | 82 | // unref(mapInstance)?.addOverlay(marker); | 
| 81 | 83 | // } | 
| 82 | 84 | |
| 85 | + const prepare = ref(false); | |
| 83 | 86 | async function initMap() { | 
| 84 | 87 | const wrapEl = unref(wrapRef); | 
| 85 | 88 | if (!wrapEl) return; | 
| ... | ... | @@ -154,7 +157,20 @@ | 
| 154 | 157 | setTimeout(`${startMethodName}()`); | 
| 155 | 158 | }; | 
| 156 | 159 | |
| 160 | + const { setTask, executeFlag } = useAsyncQueue(); | |
| 157 | 161 | onMounted(() => { | 
| 162 | + let interval: Nullable<NodeJS.Timer> = setInterval(() => { | |
| 163 | + if ((window as any).BMapGL) { | |
| 164 | + prepare.value = true; | |
| 165 | + executeFlag.value = true; | |
| 166 | + clearInterval(interval!); | |
| 167 | + interval = null; | |
| 168 | + } | |
| 169 | + }, 1000); | |
| 170 | + if (!(window as any).BMapGL) { | |
| 171 | + setTask(initMap); | |
| 172 | + return; | |
| 173 | + } | |
| 158 | 174 | initMap(); | 
| 159 | 175 | }); | 
| 160 | 176 | |
| ... | ... | @@ -225,6 +241,7 @@ | 
| 225 | 241 | </span> | 
| 226 | 242 | </Button> | 
| 227 | 243 | </div> | 
| 244 | + <Spin class="w-full h-full" :spinning="!prepare" tip="Loading..." /> | |
| 228 | 245 | <div ref="wrapRef" :id="wrapId" class="w-full h-full"></div> | 
| 229 | 246 | <HistoryDataModel @register="register" @ok="handleRenderHistroyData" /> | 
| 230 | 247 | </div> | ... | ... |