yh-hiprint 2.6.5 → 2.6.6

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.
Files changed (2) hide show
  1. package/index.js +75 -71
  2. package/package.json +2 -12
package/index.js CHANGED
@@ -1,12 +1,20 @@
1
1
  import './libs/jquery';
2
- import { ElDialog, ElSelect, ElOption, ElButton, ElMessageBox, ElLoading } from 'element-plus';
2
+ import { ElDialog, ElSelect, ElOption, ElButton, ElMessageBox,ElLoading } from 'element-plus';
3
+ import { createApp,createVNode } from 'vue';
3
4
  export { hiprint, defaultElementTypeProvider, print, print2, usePaper, useScale, useDataSource } from './hooks/useHiprint';
4
5
  export { default as fontSize } from './font-size';
5
6
  export { default as scale } from './scale';
6
7
  export { default as zIndex } from './z-index';
7
8
  export { default as panel } from './panel';
8
9
  import { getPrintTemplate } from 'yh-hiprint/libs/index.js';
9
- import { createVNode, render, createApp } from 'vue';
10
+ import printCss from 'yh-hiprint/libs/css/print-lock.css?inline';
11
+
12
+ export function guid () {
13
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (t) {
14
+ var e = (16 * Math.random()) | 0;
15
+ return ('x' == t ? e : (3 & e) | 8).toString(16);
16
+ });
17
+ }
10
18
 
11
19
  export function cLog (string, isError = false) {
12
20
  if (isError) {
@@ -17,7 +25,7 @@ export function cLog (string, isError = false) {
17
25
  }
18
26
 
19
27
  const hiprintFun = async ({ code, params, data, isCustom, returnHtml }) => {
20
- if (hiprint.hiwebSocket && hiprint.hiwebSocket.opened) {
28
+ if (electronBrowserAPI && electronBrowserAPI.print) {
21
29
  clientPrintHandler(code, params, data);
22
30
  } else {
23
31
  let height = document.documentElement.clientHeight;
@@ -120,15 +128,8 @@ const clientPrintHandler = async (code, params, data) => {
120
128
 
121
129
  loading.setText("正在连接打印客户端");
122
130
 
123
- // 检查客户端是否连接
124
- if (!hiprint.hiwebSocket.opened) {
125
- loading?.close();
126
- ElMessageBox.alert("打印客户端未连接", '提示');
127
- return false;
128
- }
129
-
130
131
  // 获取打印机列表
131
- let printerList = hiprint.hiwebSocket.getPrinterList();
132
+ let printerList = await electronBrowserAPI.getPrinterList();
132
133
  if (!printerList || printerList.length === 0) {
133
134
  loading?.close();
134
135
  ElMessageBox.alert("未获取到打印机列表", '提示');
@@ -162,6 +163,10 @@ const showPrinterSelectionDialog = (printerList, templateJson, templateData, tem
162
163
  json = JSON.parse(templateJson);
163
164
  } catch (error) {
164
165
  console.error('客户端打印解析模板失败:', error);
166
+ ElMessageBox.alert('客户端打印解析模板失败', '提示');
167
+ document.body.removeChild(mountPoint);
168
+ resolve(false);
169
+ return;
165
170
  }
166
171
  // 创建挂载点
167
172
  const mountPoint = document.createElement('div');
@@ -200,41 +205,73 @@ const showPrinterSelectionDialog = (printerList, templateJson, templateData, tem
200
205
  let hiprintTemplate = new hiprint.PrintTemplate({ template: json });
201
206
 
202
207
  loading.setText("正在发送打印任务");
203
-
204
- // 发送到客户端打印
205
- hiprintTemplate.print2(data || templateData, {
208
+ const newGuid = guid();
209
+ const printOption = {
206
210
  printer: this.selectedPrinter,
207
211
  title: templateName,
212
+ imgToBase64: true,
213
+ id: newGuid,
214
+ templateId: newGuid,
208
215
  pageSize: {
209
216
  width: (json?.panels[0]?.width || 210) * 1000,
210
217
  height: (json?.panels[0]?.height || 297) * 1000
211
218
  }
212
- });
219
+ };
220
+ const pCss = `<style rel="stylesheet" type="text/css">${printCss}</style>`;
221
+ const html = hiprintTemplate.getHtml(data || templateData, printOption)[0].outerHTML;
222
+
223
+ printOption.html = `${pCss}${html}`;
213
224
 
214
225
  // 监听打印结果
215
- const printSuccessHandler = (result) => {
216
- loading.close();
217
- ElMessageBox.alert("打印任务已发送到打印机", '提示');
218
- cleanup();
219
- this.handleClose(true);
226
+ const handlePrintResult = (event) => {
227
+ if (event.data && event.data.type) {
228
+ switch (event.data.type) {
229
+ case 'PRINT_SUCCESS':
230
+ loading.close();
231
+ this.handleClose(true);
232
+ ElMessage.success("打印任务已发送到打印机");
233
+ // 移除事件监听器
234
+ window.removeEventListener('message', handlePrintResult);
235
+ clearTimeout(timeoutId);
236
+ break;
237
+ case 'PRINT_ERROR':
238
+ loading.close();
239
+ const error = event.data.data;
240
+ ElMessageBox.alert("打印失败: " + (error.msg || "未知错误"), '提示');
241
+ this.handleClose(false);
242
+ // 移除事件监听器
243
+ window.removeEventListener('message', handlePrintResult);
244
+ clearTimeout(timeoutId);
245
+ break;
246
+ default:
247
+ break;
248
+ }
249
+ }
220
250
  };
221
251
 
222
- const printErrorHandler = (error) => {
252
+ // 添加事件监听器
253
+ window.addEventListener('message', handlePrintResult);
254
+
255
+ // 添加超时机制,防止监听器泄漏
256
+ const timeoutId = setTimeout(() => {
257
+ window.removeEventListener('message', handlePrintResult);
223
258
  loading.close();
224
- ElMessageBox.alert("打印失败: " + (error.msg || "未知错误"), '提示');
225
- cleanup();
259
+ ElMessageBox.alert("打印超时", '提示');
226
260
  this.handleClose(false);
227
- };
228
-
229
- // 注册事件监听器
230
- hiprint.hiwebSocket.socket.on('success', printSuccessHandler);
231
- hiprint.hiwebSocket.socket.on('error', printErrorHandler);
261
+ }, 60*1000); // 30秒超时
262
+
263
+ // 发送到客户端打印
264
+ try {
265
+ electronBrowserAPI.print(printOption);
266
+ } catch (error) {
267
+ console.error('客户端打印发送失败:', error);
268
+ loading.close();
269
+ window.removeEventListener('message', handlePrintResult);
270
+ clearTimeout(timeoutId);
271
+ ElMessageBox.alert("客户端打印发送失败: " + error.message, '提示');
272
+ this.handleClose(false);
273
+ }
232
274
 
233
- // 清理函数
234
- const cleanup = () => {
235
- hiprint.hiwebSocket.socket.off('success', printSuccessHandler);
236
- hiprint.hiwebSocket.socket.off('error', printErrorHandler);
237
- };
238
275
 
239
276
  } catch (error) {
240
277
  if (loading) {
@@ -247,12 +284,10 @@ const showPrinterSelectionDialog = (printerList, templateJson, templateData, tem
247
284
  },
248
285
  handleClose: function (result = false) {
249
286
  this.visible = false;
250
- // 延迟销毁,确保动画完成
251
- setTimeout(() => {
252
- app.unmount();
253
- document.body.removeChild(mountPoint);
254
- resolve(result);
255
- }, 300);
287
+ // 移除固定延迟,直接执行卸载逻辑
288
+ app.unmount();
289
+ document.body.removeChild(mountPoint);
290
+ resolve(result);
256
291
  }
257
292
  },
258
293
  render () {
@@ -309,41 +344,10 @@ const showPrinterSelectionDialog = (printerList, templateJson, templateData, tem
309
344
  // window.silentHiprint = silentHiprint;
310
345
 
311
346
  export default {
312
- install (app, { router, pinia, isAdmin, clientPrint = false, clientUrl = 'http://localhost:17521', clientApp = "https://gitee.com/CcSimple/electron-hiprint/releases", clinetTip = false }) {
347
+ install (app, { router, pinia, isAdmin, clientPrint = false }) {
313
348
  app.provide('$hiprint', hiprintFun);
314
349
  app.provide('$silentHiprint', silentHiprint);
315
350
 
316
- // 如果启用了客户端打印,初始化连接
317
- if (clientPrint) {
318
- // 确保DOM加载完成后再启动连接
319
- const initializeClientPrint = () => {
320
- if (hiprint.hiwebSocket && !hiprint.hiwebSocket.opened) {
321
- window.hiwebSocket.host = clientUrl;
322
- hiprint.hiwebSocket.start();
323
- if (clinetTip) {
324
- // 添加连接状态检查和提示逻辑
325
- setTimeout(() => {
326
- if (!hiprint.hiwebSocket.opened) {
327
- ElMessageBox.alert(
328
- `连接【${clientUrl}】失败!<br>请确保已<a href="${clientApp}" target="_blank">下载</a>并<a href="hiprint://" target="_blank">运行</a>打印客户端!`,
329
- '客户端未连接',
330
- {
331
- dangerouslyUseHTMLString: true,
332
- confirmButtonText: '确定'
333
- }
334
- );
335
- }
336
- }, 3000);
337
- }
338
- }
339
- };
340
-
341
- if (document.readyState === 'loading') {
342
- document.addEventListener('DOMContentLoaded', initializeClientPrint);
343
- } else {
344
- initializeClientPrint();
345
- }
346
- }
347
351
 
348
352
  if (router) {
349
353
  router.addRoute('Index', {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yh-hiprint",
3
- "version": "2.6.5",
3
+ "version": "2.6.6",
4
4
  "description": "Hiprint for Vue3 by NoahLiu in ForceCon in Hunan Changesha",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -10,21 +10,11 @@
10
10
  "pub:npm": "npm publish --registry https://registry.npmjs.org/ --no-git-checks"
11
11
  },
12
12
  "dependencies": {
13
- "@babel/core": "7.28.4",
14
- "@babel/generator": "7.28.3",
15
- "@babel/parser": "7.28.4",
16
- "@babel/plugin-transform-arrow-functions": "^7.27.1",
17
- "@babel/plugin-transform-block-scoping": "^7.28.4",
18
- "@babel/plugin-transform-computed-properties": "^7.27.1",
19
- "@babel/plugin-transform-shorthand-properties": "^7.27.1",
20
- "@babel/plugin-transform-template-literals": "^7.27.1",
21
- "@babel/traverse": "7.28.4",
22
13
  "@nuintun/qrcode": "^5.0.1",
23
14
  "canvg": "4.0.1",
24
15
  "html2canvas": "1.4.1",
25
16
  "jspdf": "2.5.1",
26
- "nzh": "1.0.9",
27
- "socket.io-client": "^4.8.1"
17
+ "nzh": "1.0.9"
28
18
  },
29
19
  "peerDependencies": {
30
20
  "vue": "^3.0.0"