产品标签打印

This commit is contained in:
qianhao.xu 2025-04-08 16:23:05 +08:00
parent f6e78109d3
commit a54269ef87
4 changed files with 258 additions and 4 deletions

View File

@ -9,7 +9,7 @@ namespace linesider_screen_bankend.Services.SQLHepler
{
var config = new ConnectionConfig()
{
ConnectionString = "Server=139.224.232.211;Database=gxassembly;Uid=root;Pwd=123456;Port=3308", // 替换为你的MySQL连接字符串
ConnectionString = "Data Source=139.224.232.211;User ID=root;Password=doantech123;Initial Catalog=GXAssembly;Port=3308", // 替换为你的MySQL连接字符串
DbType = DbType.MySql, // 数据库类型设置为MySQL
IsAutoCloseConnection = true, // 自动释放
InitKeyType = InitKeyType.Attribute // 从实体特性读取主键信息

View File

@ -0,0 +1,249 @@
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;
namespace linesider_screen_bankend.Controller
{
/// <summary>
/// 只打印产品标签
/// </summary>
public class OnlyPrintProductLabelController : WebApiController
{
private readonly Action<string> _logAction;
public OnlyPrintProductLabelController(Action<string> logAction)
{
_logAction = logAction;
}
[Route(HttpVerbs.Get, "/hello")]
public string GetHello()
{
_logAction?.Invoke($"GET /api/hello from {HttpContext.RemoteEndPoint}");
return "Hello from WPF!";
}
//TODO 1.出首件校验标签
[Route(HttpVerbs.Get, "/first_inspection_label")]
public async Task FirstInspectionLabel()
{
_logAction?.Invoke($"GET /api/first_inspection_label from {HttpContext.RemoteEndPoint}");
// 获取所有查询参数
var queryParams = HttpContext.Request.QueryString;
// 获取特定参数
string workorder = queryParams["workorder"];
string model = queryParams["model"];
string direction = queryParams["direction"];
try
{
ProWorkorder workorderInfo = SqlSugarHelper.Db.Queryable<ProWorkorder>()
.Where(x => x.Workorder == workorder)
.First();
if (workorderInfo != null)
{
// 使用 using 语句确保资源正确释放
using (var bartender = new BartenderPrintHelper())
{
// 准备打印数据
string templatePath = ConfigurationManager.AppSettings["label_path"];
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: 1, // 打印2份
serializedLabels: 1 // 序列化标签数为1
);
if (success)
{
_logAction?.Invoke("标签打印成功");
}
}
// 模拟从数据库获取用户
var user = new { Id = 123, Name = "张三", Age = 25 };
// 返回成功响应
await this.HttpContext.SendApiSuccessAsync(null);
}
}
catch (Exception ex)
{
// 返回错误响应
await this.HttpContext.SendApiFailAsync(500, ex.Message);
}
}
//TODO 2.校验是否首件标签是否合格
[Route(HttpVerbs.Get, "/first_inspection_label_isQualified")]
public async Task FirstInspectionLabelIsQualified()
{
_logAction?.Invoke($"GET /api/first_inspection_label_isQualified from {HttpContext.RemoteEndPoint}");
// 获取所有查询参数
var queryParams = HttpContext.Request.QueryString;
// 获取特定参数
string workorder = queryParams["workorder"];
try
{
await this.HttpContext.SendApiSuccessAsync(null);
}
catch (Exception ex)
{
// 返回错误响应
await this.HttpContext.SendApiFailAsync(500, ex.Message);
}
}
//TODO 3.出自由标签
[Route(HttpVerbs.Get, "/freenum_label")]
public async Task FreeNumLabel()
{
_logAction?.Invoke($"GET /api/freenum_label from {HttpContext.RemoteEndPoint}");
// 获取所有查询参数
var queryParams = HttpContext.Request.QueryString;
// 获取特定参数
string workorder = queryParams["workorder"];
int LabelNum = int.Parse(queryParams["label_num"]);
try
{
ProWorkorder workorderInfo = SqlSugarHelper.Db.Queryable<ProWorkorder>()
.Where(x => x.Workorder == workorder)
.First();
// 使用 using 语句确保资源正确释放
using (var bartender = new BartenderPrintHelper())
{
// 准备打印数据
string templatePath = ConfigurationManager.AppSettings["label_path"];
var variables = new Dictionary<string, string>
{
{ "ProductName", "高级笔记本电脑" },
{ "SerialNumber", "SN-20230401-001" },
{ "ManufactureDate", DateTime.Now.ToString("yyyy-MM-dd") }
};
// 打印标签
bool success = bartender.PrintLabel(
templatePath: templatePath,
subStringValues: variables,
copies: 2, // 打印2份
serializedLabels: 1 // 序列化标签数为1
);
if (success)
{
_logAction?.Invoke("标签打印成功");
}
}
// 模拟从数据库获取用户
var user = new { Id = 123, Name = "张三", Age = 25 };
// 返回成功响应
await this.HttpContext.SendApiSuccessAsync(null);
}
catch (Exception ex)
{
// 返回错误响应
await this.HttpContext.SendApiFailAsync(500, ex.Message);
}
}
//TODO 4.补打标签
[Route(HttpVerbs.Get, "/supplement_label")]
public async Task SupplementLabel()
{
_logAction?.Invoke($"GET /api/supplement_label from {HttpContext.RemoteEndPoint}");
// 获取所有查询参数
var queryParams = HttpContext.Request.QueryString;
// 获取特定参数
string workorder = queryParams["workorder"];
int LabelNum = int.Parse(queryParams["label_num"]);
try
{
ProWorkorder workorderInfo = SqlSugarHelper.Db.Queryable<ProWorkorder>()
.Where(x => x.Workorder == workorder)
.First();
// 使用 using 语句确保资源正确释放
using (var bartender = new BartenderPrintHelper())
{
// 准备打印数据
string templatePath = ConfigurationManager.AppSettings["label_path"];
var variables = new Dictionary<string, string>
{
{ "ProductName", "高级笔记本电脑" },
{ "SerialNumber", "SN-20230401-001" },
{ "ManufactureDate", DateTime.Now.ToString("yyyy-MM-dd") }
};
// 打印标签
bool success = bartender.PrintLabel(
templatePath: templatePath,
subStringValues: variables,
copies: 2, // 打印2份
serializedLabels: 1 // 序列化标签数为1
);
if (success)
{
_logAction?.Invoke("标签打印成功");
}
}
// 模拟从数据库获取用户
var user = new { Id = 123, Name = "张三", Age = 25 };
// 返回成功响应
await this.HttpContext.SendApiSuccessAsync(null);
}
catch (Exception ex)
{
// 返回错误响应
await this.HttpContext.SendApiFailAsync(500, ex.Message);
}
}
}
}

View File

@ -91,9 +91,10 @@ namespace linesider_screen_bankend.Views
var server = new WebServer(o => o
.WithUrlPrefix(url)
.WithMode(HttpListenerMode.EmbedIO))
.WithWebApi("/print_api", m => m
.WithController<PrintCommunicationController>(() => new PrintCommunicationController(AddLog)))
.WithStaticFolder("/", Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot"), true);
.WithWebApi("/print_api", m => m
.WithController<PrintCommunicationController>(() => new PrintCommunicationController(AddLog))
.WithController<OnlyPrintProductLabelController>(() => new OnlyPrintProductLabelController(AddLog)))
.WithStaticFolder("/", Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot"), true);
// 添加请求日志中间件
server.OnHttpException += async (context, exception) =>

View File

@ -0,0 +1,4 @@
{
"label_path": "C:\\Labels\\MyLabel.btw" //
}