Skip to content

Commit 6cd0021

Browse files
author
caoxinke
committed
优化Request。
1 parent d1a5c65 commit 6cd0021

File tree

11 files changed

+7607
-6486
lines changed

11 files changed

+7607
-6486
lines changed

dist/iclient9-leaflet.js

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

dist/iclient9-openlayers.js

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

src/common/iPortal/iPortal.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ SuperMap.iPortal = SuperMap.Class({
66

77
initialize: function (iportalUrl, token) {
88
this.iportalUrl = iportalUrl;
9-
this.request = new SuperMap.Request();
109
this.token = token;
1110
},
1211

1312
load: function () {
1413
var me = this;
15-
return me.request.get(me.iportalUrl + '/web').then(function (result) {
14+
return SuperMap.Request.get(me.iportalUrl + '/web').then(function (result) {
1615
if (result) {
1716
SuperMap.Credential.CREDENTIAL = new SuperMap.Credential(me.token, 'token');
1817
}
@@ -21,7 +20,7 @@ SuperMap.iPortal = SuperMap.Class({
2120

2221
queryServices: function (queryParams) {
2322
var serviceUrl = this.iportalUrl + "/web/services";
24-
return this.request.get(serviceUrl, queryParams).then(function (result) {
23+
return SuperMap.Request.get(serviceUrl, queryParams).then(function (result) {
2524
var services = [];
2625
result.content.map(function (serviceJsonObj) {
2726
services.push(new SuperMap.iPortalService(serviceUrl, serviceJsonObj));
@@ -32,7 +31,7 @@ SuperMap.iPortal = SuperMap.Class({
3231

3332
deleteServices: function (ids) {
3433
var serviceUrl = this.iportalUrl + "/web/services";
35-
return this.request.delete(serviceUrl, {ids: ids});
34+
return SuperMap.Request.delete(serviceUrl, {ids: ids});
3635
}
3736

3837
})

src/common/iPortal/iPortalService.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,11 @@ SuperMap.iPortalService = SuperMap.Class({
3434
params = params || {};
3535
SuperMap.Util.extend(this, params);
3636
this.serviceUrl = seviceUrl + "/" + this.id;
37-
this.request = new SuperMap.Request();
3837
},
3938

4039
load: function () {
4140
var me = this;
42-
return me.request.get(me.serviceUrl).then(function (serviceInfo) {
41+
return SuperMap.Request.get(me.serviceUrl).then(function (serviceInfo) {
4342
for (var key in serviceInfo) {
4443
me[key] = serviceInfo[key];
4544
}
@@ -56,7 +55,7 @@ SuperMap.iPortalService = SuperMap.Class({
5655
var options = {
5756
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
5857
};
59-
return this.request.put(this.serviceUrl, JSON.stringify(serviceUpdateParam), options);
58+
return SuperMap.Request.put(this.serviceUrl, JSON.stringify(serviceUpdateParam), options);
6059
}
6160

6261
});

src/common/online/Online.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ SuperMap.Online = SuperMap.Class({
3131
},
3232

3333
load: function () {
34-
return fetch(this.rootUrl).then(function (response) {
34+
return SuperMap.Request.get(this.rootUrl).then(function (response) {
3535
return response;
3636
});
3737
},

src/common/online/OnlineData.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ SuperMap.OnlineData = SuperMap.Class({
7777
return;
7878
}
7979
var me = this;
80-
return new new SuperMap.Request().get(this.serviceUrl).then(function (result) {
80+
return SuperMap.Request.get(this.serviceUrl).then(function (result) {
8181
SuperMap.Util.extend(me, result);
8282
});
8383
},

src/common/util/Request.js

Lines changed: 34 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,13 @@ SuperMap.Support = {
55
cors: ((window.XMLHttpRequest && 'withCredentials' in new window.XMLHttpRequest()))
66
};
77

8-
SuperMap.Request = SuperMap.Class({
9-
10-
initialize: function () {
11-
},
8+
SuperMap.Request = {
129

1310
get: function (url, params, options) {
14-
options = options || {};
1511
var type = 'GET';
16-
url = url.endWith('.json') ? url : url + '.json';
17-
url = params ? SuperMap.Util.urlAppend(url, this.getParameterString(params || {})) : url;
18-
var tokenParam = '';
19-
var separator = url.indexOf("?") > -1 ? "&" : "?";
20-
if (SuperMap.Credential.CREDENTIAL && SuperMap.Credential.CREDENTIAL.getUrlParameters()) {
21-
tokenParam = SuperMap.Credential.CREDENTIAL.getUrlParameters();
22-
}
23-
var requestLength = url.length;
24-
if (requestLength <= 2000) {
25-
url = tokenParam !== '' ? url + separator + tokenParam : url;
12+
url = this._appendUrlTokenParameter(url);
13+
url = SuperMap.Util.urlAppend(url, this._getParameterString(params || {}));
14+
if (url.length <= 2000) {
2615
if (SuperMap.Support.cors) {
2716
return this._fetch(url, params, options, type);
2817
}
@@ -31,90 +20,73 @@ SuperMap.Request = SuperMap.Class({
3120
return this._fetchJsonp(url, options);
3221
}
3322
}
34-
url = url.substring(0, url.indexOf('?')) + '_method= GET';
35-
url = tokenParam !== '' ? url + '&' + tokenParam : url;
36-
return this.post(url, params, options);
23+
return this._postSimulatie(type, url.substring(0, url.indexOf('?') - 1), params, options);
3724
},
3825

3926
delete: function (url, params, options) {
40-
options = options || {};
41-
url = url.endWith('.json') ? url : url + '.json';
4227
var type = 'DELETE';
43-
var requestLength = url.length;
44-
url = params ? SuperMap.Util.urlAppend(url, this.getParameterString(params || {})) : url;
45-
var tokenParam = '';
46-
var separator = url.indexOf("?") > -1 ? "&" : "?";
47-
if (SuperMap.Credential.CREDENTIAL && SuperMap.Credential.CREDENTIAL.getUrlParameters()) {
48-
tokenParam = SuperMap.Credential.CREDENTIAL.getUrlParameters();
49-
}
50-
if (requestLength <= 2000) {
51-
if (SuperMap.Support.cors) {
52-
url = tokenParam !== '' ? url + separator + tokenParam : url;
53-
return this._fetch(url, params, options, type);
54-
}
28+
url = this._appendUrlTokenParameter(url);
29+
url = SuperMap.Util.urlAppend(url, this._getParameterString(params || {}));
30+
if (url.length <= 2000 && SuperMap.Support.cors) {
31+
return this._fetch(url, params, options, type);
5532
}
56-
url = url.substring(0, url.indexOf('?')) + '_method= DELETE';
57-
url = tokenParam !== '' ? url + '&' + tokenParam : url;
58-
return this.post(url, params, options);
33+
return this._postSimulatie(type, url.substring(0, url.indexOf('?') - 1), params, options);
5934
},
6035

6136
post: function (url, params, options) {
62-
options = options || {};
63-
var type = 'POST';
64-
url = url.endWith('.json') ? url : url + '.json';
65-
var separator = url.indexOf("?") > -1 ? "&" : "?";
66-
if (SuperMap.Credential.CREDENTIAL && SuperMap.Credential.CREDENTIAL.getUrlParameters()) {
67-
url += separator + SuperMap.Credential.CREDENTIAL.getUrlParameters();
68-
}
69-
return this._fetch(url, params, options, type);
37+
return this._fetch(this._appendUrlTokenParameter(url), params, options, 'POST');
7038
},
7139

7240
put: function (url, params, options) {
73-
options = options || {};
74-
var type = 'PUT';
75-
url = url.endWith('.json') ? url : url + '.json';
41+
return this._fetch(this._appendUrlTokenParameter(url), params, options, 'PUT');
42+
},
43+
44+
_postSimulatie: function (type, url, params, options) {
7645
var separator = url.indexOf("?") > -1 ? "&" : "?";
46+
url += separator + '_method= ' + type + this._appendUrlTokenParameter(url);
47+
return this.post(url, params, options);
48+
},
49+
50+
_appendUrlTokenParameter: function (url) {
51+
url = url.indexOf('.json') !== -1 ? url : url + '.json';
7752
if (SuperMap.Credential.CREDENTIAL && SuperMap.Credential.CREDENTIAL.getUrlParameters()) {
53+
var separator = url.indexOf("?") > -1 ? "&" : "?";
7854
url += separator + SuperMap.Credential.CREDENTIAL.getUrlParameters();
7955
}
80-
return this._fetch(url, params, options, type);
56+
return url;
8157
},
8258

8359
_fetch: function (url, params, options, type) {
60+
options = options || {};
8461
if (options.timeout) {
85-
return this.timeout(options.timeout, fetch(url, {
62+
return this._timeout(options.timeout, fetch(url, {
8663
method: type,
8764
headers: options.headers,
8865
body: type === 'PUT' || type === 'POST' ? params : undefined,
8966
credentials: options.withCredentials ? 'include' : 'omit',
9067
mode: 'cors'
9168
}).then(function (response) {
92-
return response.json();
93-
}).then(function (json) {
94-
return json;
69+
return response;
9570
}));
9671
}
9772
return fetch(url, {
9873
method: type,
9974
body: type === 'PUT' || type === 'POST' ? params : undefined,
10075
headers: options.headers
10176
}).then(function (response) {
102-
return response.json();
103-
}).then(function (json) {
104-
return json;
105-
})
77+
return response;
78+
});
10679
},
10780

10881
_fetchJsonp: function (url, options) {
82+
options = options || {};
10983
return fetchJsonp(url, {method: 'GET', timeout: options.timeout})
11084
.then(function (response) {
111-
return response.json();
112-
}).then(function (json) {
113-
return json;
85+
return response;
11486
});
11587
},
11688

117-
timeout: function (seconds, promise) {
89+
_timeout: function (seconds, promise) {
11890
return new Promise(function (resolve, reject) {
11991
setTimeout(function () {
12092
reject(new Error("timeout"))
@@ -123,7 +95,7 @@ SuperMap.Request = SuperMap.Class({
12395
})
12496
},
12597

126-
getParameterString: function (params) {
98+
_getParameterString: function (params) {
12799
var paramsArray = [];
128100
for (var key in params) {
129101
var value = params[key];
@@ -139,8 +111,7 @@ SuperMap.Request = SuperMap.Class({
139111
);
140112
}
141113
encodedValue = '[' + encodedItemArray.join(",") + ']';
142-
}
143-
else {
114+
} else {
144115
encodedValue = encodeURIComponent(value);
145116
}
146117
paramsArray.push(encodeURIComponent(key) + "=" + encodedValue);
@@ -149,18 +120,4 @@ SuperMap.Request = SuperMap.Class({
149120
return paramsArray.join("&");
150121
}
151122

152-
});
153-
154-
String.prototype.endWith = function (s) {
155-
if (s === null || s === "" || this.length === 0 || s.length > this.length)
156-
return false;
157-
if (this.substring(this.length - s.length) == s)
158-
return true;
159-
else
160-
return false;
161-
return true;
162-
}
163-
164-
module.exports = function () {
165-
return new SuperMap.Request();
166-
};
123+
}

src/leaflet/overlay/PBFTileVectorLayer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ L.PBFTileVectorLayer = L.VectorGrid.extend({
132132

133133
var tileUrl = L.Util.template(this._url, L.extend(data, this.options));
134134

135-
return fetch(tileUrl).then(function (response) {
135+
return SuperMap.Request.get(tileUrl).then(function (response) {
136136

137137
if (!response.ok) {
138138
return {layers: []};

src/leaflet/overlay/TileVectorLayer.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
require('../core/Base');
88
require('./vectortile/VectorGrid');
99
var CartoCSSToLeaflet = require('./carto/CartoCSSToLeaflet');
10-
var fetchJsonp = require('fetch-jsonp');
1110

1211
TileVectorLayer = L.VectorGrid.extend({
1312

@@ -66,8 +65,8 @@ TileVectorLayer = L.VectorGrid.extend({
6665
//主要设置scales
6766
initMapScales: function () {
6867
var me = this;
69-
var mapUrl = me.options.url + ".jsonp";
70-
fetchJsonp(mapUrl, {
68+
var mapUrl = me.options.url + ".json";
69+
SuperMap.Request.get(mapUrl, null, {
7170
timeout: me.options.timeout
7271
}).then(function (response) {
7372
return response.json();
@@ -81,8 +80,8 @@ TileVectorLayer = L.VectorGrid.extend({
8180
//获取服务器layers资源下的风格信息(当CartoCSS中不存在相应图层渲染信息时使用)
8281
initLayersInfo: function () {
8382
var me = this;
84-
var layersUrl = me.options.url + "/layers.jsonp";
85-
fetchJsonp(layersUrl, {
83+
var layersUrl = me.options.url + "/layers.json";
84+
SuperMap.Request.get(layersUrl, null, {
8685
timeout: me.options.timeout
8786
}).then(function (response) {
8887
return response.json();
@@ -151,8 +150,8 @@ TileVectorLayer = L.VectorGrid.extend({
151150
//等待服务器的carto返回之后拼接本地配置的cartoCSS,并调用onAdd出图
152151
getVectorStylesFromServer: function () {
153152
var me = this;
154-
var vectorStyleUrl = me.options.url + "/tileFeature/vectorstyles.jsonp";
155-
fetchJsonp(vectorStyleUrl, {
153+
var vectorStyleUrl = me.options.url + "/tileFeature/vectorstyles.json";
154+
SuperMap.Request.get(vectorStyleUrl, null, {
156155
timeout: me.options.timeout
157156
}).then(function (response) {
158157
return response.json()
@@ -319,7 +318,7 @@ TileVectorLayer = L.VectorGrid.extend({
319318
if (!options.url) {
320319
return;
321320
}
322-
this._tileUrl = options.url + "/tileFeature.jsonp?";
321+
this._tileUrl = options.url + "/tileFeature.json?";
323322
this._tileUrl += this._createURLParam(options);
324323
},
325324

src/leaflet/overlay/vectortile/VectorTile.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ require('./PointSymbolizer');
55
require('./LineSymbolizer');
66
require('./RegionSymbolizer');
77
var tileFeatureProcessor = require('./TileFeatureProcessor');
8-
var fetchJsonp = require('fetch-jsonp');
98
VectorTile = L.Class.extend({
109

1110
initialize: function (options, done) {
@@ -19,7 +18,7 @@ VectorTile = L.Class.extend({
1918
renderTile: function () {
2019
var me = this, layer = me.layer, coords = me.coords;
2120
var tileFeatureUrl = layer._getTileUrl(coords);
22-
fetchJsonp(tileFeatureUrl, {
21+
SuperMap.Request.get(tileFeatureUrl, null, {
2322
timeout: 10000,
2423
}).then(function (response) {
2524
return response.json()

0 commit comments

Comments
 (0)