提交 30d539dc 作者: test

feat: 优化 HandlerUtil 方法命名和 Type 定义

上级 f8cca341
...@@ -941,6 +941,29 @@ export interface MapboxInstance { ...@@ -941,6 +941,29 @@ export interface MapboxInstance {
setLayoutProperty: (layerId: string, name: string, value: any) => void setLayoutProperty: (layerId: string, name: string, value: any) => void
} }
/**
* 站点数据类型定义
*/
export interface Station extends Recordable {
/**
* key 用于标识站点是什么类型的数据(例如:降水量、气温...)
*/
key: string
value: string | number
suffix?: string
stationCode?: string
stationName?: string
city: string
cnty: string
lon: string
lat: string
}
/**
* 判断是否为非空对象
* @param obj 对象
* @returns 是否为非空对象
*/
function isPlainObject(obj: any) { function isPlainObject(obj: any) {
if (typeof obj !== 'object' || obj === null) { if (typeof obj !== 'object' || obj === null) {
return false return false
...@@ -950,9 +973,12 @@ function isPlainObject(obj: any) { ...@@ -950,9 +973,12 @@ function isPlainObject(obj: any) {
return proto === null || proto === Object.prototype return proto === null || proto === Object.prototype
} }
/**
* 数据处理工具
*/
export const HandlerUtil = { export const HandlerUtil = {
isPlainObject, isPlainObject,
createDefaultGeoJSONFeatureData(station: Recordable) { createDefaultPointFeature(station: Station, map = {} as Recordable) {
return { return {
type: 'Feature', type: 'Feature',
geometry: { geometry: {
...@@ -962,18 +988,18 @@ export const HandlerUtil = { ...@@ -962,18 +988,18 @@ export const HandlerUtil = {
properties: { properties: {
key: station.key, key: station.key,
suffix: station.suffix, suffix: station.suffix,
value: Number(station.value), value: station.value,
popup: HandlerUtil.createDefaultStylePopupHtml({ '{key}': '{value}{suffix}' }, station), popup: HandlerUtil.createDefaultPopupHtml(station, { '{key}': '{value}{suffix}', ...map }),
}, },
} }
}, },
createDefaultGeoJSONSymbolSourceData(features: GeoJSON.Feature<GeoJSON.Geometry>[] = []) { createFeatureCollection(features: GeoJSON.Feature<GeoJSON.Geometry>[] = []) {
return { return {
type: 'FeatureCollection', type: 'FeatureCollection',
features: features || [], features: features || [],
} }
}, },
createDefaultStylePopupHtml(map: Recordable, station: Recordable) { createDefaultPopupHtml(station: Station, map: Recordable) {
const htmls = ['<div class="custom-popup">'] const htmls = ['<div class="custom-popup">']
if (isPlainObject(station)) { if (isPlainObject(station)) {
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
import { TimeBarWidget, formatTime, useTimeBarWidget } from '@/components/Map/Widgets/TimeBar' import { TimeBarWidget, formatTime, useTimeBarWidget } from '@/components/Map/Widgets/TimeBar'
import { ToolBoxWidget, useToolBoxWidget } from '@/components/Map/Widgets/ToolBox' import { ToolBoxWidget, useToolBoxWidget } from '@/components/Map/Widgets/ToolBox'
import { BottomBarWidget, useBottomBarWidget } from '@/components/Map/Widgets/BottomBar' import { BottomBarWidget, useBottomBarWidget } from '@/components/Map/Widgets/BottomBar'
import type { MapboxInstance } from '@/components/Map/Mapbox' import type { MapboxInstance, Station } from '@/components/Map/Mapbox'
import { HandlerUtil } from '@/components/Map/Mapbox' import { HandlerUtil } from '@/components/Map/Mapbox'
useShare() useShare()
...@@ -75,12 +75,12 @@ ...@@ -75,12 +75,12 @@
}), }),
// 此处使用 // 此处使用
(response) => { (response) => {
const features = response.body.liveVos.map(function (item) { const features = response.body.liveVos.map((item: Station) => {
item.key = '气温' item.key = '气温'
item.suffix = '°C' item.suffix = '°C'
return HandlerUtil.createDefaultGeoJSONFeatureData(item) return HandlerUtil.createDefaultPointFeature(item)
}) })
return HandlerUtil.createDefaultGeoJSONSymbolSourceData(features) return HandlerUtil.createFeatureCollection(features)
}, },
) )
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
import { TimeBarWidget, formatTime, useTimeBarWidget } from '@/components/Map/Widgets/TimeBar' import { TimeBarWidget, formatTime, useTimeBarWidget } from '@/components/Map/Widgets/TimeBar'
import { ToolBoxWidget, useToolBoxWidget } from '@/components/Map/Widgets/ToolBox' import { ToolBoxWidget, useToolBoxWidget } from '@/components/Map/Widgets/ToolBox'
import { BottomBarWidget, useBottomBarWidget } from '@/components/Map/Widgets/BottomBar' import { BottomBarWidget, useBottomBarWidget } from '@/components/Map/Widgets/BottomBar'
import type { MapboxInstance } from '@/components/Map/Mapbox' import type { MapboxInstance, Station } from '@/components/Map/Mapbox'
import { HandlerUtil } from '@/components/Map/Mapbox' import { HandlerUtil } from '@/components/Map/Mapbox'
import { useGlobSetting } from '/@/hooks/setting' import { useGlobSetting } from '/@/hooks/setting'
...@@ -124,12 +124,12 @@ ...@@ -124,12 +124,12 @@
}), }),
// 此处使用 // 此处使用
(response) => { (response) => {
const features = response.body.liveVos.map(function (item) { const features = response.body.liveVos.map((item: Station) => {
item.key = '风速' item.key = '风速'
item.suffix = 'm/s' item.suffix = 'm/s'
return HandlerUtil.createDefaultGeoJSONFeatureData(item) return HandlerUtil.createDefaultPointFeature(item)
}) })
return HandlerUtil.createDefaultGeoJSONSymbolSourceData(features) return HandlerUtil.createFeatureCollection(features)
}, },
) )
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论