Skip to content

Commit de7bb4e

Browse files
committed
【update】地图打印参数根据模板的配置校验; review by luox
1 parent fb862b2 commit de7bb4e

File tree

4 files changed

+295
-165
lines changed

4 files changed

+295
-165
lines changed

examples/mapboxgl/webPrintingJob.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ <h4 class="panel-title">
344344
window.URL.revokeObjectURL(link.href);
345345
$('#printBtn').show();
346346
$('.progress').hide();
347-
} else if (!res.result) {
347+
} else if (!res.result || res.result.status === 'ERROR') {
348348
alert('打印失败');
349349
$('#printBtn').show();
350350
$('.progress').hide();

src/common/iServer/WebPrintingJobLayoutOptions.js

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ import { WebPrintingJobLegendOptions } from './WebPrintingJobLegendOptions';
1919
* @param {string} option.subTitle - 地图副标题名称。
2020
* @param {string} option.author - 地图作者名称。
2121
* @param {string} option.copyright - 版权描述信息。
22-
* @param {WebPrintingJobLittleMapOptions} option.littleMapOptions - 小地图参数类。
23-
* @param {WebPrintingJobLegendOptions} option.legendOptions - 图例参数类。
22+
* @param {string} [option.summaryText] - 自定义描述信息。
23+
* @param {string} [option.time] - 打印时间。
24+
* @param {WebPrintingJobLittleMapOptions} [option.littleMapOptions] - 小地图参数类。
25+
* @param {WebPrintingJobLegendOptions} [option.legendOptions] - 图例参数类。
2426
* @param {WebPrintingJobScaleBarOptions} [option.scaleBarOptions] - 地图比例尺参数类。
2527
* @param {WebPrintingJobNorthArrowOptions} [option.northArrowOptions] - 地图指北针参数类。
2628
* @usage
@@ -52,6 +54,16 @@ export class WebPrintingJobLayoutOptions {
5254
* @description 地图版权描述信息。
5355
*/
5456
this.copyright = null;
57+
/**
58+
* @member {string} WebPrintingJobLayoutOptions.prototype.summaryText
59+
* @description 自定义描述信息。
60+
*/
61+
this.summaryText = null;
62+
/**
63+
* @member {string} WebPrintingJobLayoutOptions.prototype.time
64+
* @description 打印时间
65+
*/
66+
this.time = null;
5567
/**
5668
* @member {WebPrintingJobScaleBarOptions} [WebPrintingJobLayoutOptions.prototype.scaleBarOptions]
5769
* @description 地图比例尺参数类。
@@ -87,6 +99,8 @@ export class WebPrintingJobLayoutOptions {
8799
this.subTitle = null;
88100
this.author = null;
89101
this.copyright = null;
102+
this.summaryText = null;
103+
this.time = null;
90104
if (this.scaleBarOptions instanceof WebPrintingJobScaleBarOptions) {
91105
this.scaleBarOptions.destroy();
92106
this.scaleBarOptions = null;
@@ -104,33 +118,5 @@ export class WebPrintingJobLayoutOptions {
104118
this.legendOptions = null;
105119
}
106120
}
107-
108-
/**
109-
* @function WebPrintingJobLayoutOptions.prototype.toJSON
110-
* @description 将 WebPrintingJobLayoutOptions 对象转化为 JSON 字符串。
111-
* @returns {string} 转换后的 JSON 字符串。
112-
*/
113-
toJSON() {
114-
var params = {
115-
templateName: this.templateName,
116-
title: this.title,
117-
subTitle: this.subTitle,
118-
author: this.author,
119-
copyright: this.copyright
120-
};
121-
if (this.scaleBarOptions) {
122-
params.scaleBarOptions = this.scaleBarOptions;
123-
}
124-
if (this.northArrowOptions) {
125-
params.northArrowOptions = this.northArrowOptions;
126-
}
127-
if (this.littleMapOptions) {
128-
params.littleMapOptions = this.littleMapOptions;
129-
}
130-
if (this.legendOptions) {
131-
params.legendOptions = this.legendOptions;
132-
}
133-
return Util.toJSON(params);
134-
}
135121
}
136122

src/common/iServer/WebPrintingService.js

Lines changed: 175 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -18,136 +18,197 @@ import { CommonServiceBase } from './CommonServiceBase';
1818
* @usage
1919
*/
2020
export class WebPrintingService extends CommonServiceBase {
21-
constructor(url, options) {
22-
super(url, options);
21+
constructor(url, options) {
22+
super(url, options);
2323

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);
3126
}
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;
3931
}
32+
}
4033

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(/^data:.+;base64,/, '');
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-
/^data:.+;base64,/,
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+
}
7141

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;
8152
}
53+
await this._processParams(params);
54+
return this.processAsync('jobs', 'POST', callback, params);
55+
}
8256

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+
}
9367

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+
}
10378

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+
}
12288

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',
128101
scope: me,
129102
success: callback,
130103
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;
134127
}
128+
return { result, options };
129+
}
135130

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);
144134
}
135+
return this.url;
136+
}
145137

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(/^data:.+;base64,/, '');
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(/^data:.+;base64,/, '');
154+
}
155+
}
156+
if (!this.templates.length) {
157+
const res = await this.getLayoutTemplates();
158+
this.templates = res.result;
151159
}
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+
}
152214
}
153-

0 commit comments

Comments
 (0)