4
4
import { Util } from '../core/Util' ;
5
5
import { SecurityManager } from '@supermap/iclient-common/security/SecurityManager' ;
6
6
import { FetchRequest } from '@supermap/iclient-common/util/FetchRequest' ;
7
+ import { EncryptRequest } from '@supermap/iclient-common/util/EncryptRequest' ;
7
8
import { Unit } from '@supermap/iclient-common/REST' ;
8
9
import { Util as CommonUtil } from '@supermap/iclient-common/commontypes/Util' ;
9
10
import { Bounds } from '@supermap/iclient-common/commontypes/Bounds' ;
@@ -16,6 +17,7 @@ import GeoJSON from 'ol/format/GeoJSON';
16
17
import * as olSize from 'ol/size' ;
17
18
import Projection from 'ol/proj/Projection' ;
18
19
import TileGrid from 'ol/tilegrid/TileGrid' ;
20
+ import decodeUtil from './bundle.esm' ;
19
21
20
22
/**
21
23
* @class VectorTileSuperMapRest
@@ -32,6 +34,7 @@ import TileGrid from 'ol/tilegrid/TileGrid';
32
34
* @param {(string|Object) } [options.attributions='Tile Data <span>© <a href='http://support.supermap.com.cn/product/iServer.aspx' target='_blank'>SuperMap iServer</a></span> with <span>© <a href='https://iclient.supermap.io' target='_blank'>SuperMap iClient</a></span>'] - 版权描述信息。
33
35
* @param {Object } [options.format] - 瓦片的要素格式化。
34
36
* @param {boolean } [options.withCredentials] - 请求是否携带 cookie。
37
+ * @param {boolean } [options.decrypt] - 瓦片是否需要解密。
35
38
* @extends {ol.source.VectorTile }
36
39
* @usage
37
40
*/
@@ -59,9 +62,7 @@ export class VectorTileSuperMapRest extends VectorTile {
59
62
overlaps : options . overlaps ,
60
63
projection : options . projection ,
61
64
state :
62
- options . format instanceof MVT &&
63
- options . style &&
64
- Object . prototype . toString . call ( options . style ) == '[object String]'
65
+ options . format instanceof MVT
65
66
? 'loading'
66
67
: options . state ,
67
68
tileClass : options . tileClass ,
@@ -77,21 +78,7 @@ export class VectorTileSuperMapRest extends VectorTile {
77
78
me . withCredentials = options . withCredentials ;
78
79
me . _tileType = options . tileType || 'ScaleXY' ;
79
80
this . vectorTileStyles = new VectorTileStyles ( ) ;
80
- if ( options . format instanceof MVT && options . style ) {
81
- if ( Object . prototype . toString . call ( options . style ) == '[object String]' ) {
82
- var url = SecurityManager . appendCredential ( options . style ) ;
83
- FetchRequest . get ( url , null , { withCredentials : options . withCredentials } )
84
- . then ( ( response ) => response . json ( ) )
85
- . then ( ( mbStyle ) => {
86
- this . _fillByStyleJSON ( mbStyle , options . source ) ;
87
- this . setState ( 'ready' ) ;
88
- } ) ;
89
- } else {
90
- this . _fillByStyleJSON ( options . style , options . source ) ;
91
- }
92
- } else {
93
- this . _fillByRestMapOptions ( options . url , options ) ;
94
- }
81
+ this . _initialized ( options ) ;
95
82
96
83
function tileUrlFunction ( tileCoord , pixelRatio , projection ) {
97
84
if ( ! me . tileGrid ) {
@@ -156,10 +143,10 @@ export class VectorTileSuperMapRest extends VectorTile {
156
143
if ( ! tileCoord ) {
157
144
return undefined ;
158
145
} else {
159
- return me . _tileUrl
146
+ return me . _tileUrl
160
147
. replace ( zRegEx , tileCoord [ 0 ] . toString ( ) )
161
148
. replace ( xRegEx , tileCoord [ 1 ] . toString ( ) )
162
- . replace ( yRegEx , function ( ) {
149
+ . replace ( yRegEx , function ( ) {
163
150
var y = [ '4' , '5' ] . indexOf ( Util . getOlVersion ( ) ) > - 1 ? - tileCoord [ 2 ] - 1 : tileCoord [ 2 ] ;
164
151
return y . toString ( ) ;
165
152
} )
@@ -284,13 +271,17 @@ export class VectorTileSuperMapRest extends VectorTile {
284
271
source = xhr . response ;
285
272
}
286
273
if ( source ) {
274
+ // console.time('瓦片解密完成');
275
+ source = me . _decryptMvt ( source ) ;
276
+ // console.timeEnd('瓦片解密完成');
277
+ // console.log('瓦片解密字节大小: ', source.byteLength);
287
278
if ( [ '4' , '5' ] . indexOf ( Util . getOlVersion ( ) ) > - 1 ) {
288
- success . call (
289
- this ,
290
- format . readFeatures ( source , { featureProjection : projection } ) ,
291
- format . readProjection ( source ) ,
292
- format . getLastExtent ( )
293
- ) ;
279
+ success . call (
280
+ this ,
281
+ format . readFeatures ( source , { featureProjection : projection } ) ,
282
+ format . readProjection ( source ) ,
283
+ format . getLastExtent ( )
284
+ ) ;
294
285
} else {
295
286
success . call (
296
287
this ,
@@ -315,6 +306,27 @@ export class VectorTileSuperMapRest extends VectorTile {
315
306
} ) ;
316
307
}
317
308
}
309
+
310
+ async _initialized ( options ) {
311
+ if ( options . format instanceof MVT && options . style ) {
312
+ let style = options . style ;
313
+ if ( Object . prototype . toString . call ( options . style ) == '[object String]' ) {
314
+ var url = SecurityManager . appendCredential ( options . style ) ;
315
+ const response = await FetchRequest . get ( url , null , { withCredentials : options . withCredentials } )
316
+ style = await response . json ( ) ;
317
+ }
318
+ this . _fillByStyleJSON ( style , options . source ) ;
319
+ } else {
320
+ this . _fillByRestMapOptions ( options . url , options ) ;
321
+ }
322
+ if ( options . format instanceof MVT ) {
323
+ if ( options . decrypt ) {
324
+ await this . _verifyVectorTileIsEncrypt ( options ) ;
325
+ }
326
+ this . setState ( 'ready' ) ;
327
+ }
328
+ }
329
+
318
330
_fillByStyleJSON ( style , source ) {
319
331
if ( ! source ) {
320
332
source = Object . keys ( style . sources ) [ 0 ] ;
@@ -375,6 +387,44 @@ export class VectorTileSuperMapRest extends VectorTile {
375
387
this . _tileUrl = CommonUtil . urlAppend ( this . _tileUrl , CommonUtil . getParameterString ( params ) ) ;
376
388
}
377
389
390
+ async _verifyVectorTileIsEncrypt ( options ) {
391
+ try {
392
+ let serviceUrl = options . url || typeof options . style === 'string' && options . style ;
393
+
394
+ if ( ! serviceUrl && Object . prototype . toString . call ( options . style ) == '[object Object]' ) {
395
+ const firstSource = Object . keys ( options . style . sources ) [ 0 ] ;
396
+ serviceUrl = options . style . sources [ firstSource ] . tiles [ 0 ] ;
397
+ }
398
+ const workspaceServerUrl = ( serviceUrl && serviceUrl . match ( / .+ (? = ( \/ r e s t j s r \/ v 1 \/ v e c t o r t i l e \/ | \/ r e s t \/ m a p s \/ ) ) / ) || [ ] ) [ 0 ] ;
399
+ if ( ! workspaceServerUrl ) {
400
+ return ;
401
+ }
402
+ const servicesResponse = await FetchRequest . get ( workspaceServerUrl ) ;
403
+ const servicesResult = await servicesResponse . json ( ) ;
404
+ const matchRestData = ( servicesResult || [ ] ) . find ( item => serviceUrl . includes ( item . name ) && item . serviceEncryptInfo ) ;
405
+ if ( ! matchRestData ) {
406
+ return ;
407
+ }
408
+ const iserverHost = workspaceServerUrl . split ( '/services/' ) [ 0 ] ;
409
+ const encryptRequest = new EncryptRequest ( iserverHost ) ;
410
+ const svckeyUrl = matchRestData && `${ iserverHost } /services/security/svckeys/${ matchRestData . serviceEncryptInfo . encrptKeyID } .json`
411
+ const svcReponse = await encryptRequest . request ( {
412
+ method : 'get' ,
413
+ url : svckeyUrl
414
+ } )
415
+ this . serviceKey = await svcReponse . json ( ) ;
416
+ } catch ( error ) {
417
+ console . error ( error ) ;
418
+ }
419
+ }
420
+
421
+ _decryptMvt ( mvtData ) {
422
+ if ( this . serviceKey ) {
423
+ return decodeUtil ( mvtData , this . serviceKey ) ;
424
+ }
425
+ return mvtData ;
426
+ }
427
+
378
428
/**
379
429
* @function VectorTileSuperMapRest.optionsFromMapJSON
380
430
* @param {string } url - 地址。
0 commit comments