my-openlayer 0.1.17 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1114 -213
- package/dist/MyOl.d.ts +99 -31
- package/dist/MyOl.js +338 -106
- package/dist/core/ConfigManager.d.ts +113 -0
- package/dist/core/ConfigManager.js +164 -0
- package/dist/core/DomPoint.d.ts +163 -14
- package/dist/core/DomPoint.js +401 -26
- package/dist/core/EventManager.d.ts +131 -0
- package/dist/core/EventManager.js +257 -0
- package/dist/core/Line.d.ts +20 -4
- package/dist/core/Line.js +36 -1
- package/dist/core/MapBaseLayers.d.ts +187 -19
- package/dist/core/MapBaseLayers.js +460 -122
- package/dist/core/MapTools.d.ts +77 -7
- package/dist/core/MapTools.js +267 -65
- package/dist/core/MeasureHandler.d.ts +13 -6
- package/dist/core/MeasureHandler.js +46 -25
- package/dist/core/Point.d.ts +13 -5
- package/dist/core/Point.js +94 -39
- package/dist/core/Polygon.d.ts +74 -22
- package/dist/core/Polygon.js +306 -124
- package/dist/index.d.ts +17 -10
- package/dist/index.js +15 -10
- package/dist/types.d.ts +280 -95
- package/dist/types.js +11 -1
- package/dist/utils/ErrorHandler.d.ts +102 -0
- package/dist/utils/ErrorHandler.js +191 -0
- package/package.json +5 -6
package/dist/types.d.ts
CHANGED
|
@@ -1,95 +1,280 @@
|
|
|
1
|
-
import BaseLayer from "ol/layer/Base";
|
|
2
|
-
export interface Feature {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}
|
|
10
|
-
export interface MapJSONData {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
export interface MapInitType {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
export
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
1
|
+
import BaseLayer from "ol/layer/Base";
|
|
2
|
+
export interface Feature {
|
|
3
|
+
type: string;
|
|
4
|
+
properties: any;
|
|
5
|
+
geometry: {
|
|
6
|
+
type: "Polygon" | "MultiPolygon" | "Point" | "LineString" | "MultiLineString" | "MultiPoint" | "GeometryCollection";
|
|
7
|
+
coordinates: any[];
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export interface MapJSONData {
|
|
11
|
+
type: string,
|
|
12
|
+
name?: string,
|
|
13
|
+
features: Feature[]
|
|
14
|
+
}
|
|
15
|
+
export interface MapInitType {
|
|
16
|
+
layers?: BaseLayer[] | { [key: string]: BaseLayer[] },
|
|
17
|
+
zoom?: number,
|
|
18
|
+
center?: number[],
|
|
19
|
+
view?: any,
|
|
20
|
+
minZoom?: number,
|
|
21
|
+
maxZoom?: number,
|
|
22
|
+
extent?: number[],
|
|
23
|
+
mapClipData?: MapJSONData,
|
|
24
|
+
token?: string,
|
|
25
|
+
annotation?: boolean //注记
|
|
26
|
+
}
|
|
27
|
+
export type AnnotationType = 'cva_c' | 'cia_c' | 'cta_c'
|
|
28
|
+
export type TiandituType = 'vec_c' | 'img_c' | 'ter_c' | string
|
|
29
|
+
export interface MapLayers {
|
|
30
|
+
vec_c?: BaseLayer[];
|
|
31
|
+
img_c?: BaseLayer[];
|
|
32
|
+
ter_c?: BaseLayer[];
|
|
33
|
+
[key: string]: BaseLayer[] | undefined;
|
|
34
|
+
}
|
|
35
|
+
export interface MapLayersOptions {
|
|
36
|
+
layers?: BaseLayer[] | MapLayers;
|
|
37
|
+
zIndex?: number;
|
|
38
|
+
mapClip?: boolean;
|
|
39
|
+
mapClipData?: MapJSONData;
|
|
40
|
+
token?: string;
|
|
41
|
+
annotation?: boolean;
|
|
42
|
+
}
|
|
43
|
+
export interface AnnotationLayerOptions {
|
|
44
|
+
type: AnnotationType,
|
|
45
|
+
token: string,
|
|
46
|
+
zIndex?: number,
|
|
47
|
+
visible?: boolean
|
|
48
|
+
}
|
|
49
|
+
export interface HeatMapOptions {
|
|
50
|
+
layerName?: string,
|
|
51
|
+
radius?: number,
|
|
52
|
+
blur?: number,
|
|
53
|
+
gradient?: string[],
|
|
54
|
+
opacity?: number,
|
|
55
|
+
visible?: boolean,
|
|
56
|
+
zIndex?: number,
|
|
57
|
+
valueKey?: string,
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* 基础选项接口 - 所有图层的公共配置
|
|
61
|
+
*/
|
|
62
|
+
export interface BaseOptions {
|
|
63
|
+
/** 图层名称 */
|
|
64
|
+
layerName?: string;
|
|
65
|
+
/** 图层层级 */
|
|
66
|
+
zIndex?: number;
|
|
67
|
+
/** 图层可见性 */
|
|
68
|
+
visible?: boolean;
|
|
69
|
+
/** 图层透明度 */
|
|
70
|
+
opacity?: number;
|
|
71
|
+
/** 是否适应视图 */
|
|
72
|
+
fitView?: boolean;
|
|
73
|
+
/** 地图裁剪 */
|
|
74
|
+
mapClip?: boolean;
|
|
75
|
+
/** 地图裁剪数据 */
|
|
76
|
+
mapClipData?: MapJSONData;
|
|
77
|
+
/** 投影选项 */
|
|
78
|
+
projectionOptOptions?: any;
|
|
79
|
+
/** 扩展属性 */
|
|
80
|
+
[propName: string]: any;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* 样式选项接口 - 图形样式相关配置
|
|
84
|
+
*/
|
|
85
|
+
export interface StyleOptions {
|
|
86
|
+
/** 描边颜色 */
|
|
87
|
+
strokeColor?: string | number[];
|
|
88
|
+
/** 描边宽度 */
|
|
89
|
+
strokeWidth?: number;
|
|
90
|
+
/** 线条虚线样式 */
|
|
91
|
+
lineDash?: number[];
|
|
92
|
+
/** 线条虚线偏移 */
|
|
93
|
+
lineDashOffset?: number;
|
|
94
|
+
/** 填充颜色 */
|
|
95
|
+
fillColor?: string;
|
|
96
|
+
/** 填充颜色回调函数 */
|
|
97
|
+
fillColorCallBack?: (feature: any) => string;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* 文本选项接口 - 文本标注相关配置
|
|
101
|
+
*/
|
|
102
|
+
export interface TextOptions {
|
|
103
|
+
/** 文本可见性 */
|
|
104
|
+
textVisible?: boolean;
|
|
105
|
+
/** 文本内容回调函数 */
|
|
106
|
+
textCallBack?: (feature: any) => string;
|
|
107
|
+
/** 文本字体 */
|
|
108
|
+
textFont?: string;
|
|
109
|
+
/** 文本填充颜色 */
|
|
110
|
+
textFillColor?: string;
|
|
111
|
+
/** 文本描边颜色 */
|
|
112
|
+
textStrokeColor?: string;
|
|
113
|
+
/** 文本描边宽度 */
|
|
114
|
+
textStrokeWidth?: number;
|
|
115
|
+
/** 文本Y轴偏移 */
|
|
116
|
+
textOffsetY?: number;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* 点位选项接口 - 点位图层专用配置
|
|
120
|
+
*/
|
|
121
|
+
export interface PointOptions extends BaseOptions, StyleOptions, TextOptions {
|
|
122
|
+
/** 名称字段键 */
|
|
123
|
+
nameKey?: string;
|
|
124
|
+
/** 图标图片 */
|
|
125
|
+
img?: string;
|
|
126
|
+
/** 图标缩放比例 */
|
|
127
|
+
scale?: number;
|
|
128
|
+
/** 是否有图标 */
|
|
129
|
+
hasImg?: boolean;
|
|
130
|
+
/** 图标颜色 */
|
|
131
|
+
iconColor?: string;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* 线条选项接口 - 线条图层专用配置
|
|
135
|
+
*/
|
|
136
|
+
export interface LineOptions extends BaseOptions, StyleOptions, TextOptions {
|
|
137
|
+
/** 线条类型 */
|
|
138
|
+
type?: string;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* 多边形选项接口 - 多边形图层专用配置
|
|
142
|
+
*/
|
|
143
|
+
export interface PolygonOptions extends BaseOptions, StyleOptions, TextOptions {
|
|
144
|
+
/** 名称字段键 */
|
|
145
|
+
nameKey?: string;
|
|
146
|
+
/** 是否为蒙版 */
|
|
147
|
+
mask?: boolean;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* 兼容性类型别名 - 保持向后兼容
|
|
151
|
+
* @deprecated 请使用具体的选项接口:PointOptions, LineOptions, PolygonOptions
|
|
152
|
+
*/
|
|
153
|
+
export type OptionsType = BaseOptions & StyleOptions & TextOptions & {
|
|
154
|
+
nameKey?: string;
|
|
155
|
+
img?: string;
|
|
156
|
+
scale?: number;
|
|
157
|
+
hasImg?: boolean;
|
|
158
|
+
iconColor?: string;
|
|
159
|
+
type?: string;
|
|
160
|
+
mask?: boolean;
|
|
161
|
+
};
|
|
162
|
+
/**
|
|
163
|
+
* 图片图层数据接口
|
|
164
|
+
*/
|
|
165
|
+
export interface ImageLayerData {
|
|
166
|
+
img: string;
|
|
167
|
+
extent: number[];
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* 蒙版图层配置接口
|
|
171
|
+
*/
|
|
172
|
+
export interface MaskLayerOptions {
|
|
173
|
+
extent?: any;
|
|
174
|
+
fillColor?: string;
|
|
175
|
+
strokeWidth?: number;
|
|
176
|
+
strokeColor?: string;
|
|
177
|
+
zIndex?: number;
|
|
178
|
+
opacity?: number;
|
|
179
|
+
visible?: boolean;
|
|
180
|
+
layerName?: string;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* 颜色映射接口
|
|
184
|
+
*/
|
|
185
|
+
export interface ColorMap {
|
|
186
|
+
[level: string]: string;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* 要素颜色更新选项接口
|
|
190
|
+
*/
|
|
191
|
+
export interface FeatureColorUpdateOptions extends BaseOptions, StyleOptions, TextOptions {
|
|
192
|
+
/** 名称字段键 */
|
|
193
|
+
nameKey?: string;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* 点位数据接口
|
|
197
|
+
*/
|
|
198
|
+
export interface PointData {
|
|
199
|
+
lgtd: number;
|
|
200
|
+
lttd: number;
|
|
201
|
+
[key: string]: any;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* 线数据接口
|
|
205
|
+
*/
|
|
206
|
+
export interface LineData {
|
|
207
|
+
type: string;
|
|
208
|
+
coordinates: number[][];
|
|
209
|
+
[key: string]: any;
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* 聚合选项接口
|
|
213
|
+
*/
|
|
214
|
+
export interface ClusterOptions extends PointOptions {
|
|
215
|
+
/** 聚合距离 */
|
|
216
|
+
distance?: number;
|
|
217
|
+
/** 最小聚合距离 */
|
|
218
|
+
minDistance?: number;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* 测量处理器类型
|
|
222
|
+
*/
|
|
223
|
+
export type MeasureHandlerType = 'LineString' | 'Polygon';
|
|
224
|
+
/**
|
|
225
|
+
* 事件类型
|
|
226
|
+
*/
|
|
227
|
+
export type EventType = 'click' | 'hover' | 'moveend';
|
|
228
|
+
/**
|
|
229
|
+
* Vue实例接口
|
|
230
|
+
*/
|
|
231
|
+
export interface VueInstance {
|
|
232
|
+
mount(element: Element | string): VueInstance;
|
|
233
|
+
unmount?(): void;
|
|
234
|
+
$destroy?(): void;
|
|
235
|
+
[key: string]: any;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Vue应用接口
|
|
239
|
+
*/
|
|
240
|
+
export interface VueApp {
|
|
241
|
+
mount(element: Element | string): VueInstance;
|
|
242
|
+
unmount(): void;
|
|
243
|
+
[key: string]: any;
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Vue 2.x 实例接口
|
|
247
|
+
*/
|
|
248
|
+
export interface VueLegacyInstance {
|
|
249
|
+
$mount(element?: Element | string): VueLegacyInstance;
|
|
250
|
+
$destroy(): void;
|
|
251
|
+
[key: string]: any;
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* DOM点位状态枚举
|
|
255
|
+
*/
|
|
256
|
+
export enum DomPointState {
|
|
257
|
+
CREATED = 'created',
|
|
258
|
+
MOUNTED = 'mounted',
|
|
259
|
+
VISIBLE = 'visible',
|
|
260
|
+
HIDDEN = 'hidden',
|
|
261
|
+
DESTROYED = 'destroyed'
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* DOM点位选项接口
|
|
265
|
+
*/
|
|
266
|
+
export interface DomPointOptions {
|
|
267
|
+
Vue: any;
|
|
268
|
+
Template: any;
|
|
269
|
+
lgtd: number;
|
|
270
|
+
lttd: number;
|
|
271
|
+
props?: any;
|
|
272
|
+
longitude?: number;
|
|
273
|
+
latitude?: number;
|
|
274
|
+
styleType?: 'default' | 'custom';
|
|
275
|
+
positioning?: 'bottom-left' | 'bottom-center' | 'bottom-right' | 'center-left' | 'center-center' | 'center-right' | 'top-left' | 'top-center' | 'top-right';
|
|
276
|
+
stopEvent?: boolean;
|
|
277
|
+
visible?: boolean;
|
|
278
|
+
className?: string;
|
|
279
|
+
zIndex?: number;
|
|
280
|
+
}
|
package/dist/types.js
CHANGED
|
@@ -1 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* DOM点位状态枚举
|
|
3
|
+
*/
|
|
4
|
+
export var DomPointState;
|
|
5
|
+
(function (DomPointState) {
|
|
6
|
+
DomPointState["CREATED"] = "created";
|
|
7
|
+
DomPointState["MOUNTED"] = "mounted";
|
|
8
|
+
DomPointState["VISIBLE"] = "visible";
|
|
9
|
+
DomPointState["HIDDEN"] = "hidden";
|
|
10
|
+
DomPointState["DESTROYED"] = "destroyed";
|
|
11
|
+
})(DomPointState || (DomPointState = {}));
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 错误类型枚举
|
|
3
|
+
*/
|
|
4
|
+
export declare enum ErrorType {
|
|
5
|
+
VALIDATION_ERROR = "VALIDATION_ERROR",
|
|
6
|
+
MAP_ERROR = "MAP_ERROR",
|
|
7
|
+
LAYER_ERROR = "LAYER_ERROR",
|
|
8
|
+
COORDINATE_ERROR = "COORDINATE_ERROR",
|
|
9
|
+
DATA_ERROR = "DATA_ERROR",
|
|
10
|
+
COMPONENT_ERROR = "COMPONENT_ERROR"
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* 自定义错误类
|
|
14
|
+
*/
|
|
15
|
+
export declare class MyOpenLayersError extends Error {
|
|
16
|
+
readonly type: ErrorType;
|
|
17
|
+
readonly timestamp: Date;
|
|
18
|
+
readonly context?: any;
|
|
19
|
+
constructor(message: string, type: ErrorType, context?: any);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* 错误处理工具类
|
|
23
|
+
*/
|
|
24
|
+
export declare class ErrorHandler {
|
|
25
|
+
private static instance;
|
|
26
|
+
private errorCallbacks;
|
|
27
|
+
private logLevel;
|
|
28
|
+
private constructor();
|
|
29
|
+
/**
|
|
30
|
+
* 获取单例实例
|
|
31
|
+
*/
|
|
32
|
+
static getInstance(): ErrorHandler;
|
|
33
|
+
/**
|
|
34
|
+
* 设置日志级别
|
|
35
|
+
* @param level 日志级别
|
|
36
|
+
*/
|
|
37
|
+
setLogLevel(level: 'debug' | 'info' | 'warn' | 'error'): void;
|
|
38
|
+
/**
|
|
39
|
+
* 添加错误回调
|
|
40
|
+
* @param callback 错误回调函数
|
|
41
|
+
*/
|
|
42
|
+
addErrorCallback(callback: (error: MyOpenLayersError) => void): void;
|
|
43
|
+
/**
|
|
44
|
+
* 移除错误回调
|
|
45
|
+
* @param callback 要移除的回调函数
|
|
46
|
+
*/
|
|
47
|
+
removeErrorCallback(callback: (error: MyOpenLayersError) => void): void;
|
|
48
|
+
/**
|
|
49
|
+
* 处理错误
|
|
50
|
+
* @param error 错误对象
|
|
51
|
+
*/
|
|
52
|
+
handleError(error: MyOpenLayersError): void;
|
|
53
|
+
/**
|
|
54
|
+
* 创建并处理错误
|
|
55
|
+
* @param message 错误消息
|
|
56
|
+
* @param type 错误类型
|
|
57
|
+
* @param context 错误上下文
|
|
58
|
+
*/
|
|
59
|
+
createAndHandleError(message: string, type: ErrorType, context?: any): MyOpenLayersError;
|
|
60
|
+
/**
|
|
61
|
+
* 记录错误日志
|
|
62
|
+
* @param error 错误对象
|
|
63
|
+
*/
|
|
64
|
+
private logError;
|
|
65
|
+
/**
|
|
66
|
+
* 验证参数
|
|
67
|
+
* @param condition 验证条件
|
|
68
|
+
* @param message 错误消息
|
|
69
|
+
* @param context 错误上下文
|
|
70
|
+
*/
|
|
71
|
+
static validate(condition: boolean, message: string, context?: any): void;
|
|
72
|
+
/**
|
|
73
|
+
* 验证地图实例
|
|
74
|
+
* @param map 地图实例
|
|
75
|
+
*/
|
|
76
|
+
static validateMap(map: any): void;
|
|
77
|
+
/**
|
|
78
|
+
* 验证坐标
|
|
79
|
+
* @param longitude 经度
|
|
80
|
+
* @param latitude 纬度
|
|
81
|
+
*/
|
|
82
|
+
static validateCoordinates(longitude: number, latitude: number): void;
|
|
83
|
+
/**
|
|
84
|
+
* 验证图层名称
|
|
85
|
+
* @param layerName 图层名称
|
|
86
|
+
*/
|
|
87
|
+
static validateLayerName(layerName: string): void;
|
|
88
|
+
/**
|
|
89
|
+
* 验证数据
|
|
90
|
+
* @param data 数据
|
|
91
|
+
* @param dataType 数据类型描述
|
|
92
|
+
*/
|
|
93
|
+
static validateData(data: any, dataType: string): void;
|
|
94
|
+
/**
|
|
95
|
+
* 安全执行函数
|
|
96
|
+
* @param fn 要执行的函数
|
|
97
|
+
* @param errorMessage 错误消息
|
|
98
|
+
* @param errorType 错误类型
|
|
99
|
+
* @param context 错误上下文
|
|
100
|
+
*/
|
|
101
|
+
static safeExecute<T>(fn: () => T, errorMessage: string, errorType?: ErrorType, context?: any): T;
|
|
102
|
+
}
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 错误类型枚举
|
|
3
|
+
*/
|
|
4
|
+
export var ErrorType;
|
|
5
|
+
(function (ErrorType) {
|
|
6
|
+
ErrorType["VALIDATION_ERROR"] = "VALIDATION_ERROR";
|
|
7
|
+
ErrorType["MAP_ERROR"] = "MAP_ERROR";
|
|
8
|
+
ErrorType["LAYER_ERROR"] = "LAYER_ERROR";
|
|
9
|
+
ErrorType["COORDINATE_ERROR"] = "COORDINATE_ERROR";
|
|
10
|
+
ErrorType["DATA_ERROR"] = "DATA_ERROR";
|
|
11
|
+
ErrorType["COMPONENT_ERROR"] = "COMPONENT_ERROR";
|
|
12
|
+
})(ErrorType || (ErrorType = {}));
|
|
13
|
+
/**
|
|
14
|
+
* 自定义错误类
|
|
15
|
+
*/
|
|
16
|
+
export class MyOpenLayersError extends Error {
|
|
17
|
+
constructor(message, type, context) {
|
|
18
|
+
super(message);
|
|
19
|
+
this.name = 'MyOpenLayersError';
|
|
20
|
+
this.type = type;
|
|
21
|
+
this.timestamp = new Date();
|
|
22
|
+
this.context = context;
|
|
23
|
+
// 确保错误堆栈正确显示(兼容Node.js和浏览器环境)
|
|
24
|
+
if (typeof Error.captureStackTrace === 'function') {
|
|
25
|
+
Error.captureStackTrace(this, MyOpenLayersError);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
// 在不支持captureStackTrace的环境中,手动设置stack
|
|
29
|
+
this.stack = (new Error()).stack;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* 错误处理工具类
|
|
35
|
+
*/
|
|
36
|
+
export class ErrorHandler {
|
|
37
|
+
constructor() {
|
|
38
|
+
this.errorCallbacks = [];
|
|
39
|
+
this.logLevel = 'error';
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* 获取单例实例
|
|
43
|
+
*/
|
|
44
|
+
static getInstance() {
|
|
45
|
+
if (!ErrorHandler.instance) {
|
|
46
|
+
ErrorHandler.instance = new ErrorHandler();
|
|
47
|
+
}
|
|
48
|
+
return ErrorHandler.instance;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* 设置日志级别
|
|
52
|
+
* @param level 日志级别
|
|
53
|
+
*/
|
|
54
|
+
setLogLevel(level) {
|
|
55
|
+
this.logLevel = level;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* 添加错误回调
|
|
59
|
+
* @param callback 错误回调函数
|
|
60
|
+
*/
|
|
61
|
+
addErrorCallback(callback) {
|
|
62
|
+
this.errorCallbacks.push(callback);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* 移除错误回调
|
|
66
|
+
* @param callback 要移除的回调函数
|
|
67
|
+
*/
|
|
68
|
+
removeErrorCallback(callback) {
|
|
69
|
+
const index = this.errorCallbacks.indexOf(callback);
|
|
70
|
+
if (index > -1) {
|
|
71
|
+
this.errorCallbacks.splice(index, 1);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* 处理错误
|
|
76
|
+
* @param error 错误对象
|
|
77
|
+
*/
|
|
78
|
+
handleError(error) {
|
|
79
|
+
// 记录错误日志
|
|
80
|
+
this.logError(error);
|
|
81
|
+
// 调用错误回调
|
|
82
|
+
this.errorCallbacks.forEach(callback => {
|
|
83
|
+
try {
|
|
84
|
+
callback(error);
|
|
85
|
+
}
|
|
86
|
+
catch (callbackError) {
|
|
87
|
+
console.error('Error in error callback:', callbackError);
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* 创建并处理错误
|
|
93
|
+
* @param message 错误消息
|
|
94
|
+
* @param type 错误类型
|
|
95
|
+
* @param context 错误上下文
|
|
96
|
+
*/
|
|
97
|
+
createAndHandleError(message, type, context) {
|
|
98
|
+
const error = new MyOpenLayersError(message, type, context);
|
|
99
|
+
this.handleError(error);
|
|
100
|
+
return error;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* 记录错误日志
|
|
104
|
+
* @param error 错误对象
|
|
105
|
+
*/
|
|
106
|
+
logError(error) {
|
|
107
|
+
const logMessage = `[${error.type}] ${error.message}`;
|
|
108
|
+
const logData = {
|
|
109
|
+
message: error.message,
|
|
110
|
+
type: error.type,
|
|
111
|
+
timestamp: error.timestamp,
|
|
112
|
+
context: error.context,
|
|
113
|
+
stack: error.stack
|
|
114
|
+
};
|
|
115
|
+
switch (this.logLevel) {
|
|
116
|
+
case 'debug':
|
|
117
|
+
console.debug(logMessage, logData);
|
|
118
|
+
break;
|
|
119
|
+
case 'info':
|
|
120
|
+
console.info(logMessage, logData);
|
|
121
|
+
break;
|
|
122
|
+
case 'warn':
|
|
123
|
+
console.warn(logMessage, logData);
|
|
124
|
+
break;
|
|
125
|
+
case 'error':
|
|
126
|
+
default:
|
|
127
|
+
console.error(logMessage, logData);
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* 验证参数
|
|
133
|
+
* @param condition 验证条件
|
|
134
|
+
* @param message 错误消息
|
|
135
|
+
* @param context 错误上下文
|
|
136
|
+
*/
|
|
137
|
+
static validate(condition, message, context) {
|
|
138
|
+
if (!condition) {
|
|
139
|
+
throw ErrorHandler.getInstance().createAndHandleError(message, ErrorType.VALIDATION_ERROR, context);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* 验证地图实例
|
|
144
|
+
* @param map 地图实例
|
|
145
|
+
*/
|
|
146
|
+
static validateMap(map) {
|
|
147
|
+
ErrorHandler.validate(map && typeof map === 'object', 'Map instance is required', { map });
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* 验证坐标
|
|
151
|
+
* @param longitude 经度
|
|
152
|
+
* @param latitude 纬度
|
|
153
|
+
*/
|
|
154
|
+
static validateCoordinates(longitude, latitude) {
|
|
155
|
+
ErrorHandler.validate(typeof longitude === 'number' && !isNaN(longitude), 'Valid longitude is required', { longitude, latitude });
|
|
156
|
+
ErrorHandler.validate(typeof latitude === 'number' && !isNaN(latitude), 'Valid latitude is required', { longitude, latitude });
|
|
157
|
+
ErrorHandler.validate(longitude >= -180 && longitude <= 180, 'Longitude must be between -180 and 180', { longitude, latitude });
|
|
158
|
+
ErrorHandler.validate(latitude >= -90 && latitude <= 90, 'Latitude must be between -90 and 90', { longitude, latitude });
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* 验证图层名称
|
|
162
|
+
* @param layerName 图层名称
|
|
163
|
+
*/
|
|
164
|
+
static validateLayerName(layerName) {
|
|
165
|
+
ErrorHandler.validate(typeof layerName === 'string' && layerName.trim().length > 0, 'Valid layer name is required', { layerName });
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* 验证数据
|
|
169
|
+
* @param data 数据
|
|
170
|
+
* @param dataType 数据类型描述
|
|
171
|
+
*/
|
|
172
|
+
static validateData(data, dataType) {
|
|
173
|
+
ErrorHandler.validate(data !== null && data !== undefined, `${dataType} is required`, { data, dataType });
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* 安全执行函数
|
|
177
|
+
* @param fn 要执行的函数
|
|
178
|
+
* @param errorMessage 错误消息
|
|
179
|
+
* @param errorType 错误类型
|
|
180
|
+
* @param context 错误上下文
|
|
181
|
+
*/
|
|
182
|
+
static safeExecute(fn, errorMessage, errorType = ErrorType.COMPONENT_ERROR, context) {
|
|
183
|
+
try {
|
|
184
|
+
return fn();
|
|
185
|
+
}
|
|
186
|
+
catch (error) {
|
|
187
|
+
const myError = ErrorHandler.getInstance().createAndHandleError(`${errorMessage}: ${error}`, errorType, { originalError: error, context });
|
|
188
|
+
throw myError;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|