yh-hiprint 2.5.3 → 2.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/hiprintPreview.vue +42 -18
- package/index.js +254 -39
- package/libs/hiprint.bundle.js +2 -2
- package/libs/index.js +25 -1
- package/package.json +1 -1
package/hiprintPreview.vue
CHANGED
|
@@ -134,26 +134,48 @@ async function getData(query) {
|
|
|
134
134
|
list = [];
|
|
135
135
|
}
|
|
136
136
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
137
|
+
|
|
138
|
+
// 检查是否启用客户端打印
|
|
139
|
+
const useClientPrint = localStorage.getItem('useClientPrint') === 'true';
|
|
140
|
+
|
|
141
|
+
if (useClientPrint && hiprint.hiwebSocket.opened) {
|
|
142
|
+
// 使用客户端打印
|
|
143
|
+
loading.setText('正在连接打印客户端');
|
|
144
|
+
|
|
145
|
+
// 获取打印机列表
|
|
146
|
+
let printerList = hiprint.hiwebSocket.getPrinterList();
|
|
147
|
+
if (!printerList || printerList.length === 0) {
|
|
148
|
+
ElMessageBox.alert('未获取到打印机列表', '提示');
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
loading.close();
|
|
153
|
+
|
|
154
|
+
// 显示打印机选择对话框
|
|
155
|
+
showPrinterSelectionDialog(printerList, hiprintTemplate, list);
|
|
146
156
|
} else {
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
if (
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
ElMessageBox.alert('
|
|
157
|
+
// 默认浏览器打印
|
|
158
|
+
let html = hiprintTemplate.getHtml(list);
|
|
159
|
+
if (opener && returnHtml && html[0]) {
|
|
160
|
+
opener.postMessage({
|
|
161
|
+
type: 'sendPrintHTML',
|
|
162
|
+
data: html[0].innerHTML,
|
|
163
|
+
});
|
|
164
|
+
ElMessageBox.alert('渲染完成!确认后返回系统').then(() => {
|
|
155
165
|
window.close();
|
|
156
166
|
});
|
|
167
|
+
} else {
|
|
168
|
+
document.body.innerHTML = '';
|
|
169
|
+
document.body.appendChild(html[0]);
|
|
170
|
+
if (hasData) {
|
|
171
|
+
setTimeout(() => {
|
|
172
|
+
window.print();
|
|
173
|
+
}, 1000);
|
|
174
|
+
} else {
|
|
175
|
+
ElMessageBox.alert('数据源没有数据,打印将取消').then(() => {
|
|
176
|
+
window.close();
|
|
177
|
+
});
|
|
178
|
+
}
|
|
157
179
|
}
|
|
158
180
|
}
|
|
159
181
|
success.value = false;
|
|
@@ -164,7 +186,9 @@ async function getData(query) {
|
|
|
164
186
|
console.error(error);
|
|
165
187
|
error.value = true;
|
|
166
188
|
} finally {
|
|
167
|
-
loading
|
|
189
|
+
if (loading) {
|
|
190
|
+
loading.close();
|
|
191
|
+
}
|
|
168
192
|
}
|
|
169
193
|
}
|
|
170
194
|
</script>
|
package/index.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
// @ts-check
|
|
2
1
|
import './libs/jquery';
|
|
2
|
+
import { ElDialog, ElSelect, ElOption, ElButton, ElMessageBox, ElLoading } from 'element-plus';
|
|
3
3
|
export { hiprint, defaultElementTypeProvider, print, print2, usePaper, useScale, useDataSource } from './hooks/useHiprint';
|
|
4
4
|
export { default as fontSize } from './font-size';
|
|
5
5
|
export { default as scale } from './scale';
|
|
6
6
|
export { default as zIndex } from './z-index';
|
|
7
7
|
export { default as panel } from './panel';
|
|
8
8
|
import { getPrintTemplate } from 'yh-hiprint/libs/index.js';
|
|
9
|
-
import {
|
|
9
|
+
import { createVNode, render, createApp } from 'vue';
|
|
10
10
|
|
|
11
11
|
export function cLog (string, isError = false) {
|
|
12
12
|
if (isError) {
|
|
@@ -17,42 +17,46 @@ export function cLog (string, isError = false) {
|
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
const hiprintFun = async ({ code, params, data, isCustom, returnHtml }) => {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
url += `¶ms=${encodeURIComponent(JSON.stringify(paramData))}`;
|
|
31
|
-
}
|
|
32
|
-
if (data) {
|
|
33
|
-
url += `&data=${encodeURIComponent(JSON.stringify(data))}`;
|
|
34
|
-
}
|
|
35
|
-
if (isCustom) {
|
|
36
|
-
url += `&isCustom=${isCustom ? '1' : '0'}`;
|
|
37
|
-
}
|
|
38
|
-
if (returnHtml) {
|
|
39
|
-
url += `&returnHtml=${returnHtml ? '1' : '0'}`;
|
|
40
|
-
}
|
|
41
|
-
let windowOpen = window.open(url, 'hiprintWindow', `height=${height}, width=${width}, top=20, left=${left}, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no`);
|
|
42
|
-
|
|
43
|
-
return new Promise((resolve, reject) => {
|
|
44
|
-
if (!windowOpen) {
|
|
45
|
-
reject("预览窗口渲染失败!");
|
|
20
|
+
if (hiprint.hiwebSocket && hiprint.hiwebSocket.opened) {
|
|
21
|
+
clientPrintHandler(code, params, data);
|
|
22
|
+
} else {
|
|
23
|
+
let height = document.documentElement.clientHeight;
|
|
24
|
+
let width = 1200;
|
|
25
|
+
let left = (document.documentElement.clientWidth - 1200) / 2;
|
|
26
|
+
// 转换数组
|
|
27
|
+
let paramData = params;
|
|
28
|
+
if (!Array.isArray(params)) {
|
|
29
|
+
paramData = [params];
|
|
46
30
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
resolve(data);
|
|
51
|
-
}
|
|
52
|
-
window.removeEventListener('message', printHTMLHandler);
|
|
31
|
+
let url = '/hiprint/#/preview?code=' + encodeURIComponent(code);
|
|
32
|
+
if (params) {
|
|
33
|
+
url += `¶ms=${encodeURIComponent(JSON.stringify(paramData))}`;
|
|
53
34
|
}
|
|
54
|
-
|
|
55
|
-
|
|
35
|
+
if (data) {
|
|
36
|
+
url += `&data=${encodeURIComponent(JSON.stringify(data))}`;
|
|
37
|
+
}
|
|
38
|
+
if (isCustom) {
|
|
39
|
+
url += `&isCustom=${isCustom ? '1' : '0'}`;
|
|
40
|
+
}
|
|
41
|
+
if (returnHtml) {
|
|
42
|
+
url += `&returnHtml=${returnHtml ? '1' : '0'}`;
|
|
43
|
+
}
|
|
44
|
+
let windowOpen = window.open(url, 'hiprintWindow', `height=${height}, width=${width}, top=20, left=${left}, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no`);
|
|
45
|
+
|
|
46
|
+
return new Promise((resolve, reject) => {
|
|
47
|
+
if (!windowOpen) {
|
|
48
|
+
reject("预览窗口渲染失败!");
|
|
49
|
+
}
|
|
50
|
+
function printHTMLHandler (e) {
|
|
51
|
+
let { type, data } = e.data;
|
|
52
|
+
if (type === "sendPrintHTML" && data) {
|
|
53
|
+
resolve(data);
|
|
54
|
+
}
|
|
55
|
+
window.removeEventListener('message', printHTMLHandler);
|
|
56
|
+
}
|
|
57
|
+
window.addEventListener('message', printHTMLHandler);
|
|
58
|
+
});
|
|
59
|
+
}
|
|
56
60
|
};
|
|
57
61
|
|
|
58
62
|
const silentHiprint = async ({ code, params = {}, data }) => {
|
|
@@ -90,15 +94,226 @@ const silentHiprint = async ({ code, params = {}, data }) => {
|
|
|
90
94
|
loading = null;
|
|
91
95
|
}
|
|
92
96
|
}
|
|
93
|
-
|
|
94
97
|
};
|
|
95
98
|
|
|
96
99
|
window.silentHiprint = silentHiprint;
|
|
97
100
|
|
|
101
|
+
// 客户端打印处理函数
|
|
102
|
+
const clientPrintHandler = async (code, params, data) => {
|
|
103
|
+
let loading = null;
|
|
104
|
+
try {
|
|
105
|
+
loading = ElLoading.service({
|
|
106
|
+
text: "正在获取打印模板"
|
|
107
|
+
});
|
|
108
|
+
let reqParams = params ? JSON.parse(params) : [];
|
|
109
|
+
// 获取打印模板
|
|
110
|
+
let res = await getPrintTemplate(code, reqParams);
|
|
111
|
+
if (res.status !== 200 || !res.data || !res.data.json) {
|
|
112
|
+
ElMessageBox.alert(res.message || "获取打印模板失败", '提示');
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
loading.setText("正在连接打印客户端");
|
|
117
|
+
|
|
118
|
+
// 检查客户端是否连接
|
|
119
|
+
if (!hiprint.hiwebSocket.opened) {
|
|
120
|
+
loading?.close();
|
|
121
|
+
ElMessageBox.alert("打印客户端未连接", '提示');
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// 获取打印机列表
|
|
126
|
+
let printerList = hiprint.hiwebSocket.getPrinterList();
|
|
127
|
+
if (!printerList || printerList.length === 0) {
|
|
128
|
+
loading?.close();
|
|
129
|
+
ElMessageBox.alert("未获取到打印机列表", '提示');
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
loading.close();
|
|
134
|
+
loading = null;
|
|
135
|
+
|
|
136
|
+
// 显示选择打印机的对话框
|
|
137
|
+
return await showPrinterSelectionDialog(printerList, res.data.json, res.data.list, res.data.name, data);
|
|
138
|
+
} catch (error) {
|
|
139
|
+
if (loading) {
|
|
140
|
+
loading.close();
|
|
141
|
+
loading = null;
|
|
142
|
+
}
|
|
143
|
+
console.error(error);
|
|
144
|
+
ElMessageBox.alert("客户端打印出错: " + error.message, '提示');
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
// 显示打印机选择对话框
|
|
150
|
+
const showPrinterSelectionDialog = (printerList, templateJson, templateData, templateName, data) => {
|
|
151
|
+
return new Promise((resolve) => {
|
|
152
|
+
// 获取上次选择的打印机
|
|
153
|
+
const lastSelectedPrinter = localStorage.getItem('lastSelectedPrinter') || '';
|
|
154
|
+
// 创建挂载点
|
|
155
|
+
const mountPoint = document.createElement('div');
|
|
156
|
+
document.body.appendChild(mountPoint);
|
|
157
|
+
|
|
158
|
+
// 创建Vue组件
|
|
159
|
+
const PrinterSelectorComponent = {
|
|
160
|
+
components: {
|
|
161
|
+
ElDialog,
|
|
162
|
+
ElSelect,
|
|
163
|
+
ElOption,
|
|
164
|
+
ElButton
|
|
165
|
+
},
|
|
166
|
+
data () {
|
|
167
|
+
return {
|
|
168
|
+
visible: true,
|
|
169
|
+
selectedPrinter: lastSelectedPrinter,
|
|
170
|
+
printerList: printerList
|
|
171
|
+
};
|
|
172
|
+
},
|
|
173
|
+
methods: {
|
|
174
|
+
handlePrint: async function () {
|
|
175
|
+
if (!this.selectedPrinter) {
|
|
176
|
+
ElMessageBox.alert("请选择打印机", '提示');
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
// 保存选择的打印机
|
|
180
|
+
localStorage.setItem('lastSelectedPrinter', this.selectedPrinter);
|
|
181
|
+
|
|
182
|
+
let loading = ElLoading.service({
|
|
183
|
+
text: "正在渲染打印内容"
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
try {
|
|
187
|
+
// 创建打印模板实例
|
|
188
|
+
let hiprintTemplate = new hiprint.PrintTemplate({ template: JSON.parse(templateJson) });
|
|
189
|
+
|
|
190
|
+
loading.setText("正在发送打印任务");
|
|
191
|
+
|
|
192
|
+
// 发送到客户端打印
|
|
193
|
+
hiprintTemplate.print2(data || templateData, {
|
|
194
|
+
printer: this.selectedPrinter,
|
|
195
|
+
title: templateName
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
// 监听打印结果
|
|
199
|
+
const printSuccessHandler = (result) => {
|
|
200
|
+
loading.close();
|
|
201
|
+
ElMessageBox.alert("打印任务已发送到打印机", '提示');
|
|
202
|
+
cleanup();
|
|
203
|
+
this.handleClose(true);
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
const printErrorHandler = (error) => {
|
|
207
|
+
loading.close();
|
|
208
|
+
ElMessageBox.alert("打印失败: " + (error.msg || "未知错误"), '提示');
|
|
209
|
+
cleanup();
|
|
210
|
+
this.handleClose(false);
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
// 注册事件监听器
|
|
214
|
+
hiprint.hiwebSocket.socket.on('success', printSuccessHandler);
|
|
215
|
+
hiprint.hiwebSocket.socket.on('error', printErrorHandler);
|
|
216
|
+
|
|
217
|
+
// 清理函数
|
|
218
|
+
const cleanup = () => {
|
|
219
|
+
hiprint.hiwebSocket.socket.off('success', printSuccessHandler);
|
|
220
|
+
hiprint.hiwebSocket.socket.off('error', printErrorHandler);
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
} catch (error) {
|
|
224
|
+
if (loading) {
|
|
225
|
+
loading.close();
|
|
226
|
+
}
|
|
227
|
+
console.error(error);
|
|
228
|
+
ElMessageBox.alert("打印过程中发生错误: " + error.message, '提示');
|
|
229
|
+
this.handleClose(false);
|
|
230
|
+
}
|
|
231
|
+
},
|
|
232
|
+
handleClose: function (result = false) {
|
|
233
|
+
this.visible = false;
|
|
234
|
+
// 延迟销毁,确保动画完成
|
|
235
|
+
setTimeout(() => {
|
|
236
|
+
app.unmount();
|
|
237
|
+
document.body.removeChild(mountPoint);
|
|
238
|
+
resolve(result);
|
|
239
|
+
}, 300);
|
|
240
|
+
}
|
|
241
|
+
},
|
|
242
|
+
render () {
|
|
243
|
+
return createVNode(ElDialog, {
|
|
244
|
+
modelValue: this.visible,
|
|
245
|
+
title: "选择打印机",
|
|
246
|
+
width: "500px",
|
|
247
|
+
onClose: () => this.handleClose(false),
|
|
248
|
+
onClosed: () => this.handleClose(false)
|
|
249
|
+
}, {
|
|
250
|
+
default: () => [
|
|
251
|
+
createVNode(ElSelect, {
|
|
252
|
+
modelValue: this.selectedPrinter,
|
|
253
|
+
"onUpdate:modelValue": (val) => { this.selectedPrinter = val; },
|
|
254
|
+
placeholder: "请选择打印机",
|
|
255
|
+
style: "width: 100%; margin-bottom: 15px;"
|
|
256
|
+
}, {
|
|
257
|
+
default: () => this.printerList.map(printer =>
|
|
258
|
+
createVNode(ElOption, {
|
|
259
|
+
key: printer.name,
|
|
260
|
+
label: printer.name,
|
|
261
|
+
value: printer.name
|
|
262
|
+
})
|
|
263
|
+
)
|
|
264
|
+
})
|
|
265
|
+
],
|
|
266
|
+
footer: () => [
|
|
267
|
+
createVNode("span", { class: "dialog-footer" }, [
|
|
268
|
+
createVNode(ElButton, {
|
|
269
|
+
onClick: () => this.handleClose(false)
|
|
270
|
+
}, {
|
|
271
|
+
default: () => "取消"
|
|
272
|
+
}),
|
|
273
|
+
createVNode(ElButton, {
|
|
274
|
+
type: "primary",
|
|
275
|
+
onClick: this.handlePrint
|
|
276
|
+
}, {
|
|
277
|
+
default: () => "打印"
|
|
278
|
+
})
|
|
279
|
+
])
|
|
280
|
+
]
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
// 创建应用实例
|
|
286
|
+
const app = createApp(PrinterSelectorComponent);
|
|
287
|
+
|
|
288
|
+
// 挂载应用
|
|
289
|
+
app.mount(mountPoint);
|
|
290
|
+
});
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
// window.silentHiprint = silentHiprint;
|
|
294
|
+
|
|
98
295
|
export default {
|
|
99
|
-
install (app, { router, pinia, isAdmin }) {
|
|
296
|
+
install (app, { router, pinia, isAdmin, clientPrint = false, clientUrl = 'http://localhost:17521' }) {
|
|
100
297
|
app.provide('$hiprint', hiprintFun);
|
|
101
298
|
app.provide('$silentHiprint', silentHiprint);
|
|
299
|
+
|
|
300
|
+
// 如果启用了客户端打印,初始化连接
|
|
301
|
+
if (clientPrint) {
|
|
302
|
+
// 确保DOM加载完成后再启动连接
|
|
303
|
+
const initializeClientPrint = () => {
|
|
304
|
+
if (hiprint.hiwebSocket && !hiprint.hiwebSocket.opened) {
|
|
305
|
+
window.hiwebSocket.host = clientUrl;
|
|
306
|
+
hiprint.hiwebSocket.start();
|
|
307
|
+
}
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
if (document.readyState === 'loading') {
|
|
311
|
+
document.addEventListener('DOMContentLoaded', initializeClientPrint);
|
|
312
|
+
} else {
|
|
313
|
+
initializeClientPrint();
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
102
317
|
if (router) {
|
|
103
318
|
router.addRoute('Index', {
|
|
104
319
|
path: 'hiprint/designer',
|
|
@@ -124,4 +339,4 @@ export default {
|
|
|
124
339
|
}
|
|
125
340
|
}
|
|
126
341
|
},
|
|
127
|
-
};
|
|
342
|
+
};
|
package/libs/hiprint.bundle.js
CHANGED
|
@@ -8619,7 +8619,7 @@ var hiprint = (function (t) {
|
|
|
8619
8619
|
return !0;
|
|
8620
8620
|
},
|
|
8621
8621
|
hasIo: function hasIo (t) {
|
|
8622
|
-
return
|
|
8622
|
+
return io;
|
|
8623
8623
|
},
|
|
8624
8624
|
send: function send (t) {
|
|
8625
8625
|
try {
|
|
@@ -8670,7 +8670,7 @@ var hiprint = (function (t) {
|
|
|
8670
8670
|
var t = this;
|
|
8671
8671
|
window.WebSocket
|
|
8672
8672
|
? this.socket ||
|
|
8673
|
-
((this.socket =
|
|
8673
|
+
((this.socket = io(this.host, {
|
|
8674
8674
|
reconnectionAttempts: 5,
|
|
8675
8675
|
})),
|
|
8676
8676
|
this.socket.on('connect', function (e) {
|
package/libs/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import axios from '@/libs/api.request.js';
|
|
2
|
-
export function getPrintTemplate(code, reqParams) {
|
|
2
|
+
export function getPrintTemplate (code, reqParams) {
|
|
3
3
|
return axios
|
|
4
4
|
.request({
|
|
5
5
|
url: `/printTemplate/data/${code}`,
|
|
@@ -11,3 +11,27 @@ export function getPrintTemplate(code, reqParams) {
|
|
|
11
11
|
return res;
|
|
12
12
|
});
|
|
13
13
|
}
|
|
14
|
+
|
|
15
|
+
// 获取打印机列表
|
|
16
|
+
export function getPrinterList () {
|
|
17
|
+
// 从 hiprint 获取打印机列表
|
|
18
|
+
if (window.hiprint && window.hiprint.hiwebSocket) {
|
|
19
|
+
return window.hiprint.hiwebSocket.getPrinterList() || [];
|
|
20
|
+
}
|
|
21
|
+
return [];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// 刷新打印机列表
|
|
25
|
+
export function refreshPrinterList () {
|
|
26
|
+
if (window.hiprint && window.hiprint.hiwebSocket) {
|
|
27
|
+
window.hiprint.hiwebSocket.refreshPrinterList();
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// 检查客户端连接状态
|
|
32
|
+
export function isClientConnected () {
|
|
33
|
+
if (window.hiprint && window.hiprint.hiwebSocket) {
|
|
34
|
+
return window.hiprint.hiwebSocket.opened || false;
|
|
35
|
+
}
|
|
36
|
+
return false;
|
|
37
|
+
}
|