Skip to content

Commit 5a6509b

Browse files
committed
优化请求逻辑
1 parent c47b7ac commit 5a6509b

File tree

60 files changed

+2238
-2445
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+2238
-2445
lines changed

dist/iclient-classic.js

Lines changed: 66 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,8 @@ var _SuperMap2 = _interopRequireDefault(_SuperMap);
13971397

13981398
__webpack_require__(26);
13991399

1400+
var _FetchRequest = __webpack_require__(5);
1401+
14001402
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
14011403

14021404
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
@@ -1634,7 +1636,7 @@ var CommonServiceBase = function () {
16341636
options.success = me.getUrlCompleted;
16351637
options.failure = me.getUrlFailed;
16361638
me.options = options;
1637-
_SuperMap2.default.Util.committer(me.options);
1639+
me._commit(me.options);
16381640
}
16391641

16401642
/**
@@ -1720,19 +1722,9 @@ var CommonServiceBase = function () {
17201722
me.index = parseInt(Math.random() * me.length);
17211723
me.url = me.urls[me.index];
17221724
url = url.replace(re, re.exec(me.url)[0]);
1723-
var isInTheSameDomain = _SuperMap2.default.Util.isInTheSameDomain(url);
1724-
if (isInTheSameDomain) {
1725-
if (url.indexOf(".jsonp") > 0) {
1726-
url = url.replace(/.jsonp/, ".json");
1727-
}
1728-
} else {
1729-
if (!(url.indexOf(".jsonp") > 0)) {
1730-
url = url.replace(/.json/, ".jsonp");
1731-
}
1732-
}
17331725
me.options.url = url;
1734-
me.options.isInTheSameDomain = isInTheSameDomain;
1735-
_SuperMap2.default.Util.committer(me.options);
1726+
me.options.isInTheSameDomain = _SuperMap2.default.Util.isInTheSameDomain(url);
1727+
me._commit(me.options);
17361728
}
17371729

17381730
/**
@@ -1802,6 +1794,33 @@ var CommonServiceBase = function () {
18021794
var error = result.error || result;
18031795
this.events.triggerEvent("processFailed", { error: error });
18041796
}
1797+
}, {
1798+
key: '_commit',
1799+
value: function _commit(options) {
1800+
if (options.method === "POST") {
1801+
if (options.params) {
1802+
options.url = _SuperMap2.default.Util.urlAppend(options.url, _SuperMap2.default.Util.getParameterString(options.params || {}));
1803+
}
1804+
options.params = options.data;
1805+
}
1806+
_FetchRequest.FetchRequest.commit(options.method, options.url, options.params, {
1807+
headers: options.headers,
1808+
withCredentials: options.withCredentials,
1809+
timeout: options.async ? 0 : null,
1810+
proxy: options.proxy
1811+
}).then(function (response) {
1812+
return response.json();
1813+
}).then(function (result) {
1814+
1815+
if (result.error) {
1816+
var failure = options.scope ? _SuperMap2.default.Function.bind(options.failure, options.scope) : options.failure;
1817+
failure(result.error);
1818+
} else {
1819+
var success = options.scope ? _SuperMap2.default.Function.bind(options.success, options.scope) : options.success;
1820+
success(result);
1821+
}
1822+
});
1823+
}
18051824
}]);
18061825

18071826
return CommonServiceBase;
@@ -1840,7 +1859,7 @@ var _SuperMap2 = _interopRequireDefault(_SuperMap);
18401859

18411860
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
18421861

1843-
var Support = exports.Support = _SuperMap2.default.Support = {
1862+
var Support = exports.Support = _SuperMap2.default.Support = _SuperMap2.default.Support || {
18441863
cors: window.XMLHttpRequest && 'withCredentials' in new window.XMLHttpRequest()
18451864
};
18461865
var FetchRequest = exports.FetchRequest = _SuperMap2.default.FetchRequest = {
@@ -1862,10 +1881,10 @@ var FetchRequest = exports.FetchRequest = _SuperMap2.default.FetchRequest = {
18621881

18631882
get: function get(url, params, options) {
18641883
var type = 'GET';
1865-
url = this._processUrl(url);
1884+
url = this._processUrl(url, options);
18661885
url = _SuperMap2.default.Util.urlAppend(url, this._getParameterString(params || {}));
1867-
if (url.length <= 2000) {
1868-
if (_SuperMap2.default.Util.isInTheSameDomain(url) || _SuperMap2.default.Support.cors && this._isMVTRequest(url)) {
1886+
if (!this.urlIsLong(url)) {
1887+
if (_SuperMap2.default.Util.isInTheSameDomain(url) || Support.cors || options.proxy) {
18691888
return this._fetch(url, params, options, type);
18701889
}
18711890
if (!_SuperMap2.default.Util.isInTheSameDomain(url)) {
@@ -1878,29 +1897,45 @@ var FetchRequest = exports.FetchRequest = _SuperMap2.default.FetchRequest = {
18781897

18791898
delete: function _delete(url, params, options) {
18801899
var type = 'DELETE';
1881-
url = this._processUrl(url);
1900+
url = this._processUrl(url, options);
18821901
url = _SuperMap2.default.Util.urlAppend(url, this._getParameterString(params || {}));
1883-
if (url.length <= 2000 && _SuperMap2.default.Support.cors) {
1902+
if (!this.urlIsLong(url) && Support.cors) {
18841903
return this._fetch(url, params, options, type);
18851904
}
18861905
return this._postSimulatie(type, url.substring(0, url.indexOf('?') - 1), params, options);
18871906
},
18881907

18891908
post: function post(url, params, options) {
1890-
return this._fetch(this._processUrl(url), params, options, 'POST');
1909+
return this._fetch(this._processUrl(url, options), params, options, 'POST');
18911910
},
18921911

18931912
put: function put(url, params, options) {
1894-
return this._fetch(this._processUrl(url), params, options, 'PUT');
1913+
return this._fetch(this._processUrl(url, options), params, options, 'PUT');
1914+
},
1915+
urlIsLong: function urlIsLong(url) {
1916+
//当前url的字节长度。
1917+
var totalLength = 0,
1918+
charCode = null;
1919+
for (var i = 0, len = url.length; i < len; i++) {
1920+
//转化为Unicode编码
1921+
charCode = url.charCodeAt(i);
1922+
if (charCode < 0x007f) {
1923+
totalLength++;
1924+
} else if (0x0080 <= charCode && charCode <= 0x07ff) {
1925+
totalLength += 2;
1926+
} else if (0x0800 <= charCode && charCode <= 0xffff) {
1927+
totalLength += 3;
1928+
}
1929+
}
1930+
return totalLength < 2000 ? false : true;
18951931
},
1896-
18971932
_postSimulatie: function _postSimulatie(type, url, params, options) {
18981933
var separator = url.indexOf("?") > -1 ? "&" : "?";
18991934
url += separator + '_method= ' + type;
19001935
return this.post(url, params, options);
19011936
},
19021937

1903-
_processUrl: function _processUrl(url) {
1938+
_processUrl: function _processUrl(url, options) {
19041939
if (this._isMVTRequest(url)) {
19051940
return url;
19061941
}
@@ -1915,6 +1950,14 @@ var FetchRequest = exports.FetchRequest = _SuperMap2.default.FetchRequest = {
19151950
}
19161951
}
19171952
}
1953+
if (options && options.proxy) {
1954+
if (typeof options.proxy === "function") {
1955+
url = options.proxy(url);
1956+
} else {
1957+
url = decodeURIComponent(url);
1958+
url = options.proxy + encodeURIComponent(url);
1959+
}
1960+
}
19181961
return url;
19191962
},
19201963

dist/iclient-classic.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)