378 lines
15 KiB
PHP
378 lines
15 KiB
PHP
<?php
|
|
|
|
namespace app\ZengJieCode\controller\admin;
|
|
|
|
use think\Db;
|
|
use think\Cache;
|
|
use Picqer\Barcode\BarcodeGeneratorPNG;
|
|
use Endroid\QrCode\QrCode;
|
|
use Endroid\QrCode\Writer\PngWriter;
|
|
|
|
class Printaction extends Base
|
|
{
|
|
protected $default_head_pic = 'http://tc.pcxbc.com/tsf/head_pic.png';
|
|
protected $max_box_num = 10;
|
|
protected $kitchenscale_db_msg = [
|
|
'cookbook' => 'app_user_cookbook', // 菜谱表
|
|
'foodlist3' => 'app_z_national_standard_food_type_3', // 食材列表3
|
|
'user' => 'app_user_data', // banner
|
|
];
|
|
|
|
// 自定义字体路径(相对于项目根目录)
|
|
protected $fontPath = 'public/tsf/jc.ttf'; // 请替换为你的字体文件路径
|
|
|
|
// 显示条形码生成页面
|
|
public function print_device_data_index()
|
|
{
|
|
|
|
$data = input();
|
|
if(array_key_exists('type',$data)){
|
|
return $this->print_device_data_index_action('down');
|
|
}else{
|
|
$result = $this->print_device_data_index_action('page');
|
|
$this->assign([
|
|
'data' => $result
|
|
]);
|
|
return $this->fetch();
|
|
}
|
|
|
|
}
|
|
|
|
public function print_device_data_index_action($data){
|
|
$zengjie = Db::connect('zengjie_db');
|
|
$pc = $zengjie->table('box_code_all')->order('id desc')->find();
|
|
// dump($pc);
|
|
$sql = "SELECT
|
|
a.mac_code,
|
|
a.sn_code,
|
|
b.box_code,
|
|
b.is_del,
|
|
FORMAT(b.box_num, '000') AS box_num
|
|
FROM
|
|
sn_code_all AS a
|
|
LEFT JOIN
|
|
box_code_all AS b ON a.batch_id = b.id
|
|
WHERE
|
|
a.batch_id IS NOT NULL
|
|
AND b.box_code = ?
|
|
ORDER BY
|
|
a.batch_id";
|
|
|
|
$result = $zengjie->query($sql, [$pc['box_code']]);
|
|
if($data == 'page'){
|
|
return $result;
|
|
}else{
|
|
$result2 = [
|
|
['Mac码','Sn码','箱号','是否注销']
|
|
];
|
|
for ($i=0; $i < count($result); $i++) {
|
|
$result2[] = [$result[$i]['mac_code'],$result[$i]['sn_code'],$result[$i]['box_code'].'-'.$result[$i]['box_num'],$result[$i]['is_del']==0?'否':'是'];
|
|
}
|
|
$excel = new \app\download\controller\Excel();
|
|
$excel->export($result2, $pc['box_code']);
|
|
}
|
|
}
|
|
|
|
|
|
// 显示外箱码生成页面
|
|
public function print_outside_box_index()
|
|
{
|
|
return $this->fetch();
|
|
}
|
|
|
|
public function print_device_barcode()
|
|
{
|
|
$zengjie = Db::connect('zengjie_db');
|
|
$img_content = $zengjie->table('sn_code_all')->where(['is_print' => 0])->field('sn_code')->find();
|
|
|
|
if (empty($img_content)) {
|
|
return $this->msg(10002, '没有可用的条形码');
|
|
} else {
|
|
$img_content = $img_content['sn_code'];
|
|
}
|
|
// dump($img_content);
|
|
// die;
|
|
return $this->barcode_action($img_content,1);
|
|
|
|
}
|
|
public function print_scan_barcode()
|
|
{
|
|
$zengjie = Db::connect('zengjie_db');
|
|
$img_content = $zengjie->table('sn_code_all')->where(['is_print' => 3])->field('sn_code')->find();
|
|
|
|
if (empty($img_content)) {
|
|
return $this->msg(10002, '没有可用的条形码');
|
|
} else {
|
|
$img_content = $img_content['sn_code'];
|
|
}
|
|
return $this->barcode_action($img_content,4);
|
|
|
|
}
|
|
|
|
public function barcode_action($img_content,$typr_num_data){
|
|
$zengjie = Db::connect('zengjie_db');
|
|
$generator = new BarcodeGeneratorPNG();
|
|
|
|
try {
|
|
/*******************************
|
|
* 可调整参数区域 - 开始
|
|
*******************************/
|
|
|
|
// 1. 字体文件路径
|
|
$fontFile = ROOT_PATH . $this->fontPath;
|
|
|
|
// 2. 图像尺寸参数
|
|
$imageWidth = 709; // 图像宽度
|
|
$imageHeight = 236; // 图像高度
|
|
|
|
// 3. 条形码参数
|
|
$barcodeScaleFactor = 0.9; // 条形码缩放比例 (0-1)
|
|
$barcodeHeightScale = 1.5; // 条形码高度比例
|
|
$barcodeYOffset = 10; // 条形码Y轴偏移量
|
|
|
|
// 4. 文本参数
|
|
$textFontSize = 35; // 文本字体大小
|
|
$textYOffset = 35; // 文本Y轴偏移量
|
|
|
|
// 5. 布局参数
|
|
$topBoxRatio = 0.65; // 上区域高度比例
|
|
$bottomBoxRatio = 0.25; // 下区域高度比例
|
|
$verticalMargin = 0.05; // 垂直边距比例
|
|
|
|
/*******************************
|
|
* 可调整参数区域 - 结束
|
|
*******************************/
|
|
|
|
// 生成条形码
|
|
$barcodeData = $generator->getBarcode($img_content, BarcodeGeneratorPNG::TYPE_CODE_128);
|
|
$barcodeImage = imagecreatefromstring($barcodeData);
|
|
|
|
// 创建画布
|
|
$image = imagecreatetruecolor($imageWidth, $imageHeight);
|
|
$white = imagecolorallocate($image, 255, 255, 255);
|
|
$black = imagecolorallocate($image, 0, 0, 0);
|
|
imagefill($image, 0, 0, $white);
|
|
|
|
// 计算布局区域
|
|
$verticalSpace = $imageHeight * (1 - $verticalMargin * 2);
|
|
$topBoxHeight = $verticalSpace * $topBoxRatio;
|
|
$bottomBoxHeight = $verticalSpace * $bottomBoxRatio;
|
|
$startY = $imageHeight * $verticalMargin;
|
|
|
|
// 处理条形码
|
|
$barcodeWidth = imagesx($barcodeImage);
|
|
$barcodeHeight = imagesy($barcodeImage);
|
|
|
|
$targetBarcodeWidth = $imageWidth * $barcodeScaleFactor;
|
|
$scale = $targetBarcodeWidth / $barcodeWidth;
|
|
$newBarcodeWidth = $targetBarcodeWidth;
|
|
$newBarcodeHeight = $barcodeHeight * $scale * $barcodeHeightScale;
|
|
|
|
$resizedBarcode = imagecreatetruecolor($newBarcodeWidth, $newBarcodeHeight);
|
|
imagefill($resizedBarcode, 0, 0, $white);
|
|
imagecopyresampled($resizedBarcode, $barcodeImage,
|
|
0, 0, 0, 0,
|
|
$newBarcodeWidth, $newBarcodeHeight,
|
|
$barcodeWidth, $barcodeHeight);
|
|
|
|
// 放置条形码(上区域居中)
|
|
$barcodeX = ($imageWidth - $newBarcodeWidth) / 2;
|
|
$barcodeY = $startY + ($topBoxHeight - $newBarcodeHeight) / 2 + $barcodeYOffset;
|
|
imagecopy($image, $resizedBarcode, $barcodeX, $barcodeY, 0, 0, $newBarcodeWidth, $newBarcodeHeight);
|
|
|
|
// 放置文本(下区域居中)
|
|
$textBox = imagettfbbox($textFontSize, 0, $fontFile, $img_content);
|
|
$textWidth = $textBox[2] - $textBox[0];
|
|
$textHeight = $textBox[1] - $textBox[7];
|
|
|
|
$textX = ($imageWidth - $textWidth) / 2;
|
|
$textY = $startY + $topBoxHeight + ($bottomBoxHeight - $textHeight) / 2 + $textYOffset;
|
|
imagettftext($image, $textFontSize, 0, $textX, $textY, $black, $fontFile, $img_content);
|
|
|
|
// 输出图像
|
|
ob_start();
|
|
imagepng($image);
|
|
$imageData = ob_get_clean();
|
|
|
|
// 标记为已打印
|
|
$result1 = $zengjie->table('sn_code_all')
|
|
->where(['sn_code' => $img_content])
|
|
->update(['is_print' => $typr_num_data]);
|
|
|
|
if($result1){
|
|
return $this->msg([
|
|
'image' => 'data:image/png;base64,' . base64_encode($imageData)
|
|
]);
|
|
}else{
|
|
return $this->msg(10002,'保存条码失败');
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
return $this->msg(10003, "生成条形码失败: " . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function print_combined_code()
|
|
{
|
|
$zengjie = Db::connect('zengjie_db');
|
|
// 获取可以打印的大箱条形码信息
|
|
$box_data = $zengjie->table('box_code_all')->where(['is_print' => 0])->field('id,box_code,box_num')->find();
|
|
|
|
$box_barcode_data = '';
|
|
if (empty($box_data)) {
|
|
return $this->msg(10002, '没有可用的编码');
|
|
} else {
|
|
$box_barcode_data = $box_data['box_code'].'-'.sprintf("%03d", (int)$box_data['box_num']);
|
|
}
|
|
// 获取大箱信息配套的sn二维码码信息
|
|
$sn_data = $zengjie->table('sn_code_all')->where(['batch_id' => $box_data['id']])->field('sn_code')->select();
|
|
$sn_qrcode_data = [];
|
|
foreach ($sn_data as $item) {
|
|
// 多重清理确保数据干净
|
|
$sn_code = $item['sn_code'];
|
|
|
|
// 1. 移除所有空字符和不可见字符
|
|
$sn_code = preg_replace('/[\x00-\x1F\x7F]/', '', $sn_code);
|
|
|
|
// 2. 去除首尾空白
|
|
$sn_code = trim($sn_code);
|
|
|
|
// 3. 确保只保留数字(根据业务需求)
|
|
// $sn_code = preg_replace('/[^\d]/', '', $sn_code);
|
|
|
|
$sn_qrcode_data[] = $sn_code;
|
|
}
|
|
$sn_qrcode_data = implode(',',$sn_qrcode_data);
|
|
|
|
// $sn_qrcode_data = $box_data['content_str'];
|
|
try {
|
|
/*******************************
|
|
* 可调整参数区域 - 开始
|
|
*******************************/
|
|
|
|
// 1. 文字相关参数
|
|
$fontFile = ROOT_PATH . $this->fontPath; // 字体文件路径
|
|
|
|
// "SN号集成码"文字参数
|
|
$snTitleText = "SN号集成码";
|
|
$snTitleFontSize = 31; // 字体大小
|
|
$snTitleYPosition = 60; // Y轴位置
|
|
|
|
// "外箱条码"文字参数
|
|
$boxTitleText = "外箱条码";
|
|
$boxTitleFontSize = 31; // 字体大小
|
|
$boxTitleYPosition = 460; // Y轴位置
|
|
|
|
// 外箱条码数字参数
|
|
$barcodeTextFontSize = 25; // 字体大小
|
|
$barcodeTextYPosition = 620; // Y轴位置
|
|
|
|
// 2. 二维码参数
|
|
$qrCodeSize = 320; // 二维码尺寸
|
|
$qrCodeMargin = 2; // 二维码边距
|
|
$qrCodeYPosition = 90; // Y轴位置
|
|
|
|
// 3. 条形码参数
|
|
$barcodeScaleFactor = 0.85; // 条形码缩放比例 (0-1)
|
|
$barcodeHeightScale = 2.0; // 条形码高度比例 (调整黑色条码高度)
|
|
$barcodeYPosition = 480; // Y轴位置
|
|
|
|
// 4. 图像尺寸参数
|
|
$imageWidth = 543; // 图像宽度 (45mm @ 300dpi)
|
|
$imageHeight = 649; // 图像高度 (55mm @ 300dpi)
|
|
|
|
/*******************************
|
|
* 可调整参数区域 - 结束
|
|
*******************************/
|
|
|
|
// 生成二维码
|
|
$qrCode = new QrCode($sn_qrcode_data);
|
|
$qrCode->setSize($qrCodeSize);
|
|
$qrCode->setMargin($qrCodeMargin);
|
|
$writer = new PngWriter();
|
|
$qrCodeResult = $writer->write($qrCode);
|
|
$qrCodeData = $qrCodeResult->getString();
|
|
|
|
// 生成条形码
|
|
$generator = new BarcodeGeneratorPNG();
|
|
$barcodeData = $generator->getBarcode($box_barcode_data, BarcodeGeneratorPNG::TYPE_CODE_128);
|
|
|
|
// 创建画布
|
|
$image = imagecreatetruecolor($imageWidth, $imageHeight);
|
|
$white = imagecolorallocate($image, 255, 255, 255);
|
|
$black = imagecolorallocate($image, 0, 0, 0);
|
|
imagefill($image, 0, 0, $white);
|
|
|
|
// 添加"SN号集成码"文字
|
|
$snTitleBox = imagettfbbox($snTitleFontSize, 0, $fontFile, $snTitleText);
|
|
$snTitleWidth = $snTitleBox[2] - $snTitleBox[0];
|
|
$snTitleX = ($imageWidth - $snTitleWidth) / 2;
|
|
imagettftext($image, $snTitleFontSize, 0, $snTitleX, $snTitleYPosition, $black, $fontFile, $snTitleText);
|
|
|
|
// 添加二维码
|
|
$qrcodeImage = imagecreatefromstring($qrCodeData);
|
|
$qrcodeWidth = imagesx($qrcodeImage);
|
|
$qrcodeHeight = imagesy($qrcodeImage);
|
|
|
|
$targetQrcodeSize = $qrCodeSize;
|
|
$resizedQrcode = imagecreatetruecolor($targetQrcodeSize, $targetQrcodeSize);
|
|
imagefill($resizedQrcode, 0, 0, $white);
|
|
imagecopyresampled($resizedQrcode, $qrcodeImage,
|
|
0, 0, 0, 0,
|
|
$targetQrcodeSize, $targetQrcodeSize,
|
|
$qrcodeWidth, $qrcodeHeight);
|
|
|
|
$qrcodeX = ($imageWidth - $targetQrcodeSize) / 2;
|
|
imagecopy($image, $resizedQrcode, $qrcodeX, $qrCodeYPosition, 0, 0, $targetQrcodeSize, $targetQrcodeSize);
|
|
|
|
// 添加"外箱条码"文字
|
|
$boxTitleBox = imagettfbbox($boxTitleFontSize, 0, $fontFile, $boxTitleText);
|
|
$boxTitleWidth = $boxTitleBox[2] - $boxTitleBox[0];
|
|
$boxTitleX = ($imageWidth - $boxTitleWidth) / 2;
|
|
imagettftext($image, $boxTitleFontSize, 0, $boxTitleX, $boxTitleYPosition, $black, $fontFile, $boxTitleText);
|
|
|
|
// 添加条形码
|
|
$barcodeImage = imagecreatefromstring($barcodeData);
|
|
$barcodeWidth = imagesx($barcodeImage);
|
|
$barcodeHeight = imagesy($barcodeImage);
|
|
|
|
$targetBarcodeWidth = $imageWidth * $barcodeScaleFactor;
|
|
$scale = $targetBarcodeWidth / $barcodeWidth;
|
|
$newBarcodeWidth = $targetBarcodeWidth;
|
|
$newBarcodeHeight = $barcodeHeight * $scale * $barcodeHeightScale;
|
|
|
|
$resizedBarcode = imagecreatetruecolor($newBarcodeWidth, $newBarcodeHeight);
|
|
imagefill($resizedBarcode, 0, 0, $white);
|
|
imagecopyresampled($resizedBarcode, $barcodeImage,
|
|
0, 0, 0, 0,
|
|
$newBarcodeWidth, $newBarcodeHeight,
|
|
$barcodeWidth, $barcodeHeight);
|
|
|
|
$barcodeX = ($imageWidth - $newBarcodeWidth) / 2;
|
|
imagecopy($image, $resizedBarcode, $barcodeX, $barcodeYPosition, 0, 0, $newBarcodeWidth, $newBarcodeHeight);
|
|
|
|
// 添加外箱条码文字
|
|
$codeTextBox = imagettfbbox($barcodeTextFontSize, 0, $fontFile, $box_barcode_data);
|
|
$codeTextWidth = $codeTextBox[2] - $codeTextBox[0];
|
|
$codeTextX = ($imageWidth - $codeTextWidth) / 2;
|
|
imagettftext($image, $barcodeTextFontSize, 0, $codeTextX, $barcodeTextYPosition, $black, $fontFile, $box_barcode_data);
|
|
|
|
// 输出图像
|
|
ob_start();
|
|
imagepng($image);
|
|
$imageData = ob_get_clean();
|
|
|
|
// 标记为已打印
|
|
$zengjie->table('box_code_all')
|
|
->where(['id' => $box_data['id']])
|
|
->update(['is_print' => 1]);
|
|
|
|
return $this->msg([
|
|
'image' => 'data:image/png;base64,' . base64_encode($imageData)
|
|
]);
|
|
|
|
} catch (\Exception $e) {
|
|
return $this->msg(10003, "生成集成码失败: " . $e->getMessage());
|
|
}
|
|
}
|
|
} |