From ad63f9a886cfd888a33ede9105996cd8a8d077d1 Mon Sep 17 00:00:00 2001 From: liuwf Date: Thu, 7 Nov 2024 16:46:34 +0800 Subject: [PATCH] =?UTF-8?q?add-=E6=B7=BB=E5=8A=A0=E5=BA=93=E4=BD=8D?= =?UTF-8?q?=E7=A0=81=E6=89=93=E5=8D=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PrintBarCode/Business/PrintBusiness.cs | 46 ++++++++++++++++++-------- PrintBarCode/Helper/JsonParser.cs | 4 +++ PrintBarCode/MessageClient.cs | 15 ++++++--- PrintBarCode/Model/SmallCodeInfo.cs | 6 ++-- 4 files changed, 50 insertions(+), 21 deletions(-) diff --git a/PrintBarCode/Business/PrintBusiness.cs b/PrintBarCode/Business/PrintBusiness.cs index 5a9a521..8c40408 100644 --- a/PrintBarCode/Business/PrintBusiness.cs +++ b/PrintBarCode/Business/PrintBusiness.cs @@ -2,6 +2,7 @@ using PrintBarCode.Model; using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -134,29 +135,46 @@ namespace PrintBarCode.Business /// public void PrintSmallBarCodeInfo(SmallCodeInfo smallCodeInfo) { - // 拼接成目标文件的完整路径 - string path = Path.Combine(basePath, "templates", "smallTemplate.prn"); - - string[] args = new string[] + try { + // 拼接成目标文件的完整路径 + string path = Path.Combine(basePath, "templates", "smallTemplate.prn"); + + string[] args = new string[] + { smallCodeInfo.QrCode, - smallCodeInfo.Text, - }; + smallCodeInfo.Text, + }; + + // 将属性值用 "~" 连接起来,不带前缀 + string TempStr = $"{smallCodeInfo.QrCode}~{smallCodeInfo.Text}"; + + string ZplStr = PrintH.GetPrintString(path, TempStr);//替换后字符串 + + bool pringFlag = zplp.SendStringToPrinter(debugConfig.PrintName, ZplStr); + if (pringFlag == false) + { + Console.WriteLine("打印错误,检查后请重新操作!"); + } + }catch (Exception ex) + { + Console.WriteLine("打印错误,检查后请重新操作!"); + } + } + - // 将属性值用 "~" 连接起来,不带前缀 - string TempStr = $"{smallCodeInfo.QrCode}~{smallCodeInfo.Text}"; - string ZplStr = PrintH.GetPrintString(path, TempStr);//替换后字符串 + /// + /// 打印空白页 + /// + public void PrintBlankPage() + { + string ZplStr = "^XA^MMT^FS ^LH0,0^FO50,50^ADN,36,20^FDTest Page^FS^FO50,350^GB700,10,10^FS^XZ";//空白页 bool pringFlag = zplp.SendStringToPrinter(debugConfig.PrintName, ZplStr); - if (pringFlag == false) - { - Console.WriteLine("打印错误,检查后请重新操作!"); - } } - } } diff --git a/PrintBarCode/Helper/JsonParser.cs b/PrintBarCode/Helper/JsonParser.cs index f4ede58..2d2b4de 100644 --- a/PrintBarCode/Helper/JsonParser.cs +++ b/PrintBarCode/Helper/JsonParser.cs @@ -23,22 +23,26 @@ namespace PrintBarCode.Helper { var rawBarCodeInfoList = JsonSerializer.Deserialize>(output.PrintContent); + //rawBarCodeInfoList.Add(new RawBarCodeInfo()); return rawBarCodeInfoList.Cast().ToList(); } else if (output.Template == "product") { var productBarCodeInfoList = JsonSerializer.Deserialize>(output.PrintContent); + // productBarCodeInfoList.Add(new ProductBarCodeInfo()); return productBarCodeInfoList.Cast().ToList(); } else if(output.Template == "bind") { var bindBarCodeInfoList = JsonSerializer.Deserialize>(output.PrintContent); + //bindBarCodeInfoList.Add(new BindBarCodeInfo()); return bindBarCodeInfoList.Cast().ToList(); } else if(output.Template == "small") { var smallCodeInfoList = JsonSerializer.Deserialize>(output.PrintContent); + // smallCodeInfoList.Add(new SmallCodeInfo()); return smallCodeInfoList.Cast().ToList(); } else diff --git a/PrintBarCode/MessageClient.cs b/PrintBarCode/MessageClient.cs index 1da5dde..fe59cae 100644 --- a/PrintBarCode/MessageClient.cs +++ b/PrintBarCode/MessageClient.cs @@ -80,6 +80,7 @@ namespace PrintBarCode private void Listening() { + int IsPrintBlank = 0; Task.Run(() => { Thread.Sleep(1000 * 2); @@ -89,9 +90,10 @@ namespace PrintBarCode { string channelWork = $"print_{debugConfig.IP}"; string jsonString = _redisHandler.ConsumeMessageFromWorker(channelWork); - // string jsonString = "{\"template\":\"product\",\"printContent\":\"[{\\\"saleOrderCode\\\":\\\"\\\",\\\"materialName\\\":\\\"多功能自动剥线钳\\\",\\\"qrcode\\\":\\\"20241019112921CP001\\\",\\\"materialSpec\\\":\\\"\\\",\\\"qty\\\":\\\"1\\\",\\\"materialCode\\\":\\\"MES.MC.A00861\\\",\\\"barcode\\\":\\\"20241019112921CP001\\\",\\\"productPlanCode\\\":\\\"20241019111402JL012\\\"}]\"}"; + // string jsonString = "{\"template\":\"small\",\"printContent\":\"[{\\\"text\\\":\\\"李华\\\",\\\"qrcode\\\":\\\"12001\\\"}]\"}"; if (!string.IsNullOrEmpty(jsonString)) { + IsPrintBlank = 1; var result = JsonParser.ParseJson(jsonString); if (result != null) { @@ -101,7 +103,6 @@ namespace PrintBarCode { addLogEvent?.Invoke(rawBarCodeInfo.ToJson()); printBusiness.PrintRawBarCodeInfo(rawBarCodeInfo); - } else if (item is ProductBarCodeInfo productBarCodeInfo) { @@ -119,9 +120,15 @@ namespace PrintBarCode Thread.Sleep(1000); } } - } - + else + { + if(IsPrintBlank == 1) + { + printBusiness.PrintBlankPage(); + IsPrintBlank = 0; + } + } Thread.Sleep(1000 * 1); } catch (Exception ex) diff --git a/PrintBarCode/Model/SmallCodeInfo.cs b/PrintBarCode/Model/SmallCodeInfo.cs index b482ca7..f59376b 100644 --- a/PrintBarCode/Model/SmallCodeInfo.cs +++ b/PrintBarCode/Model/SmallCodeInfo.cs @@ -11,10 +11,10 @@ namespace PrintBarCode.Model { [JsonPropertyName("text")] - public string Text { get; set; } - + public string Text { get; set; } = ""; + [JsonPropertyName("qrcode")] - public string QrCode { get; set; } + public string QrCode { get; set; } = ""; }