Skip to content

Commit 7a0e4cb

Browse files
2 parents 2b92766 + 1ef924b commit 7a0e4cb

File tree

14 files changed

+1081
-563
lines changed

14 files changed

+1081
-563
lines changed

src/common/commontypes/Util.js

+28
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,34 @@ const Util = {
11101110
}
11111111
rgba.push(opacity);
11121112
return "rgba(" + rgba.join(",") + ")";
1113+
},
1114+
/**
1115+
* @description 是否是绝对地址。
1116+
* @private
1117+
* @param {string} url - 验证地址。
1118+
* @returns {boolean} 是否是绝对地址。
1119+
*/
1120+
isAbsoluteURL(url) {
1121+
try {
1122+
const res = new URL(url);
1123+
return !!res;
1124+
} catch (_) {
1125+
return false;
1126+
}
1127+
},
1128+
/**
1129+
* @description 相对地址转绝对地址。
1130+
* @private
1131+
* @param {string} url - 相对地址。
1132+
* @param {string} base - 基础地址。
1133+
* @returns {string} 完整地址。
1134+
*/
1135+
relative2absolute(url, base) {
1136+
let newUrl = new URL(url, base);
1137+
if (newUrl && newUrl.href) {
1138+
return decodeURIComponent(newUrl.href);
1139+
}
1140+
return;
11131141
}
11141142
};
11151143

src/common/index.common.js

+2
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ import { ElasticSearch } from './thirdparty';
329329
import {
330330
isCORS,
331331
setCORS,
332+
setRequestHeaders,
332333
FetchRequest,
333334
ColorsPickerUtil,
334335
ArrayStatistic,
@@ -501,6 +502,7 @@ export { Format, GeoJSONFormat, JSONFormat, WKTFormat };
501502
export {
502503
isCORS,
503504
setCORS,
505+
setRequestHeaders,
504506
FetchRequest,
505507
EncryptRequest,
506508
getServiceKey,

src/common/mapping/WebMapV2.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ export function createWebMapV2Extending(SuperClass, { MapManager, mapRepo, crsMa
334334
style.layers.forEach(layer => {
335335
layer.layout && (layer.layout.visibility = this._getVisibility(layerInfo.visible));
336336
});
337-
this.map.addStyle(style);
337+
this.map.addStyle(style, undefined, undefined, undefined, url);
338338
const layerIds = [];
339339
style.layers.forEach((item) => {
340340
if (item.type !== 'background') {

src/common/namespace.js

+2
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ import {
337337
ElasticSearch,
338338
isCORS,
339339
setCORS,
340+
setRequestHeaders,
340341
FetchRequest,
341342
EncryptRequest,
342343
getServiceKey,
@@ -497,6 +498,7 @@ SuperMap.setCORS = setCORS;
497498
SuperMap.isCORS = isCORS;
498499
SuperMap.setRequestTimeout = setRequestTimeout;
499500
SuperMap.getRequestTimeout = getRequestTimeout;
501+
SuperMap.setRequestHeaders = setRequestHeaders;
500502
SuperMap.FetchRequest = FetchRequest;
501503
SuperMap.EncryptRequest = EncryptRequest;
502504
SuperMap.getServiceKey = getServiceKey;

src/common/util/FetchRequest.js

+43
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ export var RequestJSONPPromise = {
171171

172172
var CORS;
173173
var RequestTimeout;
174+
var RequestHeadersGetter;
174175
/**
175176
* @function setCORS
176177
* @description 设置是否允许跨域请求,全局配置,优先级低于 service 下的 crossOring 参数。
@@ -279,6 +280,43 @@ export var getRequestTimeout = function () {
279280
return RequestTimeout || 45000;
280281
}
281282

283+
/**
284+
* @function setRequestHeaders
285+
* @category BaseTypes Util
286+
* @description 设置请求自定义 request headers。
287+
* @param {function} func - 请求自定义 request headers 回调函数。
288+
* @usage
289+
* ```
290+
* // 浏览器
291+
<script type="text/javascript" src="{cdn}"></script>
292+
<script>
293+
const headers = function (url) { return { token: !!url }; };
294+
{namespace}.setRequestHeaders(headers);
295+
296+
</script>
297+
298+
// ES6 Import
299+
import { setRequestHeaders } from '{npm}';
300+
301+
const headers = function (url) { return { token: !!url }; };
302+
setRequestHeaders(headers);
303+
* ```
304+
*/
305+
export var setRequestHeaders = function (headers) {
306+
return RequestHeadersGetter = headers;
307+
}
308+
309+
/**
310+
* @private
311+
* @function getRequestTimeout
312+
* @category BaseTypes Util
313+
* @description 获取请求超时的时间。
314+
* @returns {number} 请求超时时间。
315+
*/
316+
export var getRequestHeaders = function () {
317+
return RequestHeadersGetter;
318+
}
319+
282320
/**
283321
* @name FetchRequest
284322
* @namespace
@@ -501,6 +539,11 @@ export var FetchRequest = {
501539
if (!options.headers['Content-Type'] && !FormData.prototype.isPrototypeOf(params)) {
502540
options.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
503541
}
542+
const customRequestHeadersGetter = getRequestHeaders();
543+
const customRequestHeaders = customRequestHeadersGetter && customRequestHeadersGetter(url);
544+
if (customRequestHeaders) {
545+
options.headers = Util.extend(options.headers, customRequestHeaders);
546+
}
504547
if (options.timeout) {
505548
return this._timeout(
506549
options.timeout,

src/common/util/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
isCORS,
77
setRequestTimeout,
88
getRequestTimeout,
9+
setRequestHeaders,
910
FetchRequest
1011
} from './FetchRequest';
1112

@@ -72,6 +73,7 @@ export {
7273
isCORS,
7374
setRequestTimeout,
7475
getRequestTimeout,
76+
setRequestHeaders,
7577
FetchRequest,
7678
EncryptRequest,
7779
getServiceKey,

src/openlayers/mapping/WebMap.js

+61-18
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ export class WebMap extends Observable {
131131
this.webMap = options.webMap;
132132
this.tileFormat = options.tileFormat && options.tileFormat.toLowerCase();
133133
this.restDataSingleRequestCount = options.restDataSingleRequestCount || 1000;
134+
this.tileRequestParameters = options.tileRequestParameters;
134135
this.createMap(options.mapSetting);
135136
if (this.webMap) {
136137
// webmap有可能是url地址,有可能是webmap对象
@@ -590,7 +591,8 @@ export class WebMap extends Observable {
590591
let options = {
591592
serverType,
592593
url,
593-
tileGrid: TileSuperMapRest.optionsFromMapJSON(url, result).tileGrid
594+
tileGrid: TileSuperMapRest.optionsFromMapJSON(url, result).tileGrid,
595+
tileLoadFunction: me.getCustomTileLoadFunction()
594596
};
595597
if (url && !CommonUtil.isInTheSameDomain(url) && !this.isIportalProxyServiceUrl(url)) {
596598
options.tileProxy = me.server + 'apps/viewer/getUrlResource.png?url=';
@@ -1178,6 +1180,36 @@ export class WebMap extends Observable {
11781180
break;
11791181
}
11801182
}
1183+
1184+
getCustomTileLoadFunction(transformImageUrl) {
1185+
const that = this;
1186+
if (this.tileRequestParameters) {
1187+
return function(imageTile, url) {
1188+
const src = transformImageUrl ? transformImageUrl(url) : url;
1189+
const requestParameters = that.tileRequestParameters(src);
1190+
if (requestParameters) {
1191+
FetchRequest.get(src, null, {
1192+
...requestParameters,
1193+
withoutFormatSuffix: true
1194+
})
1195+
.then(function (response) {
1196+
return response.blob();
1197+
})
1198+
.then(function (blob) {
1199+
const imageUrl = URL.createObjectURL(blob);
1200+
imageTile.getImage().src = imageUrl;
1201+
})
1202+
.catch(function (error) {
1203+
console.error('Error fetching the image:', error);
1204+
imageTile.setState('error');
1205+
});
1206+
} else {
1207+
imageTile.getImage().src = src;
1208+
}
1209+
}
1210+
}
1211+
}
1212+
11811213
/**
11821214
* @private
11831215
* @function WebMap.prototype.createDynamicTiledSource
@@ -1208,7 +1240,8 @@ export class WebMap extends Observable {
12081240
// crossOrigin: 'anonymous', //在IE11.0.9600版本,会影响通过注册服务打开的iserver地图,不出图。因为没有携带cookie会报跨域问题
12091241
// extent: this.baseLayerExtent,
12101242
// prjCoordSys: {epsgCode: isBaseLayer ? layerInfo.projection.split(':')[1] : this.baseProjection.split(':')[1]},
1211-
format: layerInfo.format
1243+
format: layerInfo.format,
1244+
tileLoadFunction: this.getCustomTileLoadFunction()
12121245
};
12131246
if (!isBaseLayer && !this.isCustomProjection(this.baseProjection)) {
12141247
options.prjCoordSys = { epsgCode: this.baseProjection.split(':')[1] };
@@ -1322,7 +1355,8 @@ export class WebMap extends Observable {
13221355
return new XYZ({
13231356
url: layerInfo.url,
13241357
wrapX: false,
1325-
crossOrigin: 'anonymous'
1358+
crossOrigin: 'anonymous',
1359+
tileLoadFunction: this.getCustomTileLoadFunction()
13261360
});
13271361
}
13281362

@@ -1368,7 +1402,8 @@ export class WebMap extends Observable {
13681402
wrapX: false,
13691403
crossOrigin: 'anonymous',
13701404
tileGrid: this._getTileGrid({ origin, resolutions, tileSize }),
1371-
projection: this.baseProjection
1405+
projection: this.baseProjection,
1406+
tileLoadFunction: this.getCustomTileLoadFunction()
13721407
};
13731408
return new XYZ(options);
13741409
}
@@ -1399,9 +1434,7 @@ export class WebMap extends Observable {
13991434
VERSION: layerInfo.version || '1.3.0'
14001435
},
14011436
projection: layerInfo.projection || that.baseProjection,
1402-
tileLoadFunction: function (imageTile, src) {
1403-
imageTile.getImage().src = src;
1404-
}
1437+
tileLoadFunction: this.getCustomTileLoadFunction()
14051438
});
14061439
}
14071440

@@ -1794,13 +1827,12 @@ export class WebMap extends Observable {
17941827
layerInfo.origin,
17951828
layerInfo.matrixIds
17961829
),
1797-
tileLoadFunction: function (imageTile, src) {
1830+
tileLoadFunction: this.getCustomTileLoadFunction(function (src) {
17981831
if (src.indexOf('tianditu.gov.cn') >= 0) {
1799-
imageTile.getImage().src = `${src}&tk=${CommonUtil.getParameters(layerInfo.url)['tk']}`;
1800-
return;
1832+
return `${src}&tk=${CommonUtil.getParameters(layerInfo.url)['tk']}`;
18011833
}
1802-
imageTile.getImage().src = src;
1803-
}
1834+
return src;
1835+
})
18041836
});
18051837
}
18061838

@@ -1951,7 +1983,7 @@ export class WebMap extends Observable {
19511983
await that.addLayer(layer, null, layerIndex);
19521984
that.layerAdded++;
19531985
that.sendMapToUser(len);
1954-
return;
1986+
continue;
19551987
}
19561988
if (
19571989
layer.layerType === 'MARKER' ||
@@ -2996,13 +3028,15 @@ export class WebMap extends Observable {
29963028
};
29973029
let featureType = layerInfo.featureType;
29983030
let style = await StyleUtils.toOpenLayersStyle(this.getDataVectorTileStyle(featureType), featureType);
3031+
const requestParameters = this.tileRequestParameters && this.tileRequestParameters(layerInfo.url);
29993032
return new olLayer.VectorTile({
30003033
//设置避让参数
30013034
source: new VectorTileSuperMapRest({
30023035
url: layerInfo.url,
30033036
projection: layerInfo.projection,
30043037
tileType: 'ScaleXY',
3005-
format: format
3038+
format: format,
3039+
...requestParameters
30063040
}),
30073041
style: style
30083042
});
@@ -5119,7 +5153,6 @@ export class WebMap extends Observable {
51195153
* @param {Object} layerInfo - 图层信息
51205154
*/
51215155
createMVTLayer(layerInfo) {
5122-
// let that = this;
51235156
let styles = layerInfo.styles;
51245157
const indexbounds = styles && styles.metadata && styles.metadata.indexbounds;
51255158
const visibleResolution = this.createVisibleResolution(
@@ -5131,14 +5164,22 @@ export class WebMap extends Observable {
51315164
const envelope = this.getEnvelope(indexbounds, layerInfo.bounds);
51325165
const styleResolutions = this.getStyleResolutions(envelope);
51335166
// const origin = [envelope.left, envelope.top];
5134-
let withCredentials = this.isIportalProxyServiceUrl(styles.sprite);
5167+
let baseUrl = layerInfo.url && layerInfo.url.split('?')[0];
5168+
let spriteUrl = styles.sprite;
5169+
if (!CommonUtil.isAbsoluteURL(styles.sprite)) {
5170+
spriteUrl = CommonUtil.relative2absolute(styles.sprite, baseUrl);
5171+
}
5172+
let withCredentials = this.isIportalProxyServiceUrl(spriteUrl);
5173+
const requestParameters = this.tileRequestParameters && this.tileRequestParameters(spriteUrl);
51355174
// 创建MapBoxStyle样式
51365175
let mapboxStyles = new MapboxStyles({
5176+
baseUrl,
51375177
style: styles,
51385178
source: styles.name,
51395179
resolutions: styleResolutions,
51405180
map: this.map,
5141-
withCredentials
5181+
withCredentials,
5182+
...requestParameters
51425183
});
51435184
return new Promise((resolve) => {
51445185
mapboxStyles.on('styleloaded', function () {
@@ -5148,13 +5189,15 @@ export class WebMap extends Observable {
51485189
//设置避让参数
51495190
declutter: true,
51505191
source: new VectorTileSuperMapRest({
5192+
baseUrl,
51515193
style: styles,
51525194
withCredentials,
51535195
projection: layerInfo.projection,
51545196
format: new MVT({
51555197
featureClass: olRenderFeature
51565198
}),
5157-
wrapX: false
5199+
wrapX: false,
5200+
...requestParameters
51585201
}),
51595202
style: mapboxStyles.featureStyleFuntion,
51605203
visible: layerInfo.visible,

0 commit comments

Comments
 (0)