Skip to content

Commit 7d21a3f

Browse files
committed
【fix】修复webmap 雪碧图 l7layer中options.withCredentials的值的问题
AI-GEN: 10% Claude
1 parent 559d8c4 commit 7d21a3f

9 files changed

Lines changed: 91 additions & 26 deletions

File tree

src/common/mapping/WebMapService.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,12 @@ export class WebMapService {
10051005

10061006
return defaultValue;
10071007
}
1008+
handleUrlWithCredentials(serviceUrl) {
1009+
if (serviceUrl && this.iportalServiceProxyUrl && serviceUrl.indexOf(this.iportalServiceProxyUrl) >= 0) {
1010+
return true;
1011+
}
1012+
return null;
1013+
}
10081014

10091015
isIportalResourceUrl(serviceUrl) {
10101016
return (

src/common/mapping/WebMapV3.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ export const LEGEND_STYLE_TYPES = {
160160
IMAGE: 'image',
161161
STYLE: 'style'
162162
};
163-
export function createWebMapV3Extending(SuperClass, { MapManager, mapRepo, crsManager, l7LayerUtil }) {
163+
export function createWebMapV3Extending(SuperClass, { MapManager, mapRepo, crsManager, l7LayerUtil, webMapService }) {
164164
return class WebMapV3 extends SuperClass {
165165
constructor(mapId, options, mapOptions = {}) {
166166
super();
@@ -495,7 +495,7 @@ export function createWebMapV3Extending(SuperClass, { MapManager, mapRepo, crsMa
495495
const mapResourceUrl = transformUrl(
496496
Object.assign({ url: `${this.options.server}web/maps/${this.mapId}` }, this.options)
497497
);
498-
return FetchRequest.get(mapResourceUrl, null, { withCredentials: this.options.withCredentials }).then((response) =>
498+
return FetchRequest.get(mapResourceUrl, null, { withCredentials: webMapService.handleUrlWithCredentials(mapResourceUrl) }).then((response) =>
499499
response.json()
500500
);
501501
}
@@ -797,7 +797,7 @@ export function createWebMapV3Extending(SuperClass, { MapManager, mapRepo, crsMa
797797

798798
_getSpriteData(sprite) {
799799
const url = sprite.replace(/.+(web\/maps\/.+)/, `${this.options.server}$1`);
800-
return FetchRequest.get(url, null, { withCredentials: this.options.withCredentials })
800+
return FetchRequest.get(url, null, { withCredentials: webMapService.handleUrlWithCredentials(url) })
801801
.then((response) => {
802802
return response.json();
803803
});

src/common/mapping/utils/L7LayerUtil.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ export function getL7Filter(filter, featureFilter) {
220220
}
221221

222222
export function L7LayerUtil(config) {
223-
const { featureFilter, expression, spec, L7Layer, L7, proj4 } = config;
223+
const { featureFilter, expression, spec, L7Layer, L7, proj4, webMapService } = config;
224224

225225
/**
226226
* @param {string} url
@@ -244,7 +244,7 @@ export function L7LayerUtil(config) {
244244
*/
245245
function getRestDataFields(datasetUrl, credential, options) {
246246
const url = addCredentialToUrl(`${datasetUrl}/fields.json?returnAll=true`, credential);
247-
return FetchRequest.get(url, null, options)
247+
return FetchRequest.get(url, null, { ...options, withCredentials: webMapService.handleUrlWithCredentials(url) })
248248
.then((res) => res.json())
249249
.then((result) => {
250250
return result.map((item) => {
@@ -270,7 +270,7 @@ export function L7LayerUtil(config) {
270270
*/
271271
function getRestDataDomains(datasetUrl, credential, options) {
272272
const url = addCredentialToUrl(`${datasetUrl}/domain.json`, credential);
273-
return FetchRequest.get(url, null, options).then((result) => {
273+
return FetchRequest.get(url, null, { ...options, withCredentials: webMapService.handleUrlWithCredentials(url) }).then((result) => {
274274
return result.json();
275275
});
276276
}
@@ -453,7 +453,7 @@ export function L7LayerUtil(config) {
453453
const nextOptions = handleWithRequestOptions(datasetUrl, options);
454454
const { fieldNames, fieldTypes } = await getRestDataFieldInfo(datasetUrl, credential, nextOptions);
455455
const nextUrl = addCredentialToUrl(url, credential);
456-
const attrDataInfo = await FetchRequest.post(nextUrl, JSON.stringify(SQLParams), nextOptions);
456+
const attrDataInfo = await FetchRequest.post(nextUrl, JSON.stringify(SQLParams), { ...nextOptions, withCredentials: webMapService.handleUrlWithCredentials(nextUrl) });
457457
const featuresRes = await attrDataInfo.json();
458458

459459
return {
@@ -468,7 +468,7 @@ export function L7LayerUtil(config) {
468468
* @param option
469469
*/
470470
function getStructDataItemJson(href, option) {
471-
return FetchRequest.get(href, null, option)
471+
return FetchRequest.get(href, null, { ...option, withCredentials: webMapService.handleUrlWithCredentials(href) })
472472
.then((res) => res.json())
473473
.then((data) => {
474474
if (data.succeed === false) {
@@ -616,10 +616,11 @@ export function L7LayerUtil(config) {
616616
*/
617617
async function getStructuredDataGeojsonByWebMap(data, options) {
618618
const allFeature = await getStructDataGeojson(data.dataId, options);
619+
const url = `${options.server}web/datas/${data.dataId}/structureddata.json`
619620
const resultRes = await FetchRequest.get(
620-
`${options.server}web/datas/${data.dataId}/structureddata.json`,
621+
url,
621622
null,
622-
options
623+
{ ...options, withCredentials: webMapService.handleUrlWithCredentials(url) }
623624
);
624625
const result = await resultRes.json();
625626
const projection = `EPSG:${result.epsgCode}`;

src/mapboxgl/mapping/WebMap.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,17 @@ export class WebMap extends createWebMapBaseExtending(mapboxgl.Evented, { mapRep
122122
spec,
123123
L7Layer,
124124
L7,
125-
proj4: this._crsManager.getProj4()
125+
proj4: this._crsManager.getProj4(),
126+
webMapService: this.webMapService
126127
});
127128
switch (type) {
128129
case 'MapStyle':
129130
return createMapStyleExtending(createMapClassExtending(mapboxgl.Evented), commonFactoryOptions);
130131
case 'WebMap3':
131132
return createWebMapV3Extending(createMapClassExtending(mapboxgl.Evented), {
132133
...commonFactoryOptions,
133-
l7LayerUtil
134+
l7LayerUtil,
135+
webMapService: this.webMapService
134136
});
135137
default:
136138
return createWebMapV2Extending(

src/maplibregl/mapping/WebMap.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,17 @@ export class WebMap extends createWebMapBaseExtending(maplibregl.Evented, { mapR
121121
spec,
122122
L7Layer,
123123
L7,
124-
proj4: this._crsManager.getProj4()
124+
proj4: this._crsManager.getProj4(),
125+
webMapService: this.webMapService
125126
});
126127
switch (type) {
127128
case 'MapStyle':
128129
return createMapStyleExtending(createMapClassExtending(maplibregl.Evented), commonFactoryOptions);
129130
case 'WebMap3':
130131
return createWebMapV3Extending(createMapClassExtending(maplibregl.Evented), {
131132
...commonFactoryOptions,
132-
l7LayerUtil
133+
l7LayerUtil,
134+
webMapService: this.webMapService
133135
});
134136
default:
135137
return createWebMapV2Extending(

test/common/mapping/utils/L7LayerUtilSpec.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ describe('L7LayerUtil', () => {
3535

3636
reRender() {}
3737
};
38-
const l7LayerUtil = L7LayerUtil({ featureFilter, expression, spec, L7Layer, L7 });
38+
const mockWebMapService = {
39+
handleUrlWithCredentials: jasmine.createSpy('handleUrlWithCredentials').and.returnValue(true)
40+
};
41+
const l7LayerUtil = L7LayerUtil({ featureFilter, expression, spec, L7Layer, L7 ,webMapService: mockWebMapService});
3942
const mapstudioWebMap_L7LayersRes = JSON.parse(mapstudioWebMap_L7Layers);
4043

4144
const scene = new mockL7.Scene();
@@ -185,7 +188,7 @@ describe('L7LayerUtil', () => {
185188

186189
it('animate line layer', (done) => {
187190
spyOn(FetchRequest, 'get').and.callFake((url, _, options) => {
188-
expect(options.withCredentials).toBeUndefined();
191+
expect(options.withCredentials).toBeTruthy();
189192
expect(options.withoutFormatSuffix).toBeTruthy();
190193
if (url.indexOf('/data-Building/rest/data/datasources/newBuilding/datasets/New_LINE/fields.json') > -1) {
191194
return Promise.resolve(new Response(RESTDATA_FIELDS_RES));
@@ -196,7 +199,7 @@ describe('L7LayerUtil', () => {
196199
return Promise.resolve();
197200
});
198201
spyOn(FetchRequest, 'post').and.callFake((url, _, options) => {
199-
expect(options.withCredentials).toBeUndefined();
202+
expect(options.withCredentials).toBeTruthy();
200203
expect(options.withoutFormatSuffix).toBeTruthy();
201204
if (url.indexOf('/data-Building/rest/data/featureResults.geojson') > -1) {
202205
return Promise.resolve(new Response(RESTDATA_FEATURES_RES));
@@ -286,7 +289,8 @@ describe('L7LayerUtil', () => {
286289

287290
it('add layer one error, one success', (done) => {
288291
spyOn(FetchRequest, 'get').and.callFake((url, _, options) => {
289-
expect(options.withCredentials).toBeUndefined();
292+
console.log('get', url, options.withCredentials);
293+
expect(options.withCredentials).toBeTruthy();
290294
expect(options.withoutFormatSuffix).toBeTruthy();
291295
if (url.indexOf('/data-Building/rest/data/datasources/newBuilding/datasets/New_LINE/fields.json') > -1) {
292296
return Promise.resolve(new Response(RESTDATA_FIELDS_RES));
@@ -297,7 +301,8 @@ describe('L7LayerUtil', () => {
297301
return Promise.resolve();
298302
});
299303
spyOn(FetchRequest, 'post').and.callFake((url, _, options) => {
300-
expect(options.withCredentials).toBeUndefined();
304+
console.log('post', url, options.withCredentials);
305+
expect(options.withCredentials).toBeTruthy();
301306
expect(options.withoutFormatSuffix).toBeTruthy();
302307
if (url.indexOf('/data-Building/rest/data/featureResults.geojson') > -1) {
303308
return Promise.reject('error test');

test/mapboxgl/mapping/WebMapV3Spec.js

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@ describe('mapboxgl-webmap3.0', () => {
2020
var id = 617580084;
2121
var mapstudioWebmap;
2222
const l7LayerUtil = L7LayerUtil({ featureFilter, expression, spec, L7Layer, L7 });
23+
const mockWebMapService = {
24+
handleUrlWithCredentials: jasmine.createSpy('handleUrlWithCredentials').and.returnValue(true)
25+
};
2326
const extendOptions = {
2427
MapManager: MapManagerUtil.default,
2528
mapRepo: mapboxgl,
2629
crsManager: new CRSManager(),
27-
l7LayerUtil
30+
l7LayerUtil,
31+
webMapService: mockWebMapService
2832
};
2933
const WebMapV3 = createWebMapV3Extending(createMapClassExtending(mapboxgl.Evented), extendOptions);
3034
beforeEach(() => {
@@ -37,7 +41,7 @@ describe('mapboxgl-webmap3.0', () => {
3741
testDiv.style.height = '500px';
3842
window.document.body.appendChild(testDiv);
3943
originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
40-
jasmine.DEFAULT_TIMEOUT_INTERVAL = 50000;
44+
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
4145
mapboxgl.Map.prototype.overlayLayersManager = {};
4246
mbglmap.prototype.getL7Scene = mapboxgl.Map.prototype.getL7Scene;
4347
mapboxgl.CRS = CRS;
@@ -172,7 +176,7 @@ describe('mapboxgl-webmap3.0', () => {
172176
if (url.indexOf('932266699.json') > -1) {
173177
return Promise.resolve(new Response(msProjectINfo_filters));
174178
}
175-
if (url.indexOf('/sprites') > -1) {
179+
if (url.indexOf('/sprite') > -1) {
176180
return Promise.resolve(new Response(spriteJson));
177181
}
178182
return Promise.resolve();
@@ -204,7 +208,7 @@ describe('mapboxgl-webmap3.0', () => {
204208
});
205209
it('filters mapId is JSON', (done) => {
206210
spyOn(FetchRequest, 'get').and.callFake((url) => {
207-
if (url.indexOf('/sprites') > -1) {
211+
if (url.indexOf('/sprite') > -1) {
208212
return Promise.resolve(new Response(msSpriteInfo));
209213
}
210214
return Promise.resolve();
@@ -8454,4 +8458,25 @@ describe('mapboxgl-webmap3.0', () => {
84548458
expect(mapstudioWebmap._appendLayers).toBe(true);
84558459
expect(map.addLocalIdeographFontFamily).toHaveBeenCalledWith('sans-serif,PingFang SC Regular');
84568460
});
8461+
8462+
it('_getSpriteData should use webMapService.handleWithCredentials for withCredentials', (done) => {
8463+
8464+
const spriteUrl = 'http://example.com/web/maps/123/sprite.json';
8465+
spyOn(FetchRequest, 'get').and.callFake((url, params, options) => {
8466+
if (url.indexOf('sprite.json') > -1) {
8467+
expect(mockWebMapService.handleUrlWithCredentials).toHaveBeenCalledWith(url);
8468+
expect(options.withCredentials).toBe(true);
8469+
return Promise.resolve(new Response(JSON.stringify({})));
8470+
}
8471+
return Promise.resolve(new Response(JSON.stringify({})));
8472+
});
8473+
8474+
mapstudioWebmap = new WebMapV3(id, { server, target: 'map' });
8475+
mapstudioWebmap._getSpriteData(spriteUrl).then(() => {
8476+
expect(mockWebMapService.handleUrlWithCredentials).toHaveBeenCalled();
8477+
done();
8478+
}).catch((e) => {
8479+
done.fail(e);
8480+
});
8481+
});
84578482
});

test/maplibregl/mapping/WebMapV3Spec.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@ describe('maplibregl-webmap3.0', () => {
2020
var id = 617580084;
2121
var mapstudioWebmap;
2222
const l7LayerUtil = L7LayerUtil({ featureFilter, expression, spec, L7Layer, L7 });
23+
const mockWebMapService = {
24+
handleUrlWithCredentials: jasmine.createSpy('handleUrlWithCredentials').and.returnValue(true)
25+
};
2326
const extendOptions = {
2427
MapManager: MapManagerUtil.default,
2528
mapRepo: maplibregl,
2629
crsManager: new CRSManager(),
27-
l7LayerUtil
30+
l7LayerUtil,
31+
webMapService: mockWebMapService
2832
};
2933
const WebMapV3 = createWebMapV3Extending(createMapClassExtending(maplibregl.Evented), extendOptions);
3034
beforeEach(() => {
@@ -1619,4 +1623,24 @@ describe('maplibregl-webmap3.0', () => {
16191623
});
16201624
});
16211625
});
1626+
it('_getSpriteData should use webMapService.handleWithCredentials for withCredentials', (done) => {
1627+
1628+
const spriteUrl = 'http://example.com/web/maps/123/sprite.json';
1629+
spyOn(FetchRequest, 'get').and.callFake((url, params, options) => {
1630+
if (url.indexOf('sprite.json') > -1) {
1631+
expect(mockWebMapService.handleUrlWithCredentials).toHaveBeenCalledWith(url);
1632+
expect(options.withCredentials).toBe(true);
1633+
return Promise.resolve(new Response(JSON.stringify({})));
1634+
}
1635+
return Promise.resolve(new Response(JSON.stringify({})));
1636+
});
1637+
1638+
mapstudioWebmap = new WebMapV3(id, { server, target: 'map' });
1639+
mapstudioWebmap._getSpriteData(spriteUrl).then(() => {
1640+
expect(mockWebMapService.handleUrlWithCredentials).toHaveBeenCalled();
1641+
done();
1642+
}).catch((e) => {
1643+
done.fail(e);
1644+
});
1645+
});
16221646
});

test/resources/WebMapV3.js

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)