Skip to content

Commit 7873a46

Browse files
committed
【API】 FIX ICL-1010。优化fetchrequest对jsonp的请求吃处理
1 parent eec87d0 commit 7873a46

17 files changed

Lines changed: 1681 additions & 427 deletions

dist/iclient-classic.js

Lines changed: 250 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2935,7 +2935,10 @@ var CommonServiceBase = exports.CommonServiceBase = function () {
29352935
if (response.text) {
29362936
return response.text();
29372937
}
2938-
return response.json();
2938+
if (response.json) {
2939+
return response.json();
2940+
}
2941+
return response;
29392942
}).then(function (text) {
29402943
var result = text;
29412944
if (typeof text === "string") {
@@ -2975,7 +2978,7 @@ _SuperMap.SuperMap.CommonServiceBase = CommonServiceBase;
29752978
Object.defineProperty(exports, "__esModule", {
29762979
value: true
29772980
});
2978-
exports.FetchRequest = exports.RequestTimeout = exports.CORS = undefined;
2981+
exports.FetchRequest = exports.getRequestTimeout = exports.setRequestTimeout = exports.isCORS = exports.setCORS = undefined;
29792982

29802983
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
29812984

@@ -2994,19 +2997,41 @@ var _Util = __webpack_require__(1);
29942997
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
29952998

29962999
var fetch = window.fetch;
2997-
29983000
/**
2999-
* @member SuperMap.CORS
3001+
* @function SuperMap.setCORS
3002+
* @description 设置是否支持跨域
3003+
* @param {boolean} cors - 是否支持跨域
3004+
*/
3005+
var setCORS = exports.setCORS = _SuperMap.SuperMap.setCORS = function (cors) {
3006+
_SuperMap.SuperMap.CORS = cors;
3007+
};
3008+
/**
3009+
* @function SuperMap.isCORS
30003010
* @description 是否支持跨域
3001-
* @type {boolean}
3011+
* @returns {boolean} 是否支持跨域
30023012
*/
3003-
var CORS = exports.CORS = _SuperMap.SuperMap.CORS = _SuperMap.SuperMap.CORS || window.XMLHttpRequest && 'withCredentials' in new window.XMLHttpRequest();
3013+
var isCORS = exports.isCORS = _SuperMap.SuperMap.isCORS = function () {
3014+
if (_SuperMap.SuperMap.CORS != undefined) {
3015+
return _SuperMap.SuperMap.CORS;
3016+
}
3017+
return window.XMLHttpRequest && 'withCredentials' in new window.XMLHttpRequest();
3018+
};
30043019
/**
3005-
* @member SuperMap.RequestTimeout
3006-
* @description 请求超时时间,默认45s
3007-
* @type {number}
3020+
* @function SuperMap.setRequestTimeout
3021+
* @description 设置请求超时时间。
3022+
* @param {number} timeout - 请求超时时间
30083023
*/
3009-
var RequestTimeout = exports.RequestTimeout = _SuperMap.SuperMap.RequestTimeout = _SuperMap.SuperMap.RequestTimeout || 45000;
3024+
var setRequestTimeout = exports.setRequestTimeout = _SuperMap.SuperMap.setRequestTimeout = function (timeout) {
3025+
return _SuperMap.SuperMap.RequestTimeout = timeout;
3026+
};
3027+
/**
3028+
* @function SuperMap.getRequestTimeout
3029+
* @description 获取请求超时时间,默认45s。
3030+
* @returns {number} 请求超时时间
3031+
*/
3032+
var getRequestTimeout = exports.getRequestTimeout = _SuperMap.SuperMap.getRequestTimeout = function () {
3033+
return _SuperMap.SuperMap.RequestTimeout || 45000;
3034+
};
30103035
var FetchRequest = exports.FetchRequest = _SuperMap.SuperMap.FetchRequest = {
30113036
commit: function commit(method, url, params, options) {
30123037
method = method ? method.toUpperCase() : method;
@@ -3023,43 +3048,72 @@ var FetchRequest = exports.FetchRequest = _SuperMap.SuperMap.FetchRequest = {
30233048
return this.get(url, params, options);
30243049
}
30253050
},
3026-
3051+
supportDirectRequest: function supportDirectRequest(url, options) {
3052+
return _Util.Util.isInTheSameDomain(url) || isCORS() || options.proxy;
3053+
},
30273054
get: function get(url, params, options) {
30283055
options = options || {};
30293056
var type = 'GET';
30303057
url = this._processUrl(url, options);
30313058
url = _Util.Util.urlAppend(url, this._getParameterString(params || {}));
3059+
if (!this.supportDirectRequest(url, options)) {
3060+
url = url.replace('.json', '.jsonp');
3061+
var config = {
3062+
url: url,
3063+
data: params
3064+
};
3065+
return _SuperMap.SuperMap.Util.RequestJSONP.GET(config);
3066+
}
30323067
if (!this.urlIsLong(url)) {
3033-
if (_Util.Util.isInTheSameDomain(url) || CORS || options.proxy) {
3034-
return this._fetch(url, params, options, type);
3035-
}
3036-
if (!_Util.Util.isInTheSameDomain(url)) {
3037-
url = url.replace('.json', '.jsonp');
3038-
return this._fetchJsonp(url, options);
3039-
}
3068+
return this._fetch(url, params, options, type);
3069+
} else {
3070+
return this._postSimulatie(type, url.substring(0, url.indexOf('?') - 1), params, options);
30403071
}
3041-
return this._postSimulatie(type, url.substring(0, url.indexOf('?') - 1), params, options);
30423072
},
30433073

30443074
delete: function _delete(url, params, options) {
30453075
options = options || {};
30463076
var type = 'DELETE';
30473077
url = this._processUrl(url, options);
30483078
url = _Util.Util.urlAppend(url, this._getParameterString(params || {}));
3049-
if (!this.urlIsLong(url) && CORS) {
3050-
return this._fetch(url, params, options, type);
3079+
if (!this.supportDirectRequest(url, options)) {
3080+
url = url.replace('.json', '.jsonp');
3081+
var config = {
3082+
url: url += "&_method=DELETE",
3083+
data: params
3084+
};
3085+
return _SuperMap.SuperMap.Util.RequestJSONP.DELETE(config);
3086+
}
3087+
if (this.urlIsLong(url)) {
3088+
return this._postSimulatie(type, url.substring(0, url.indexOf('?') - 1), params, options);
30513089
}
3052-
return this._postSimulatie(type, url.substring(0, url.indexOf('?') - 1), params, options);
3090+
return this._fetch(url, params, options, type);
30533091
},
3054-
30553092
post: function post(url, params, options) {
30563093
options = options || {};
3094+
if (!this.supportDirectRequest(url, options)) {
3095+
url = url.replace('.json', '.jsonp');
3096+
var config = {
3097+
url: url += "&_method=POST",
3098+
data: params
3099+
};
3100+
return _SuperMap.SuperMap.Util.RequestJSONP.POST(config);
3101+
}
30573102
return this._fetch(this._processUrl(url, options), params, options, 'POST');
30583103
},
30593104

30603105
put: function put(url, params, options) {
30613106
options = options || {};
3062-
return this._fetch(this._processUrl(url, options), params, options, 'PUT');
3107+
url = this._processUrl(url, options);
3108+
if (!this.supportDirectRequest(url, options)) {
3109+
url = url.replace('.json', '.jsonp');
3110+
var config = {
3111+
url: url += "&_method=PUT",
3112+
data: params
3113+
};
3114+
return _SuperMap.SuperMap.Util.RequestJSONP.DELETE(config);
3115+
}
3116+
return this._fetch(url, params, options, 'PUT');
30633117
},
30643118
urlIsLong: function urlIsLong(url) {
30653119
//当前url的字节长度。
@@ -3126,7 +3180,7 @@ var FetchRequest = exports.FetchRequest = _SuperMap.SuperMap.FetchRequest = {
31263180
body: type === 'PUT' || type === 'POST' ? params : undefined,
31273181
credentials: options.withCredentials ? 'include' : 'omit',
31283182
mode: 'cors',
3129-
timeout: RequestTimeout
3183+
timeout: getRequestTimeout()
31303184
}).then(function (response) {
31313185
return response;
31323186
}));
@@ -3137,15 +3191,18 @@ var FetchRequest = exports.FetchRequest = _SuperMap.SuperMap.FetchRequest = {
31373191
headers: options.headers,
31383192
credentials: options.withCredentials ? 'include' : 'omit',
31393193
mode: 'cors',
3140-
timeout: RequestTimeout
3194+
timeout: getRequestTimeout()
31413195
}).then(function (response) {
31423196
return response;
31433197
});
31443198
},
31453199

31463200
_fetchJsonp: function _fetchJsonp(url, options) {
31473201
options = options || {};
3148-
return (0, _fetchJsonp3.default)(url, { method: 'GET', timeout: options.timeout }).then(function (response) {
3202+
return (0, _fetchJsonp3.default)(url, {
3203+
method: 'GET',
3204+
timeout: options.timeout
3205+
}).then(function (response) {
31493206
return response;
31503207
});
31513208
},
@@ -3186,6 +3243,172 @@ var FetchRequest = exports.FetchRequest = _SuperMap.SuperMap.FetchRequest = {
31863243
return url.indexOf('.mvt') > -1 || url.indexOf('.pbf') > -1;
31873244
}
31883245
};
3246+
_SuperMap.SuperMap.Util.RequestJSONP = {
3247+
limitLength: 1500,
3248+
queryKeys: [],
3249+
queryValues: [],
3250+
supermap_callbacks: {},
3251+
addQueryStrings: function addQueryStrings(values) {
3252+
var me = this;
3253+
for (var key in values) {
3254+
me.queryKeys.push(key);
3255+
if (typeof values[key] !== "string") {
3256+
values[key] = _SuperMap.SuperMap.Util.toJSON(values[key]);
3257+
}
3258+
var tempValue = encodeURIComponent(values[key]);
3259+
me.queryValues.push(tempValue);
3260+
}
3261+
},
3262+
issue: function issue(config) {
3263+
var me = this,
3264+
uid = me.getUid(),
3265+
url = config.url,
3266+
splitQuestUrl = [];
3267+
var p = new Promise(function (resolve) {
3268+
me.supermap_callbacks[uid] = function (response) {
3269+
delete me.supermap_callbacks[uid];
3270+
resolve(response);
3271+
};
3272+
});
3273+
3274+
// me.addQueryStrings({
3275+
// callback: "SuperMap.Util.RequestJSONP.supermap_callbacks[" + uid + "]"
3276+
// });
3277+
var sectionURL = url,
3278+
keysCount = 0; //此次sectionURL中有多少个key
3279+
var length = me.queryKeys ? me.queryKeys.length : 0;
3280+
for (var i = 0; i < length; i++) {
3281+
if (sectionURL.length + me.queryKeys[i].length + 2 >= me.limitLength) {
3282+
//+2 for ("&"or"?")and"="
3283+
if (keysCount == 0) {
3284+
return false;
3285+
}
3286+
if (splitQuestUrl == null) {
3287+
splitQuestUrl = new Array();
3288+
}
3289+
splitQuestUrl.push(sectionURL);
3290+
sectionURL = url;
3291+
keysCount = 0;
3292+
i--;
3293+
} else {
3294+
if (sectionURL.length + me.queryKeys[i].length + 2 + me.queryValues[i].length > me.limitLength) {
3295+
var leftValue = me.queryValues[i];
3296+
while (leftValue.length > 0) {
3297+
var leftLength = me.limitLength - sectionURL.length - me.queryKeys[i].length - 2; //+2 for ("&"or"?")and"="
3298+
if (sectionURL.indexOf("?") > -1) {
3299+
sectionURL += "&";
3300+
} else {
3301+
sectionURL += "?";
3302+
}
3303+
var tempLeftValue = leftValue.substring(0, leftLength);
3304+
//避免 截断sectionURL时,将类似于%22这样的符号截成两半,从而导致服务端组装sectionURL时发生错误
3305+
if (tempLeftValue.substring(leftLength - 1, leftLength) === "%") {
3306+
leftLength -= 1;
3307+
tempLeftValue = leftValue.substring(0, leftLength);
3308+
} else if (tempLeftValue.substring(leftLength - 2, leftLength - 1) === "%") {
3309+
leftLength -= 2;
3310+
tempLeftValue = leftValue.substring(0, leftLength);
3311+
}
3312+
3313+
sectionURL += me.queryKeys[i] + "=" + tempLeftValue;
3314+
leftValue = leftValue.substring(leftLength);
3315+
if (tempLeftValue.length > 0) {
3316+
if (splitQuestUrl == null) {
3317+
splitQuestUrl = new Array();
3318+
}
3319+
splitQuestUrl.push(sectionURL);
3320+
sectionURL = url;
3321+
keysCount = 0;
3322+
}
3323+
}
3324+
} else {
3325+
keysCount++;
3326+
if (sectionURL.indexOf("?") > -1) {
3327+
sectionURL += "&";
3328+
} else {
3329+
sectionURL += "?";
3330+
}
3331+
sectionURL += me.queryKeys[i] + "=" + me.queryValues[i];
3332+
}
3333+
}
3334+
}
3335+
if (splitQuestUrl == null) {
3336+
splitQuestUrl = new Array();
3337+
}
3338+
splitQuestUrl.push(sectionURL);
3339+
me.send(splitQuestUrl, "SuperMap.Util.RequestJSONP.supermap_callbacks[" + uid + "]", config && config.proxy);
3340+
return p;
3341+
},
3342+
3343+
getUid: function getUid() {
3344+
var uid = new Date().getTime(),
3345+
random = Math.floor(Math.random() * 1e17);
3346+
return uid * 1000 + random;
3347+
},
3348+
3349+
send: function send(splitQuestUrl, callback, proxy) {
3350+
var len = splitQuestUrl.length;
3351+
if (len > 0) {
3352+
var jsonpUserID = new Date().getTime();
3353+
for (var i = 0; i < len; i++) {
3354+
var url = splitQuestUrl[i];
3355+
if (url.indexOf("?") > -1) {
3356+
url += "&";
3357+
} else {
3358+
url += "?";
3359+
}
3360+
url += "sectionCount=" + len;
3361+
url += "&sectionIndex=" + i;
3362+
url += "&jsonpUserID=" + jsonpUserID;
3363+
if (proxy) {
3364+
url = decodeURIComponent(url);
3365+
url = proxy + encodeURIComponent(url);
3366+
}
3367+
(0, _fetchJsonp3.default)(url, {
3368+
jsonpCallbackFunction: callback,
3369+
timeout: 30000
3370+
});
3371+
}
3372+
}
3373+
},
3374+
3375+
GET: function GET(config) {
3376+
var me = this;
3377+
me.queryKeys.length = 0;
3378+
me.queryValues.length = 0;
3379+
me.addQueryStrings(config.params);
3380+
return me.issue(config);
3381+
},
3382+
3383+
POST: function POST(config) {
3384+
var me = this;
3385+
me.queryKeys.length = 0;
3386+
me.queryValues.length = 0;
3387+
me.addQueryStrings({
3388+
requestEntity: config.data
3389+
});
3390+
return me.issue(config);
3391+
},
3392+
3393+
PUT: function PUT(config) {
3394+
var me = this;
3395+
me.queryKeys.length = 0;
3396+
me.queryValues.length = 0;
3397+
me.addQueryStrings({
3398+
requestEntity: config.data
3399+
});
3400+
return me.issue(config);
3401+
},
3402+
DELETE: function DELETE(config) {
3403+
var me = this;
3404+
me.queryKeys.length = 0;
3405+
me.queryValues.length = 0;
3406+
me.addQueryStrings({
3407+
requestEntity: config.data
3408+
});
3409+
return me.issue(config);
3410+
}
3411+
};
31893412

31903413
/***/ }),
31913414
/* 8 */

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)