Skip to content

Commit 8d602f0

Browse files
committed
【feature】1) webmap中出图加上crossOrigin参数,防止报错跨域
2)判断是否增加代理,和dv保持一致。一级域名一样不加代理 (reviewed by yuzy)
1 parent 772042a commit 8d602f0

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

src/openlayers/mapping/WebMap.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -681,9 +681,9 @@ export class WebMap extends ol.Observable {
681681
url: layerInfo.url,
682682
wrapX: false,
683683
serverType: serverType,
684+
crossOrigin: 'anonymous',
684685
// extent: this.baseLayerExtent,
685-
prjCoordSys:{ epsgCode: isBaseLayer ? layerInfo.projection.split(':')[1] : this.baseProjection.split(':')[1] },
686-
tileProxy: this.tileProxy
686+
prjCoordSys:{ epsgCode: isBaseLayer ? layerInfo.projection.split(':')[1] : this.baseProjection.split(':')[1] }
687687
};
688688
if(layerInfo.visibleScales && layerInfo.visibleScales.length >0){
689689
let result = this.getReslutionsFromScales(layerInfo.visibleScales, 96, layerInfo.coordUnit);
@@ -695,6 +695,10 @@ export class WebMap extends ol.Observable {
695695
options.tileGrid = tileGrid;
696696
}else{
697697
options.extent = this.baseLayerExtent;
698+
}
699+
//主机名相同时不添加代理
700+
if (layerInfo.url && !this.isSameDomain(layerInfo.url)) {
701+
options.tileProxy = this.server + 'apps/viewer/getUrlResource.png?url=';
698702
}
699703
let source = new ol.source.TileSuperMapRest(options);
700704
SecurityManager[`register${keyfix}`](layerInfo.url);
@@ -739,6 +743,7 @@ export class WebMap extends ol.Observable {
739743
return new ol.source.XYZ({
740744
wrapX: false,
741745
projection: projection,
746+
crossOrigin: 'anonymous',
742747
tileUrlFunction: function (coordinates) {
743748
let /*quadDigits = '', */[z, x, y] = [...coordinates];
744749
y = y > 0 ? y - 1 : -y - 1;
@@ -770,7 +775,7 @@ export class WebMap extends ol.Observable {
770775
return new ol.source.XYZ({
771776
url: layerInfo.url,
772777
wrapX: false,
773-
crossOrigin: window.location.host
778+
crossOrigin: 'anonymous'
774779
})
775780
}
776781

@@ -3411,6 +3416,26 @@ export class WebMap extends ol.Observable {
34113416
}
34123417
return resolutions;
34133418
}
3419+
/**
3420+
* 判断是否同域名(如果是域名,只判断后门两级域名是否相同,第一级忽略),如果是ip地址则需要完全相同。
3421+
* @param {*} url
3422+
*/
3423+
isSameDomain (url) {
3424+
let documentUrlArray = url.split("://"), substring = documentUrlArray[1];
3425+
let domainIndex = substring.indexOf("/"), domain = substring.substring(0, domainIndex);
3426+
3427+
let documentUrl = document.location.toString();
3428+
let docUrlArray = documentUrl.split("://"), documentSubstring = docUrlArray[1];
3429+
let docuDomainIndex = documentSubstring.indexOf("/"), docDomain = documentSubstring.substring(0, docuDomainIndex);
3430+
3431+
if(domain.indexOf(':') >-1 || window.location.port !== "") {
3432+
//说明用的是ip地址,判断完整域名判断
3433+
return domain === docDomain;
3434+
} else {
3435+
let domainArray = domain.split('.'), docDomainArray = docDomain.split('.');
3436+
return domainArray[1] === docDomainArray[1] && domainArray[2] === docDomainArray[2];
3437+
}
3438+
}
34143439
}
34153440

34163441
ol.supermap.WebMap = WebMap;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy