只打印箱标的控制器

This commit is contained in:
qianhao.xu 2025-04-11 15:06:11 +08:00
parent aea1664344
commit 14ebf1f1bf
4 changed files with 120 additions and 3 deletions

View File

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="label_path" value="D:\label\gxassembly_production_label.btw"/>
<add key="product_label_path" value="D:\label\gxassembly_production_label.btw"/>
<add key="box_label_path" value="D:\label\gxassembly_production_label.btw"/>
</appSettings>
</configuration>

View File

@ -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
{
/// <summary>
/// 只打印箱标的控制器
/// </summary>
public class OnlyPrintBoxLabelController : WebApiController
{
private readonly Action<string> _logAction;
// 准备打印数据
string templatePath = null;
public OnlyPrintBoxLabelController(Action<string> 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<ProWorkorder>()
.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<string, string>
{
{ "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);
}
}
}
}

View File

@ -31,7 +31,7 @@ namespace linesider_screen_bankend.Controller
public OnlyPrintProductLabelController(Action<string> logAction)
{
_logAction = logAction;
templatePath = ConfigurationManager.AppSettings["label_path"];
templatePath = ConfigurationManager.AppSettings["product_label_path"];
}
[Route(HttpVerbs.Get, "/hello")]

View File

@ -98,7 +98,9 @@ namespace linesider_screen_bankend.Views
)
.WithWebApi("/print_api", m => m
.WithController<PrintCommunicationController>(() => new PrintCommunicationController(AddLog))
.WithController<OnlyPrintProductLabelController>(() => new OnlyPrintProductLabelController(AddLog)))
.WithController<OnlyPrintProductLabelController>(() => new OnlyPrintProductLabelController(AddLog))
.WithController<OnlyPrintBoxLabelController>(() => new OnlyPrintBoxLabelController(AddLog))
)
.WithStaticFolder("/", Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot"), true);
// 添加请求日志中间件