@@ -18,136 +18,197 @@ import { CommonServiceBase } from './CommonServiceBase';
18
18
* @usage
19
19
*/
20
20
export class WebPrintingService extends CommonServiceBase {
21
- constructor ( url , options ) {
22
- super ( url , options ) ;
21
+ constructor ( url , options ) {
22
+ super ( url , options ) ;
23
23
24
- if ( options ) {
25
- Util . extend ( this , options ) ;
26
- }
27
- this . CLASS_NAME = 'SuperMap.WebPrintingService' ;
28
- if ( ! this . url ) {
29
- return ;
30
- }
24
+ if ( options ) {
25
+ Util . extend ( this , options ) ;
31
26
}
32
-
33
- /**
34
- * @function WebPrintingService.prototype.destroy
35
- * @description 释放资源,将引用资源的属性置空。
36
- */
37
- destroy ( ) {
38
- super . destroy ( ) ;
27
+ this . templates = [ ] ;
28
+ this . CLASS_NAME = 'SuperMap.WebPrintingService' ;
29
+ if ( ! this . url ) {
30
+ return ;
39
31
}
32
+ }
40
33
41
- /**
42
- * @function WebPrintingService.prototype.createWebPrintingJob
43
- * @description 创建 Web 打印任务。
44
- * @param {WebPrintingJobParameters } params - Web 打印的请求参数。
45
- * @param {RequestCallback } [callback] - 回调函数,该参数未传时可通过返回的 promise 获取结果。
46
- * @returns {Promise } Promise 对象。
47
- */
48
- createWebPrintingJob ( params , callback ) {
49
- if ( ! params ) {
50
- return ;
51
- }
52
- if ( params . layoutOptions ) {
53
- if ( params . layoutOptions . legendOptions ) {
54
- ! params . layoutOptions . legendOptions . title && ( params . layoutOptions . legendOptions . title = '' ) ;
55
- params . layoutOptions . legendOptions . picAsBase64 =
56
- params . layoutOptions . legendOptions . picAsBase64 &&
57
- params . layoutOptions . legendOptions . picAsBase64 . replace ( / ^ d a t a : .+ ; b a s e 6 4 , / , '' ) ;
58
- if (
59
- params . layoutOptions . legendOptions . customItems &&
60
- params . layoutOptions . legendOptions . customItems . hasOwnProperty ( 'picAsBase64' )
61
- ) {
62
- params . layoutOptions . legendOptions . customItems . picAsBase64 = params . layoutOptions . legendOptions . customItems . picAsBase64 . replace (
63
- / ^ d a t a : .+ ; b a s e 6 4 , / ,
64
- ''
65
- ) ;
66
- }
67
- }
68
- }
69
- return this . processAsync ( 'jobs' , 'POST' , callback , params )
70
- }
34
+ /**
35
+ * @function WebPrintingService.prototype.destroy
36
+ * @description 释放资源,将引用资源的属性置空。
37
+ */
38
+ destroy ( ) {
39
+ super . destroy ( ) ;
40
+ }
71
41
72
- /**
73
- * @function WebPrintingService.prototype.getPrintingJob
74
- * @description 获取 Web 打印输出文档任务, 轮询获取打印状态,只有当状态为完成或失败才返回结果。
75
- * @param {string } jobId - Web 打印任务 ID
76
- * @param {RequestCallback } callback - 回调函数。
77
- */
78
- getPrintingJob ( jobId , callback ) {
79
- var me = this ;
80
- me . rollingProcess ( me . _processUrl ( `jobs/${ jobId } ` ) , callback ) ;
42
+ /**
43
+ * @function WebPrintingService.prototype.createWebPrintingJob
44
+ * @description 创建 Web 打印任务。
45
+ * @param {WebPrintingJobParameters } params - Web 打印的请求参数。
46
+ * @param {RequestCallback } [callback] - 回调函数,该参数未传时可通过返回的 promise 获取结果。
47
+ * @returns {Promise } Promise 对象。
48
+ */
49
+ async createWebPrintingJob ( params , callback ) {
50
+ if ( ! params ) {
51
+ return ;
81
52
}
53
+ await this . _processParams ( params ) ;
54
+ return this . processAsync ( 'jobs' , 'POST' , callback , params ) ;
55
+ }
82
56
83
- /**
84
- * @function WebPrintingService.prototype.getPrintingJobResult
85
- * @description 获取 Web 打印任务的输出文档 。
86
- * @param {string } jobId - Web 打印输出文档任务 ID。
87
- * @param {RequestCallback } [ callback] - 回调函数,该参数未传时可通过返回的 promise 获取结果 。
88
- * @returns { Promise } Promise 对象。
89
- */
90
- getPrintingJobResult ( jobId , callback ) {
91
- return this . processAsync ( `jobs/${ jobId } /result` , 'GET' , callback ) ;
92
- }
57
+ /**
58
+ * @function WebPrintingService.prototype.getPrintingJob
59
+ * @description 获取 Web 打印输出文档任务, 轮询获取打印状态,只有当状态为完成或失败才返回结果 。
60
+ * @param {string } jobId - Web 打印任务 ID
61
+ * @param {RequestCallback } callback - 回调函数。
62
+ */
63
+ getPrintingJob ( jobId , callback ) {
64
+ var me = this ;
65
+ me . rollingProcess ( me . _processUrl ( `jobs/${ jobId } ` ) , callback ) ;
66
+ }
93
67
94
- /**
95
- * @function WebPrintingService.prototype.getLayoutTemplates
96
- * @description 查询 Web 打印服务所有可用的模板信息。
97
- * @param {RequestCallback } [callback] - 回调函数,该参数未传时可通过返回的 promise 获取结果。
98
- * @returns {Promise } Promise 对象。
99
- */
100
- getLayoutTemplates ( callback ) {
101
- return this . processAsync ( 'layouts' , 'GET' , callback ) ;
102
- }
68
+ /**
69
+ * @function WebPrintingService.prototype.getPrintingJobResult
70
+ * @description 获取 Web 打印任务的输出文档。
71
+ * @param {string } jobId - Web 打印输出文档任务 ID。
72
+ * @param {RequestCallback } [callback] - 回调函数,该参数未传时可通过返回的 promise 获取结果。
73
+ * @returns {Promise } Promise 对象。
74
+ */
75
+ getPrintingJobResult ( jobId , callback ) {
76
+ return this . processAsync ( `jobs/${ jobId } /result` , 'GET' , callback ) ;
77
+ }
103
78
104
- /**
105
- * @function WebPrintingService.prototype.rollingProcess
106
- * @description 轮询查询 Web 打印任务。
107
- * @param {Object } result - 服务器返回的结果对象。
108
- */
109
- rollingProcess ( url , callback ) {
110
- var me = this ;
111
- this . id && clearInterval ( this . id ) ;
112
- this . id = setInterval ( function ( ) {
113
- me . request ( {
114
- url,
115
- method : 'GET' ,
116
- scope : me ,
117
- success : callback ,
118
- failure : callback
119
- } ) ;
120
- } , 1000 ) ;
121
- }
79
+ /**
80
+ * @function WebPrintingService.prototype.getLayoutTemplates
81
+ * @description 查询 Web 打印服务所有可用的模板信息。
82
+ * @param {RequestCallback } [callback] - 回调函数,该参数未传时可通过返回的 promise 获取结果。
83
+ * @returns {Promise } Promise 对象。
84
+ */
85
+ getLayoutTemplates ( callback ) {
86
+ return this . processAsync ( 'layouts' , 'GET' , callback ) ;
87
+ }
122
88
123
- processAsync ( url , method , callback , params ) {
124
- var me = this ;
125
- let requestConfig = {
126
- url : me . _processUrl ( url ) ,
127
- method,
89
+ /**
90
+ * @function WebPrintingService.prototype.rollingProcess
91
+ * @description 轮询查询 Web 打印任务。
92
+ * @param {Object } result - 服务器返回的结果对象。
93
+ */
94
+ rollingProcess ( url , callback ) {
95
+ var me = this ;
96
+ this . id && clearInterval ( this . id ) ;
97
+ this . id = setInterval ( function ( ) {
98
+ me . request ( {
99
+ url,
100
+ method : 'GET' ,
128
101
scope : me ,
129
102
success : callback ,
130
103
failure : callback
131
- } ;
132
- params && ( requestConfig . data = Util . toJSON ( params ) ) ;
133
- return me . request ( requestConfig ) ;
104
+ } ) ;
105
+ } , 1000 ) ;
106
+ }
107
+
108
+ processAsync ( url , method , callback , params ) {
109
+ var me = this ;
110
+ let requestConfig = {
111
+ url : me . _processUrl ( url ) ,
112
+ method,
113
+ scope : me ,
114
+ success : callback ,
115
+ failure : callback
116
+ } ;
117
+ params && ( requestConfig . data = Util . toJSON ( params ) ) ;
118
+ return me . request ( requestConfig ) ;
119
+ }
120
+
121
+ transformResult ( result , options ) {
122
+ result = Util . transformResult ( result ) ;
123
+ if ( result . status === 'FINISHED' || result . status === 'ERROR' ) {
124
+ clearInterval ( this . id ) ;
125
+ } else if ( result . status === 'RUNNING' ) {
126
+ options . success = false ;
134
127
}
128
+ return { result, options } ;
129
+ }
135
130
136
- transformResult ( result , options ) {
137
- result = Util . transformResult ( result ) ;
138
- if ( result . status === 'FINISHED' || result . status === 'ERROR' ) {
139
- clearInterval ( this . id ) ;
140
- } else if ( result . status === 'RUNNING' ) {
141
- options . success = false ;
142
- }
143
- return { result, options } ;
131
+ _processUrl ( appendContent ) {
132
+ if ( appendContent ) {
133
+ return Util . urlPathAppend ( this . url , appendContent ) ;
144
134
}
135
+ return this . url ;
136
+ }
145
137
146
- _processUrl ( appendContent ) {
147
- if ( appendContent ) {
148
- return Util . urlPathAppend ( this . url , appendContent ) ;
149
- }
150
- return this . url ;
138
+ async _processParams ( params ) {
139
+ if ( ! params . layoutOptions ) {
140
+ return ;
141
+ }
142
+ const { legendOptions, templateName } = params . layoutOptions ;
143
+ if ( legendOptions ) {
144
+ ! params . layoutOptions . legendOptions . title && ( params . layoutOptions . legendOptions . title = '' ) ;
145
+ params . layoutOptions . legendOptions . picAsBase64 =
146
+ params . layoutOptions . legendOptions . picAsBase64 &&
147
+ params . layoutOptions . legendOptions . picAsBase64 . replace ( / ^ d a t a : .+ ; b a s e 6 4 , / , '' ) ;
148
+ if (
149
+ params . layoutOptions . legendOptions . customItems &&
150
+ params . layoutOptions . legendOptions . customItems . hasOwnProperty ( 'picAsBase64' )
151
+ ) {
152
+ params . layoutOptions . legendOptions . customItems . picAsBase64 =
153
+ params . layoutOptions . legendOptions . customItems . picAsBase64 . replace ( / ^ d a t a : .+ ; b a s e 6 4 , / , '' ) ;
154
+ }
155
+ }
156
+ if ( ! this . templates . length ) {
157
+ const res = await this . getLayoutTemplates ( ) ;
158
+ this . templates = res . result ;
151
159
}
160
+ const matchTemplate = this . templates . find ( ( item ) => item . templateName === templateName ) ;
161
+ //根据模板判断哪些参数需要传递
162
+ const {
163
+ hasCopyright,
164
+ hasSubtitle,
165
+ hasAuthor,
166
+ hasScaleBar,
167
+ hasTitle,
168
+ hasTime,
169
+ hasSummaryText,
170
+ hasLittleMap,
171
+ hasNorthArrow,
172
+ hasLegend
173
+ } = matchTemplate . layoutOptions ;
174
+ const layoutOptions = params . layoutOptions ;
175
+ if ( ! hasTitle ) {
176
+ delete params . layoutOptions . title ;
177
+ } else if ( layoutOptions . title === void 0 ) {
178
+ params . layoutOptions . title = null ;
179
+ }
180
+ if ( ! hasSubtitle ) {
181
+ delete params . layoutOptions . subTitle ;
182
+ } else if ( layoutOptions . subTitle === void 0 ) {
183
+ params . layoutOptions . subTitle = null ;
184
+ }
185
+ if ( ! hasAuthor ) {
186
+ delete params . layoutOptions . author ;
187
+ } else if ( layoutOptions . author === void 0 ) {
188
+ params . layoutOptions . author = null ;
189
+ }
190
+ if ( ! hasCopyright ) {
191
+ delete params . layoutOptions . copyright ;
192
+ } else if ( layoutOptions . copyright === void 0 ) {
193
+ params . layoutOptions . copyright = null ;
194
+ }
195
+ if ( ! hasSummaryText || ! layoutOptions . summaryText ) {
196
+ delete params . layoutOptions . summaryText ;
197
+ }
198
+ if ( ! hasTime || ! layoutOptions . time ) {
199
+ delete params . layoutOptions . time ;
200
+ }
201
+ if ( ! hasLittleMap || ! layoutOptions . littleMapOptions ) {
202
+ delete params . layoutOptions . littleMapOptions ;
203
+ }
204
+ if ( ! hasScaleBar || ! layoutOptions . scaleBarOptions ) {
205
+ delete params . layoutOptions . scaleBarOptions ;
206
+ }
207
+ if ( ! hasNorthArrow || ! layoutOptions . northArrowOptions ) {
208
+ delete params . layoutOptions . northArrowOptions ;
209
+ }
210
+ if ( ! hasLegend || ! layoutOptions . legendOptions ) {
211
+ delete params . layoutOptions . legendOptions ;
212
+ }
213
+ }
152
214
}
153
-
0 commit comments