针对于条形码生成进行修改

This commit is contained in:
dxc
2026-02-02 16:43:35 +08:00
parent cf6a4a8957
commit 11a4e5f48a
4 changed files with 364 additions and 134 deletions

View File

@ -88,5 +88,5 @@ class StockBuy(db.Model):
# [新增] 返回全局打印ID及其格式化字符串
'global_print_id': self.global_print_id,
'global_print_id_str': f"{self.global_print_id:08d}" if self.global_print_id else ""
'global_print_id_str': f"{self.global_print_id:010d}" if self.global_print_id else ""
}

View File

@ -79,7 +79,7 @@ class BuyInboundService:
# ------------------------------------------------------------------
# 2. 自动生成 SKU (格式: 00000001)
# ------------------------------------------------------------------
generated_sku = f"{next_global_id:08d}"
generated_sku = str(next_global_id).zfill(10)
# ------------------------------------------------------------------
# 3. 条码逻辑处理 (核心修改)

View File

@ -26,12 +26,13 @@ class LabelPrintService:
LABEL_HEIGHT = int(LABEL_HEIGHT_MM * DOTS_PER_MM)
# 顶部留白: 10mm (120px) - 内容从这里开始
TOP_MARGIN_MM = 5
TOP_MARGIN_MM = 2
TOP_MARGIN_PX = int(TOP_MARGIN_MM * DOTS_PER_MM)
@staticmethod
def _get_font(size):
"""获取字体"""
# 尝试加载中文字体,否则乱码
font_names = ["simhei.ttf", "msyh.ttf", "SimHei.ttf", "arial.ttf"]
base_dirs = [os.getcwd(), os.path.dirname(__file__)]
@ -48,6 +49,10 @@ class LabelPrintService:
生成真实的条形码图片
"""
try:
# 确保内容字符串有效
if not content:
content = "0000000000"
# 使用 Code128 编码(通用性最强)
code128 = barcode.get('code128', content, writer=ImageWriter())
@ -85,31 +90,31 @@ class LabelPrintService:
sku_code = data.get('sku')
if not sku_code:
# 兜底逻辑:如果没有 SKU尝试用 serial_number 或 global_id
sku_code = data.get('serial_number') or str(data.get('global_print_id', '00000000')).zfill(8)
sku_code = data.get('serial_number') or str(data.get('global_print_id', '0000000000')).zfill(10)
# ==================== 绘制布局 ====================
# X 轴偏移 (5mm)
GLOBAL_OFFSET_X = int(5 * LabelPrintService.DOTS_PER_MM)
GLOBAL_OFFSET_X = int(2 * LabelPrintService.DOTS_PER_MM)
# Y 轴起始: 10mm 处
CURRENT_Y = LabelPrintService.TOP_MARGIN_PX
# --- A. 绘制真实条形码 ---
bc_w = int(28 * LabelPrintService.DOTS_PER_MM) # 条码宽约 28mm
bc_h = int(6 * LabelPrintService.DOTS_PER_MM) # 条码高约 6mm
# --- A. 绘制真实条形码 (内容为 SKU) ---
bc_w = int(35 * LabelPrintService.DOTS_PER_MM) # 条码宽约 28mm
bc_h = int(10 * LabelPrintService.DOTS_PER_MM) # 条码高约 6mm
# 生成并粘贴条码
bc_img = LabelPrintService._generate_barcode_image(sku_code, bc_w, bc_h)
# 粘贴位置: (X, Y)
img.paste(bc_img, (GLOBAL_OFFSET_X - 5, CURRENT_Y))
img.paste(bc_img, (GLOBAL_OFFSET_X - 10, CURRENT_Y))
# 更新 Y 坐标 (条码高度 + 间距)
CURRENT_Y += bc_h + 5
# --- B. 绘制文字 (统一加粗) ---
# 准备数据
# 准备数据 (名称、规格等)
name = str(data.get('material_name', '') or '-')
if len(name) > 8: name = name[:7] + ".."
@ -123,15 +128,43 @@ class LabelPrintService:
attr = f"{cat}/{typ}"
if len(attr) > 11: attr = attr[:10] + ".."
# 底部显示的文字SKU 编号
sku_str = f"NO.{sku_code}"
# -----------------------------------------------------------
# [修改点] 底部显示的文字:不再显示 NO.SKU而是显示 SN 或 BN
# -----------------------------------------------------------
bottom_text = ""
# 1. 优先检查前端是否传了明确的 print_no (打印值) 和 print_label (序/批)
if data.get('print_no'):
val = str(data.get('print_no'))
label_type = data.get('print_label', '')
if label_type == '':
bottom_text = f"SN: {val}"
elif label_type == '':
bottom_text = f"BN: {val}"
else:
bottom_text = f"NO: {val}"
# 2. 如果没有前端打印参数,检查数据中的序列号
elif data.get('serial_number'):
bottom_text = f"SN: {data.get('serial_number')}"
# 3. 检查数据中的批号
elif data.get('batch_number'):
bottom_text = f"BN: {data.get('batch_number')}"
# 4. 兜底 (如果什么都没有,才显示 SKU)
else:
bottom_text = f"NO: {sku_code}"
# -----------------------------------------------------------
lines = [
f"名: {name}",
f"规: {spec}",
f"库: {loc}",
f"属: {attr}",
f"{sku_str}"
f"{bottom_text}" # 这里放置计算好的 SN/BN 文字
]
line_height = 36 # 行高 (字号30 + 6间距)