From 14ebf1f1bf9157a0778398a0290842fe60be35e0 Mon Sep 17 00:00:00 2001 From: "qianhao.xu" Date: Fri, 11 Apr 2025 15:06:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=AA=E6=89=93=E5=8D=B0=E7=AE=B1=E6=A0=87?= =?UTF-8?q?=E7=9A=84=E6=8E=A7=E5=88=B6=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../linesider_screen_bankend/App.config | 3 +- .../Controller/OnlyPrintBoxLabelController.cs | 114 ++++++++++++++++++ .../OnlyPrintProductLabelController.cs | 2 +- .../Views/MainWindow.xaml.cs | 4 +- 4 files changed, 120 insertions(+), 3 deletions(-) create mode 100644 linesider_screen_bankend/linesider_screen_bankend/Controller/OnlyPrintBoxLabelController.cs diff --git a/linesider_screen_bankend/linesider_screen_bankend/App.config b/linesider_screen_bankend/linesider_screen_bankend/App.config index 0c7d30e..0a3c1b0 100644 --- a/linesider_screen_bankend/linesider_screen_bankend/App.config +++ b/linesider_screen_bankend/linesider_screen_bankend/App.config @@ -1,6 +1,7 @@  - + + \ No newline at end of file diff --git a/linesider_screen_bankend/linesider_screen_bankend/Controller/OnlyPrintBoxLabelController.cs b/linesider_screen_bankend/linesider_screen_bankend/Controller/OnlyPrintBoxLabelController.cs new file mode 100644 index 0000000..955b557 --- /dev/null +++ b/linesider_screen_bankend/linesider_screen_bankend/Controller/OnlyPrintBoxLabelController.cs @@ -0,0 +1,114 @@ +using EmbedIO.Routing; +using EmbedIO.WebApi; +using EmbedIO; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using DOAN.Model.MES.product; +using linesider_screen_bankend.Core.Tools; +using linesider_screen_bankend.Services.SQLHepler; +using Google.Protobuf.WellKnownTypes; +using System.Configuration; +using linesider_screen_bankend.Core.Tools.HttpHepler; +using System.Net.Http; +using static Mysqlx.Crud.Order.Types; +using DOAN.Model.Mobile; + +namespace linesider_screen_bankend.Controller +{ + /// + /// 只打印箱标的控制器 + /// + + public class OnlyPrintBoxLabelController : WebApiController + { + private readonly Action _logAction; + // 准备打印数据 + string templatePath = null; + + public OnlyPrintBoxLabelController(Action logAction) + { + _logAction = logAction; + templatePath = ConfigurationManager.AppSettings["box_label_path"]; + } + + [Route(HttpVerbs.Get, "/hello")] + public string GetHello() + { + _logAction?.Invoke($"GET /api/hello from {HttpContext.RemoteEndPoint}"); + return "Hello from WPF!"; + } + + [Route(HttpVerbs.Get, "/freenum_box_label")] + public async Task FreeNumLabel() + { + _logAction?.Invoke($"GET /api/freenum_box_label from {HttpContext.RemoteEndPoint}"); + // 获取所有查询参数 + var queryParams = HttpContext.Request.QueryString; + // 获取特定参数 + string workorder = queryParams["workorder"]; + int LabelNum = int.Parse(queryParams["label_num"]); + // 获取特定参数 + string model = queryParams["model"]; + string direction = queryParams["direction"]; + + try + { + ProWorkorder workorderInfo = SqlSugarHelper.Db.Queryable() + .Where(x => x.Workorder == workorder) + .First(); + + if (workorderInfo == null) + { + // 返回成功响应 + await this.HttpContext.SendApiFailAsync(500, "工单不在数据库"); + return; + } + // 使用 using 语句确保资源正确释放 + using (var bartender = new BartenderPrintHelper()) + { + + var variables = new Dictionary + { + { "partnumber", workorderInfo.Specification }, + { "model", model }, + { "direction", direction }, + { "productdate", DateTime.Now.ToString("yyyy/MM/dd") }, + {"workorder",workorderInfo.Workorder+"|"+workorderInfo.Specification+"|"+model+"|"+direction } + }; + + // 打印标签 + bool success = bartender.PrintLabel( + templatePath: templatePath, + subStringValues: variables, + copies: LabelNum, // 打印LabelNum份 + serializedLabels: 1 // 序列化标签数为1 + ); + + if (success) + { + _logAction?.Invoke("标签打印成功"); + + } + + } + + + + // 返回成功响应 + await this.HttpContext.SendApiSuccessAsync(null); + } + catch (Exception ex) + { + // 返回错误响应 + await this.HttpContext.SendApiFailAsync(500, ex.Message); + } + + } + + + } + +} diff --git a/linesider_screen_bankend/linesider_screen_bankend/Controller/OnlyPrintProductLabelController.cs b/linesider_screen_bankend/linesider_screen_bankend/Controller/OnlyPrintProductLabelController.cs index e0e2e49..b45cd09 100644 --- a/linesider_screen_bankend/linesider_screen_bankend/Controller/OnlyPrintProductLabelController.cs +++ b/linesider_screen_bankend/linesider_screen_bankend/Controller/OnlyPrintProductLabelController.cs @@ -31,7 +31,7 @@ namespace linesider_screen_bankend.Controller public OnlyPrintProductLabelController(Action logAction) { _logAction = logAction; - templatePath = ConfigurationManager.AppSettings["label_path"]; + templatePath = ConfigurationManager.AppSettings["product_label_path"]; } [Route(HttpVerbs.Get, "/hello")] diff --git a/linesider_screen_bankend/linesider_screen_bankend/Views/MainWindow.xaml.cs b/linesider_screen_bankend/linesider_screen_bankend/Views/MainWindow.xaml.cs index 5feb40f..04356a0 100644 --- a/linesider_screen_bankend/linesider_screen_bankend/Views/MainWindow.xaml.cs +++ b/linesider_screen_bankend/linesider_screen_bankend/Views/MainWindow.xaml.cs @@ -98,7 +98,9 @@ namespace linesider_screen_bankend.Views ) .WithWebApi("/print_api", m => m .WithController(() => new PrintCommunicationController(AddLog)) - .WithController(() => new OnlyPrintProductLabelController(AddLog))) + .WithController(() => new OnlyPrintProductLabelController(AddLog)) + .WithController(() => new OnlyPrintBoxLabelController(AddLog)) + ) .WithStaticFolder("/", Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot"), true); // 添加请求日志中间件