Skip to content

Commit 5d6b0b3

Browse files
committed
【feature】1) iserver rest map地图,在支持webp的前提下,使用webp出图,否则还使用之前png出图方式
【bug】1) 通过web添加,有令牌的restmap服务。报错出图不了 (reviewed by chengl)
1 parent 758b563 commit 5d6b0b3

File tree

1 file changed

+106
-5
lines changed

1 file changed

+106
-5
lines changed

src/openlayers/mapping/WebMap.js

Lines changed: 106 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,7 @@ export class WebMap extends Observable {
882882
*/
883883
createDynamicTiledSource(layerInfo, isBaseLayer) {
884884
let serverType = "IPORTAL",
885-
credential = layerInfo.credential,
885+
credential = layerInfo.credential ? layerInfo.credential.token : undefined,
886886
keyfix = 'Token',
887887
keyParams = layerInfo.url;
888888

@@ -902,7 +902,8 @@ export class WebMap extends Observable {
902902
serverType: serverType,
903903
crossOrigin: 'anonymous',
904904
// extent: this.baseLayerExtent,
905-
prjCoordSys:{ epsgCode: isBaseLayer ? layerInfo.projection.split(':')[1] : this.baseProjection.split(':')[1] }
905+
prjCoordSys:{ epsgCode: isBaseLayer ? layerInfo.projection.split(':')[1] : this.baseProjection.split(':')[1] },
906+
format: layerInfo.format
906907
};
907908
if(layerInfo.visibleScales && layerInfo.visibleScales.length >0){
908909
let visibleResolutions = [];
@@ -1069,6 +1070,9 @@ export class WebMap extends Observable {
10691070
}
10701071
// bug IE11 不会自动编码
10711072
url += '.json?prjCoordSys=' + encodeURI(JSON.stringify(projection));
1073+
if(layerInfo.credential) {
1074+
url = `${url}&token=${encodeURI(layerInfo.credential.token)}`;
1075+
}
10721076

10731077
}else{
10741078
url += (url.indexOf('?') > -1 ? '&SERVICE=WMS&REQUEST=GetCapabilities' : '?SERVICE=WMS&REQUEST=GetCapabilities');
@@ -1079,10 +1083,13 @@ export class WebMap extends Observable {
10791083
};
10801084
FetchRequest.get(that.getRequestUrl(url), null, options).then(function (response) {
10811085
return layerInfo.layerType === "TILE" ? response.json() : response.text();
1082-
}).then(function (result) {
1086+
}).then(async function (result) {
10831087
if (layerInfo.layerType === "TILE") {
10841088
layerInfo.extent = [result.bounds.left, result.bounds.bottom, result.bounds.right, result.bounds.top];
10851089
layerInfo.projection = `EPSG:${result.prjCoordSys.epsgCode}`;
1090+
let token = layerInfo.credential ? layerInfo.credential.token : undefined;
1091+
let isSupprtWebp = await that.isSupportWebp(layerInfo.url, token);
1092+
layerInfo.format = isSupprtWebp ? 'webp' : 'png';
10861093
callback(layerInfo);
10871094
} else {
10881095
layerInfo.projection = that.baseProjection;
@@ -1111,7 +1118,7 @@ export class WebMap extends Observable {
11111118
}
11121119
return FetchRequest.get(that.getRequestUrl(`${layerInfo.url}.json`), null, options).then(function (response) {
11131120
return response.json();
1114-
}).then(function (result) {
1121+
}).then(async function (result) {
11151122
// layerInfo.projection = mapInfo.projection;
11161123
// layerInfo.extent = [mapInfo.extent.leftBottom.x, mapInfo.extent.leftBottom.y, mapInfo.extent.rightTop.x, mapInfo.extent.rightTop.y];
11171124
// 比例尺 单位
@@ -1121,8 +1128,12 @@ export class WebMap extends Observable {
11211128
}
11221129
layerInfo.maxZoom = result.maxZoom;
11231130
layerInfo.maxZoom = result.minZoom;
1131+
let token = layerInfo.credential ? layerInfo.credential.token : undefined;
1132+
let isSupprtWebp = await that.isSupportWebp(layerInfo.url, token);
1133+
layerInfo.format = isSupprtWebp ? 'webp' : 'png';
11241134
// 请求结果完成 继续添加图层
11251135
if(mapInfo){
1136+
//todo 这个貌似没有用到,下次优化
11261137
callback && callback(mapInfo, null, true, that);
11271138
}else{
11281139
callback && callback(layerInfo);
@@ -4170,7 +4181,9 @@ export class WebMap extends Observable {
41704181
return resolutions;
41714182
}
41724183
/**
4173-
* 判断是否同域名(如果是域名,只判断后门两级域名是否相同,第一级忽略),如果是ip地址则需要完全相同。
4184+
* @private
4185+
* @function ol.supermap.WebMap.prototype.isSameDomain
4186+
* @description 判断是否同域名(如果是域名,只判断后门两级域名是否相同,第一级忽略),如果是ip地址则需要完全相同。
41744187
* @param {*} url
41754188
*/
41764189
isSameDomain (url) {
@@ -4189,4 +4202,92 @@ export class WebMap extends Observable {
41894202
return domainArray[1] === docDomainArray[1] && domainArray[2] === docDomainArray[2];
41904203
}
41914204
}
4205+
/**
4206+
* @private
4207+
* @function ol.supermap.WebMap.prototype.isSupportWebp
4208+
* @description 判断是否支持webP
4209+
* @param {*} url 服务地址
4210+
* @param {*} token 服务token
4211+
* @returns {boolean}
4212+
*/
4213+
isSupportWebp(url, token) {
4214+
// 还需要判断浏览器
4215+
let isIE = this.isIE();
4216+
if(isIE || (this.isFirefox() && this.getFirefoxVersion() < 65) ||
4217+
(this.isChrome() && this.getChromeVersion() < 32)) {
4218+
return false;
4219+
}
4220+
url = token ? `${url}/tileImage.webp?token=${token}` : `${url}/tileImage.webp`;
4221+
url = this.getRequestUrl(url);
4222+
return FetchRequest.get(url, null, {
4223+
withCredentials: this.withCredentials
4224+
}).then(function (response) {
4225+
if(response.status !== 200) {
4226+
throw response.status;
4227+
}
4228+
return response;
4229+
}).then(() => {
4230+
return true;
4231+
}).catch(() => {
4232+
return false;
4233+
})
4234+
}
4235+
/**
4236+
* @private
4237+
* @function ol.supermap.WebMap.prototype.isIE
4238+
* @description 判断当前浏览器是否为IE
4239+
* @returns {boolean}
4240+
*/
4241+
isIE() {
4242+
if (!!window.ActiveXObject || "ActiveXObject" in window) {
4243+
return true;
4244+
}
4245+
return false;
4246+
}
4247+
4248+
/**
4249+
* @private
4250+
* @function ol.supermap.WebMap.prototype.isFirefox
4251+
* @description 判断当前浏览器是否为 firefox
4252+
* @returns {boolean}
4253+
*/
4254+
isFirefox(){
4255+
let userAgent = navigator.userAgent;
4256+
return userAgent.indexOf("Firefox") > -1;
4257+
}
4258+
4259+
/**
4260+
* @private
4261+
* @function ol.supermap.WebMap.prototype.isChrome
4262+
* @description 判断当前浏览器是否为谷歌
4263+
* @returns {boolean}
4264+
*/
4265+
isChrome() {
4266+
let userAgent = navigator.userAgent;
4267+
return userAgent.indexOf("Chrome") > -1;
4268+
}
4269+
4270+
/**
4271+
* @private
4272+
* @function ol.supermap.WebMap.prototype.getFirefoxVersion
4273+
* @description 获取火狐浏览器的版本号
4274+
* @returns {Number}
4275+
*/
4276+
getFirefoxVersion() {
4277+
let userAgent = navigator.userAgent.toLowerCase(),
4278+
version = userAgent.match(/firefox\/([\d.]+)/);
4279+
return +version[1];
4280+
}
4281+
4282+
/**
4283+
* @private
4284+
* @function ol.supermap.WebMap.prototype.getChromeVersion
4285+
* @description 获取谷歌浏览器版本号
4286+
* @returns {Number}
4287+
*/
4288+
getChromeVersion() {
4289+
let userAgent = navigator.userAgent.toLowerCase(),
4290+
version = userAgent.match(/chrome\/([\d.]+)/);
4291+
return +version[1];
4292+
}
41924293
}

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