Skip to content

Commit b179eb8

Browse files
committed
【feature】mapboxgl出图支持传自定义的requestParameters; review by luox
1 parent 449ebf0 commit b179eb8

File tree

4 files changed

+131
-36
lines changed

4 files changed

+131
-36
lines changed

src/mapboxgl/mapping/utils/L7LayerUtil.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -638,12 +638,14 @@ function vectorSourceToL7Source(source, sourceLayer, options) {
638638
sourceLayer
639639
}
640640
};
641-
if (isIportalProxyServiceUrl(result.data, options)) {
642-
Object.assign(result.parser, {
643-
requestParameters: {
644-
credentials: 'include'
645-
}
646-
});
641+
const requestParameters = options.transformRequest(result.data, 'Tile');
642+
if (requestParameters) {
643+
if (requestParameters.credentials) {
644+
result.parser.requestParameters = { credentials: requestParameters.credentials };
645+
}
646+
if (requestParameters.headers) {
647+
result.parser.requestParameters = { ...result.parser.requestParameters, headers: requestParameters.headers };
648+
}
647649
}
648650
return result;
649651
}

src/mapboxgl/mapping/webmap/v3/WebMap.js

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,8 @@ export class WebMap extends mapboxgl.Evented {
231231
const copyLayer = { ...matchLayer, ...layerInfo, id: copyLayerId };
232232
if (isL7Layer(copyLayer)) {
233233
const layers = [copyLayer];
234-
await addL7Layers({
235-
map: this.map,
236-
webMapInfo: { ...this._mapInfo, layers, sources: this._mapInfo.sources },
237-
l7Layers: layers,
238-
spriteDatas: this._spriteDatas,
239-
options: this.options
240-
});
234+
const params = this._getAddL7LayersParams(layers, this._mapInfo.sources, layers);
235+
await addL7Layers(params);
241236
return;
242237
}
243238
if (typeof copyLayer.source === 'object') {
@@ -327,18 +322,8 @@ export class WebMap extends mapboxgl.Evented {
327322
const fontFamilys = this._getLabelFontFamily();
328323
// 初始化 map
329324
const mapOptions = {
330-
transformRequest: (url, resourceType) => {
331-
const res = { url };
332-
if (
333-
resourceType === 'Tile' &&
334-
this.options.iportalServiceProxyUrl &&
335-
url.indexOf(this.options.iportalServiceProxyUrl) >= 0
336-
) {
337-
res.credentials = 'include';
338-
}
339-
return res;
340-
},
341325
...this.mapOptions,
326+
transformRequest: this._getTransformRequest(),
342327
container: this.options.target,
343328
crs: this._baseProjection,
344329
center,
@@ -364,6 +349,23 @@ export class WebMap extends mapboxgl.Evented {
364349
});
365350
}
366351

352+
_getTransformRequest() {
353+
if (this.mapOptions.transformRequest) {
354+
return this.mapOptions.transformRequest;
355+
}
356+
return (url, resourceType) => {
357+
if (resourceType === 'Tile') {
358+
const withCredentials = this.options.iportalServiceProxyUrl && url.indexOf(this.options.iportalServiceProxyUrl) >= 0;
359+
return {
360+
url: url,
361+
credentials: withCredentials ? 'include' : undefined,
362+
...(this.options.tileTransformRequest && this.options.tileTransformRequest(url))
363+
};
364+
}
365+
return { url };
366+
}
367+
}
368+
367369
_setBaseProjection() {
368370
let crs = this._mapInfo.crs;
369371
let baseProjection = crs;
@@ -480,16 +482,8 @@ export class WebMap extends mapboxgl.Evented {
480482
});
481483
const l7Layers = layers.filter((layer) => isL7Layer(layer));
482484
if (l7Layers.length > 0) {
483-
await addL7Layers({
484-
map: this.map,
485-
webMapInfo: { ...this._mapInfo, layers, sources },
486-
l7Layers,
487-
spriteDatas: this._spriteDatas,
488-
options: {
489-
...this.options,
490-
emitterEvent: this.fire.bind(this)
491-
}
492-
});
485+
const params = this._getAddL7LayersParams(layers, sources, l7Layers);
486+
await addL7Layers(params);
493487
}
494488
this._createLegendInfo();
495489
this._sendMapToUser();
@@ -499,6 +493,20 @@ export class WebMap extends mapboxgl.Evented {
499493
}
500494
}
501495

496+
_getAddL7LayersParams(layers, sources, l7Layers) {
497+
return {
498+
map: this.map,
499+
webMapInfo: { ...this._mapInfo, layers, sources },
500+
l7Layers,
501+
spriteDatas: this._spriteDatas,
502+
options: {
503+
...this.options,
504+
emitterEvent: this.fire.bind(this),
505+
transformRequest: this._getTransformRequest()
506+
}
507+
}
508+
}
509+
502510
/**
503511
* @private
504512
* @function WebMap.prototype._setUniqueId

test/mapboxgl/mapping/WebMapV3Spec.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,18 +615,27 @@ describe('mapboxgl-webmap3.0', () => {
615615
});
616616
spyOn(mapboxgl, 'Map').and.callFake(mbglmap);
617617
const mapInfo = JSON.parse(mapstudioWebMap_raster);
618+
const iportalServiceProxyUrl = 'http://localhost:8195/portalproxy';
619+
const tileCustomRequestHeaders = { 'Authorization': 'test token' };
618620
mapstudioWebmap = new WebMapV3(mapInfo, {
619621
server: server,
620-
iportalServiceProxyUrl: 'http://localhost:8195/portalproxy'
622+
iportalServiceProxyUrl,
623+
tileTransformRequest: (url) => {
624+
if (url.includes(iportalServiceProxyUrl)) {
625+
return { headers: tileCustomRequestHeaders };
626+
}
627+
}
621628
});
622629
mapstudioWebmap.initializeMap(mapInfo);
623630
expect(mapstudioWebmap.map).not.toBeUndefined();
624631
let mockTileUrl = 'http://localhost:8195/portalproxy/7c851958ab40a5e0/iserver/services/map_world1_y6nykx3f/rest/maps/World1/tileimage.png?scale=6.760654286410619e-9&x=1&y=0&width=256&height=256&transparent=true&redirect=false&cacheEnabled=true&origin=%7B%22x%22%3A-180%2C%22y%22%3A90%7D';
625632
let transformed = mapstudioWebmap.map.options.transformRequest(mockTileUrl, 'Tile');
626633
expect(transformed.credentials).toBe('include');
634+
expect(transformed.headers).toEqual(tileCustomRequestHeaders);
627635
mockTileUrl = 'https://maptiles.supermapol.com/iserver/services/map_China/rest/maps/China_Dark';
628636
transformed = mapstudioWebmap.map.options.transformRequest(mockTileUrl, 'Tile');
629637
expect(transformed.credentials).toBeUndefined();
638+
expect(transformed.headers).toBeUndefined();
630639
done();
631640
});
632641

test/mapboxgl/mapping/utils/L7LayerUtilSpec.js

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ describe('L7LayerUtil', () => {
2020
const options = {
2121
withCredentials: true,
2222
server: 'http://localhost:8190/iportal/',
23-
emitterEvent: function() {}
23+
emitterEvent: function() {},
24+
transformRequest: function() {}
2425
};
2526

2627
const addOptions = {
@@ -481,6 +482,7 @@ describe('L7LayerUtil', () => {
481482
webMapInfo: { ...mapstudioWebMap_L7LayersRes, layers, sources },
482483
l7Layers: layers,
483484
options: {
485+
...addOptions.options,
484486
withCredentials: false,
485487
server: '/iportal/',
486488
iportalServiceProxyUrl: 'http://localhost:8195/portalproxy'
@@ -571,4 +573,78 @@ describe('L7LayerUtil', () => {
571573
expect(result.field).toEqual(['smpid', '新建字段']);
572574
expect(result.values).not.toBeUndefined();
573575
});
576+
577+
it('add mvt source and requestParameters', (done) => {
578+
const layers = [
579+
{
580+
filter: ['all', ['==', 'smpid', 1]],
581+
layout: {
582+
'text-z-offset': 200000,
583+
'text-letter-spacing': 0,
584+
visibility: 'visible',
585+
'text-field': '{smpid}',
586+
'text-anchor': 'center',
587+
'text-size': 36,
588+
'text-allow-overlap': true
589+
},
590+
metadata: {
591+
MapStudio: {
592+
title: 'ms_label_县级行政区划_1719818803020_5'
593+
}
594+
},
595+
maxzoom: 24,
596+
paint: {
597+
'text-halo-color': '#242424',
598+
'text-halo-blur': 2,
599+
'text-color': '#FFFFFF',
600+
'text-halo-width': 1,
601+
'text-opacity': 0.9,
602+
'text-translate': [0, 0]
603+
},
604+
source: 'ms_label_县级行政区划_1719818803020_5_source',
605+
'source-layer': '932916417$geometry',
606+
id: 'ms_label_县级行政区划_1719818803020_5',
607+
type: 'symbol',
608+
minzoom: 0
609+
}
610+
];
611+
const sources = {
612+
ms_label_县级行政区划_1719818803020_5_source: {
613+
tiles: [
614+
'http://localhost:8190/iportal/services/../web/datas/932916417/structureddata/pointonsurface/tiles/{z}/{x}/{y}.mvt?epsgCode=3857&returnedFieldNames=%5B%22smpid%22%2C%22pac%22%2C%22Video%22%2C%22SmUserID%22%2C%22name%22%2C%22Image%22%2C%22objectid%22%2C%22URL%22%5D&geometryFieldName=geometry'
615+
],
616+
bounds: [102.98962307000005, 30.090978575000065, 104.89626180000005, 31.437765225000078],
617+
type: 'vector'
618+
}
619+
};
620+
const tileCustomRequestHeaders = { 'Authorization': 'test token' };
621+
const nextOptions = {
622+
...addOptions,
623+
webMapInfo: { ...mapstudioWebMap_L7LayersRes, layers, sources },
624+
l7Layers: layers,
625+
options: {
626+
...addOptions.options,
627+
transformRequest: function() {
628+
return {
629+
credentials: 'include',
630+
headers: tileCustomRequestHeaders
631+
}
632+
}
633+
}
634+
};
635+
const spy1 = spyOn(nextOptions.map, 'addLayer').and.callThrough();
636+
const spy2 = spyOn(L7, 'PointLayer').and.callFake(mockL7.PointLayer);
637+
addL7Layers(nextOptions).then(() => {
638+
expect(nextOptions.map.addLayer.calls.count()).toEqual(1);
639+
expect(layerMaplist['ms_label_县级行政区划_1719818803020_5']).toBeTruthy();
640+
const matchLayer = layerMaplist['ms_label_县级行政区划_1719818803020_5'].getLayer();
641+
expect(matchLayer.featureId).toBe('smpid');
642+
expect(matchLayer.l7layer.layerSource.parser.requestParameters).not.toBeUndefined();
643+
expect(matchLayer.l7layer.layerSource.parser.requestParameters.credentials).toBe('include');
644+
expect(matchLayer.l7layer.layerSource.parser.requestParameters.headers).toEqual(tileCustomRequestHeaders);
645+
spy1.calls.reset();
646+
spy2.calls.reset();
647+
done();
648+
});
649+
});
574650
});

0 commit comments

Comments
 (0)