From 860560648a27010ab8d919eff55c3b4fdb682fab Mon Sep 17 00:00:00 2001
From: zjk <852185829@qq.com>
Date: 星期五, 24 七月 2026 15:56:46 +0800
Subject: [PATCH] feat(打印): 处理打印机缺纸错误并添加节流提示

---
 utils/visitorDevice.js              |   19 +++++++++++++++++++
 pages/appointment-scan/n-index.nvue |    9 +++++++++
 2 files changed, 28 insertions(+), 0 deletions(-)

diff --git a/pages/appointment-scan/n-index.nvue b/pages/appointment-scan/n-index.nvue
index 2b87f6f..f2df429 100644
--- a/pages/appointment-scan/n-index.nvue
+++ b/pages/appointment-scan/n-index.nvue
@@ -2475,6 +2475,10 @@
   }
 };
 
+const handlePrinterOutOfPaper = () => {
+  openNoticeDialog('提示', '打印机缺纸,请联系管理员');
+};
+
 // -------------------- 区域数据加载 --------------------
 const loadVisitAreas = async () => {
   try {
@@ -2511,6 +2515,7 @@
   // 必须在页面挂载的第一时间注册扫码和读卡的事件监听,
   // 绝对不能放在 await 数据加载之后,否则会导致进入页面前几十秒内刷卡/扫码无反应!
   registerAppointmentScanHandler();
+  uni.$on('Hardware-PrintOutOfPaper', handlePrinterOutOfPaper);
   syncReadCardByStep(currentStepName.value);
 
   // 首页扫码成功后会直接跳到预约登记的身份证步骤,这里需要保留并恢复之前已缓存的二维码数据,
@@ -2554,6 +2559,7 @@
 onUnmounted(() => {
   clearInactivityTimer();
   unregisterAppointmentScanHandler();
+  uni.$off('Hardware-PrintOutOfPaper', handlePrinterOutOfPaper);
   dismissNameInput();
   stopScanLineAnimation();
   stopNoticeDialogCountdown();
@@ -3260,6 +3266,9 @@
     goNext();
   } catch (error) {
     console.error('打印失败:', error);
+    if (error?.code === 2) {
+      return;
+    }
     openNoticeDialog('打印失败', '请检查打印机状态或纸张是否充足');
   }
 };
diff --git a/utils/visitorDevice.js b/utils/visitorDevice.js
index c3309af..74e9bee 100644
--- a/utils/visitorDevice.js
+++ b/utils/visitorDevice.js
@@ -14,6 +14,21 @@
 let initResult = null;
 const PLUGIN_DEBUG_ENABLED = true;
 let debugModalQueue = Promise.resolve();
+let lastPrinterOutOfPaperNoticeAt = 0;
+
+const emitPrinterOutOfPaperNoticeOnce = () => {
+  const now = Date.now();
+  // 打印机缺纸时,原生插件可能在短时间内连续回调很多次 code=2,这里做节流避免重复弹窗轰炸。
+  if (now - lastPrinterOutOfPaperNoticeAt < 10000) {
+    return;
+  }
+
+  lastPrinterOutOfPaperNoticeAt = now;
+  uni.$emit('Hardware-PrintOutOfPaper', {
+    code: 2,
+    message: '打印机缺纸,请联系管理员',
+  });
+};
 
 const getPluginMethod = (methodNames = []) => {
   if (!HardwarePlugin) {
@@ -540,6 +555,10 @@
       if (methodName) {
         HardwarePlugin[methodName]({ options: printList }, (res) => {
 			// showPluginDebugModal('打印凭证回调', res);
+          if (res && res.code === 2) {
+            emitPrinterOutOfPaperNoticeOnce();
+          }
+
           if (res && res.code === 0) {
             resolve(res);
           } else {

--
Gitblit v1.7.1