4
4
import 'promise-polyfill/dist/polyfill' ;
5
5
import 'fetch-ie8' ;
6
6
import fetchJsonp from 'fetch-jsonp' ;
7
- import {
8
- SuperMap
9
- } from '../SuperMap' ;
10
- import {
11
- Util
12
- } from '../commontypes/Util' ;
7
+ import { SuperMap } from '../SuperMap' ;
8
+ import { Util } from '../commontypes/Util' ;
13
9
14
- const fetch = window . fetch ;
10
+ let fetch = window . fetch ;
11
+ export var setFetch = function ( newFetch ) {
12
+ fetch = newFetch ;
13
+ }
15
14
/**
16
15
* @function SuperMap.setCORS
17
16
* @description 设置是否允许跨域请求,全局配置,优先级低于 service 下的 crossOring 参数。
@@ -64,12 +63,13 @@ export var FetchRequest = SuperMap.FetchRequest = {
64
63
}
65
64
} ,
66
65
supportDirectRequest : function ( url , options ) {
67
- if ( Util . isInTheSameDomain ( url ) ) {
68
- return true ;
69
- } if ( options . crossOrigin != undefined ) {
70
- return options . crossOrigin ;
71
- } else {
72
- return isCORS ( ) || options . proxy
66
+ if ( Util . isInTheSameDomain ( url ) ) {
67
+ return true ;
68
+ }
69
+ if ( options . crossOrigin != undefined ) {
70
+ return options . crossOrigin ;
71
+ } else {
72
+ return isCORS ( ) || options . proxy ;
73
73
}
74
74
} ,
75
75
get : function ( url , params , options ) {
@@ -90,7 +90,6 @@ export var FetchRequest = SuperMap.FetchRequest = {
90
90
} else {
91
91
return this . _postSimulatie ( type , url . substring ( 0 , url . indexOf ( '?' ) - 1 ) , params , options ) ;
92
92
}
93
-
94
93
} ,
95
94
96
95
delete : function ( url , params , options ) {
@@ -110,7 +109,6 @@ export var FetchRequest = SuperMap.FetchRequest = {
110
109
return this . _postSimulatie ( type , url . substring ( 0 , url . indexOf ( '?' ) - 1 ) , params , options ) ;
111
110
}
112
111
return this . _fetch ( url , params , options , type ) ;
113
-
114
112
} ,
115
113
post : function ( url , params , options ) {
116
114
options = options || { } ;
@@ -123,7 +121,6 @@ export var FetchRequest = SuperMap.FetchRequest = {
123
121
return SuperMap . Util . RequestJSONPPromise . POST ( config ) ;
124
122
}
125
123
return this . _fetch ( this . _processUrl ( url , options ) , params , options , 'POST' ) ;
126
-
127
124
} ,
128
125
129
126
put : function ( url , params , options ) {
@@ -136,10 +133,8 @@ export var FetchRequest = SuperMap.FetchRequest = {
136
133
data : params
137
134
} ;
138
135
return SuperMap . Util . RequestJSONPPromise . PUT ( config ) ;
139
-
140
136
}
141
137
return this . _fetch ( url , params , options , 'PUT' ) ;
142
-
143
138
} ,
144
139
urlIsLong : function ( url ) {
145
140
//当前url的字节长度。
@@ -156,10 +151,10 @@ export var FetchRequest = SuperMap.FetchRequest = {
156
151
totalLength += 3 ;
157
152
}
158
153
}
159
- return ( totalLength < 2000 ) ? false : true ;
154
+ return totalLength < 2000 ? false : true ;
160
155
} ,
161
156
_postSimulatie : function ( type , url , params , options ) {
162
- var separator = url . indexOf ( "?" ) > - 1 ? "&" : "?" ;
157
+ var separator = url . indexOf ( '?' ) > - 1 ? '&' : '?' ;
163
158
url += separator + '_method=' + type ;
164
159
if ( typeof params !== 'string' ) {
165
160
params = JSON . stringify ( params ) ;
@@ -173,17 +168,17 @@ export var FetchRequest = SuperMap.FetchRequest = {
173
168
}
174
169
175
170
if ( url . indexOf ( '.json' ) === - 1 && ! options . withoutFormatSuffix ) {
176
- if ( url . indexOf ( "?" ) < 0 ) {
177
- url += '.json'
171
+ if ( url . indexOf ( '?' ) < 0 ) {
172
+ url += '.json' ;
178
173
} else {
179
- var urlArrays = url . split ( "?" ) ;
174
+ var urlArrays = url . split ( '?' ) ;
180
175
if ( urlArrays . length === 2 ) {
181
- url = urlArrays [ 0 ] + " .json?" + urlArrays [ 1 ]
176
+ url = urlArrays [ 0 ] + ' .json?' + urlArrays [ 1 ] ;
182
177
}
183
178
}
184
179
}
185
180
if ( options && options . proxy ) {
186
- if ( typeof options . proxy === " function" ) {
181
+ if ( typeof options . proxy === ' function' ) {
187
182
url = options . proxy ( url ) ;
188
183
} else {
189
184
url = decodeURIComponent ( url ) ;
@@ -200,70 +195,82 @@ export var FetchRequest = SuperMap.FetchRequest = {
200
195
options . headers [ 'Content-Type' ] = 'application/x-www-form-urlencoded;charset=UTF-8' ;
201
196
}
202
197
if ( options . timeout ) {
203
- return this . _timeout ( options . timeout , fetch ( url , {
204
- method : type ,
205
- headers : options . headers ,
206
- body : type === 'PUT' || type === 'POST' ? params : undefined ,
207
- credentials : options . withCredentials ? 'include' : 'omit' ,
208
- mode : 'cors' ,
209
- timeout : getRequestTimeout ( )
210
- } ) . then ( function ( response ) {
211
- return response ;
212
- } ) ) ;
198
+ return this . _timeout (
199
+ options . timeout ,
200
+ fetch ( url , {
201
+ method : type ,
202
+ headers : options . headers ,
203
+ body : type === 'PUT' || type === 'POST' ? params : undefined ,
204
+ credentials : this . _getWithCredentials ( options ) ,
205
+ mode : 'cors' ,
206
+ timeout : getRequestTimeout ( )
207
+ } ) . then ( function ( response ) {
208
+ return response ;
209
+ } )
210
+ ) ;
213
211
}
214
212
return fetch ( url , {
215
213
method : type ,
216
214
body : type === 'PUT' || type === 'POST' ? params : undefined ,
217
215
headers : options . headers ,
218
- credentials : options . withCredentials ? 'include' : 'omit' ,
216
+ credentials : this . _getWithCredentials ( options ) ,
219
217
mode : 'cors' ,
220
218
timeout : getRequestTimeout ( )
221
219
} ) . then ( function ( response ) {
222
220
return response ;
223
221
} ) ;
224
222
} ,
225
223
224
+ _getWithCredentials : function ( options ) {
225
+ if ( options . withCredentials === true ) {
226
+ return 'include' ;
227
+ }
228
+ if ( options . withCredentials === false ) {
229
+ return 'omit' ;
230
+ }
231
+ return 'same-origin' ;
232
+ } ,
233
+
226
234
_fetchJsonp : function ( url , options ) {
227
235
options = options || { } ;
228
236
return fetchJsonp ( url , {
229
- method : 'GET' ,
230
- timeout : options . timeout
231
- } )
232
- . then ( function ( response ) {
233
- return response ;
234
- } ) ;
237
+ method : 'GET' ,
238
+ timeout : options . timeout
239
+ } ) . then ( function ( response ) {
240
+ return response ;
241
+ } ) ;
235
242
} ,
236
243
237
244
_timeout : function ( seconds , promise ) {
238
245
return new Promise ( function ( resolve , reject ) {
239
246
setTimeout ( function ( ) {
240
- reject ( new Error ( " timeout" ) )
241
- } , seconds )
242
- promise . then ( resolve , reject )
243
- } )
247
+ reject ( new Error ( ' timeout' ) ) ;
248
+ } , seconds ) ;
249
+ promise . then ( resolve , reject ) ;
250
+ } ) ;
244
251
} ,
245
252
246
253
_getParameterString : function ( params ) {
247
254
var paramsArray = [ ] ;
248
255
for ( var key in params ) {
249
256
var value = params [ key ] ;
250
- if ( ( value != null ) && ( typeof value !== 'function' ) ) {
257
+ if ( value != null && typeof value !== 'function' ) {
251
258
var encodedValue ;
252
259
if ( Array . isArray ( value ) || value . toString ( ) === '[object Object]' ) {
253
260
encodedValue = encodeURIComponent ( JSON . stringify ( value ) ) ;
254
261
} else {
255
262
encodedValue = encodeURIComponent ( value ) ;
256
263
}
257
- paramsArray . push ( encodeURIComponent ( key ) + "=" + encodedValue ) ;
264
+ paramsArray . push ( encodeURIComponent ( key ) + '=' + encodedValue ) ;
258
265
}
259
266
}
260
- return paramsArray . join ( "&" ) ;
267
+ return paramsArray . join ( '&' ) ;
261
268
} ,
262
269
263
270
_isMVTRequest : function ( url ) {
264
- return ( url . indexOf ( '.mvt' ) > - 1 || url . indexOf ( '.pbf' ) > - 1 ) ;
271
+ return url . indexOf ( '.mvt' ) > - 1 || url . indexOf ( '.pbf' ) > - 1 ;
265
272
}
266
- } ;
273
+ }
267
274
SuperMap . Util . RequestJSONPPromise = {
268
275
limitLength : 1500 ,
269
276
queryKeys : [ ] ,
@@ -273,7 +280,7 @@ SuperMap.Util.RequestJSONPPromise = {
273
280
var me = this ;
274
281
for ( var key in values ) {
275
282
me . queryKeys . push ( key ) ;
276
- if ( typeof values [ key ] !== " string" ) {
283
+ if ( typeof values [ key ] !== ' string' ) {
277
284
values [ key ] = SuperMap . Util . toJSON ( values [ key ] ) ;
278
285
}
279
286
var tempValue = encodeURIComponent ( values [ key ] ) ;
@@ -299,7 +306,8 @@ SuperMap.Util.RequestJSONPPromise = {
299
306
keysCount = 0 ; //此次sectionURL中有多少个key
300
307
var length = me . queryKeys ? me . queryKeys . length : 0 ;
301
308
for ( var i = 0 ; i < length ; i ++ ) {
302
- if ( sectionURL . length + me . queryKeys [ i ] . length + 2 >= me . limitLength ) { //+2 for ("&"or"?")and"="
309
+ if ( sectionURL . length + me . queryKeys [ i ] . length + 2 >= me . limitLength ) {
310
+ //+2 for ("&"or"?")and"="
303
311
if ( keysCount == 0 ) {
304
312
return false ;
305
313
}
@@ -312,22 +320,22 @@ SuperMap.Util.RequestJSONPPromise = {
312
320
var leftValue = me . queryValues [ i ] ;
313
321
while ( leftValue . length > 0 ) {
314
322
var leftLength = me . limitLength - sectionURL . length - me . queryKeys [ i ] . length - 2 ; //+2 for ("&"or"?")and"="
315
- if ( sectionURL . indexOf ( "?" ) > - 1 ) {
316
- sectionURL += "&" ;
323
+ if ( sectionURL . indexOf ( '?' ) > - 1 ) {
324
+ sectionURL += '&' ;
317
325
} else {
318
- sectionURL += "?" ;
326
+ sectionURL += '?' ;
319
327
}
320
328
var tempLeftValue = leftValue . substring ( 0 , leftLength ) ;
321
329
//避免 截断sectionURL时,将类似于%22这样的符号截成两半,从而导致服务端组装sectionURL时发生错误
322
- if ( tempLeftValue . substring ( leftLength - 1 , leftLength ) === "%" ) {
330
+ if ( tempLeftValue . substring ( leftLength - 1 , leftLength ) === '%' ) {
323
331
leftLength -= 1 ;
324
332
tempLeftValue = leftValue . substring ( 0 , leftLength ) ;
325
- } else if ( tempLeftValue . substring ( leftLength - 2 , leftLength - 1 ) === "%" ) {
333
+ } else if ( tempLeftValue . substring ( leftLength - 2 , leftLength - 1 ) === '%' ) {
326
334
leftLength -= 2 ;
327
335
tempLeftValue = leftValue . substring ( 0 , leftLength ) ;
328
336
}
329
337
330
- sectionURL += me . queryKeys [ i ] + "=" + tempLeftValue ;
338
+ sectionURL += me . queryKeys [ i ] + '=' + tempLeftValue ;
331
339
leftValue = leftValue . substring ( leftLength ) ;
332
340
if ( tempLeftValue . length > 0 ) {
333
341
splitQuestUrl . push ( sectionURL ) ;
@@ -337,21 +345,24 @@ SuperMap.Util.RequestJSONPPromise = {
337
345
}
338
346
} else {
339
347
keysCount ++ ;
340
- if ( sectionURL . indexOf ( "?" ) > - 1 ) {
341
- sectionURL += "&" ;
348
+ if ( sectionURL . indexOf ( '?' ) > - 1 ) {
349
+ sectionURL += '&' ;
342
350
} else {
343
- sectionURL += "?" ;
351
+ sectionURL += '?' ;
344
352
}
345
- sectionURL += me . queryKeys [ i ] + "=" + me . queryValues [ i ] ;
353
+ sectionURL += me . queryKeys [ i ] + '=' + me . queryValues [ i ] ;
346
354
}
347
355
}
348
356
}
349
357
splitQuestUrl . push ( sectionURL ) ;
350
- me . send ( splitQuestUrl , "SuperMap.Util.RequestJSONPPromise.supermap_callbacks[" + uid + "]" , config && config . proxy ) ;
358
+ me . send (
359
+ splitQuestUrl ,
360
+ 'SuperMap.Util.RequestJSONPPromise.supermap_callbacks[' + uid + ']' ,
361
+ config && config . proxy
362
+ ) ;
351
363
return p ;
352
364
} ,
353
365
354
-
355
366
getUid : function ( ) {
356
367
var uid = new Date ( ) . getTime ( ) ,
357
368
random = Math . floor ( Math . random ( ) * 1e17 ) ;
@@ -364,22 +375,22 @@ SuperMap.Util.RequestJSONPPromise = {
364
375
var jsonpUserID = new Date ( ) . getTime ( ) ;
365
376
for ( var i = 0 ; i < len ; i ++ ) {
366
377
var url = splitQuestUrl [ i ] ;
367
- if ( url . indexOf ( "?" ) > - 1 ) {
368
- url += "&" ;
378
+ if ( url . indexOf ( '?' ) > - 1 ) {
379
+ url += '&' ;
369
380
} else {
370
- url += "?" ;
381
+ url += '?' ;
371
382
}
372
- url += " sectionCount=" + len ;
373
- url += " §ionIndex=" + i ;
374
- url += " &jsonpUserID=" + jsonpUserID ;
383
+ url += ' sectionCount=' + len ;
384
+ url += ' §ionIndex=' + i ;
385
+ url += ' &jsonpUserID=' + jsonpUserID ;
375
386
if ( proxy ) {
376
387
url = decodeURIComponent ( url ) ;
377
388
url = proxy + encodeURIComponent ( url ) ;
378
389
}
379
390
fetchJsonp ( url , {
380
391
jsonpCallbackFunction : callback ,
381
392
timeout : 30000
382
- } )
393
+ } ) ;
383
394
}
384
395
}
385
396
} ,
0 commit comments