feat(printer): 添加订单打印功能

- 在 PayNotifyLogic 中增加订单打印逻辑
- 实现 XpsdkPrintApi 类的订单打印方法
- 添加 OrderXprinterPushSend 队列消费者处理订单打印
- 优化打印内容格式,包括订单信息、商品列表等
This commit is contained in:
mkm 2024-12-23 17:52:58 +08:00
parent be6cf0aa3f
commit df8f3523f6
3 changed files with 115 additions and 34 deletions

View File

@ -647,6 +647,9 @@ class PayNotifyLogic extends BaseLogic
if ($order['uid'] != 1) {
Redis::send('order_wetcha_push_send', ['order' => $order]);
}
if($order['store_id']==21){
Redis::send('order_xprinter_push_send', ['order' => $order]);
}
} catch (\Exception $e) {
Log::error('订单推送失败:' . $e->getMessage());
// 异常处理代码,例如记录日志或发送通知等。

View File

@ -25,44 +25,49 @@ class XpsdkPrintApi
* 注意对齐标签L C R CB 请勿嵌套使用,嵌套使用内层标签有效,外层失效;
* 同一行请勿使用多个对齐标签,否则只有最后一个对齐标签有效
*/
public function printFontAlign($sn)
public function printFontAlign($sn,$order)
{
$system_store=$order['system_store'];
$system_phone=$order['system_phone'];
$verify_code=$order['verify_code'];
$order_id=$order['order_id'];
$create_time=$order['create_time'];
$pay_price=$order['pay_price'];
$total_price=$order['total_price'];
$deduction_price=$order['deduction_price'];
$pay_type_name=$order['pay_type_name'];
$printContent = <<<EOF
不加标签:默认字体大小<BR>
<BR>
L标签<L>左对齐<BR></L>
<BR>
R标签<R>右对齐<BR></R>
<BR>
C标签<C>居中对齐<BR></C>
<BR>
N标签<N>字体正常大小<BR></N>
<BR>
HB标签<HB>字体变高一倍<BR></HB>
<BR>
WB标签<WB>字体变宽一倍<BR></WB>
<BR>
B标签<B>字体放大一倍<BR></B>
<BR>
HB2标签<HB2>字体变高二倍<BR></HB2>
<BR>
WB2标签<WB2>字体变宽二倍<BR></WB2>
<BR>
B2标签<B2>字体放大二倍<BR></B2>
<BR>
BOLD标签<BOLD>字体加粗<BR></BOLD>
<C><BOLD><B>{$system_store}</B></BOLD><BR></C>
<L><N>==============================
<L><N>核销码:<BOLD>{$verify_code}</BOLD>
<L><N>单号:{$order_id}
<L><N>下单时间:{$create_time}
<L><N>==============================
<L>单价 数量 小计
<L>
EOF;
// 使用 for 循环生成商品列表
foreach ($order['product_arr'] as $k=>$v) {
$printContent .= <<<EOF
<L>{$v['name']}
<L>{$v['price']} *{$v['quantity']} {$v['subtotal']}
EOF;
}
$printContent = $printContent . '<BR>';
// 嵌套使用对齐和字体
$printContent = $printContent . '<C>嵌套使用:<BOLD>居中加粗</BOLD><BR></C>';
// 打印条形码和二维码
$printContent = $printContent . '<BR>';
$printContent = $printContent . '<C><BARCODE>9884822189</BARCODE></C>';
$printContent = $printContent . '<C><QR>https://www.xpyun.net</QR></C>';
// 继续生成剩余的 printContent
$printContent .= <<<EOF
<L>
<L><N>==============================
<N>合计:{$total_price}
<L><N>优惠:{$deduction_price}
<L><N>实付款:{$pay_price}
<L><N>支付方式:{$pay_type_name}
<L><N>店铺电话:{$system_phone}
<L><N>==============================
<C><BOLD>欢迎下次光临!</BOLD><BR></C>
EOF;
$request = new PrintRequest();
$request->generateSign();
@ -81,7 +86,7 @@ EOF;
//打印模式:
//值为 0 或不指定则会检查打印机是否在线,如果不在线 则不生成打印订单,直接返回设备不在线状态码;如果在线则生成打印订单,并返回打印订单号。
//值为 1不检查打印机是否在线直接生成打印订单并返回打印订单号。如果打印机不在线订单将缓存在打印队列中打印机正常在线时会自动打印。
$request->mode = 1;
$request->mode = 0;
$result = $this->service->xpYunPrint($request);
return $result->content;

View File

@ -0,0 +1,73 @@
<?php
namespace app\queue\redis;
use app\common\enum\PayEnum;
use app\common\model\store_order_cart_info\StoreOrderCartInfo;
use app\common\model\store_product\StoreProduct;
use app\common\model\system_store\SystemStore;
use app\common\service\xpyun\XpsdkPrintApi;
use Webman\RedisQueue\Consumer;
use support\Log;
/**
* 芯华云订单推送队列消费
*/
class OrderXprinterPushSend implements Consumer
{
// 要消费的队列名
public $queue = 'order_xprinter_push_send';
// 连接名,对应 plugin/webman/redis-queue/redis.php 里的连接`
public $connection = 'default';
// 消费
public function consume($data)
{
$order=$data['order'];
$total_price = $order['total_price'];
$pay_price = $order['pay_price'];
$deduction_price = $order['deduction_price'];
$pay_time = date('Y-m-d H:i:s', $order['pay_time']);
$pay_type = PayEnum::getPaySceneDesc($order['pay_type']);
$SystemStore = SystemStore::where('id', $order['store_id'])->find();
$cart_info=StoreOrderCartInfo::where('oid',$order['id'])->select()->each(function($item){
$find = StoreProduct::with('unitName')->withTrashed()->find();
$item['store_name']=$find['store_name'];
return $item;
});
$product_arr=[];
foreach ($cart_info as $k=>$v){
$product_arr[]=[
'name'=>$v['name'],
'price'=>$v['price'].'元',
'quantity'=>$v['cart_num'],
'subtotal'=>$v['total_price'].'元'
];
}
$api=new XpsdkPrintApi();
$order=[
'system_store'=>$SystemStore['name']??'',
'system_phone'=>$SystemStore['phone']??'',
'verify_code'=>$order['verify_code'],
'order_id'=>$order['order_id'],
'create_time'=>$pay_time,
'pay_price'=>$pay_price,
'total_price'=>$total_price,
'deduction_price'=>$deduction_price,
'pay_type_name'=>$pay_type,
'product_arr'=>$product_arr
];
($api->printFontAlign('74AMP5N6DP21148',$order));
}
// 消费失败时
public function onConsumeFailure(\Throwable $exception, $package)
{
$package['max_attempts'] = 0;
Log::error('推送订单失败', ['order_id' => $package['data'], 'error' => $package['error']]);
return true;
}
}