Skip to content

Commit 3a7fd04

Browse files
[fix]ICL-1576 mapboxgl设置token后,url参数中未包含token
1 parent 0beeebb commit 3a7fd04

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

src/mapboxgl/mapping/InitMap.js

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { MapService } from '../services/MapService';
33
import { FetchRequest } from '@supermapgis/iclient-common/util/FetchRequest';
44
import { InitMapServiceBase, isPlaneProjection, getZoom, getTileset, getTileFormat } from '@supermapgis/iclient-common/iServer/InitMapServiceBase';
55
import { Util } from '@supermapgis/iclient-common/commontypes/Util';
6+
import { SecurityManager } from '@supermapgis/iclient-common/security/SecurityManager';
67
import proj4 from 'proj4';
78

89
/**
@@ -179,6 +180,7 @@ async function createMapOptions(url, resetServiceInfo, options) {
179180
sourceType === 'vector-tile'
180181
? Util.urlAppend(Util.urlPathAppend(url, 'tileFeature/vectorstyles.json'), 'type=MapBox_GL&styleonly=true&tileURLTemplate=ZXY')
181182
: url;
183+
tileUrl = SecurityManager.appendCredential(tileUrl);
182184
let nonEnhanceExtraInfo = {};
183185
let enhanceExtraInfo = {};
184186
let zoom;

src/maplibregl/mapping/InitMap.js

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { FetchRequest } from '@supermapgis/iclient-common/util/FetchRequest';
33
import { MapService } from '../services/MapService';
44
import { InitMapServiceBase, isPlaneProjection, getZoom, getTileset, getTileFormat } from '@supermapgis/iclient-common/iServer/InitMapServiceBase';
55
import { Util } from '@supermapgis/iclient-common/commontypes/Util';
6+
import { SecurityManager } from '@supermapgis/iclient-common/security/SecurityManager';
67
import proj4 from 'proj4';
78
/**
89
* @function initMap
@@ -172,6 +173,7 @@ async function createMapOptions(url, resetServiceInfo, options) {
172173
sourceType === 'vector-tile'
173174
? Util.urlAppend(Util.urlPathAppend(url, 'tileFeature/vectorstyles.json'), 'type=MapBox_GL&styleonly=true&tileURLTemplate=ZXY')
174175
: url;
176+
tileUrl = SecurityManager.appendCredential(tileUrl);
175177
let nonEnhanceExtraInfo = {};
176178
let enhanceExtraInfo = {};
177179
let zoom;

test/mapboxgl/mapping/InitMapSpec.js

+41
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import mapboxgl from 'mapbox-gl';
22
import mbglmap from '../../tool/mock_mapboxgl_map';
33
import { initMap } from '../../../src/mapboxgl/mapping/InitMap';
44
import { FetchRequest } from '../../../src/common/util/FetchRequest';
5+
import { SecurityManager } from '../../../src/common/security/SecurityManager';
56

67
describe('mapboxgl_InitMap', () => {
78
let originalTimeout, testDiv;
@@ -126,6 +127,46 @@ describe('mapboxgl_InitMap', () => {
126127
url + '/zxyTileImage.png?z={z}&x={x}&y={y}&width=256&height=256&transparent=true'
127128
);
128129
});
130+
it('initMap 3857, registerToken', async () => {
131+
const url = 'http:/fake:8090/iserver/iserver/services/map-china400/rest/maps/China';
132+
const mapServiceInfo = {
133+
dynamicProjection: false,
134+
prjCoordSys: {
135+
epsgCode: 3857
136+
},
137+
bounds: {
138+
top: 20037508.342789087,
139+
left: -20037508.342789248,
140+
bottom: -20037508.34278914,
141+
leftBottom: {
142+
x: -20037508.342789248,
143+
y: -20037508.34278914
144+
},
145+
right: 20037508.342789244,
146+
rightTop: {
147+
x: 20037508.342789244,
148+
y: 20037508.342789087
149+
}
150+
},
151+
center: {
152+
x: -7.450580596923828e-9,
153+
y: -2.60770320892334e-8
154+
}
155+
};
156+
spyOn(FetchRequest, 'get').and.callFake(() => {
157+
return Promise.resolve(new Response(JSON.stringify(mapServiceInfo)));
158+
});
159+
SecurityManager.registerToken(url, "test-token")
160+
const resData = await initMap(url);
161+
const map = resData.map;
162+
expect(map).not.toBeUndefined();
163+
expect(map.options.crs).toBe('EPSG:3857');
164+
expect(map.options.style.layers.length).toBe(1);
165+
expect(Object.values(map.options.style.sources)[0].tiles[0]).toBe(
166+
url + '/zxyTileImage.png?token=test-token&z={z}&x={x}&y={y}&width=256&height=256&transparent=true'
167+
);
168+
SecurityManager.destroyToken(url)
169+
});
129170

130171
it('initMap 4326, dynamicProjection true, non mapbox-gl-enhance', async () => {
131172
const url = 'http:/fake:8090/iserver/iserver/services/map-china400/rest/maps/China';

test/maplibregl/mapping/InitMapSpec.js

+42
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import maplibregl from 'maplibre-gl';
22
import mbglmap from '../../tool/mock_maplibregl_map';
33
import { initMap } from '../../../src/maplibregl/mapping/InitMap';
44
import { FetchRequest } from '../../../src/common/util/FetchRequest';
5+
import { SecurityManager } from '../../../src/common/security/SecurityManager';
56

67
describe('maplibregl_InitMap', () => {
78
let originalTimeout, testDiv;
@@ -120,6 +121,47 @@ describe('maplibregl_InitMap', () => {
120121
expect(map.getCenter().toArray()).not.toEqual([mapServiceInfo.center.x, mapServiceInfo.center.y]);
121122
});
122123

124+
it('initMap 3857, registerToken', async () => {
125+
const url = GlobeParameter.ChinaURL;
126+
const mapServiceInfo = {
127+
dynamicProjection: false,
128+
prjCoordSys: {
129+
epsgCode: 3857
130+
},
131+
bounds: {
132+
top: 20037508.342789087,
133+
left: -20037508.342789248,
134+
bottom: -20037508.34278914,
135+
leftBottom: {
136+
x: -20037508.342789248,
137+
y: -20037508.34278914
138+
},
139+
right: 20037508.342789244,
140+
rightTop: {
141+
x: 20037508.342789244,
142+
y: 20037508.342789087
143+
}
144+
},
145+
center: {
146+
x: -7.450580596923828e-9,
147+
y: -2.60770320892334e-8
148+
}
149+
};
150+
spyOn(FetchRequest, 'get').and.callFake(() => {
151+
return Promise.resolve(new Response(JSON.stringify(mapServiceInfo)));
152+
});
153+
SecurityManager.registerToken(url, "test-token")
154+
const resData = await initMap(url);
155+
const map = resData.map;
156+
expect(map).not.toBeUndefined();
157+
expect(map.getCenter().toArray()).not.toEqual([mapServiceInfo.center.x, mapServiceInfo.center.y]);
158+
expect(Object.values(map.options.style.sources)[0].tiles[0]).toBe(
159+
url + '/zxyTileImage.png?token=test-token&z={z}&x={x}&y={y}&width=256&height=256&transparent=true'
160+
);
161+
SecurityManager.destroyToken(url)
162+
});
163+
164+
123165
it('initMap 4326, dynamicProjection true', async () => {
124166
const url = GlobeParameter.ChinaURL;
125167
const mapServiceInfo = {

0 commit comments

Comments
 (0)