From df8f3523f680f85cf6c48879db2de5199639e4b3 Mon Sep 17 00:00:00 2001 From: mkm <727897186@qq.com> Date: Mon, 23 Dec 2024 17:52:58 +0800 Subject: [PATCH 1/4] =?UTF-8?q?feat(printer):=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E8=AE=A2=E5=8D=95=E6=89=93=E5=8D=B0=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 PayNotifyLogic 中增加订单打印逻辑 - 实现 XpsdkPrintApi 类的订单打印方法 - 添加 OrderXprinterPushSend 队列消费者处理订单打印 - 优化打印内容格式,包括订单信息、商品列表等 --- app/common/logic/PayNotifyLogic.php | 3 + app/common/service/xpyun/XpsdkPrintApi.php | 73 ++++++++++++---------- app/queue/redis/OrderXprinterPushSend.php | 73 ++++++++++++++++++++++ 3 files changed, 115 insertions(+), 34 deletions(-) create mode 100644 app/queue/redis/OrderXprinterPushSend.php diff --git a/app/common/logic/PayNotifyLogic.php b/app/common/logic/PayNotifyLogic.php index b2b2a6d04..52f6c5e8a 100644 --- a/app/common/logic/PayNotifyLogic.php +++ b/app/common/logic/PayNotifyLogic.php @@ -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()); // 异常处理代码,例如记录日志或发送通知等。 diff --git a/app/common/service/xpyun/XpsdkPrintApi.php b/app/common/service/xpyun/XpsdkPrintApi.php index e363fec4a..db9fd7195 100644 --- a/app/common/service/xpyun/XpsdkPrintApi.php +++ b/app/common/service/xpyun/XpsdkPrintApi.php @@ -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 = << -
-L标签:左对齐
-
-R标签:右对齐
-
-C标签:居中对齐
-
-N标签:字体正常大小
-
-HB标签:字体变高一倍
-
-WB标签:字体变宽一倍
-
-B标签:字体放大一倍
-
-HB2标签:字体变高二倍
-
-WB2标签:字体变宽二倍
-
-B2标签:字体放大二倍
-
-BOLD标签:字体加粗
+{$system_store}
+============================== +核销码:{$verify_code} +单号:{$order_id} +下单时间:{$create_time} +============================== +单价 数量 小计 + EOF; +// 使用 for 循环生成商品列表 +foreach ($order['product_arr'] as $k=>$v) { + $printContent .= <<{$v['name']} +{$v['price']} *{$v['quantity']} {$v['subtotal']} +EOF; +} - $printContent = $printContent . '
'; - // 嵌套使用对齐和字体 - $printContent = $printContent . '嵌套使用:居中加粗
'; - - // 打印条形码和二维码 - $printContent = $printContent . '
'; - $printContent = $printContent . '9884822189'; - $printContent = $printContent . 'https://www.xpyun.net'; - +// 继续生成剩余的 printContent +$printContent .= << +============================== +合计:{$total_price}元 +优惠:{$deduction_price}元 +实付款:{$pay_price}元 +支付方式:{$pay_type_name} +店铺电话:{$system_phone} +============================== + 欢迎下次光临!
+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; diff --git a/app/queue/redis/OrderXprinterPushSend.php b/app/queue/redis/OrderXprinterPushSend.php new file mode 100644 index 000000000..01cb86f6d --- /dev/null +++ b/app/queue/redis/OrderXprinterPushSend.php @@ -0,0 +1,73 @@ +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; + } +} From 2be22adcb324bf5162870e6fee97b82ee9769315 Mon Sep 17 00:00:00 2001 From: mkm <727897186@qq.com> Date: Mon, 23 Dec 2024 18:12:15 +0800 Subject: [PATCH 2/4] =?UTF-8?q?fix(printer):=20=E4=BF=AE=E5=A4=8D=E5=B0=8F?= =?UTF-8?q?=E7=A5=A8=E6=89=93=E5=8D=B0=E5=95=86=E5=93=81=E5=90=8D=E7=A7=B0?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 PayNotifyLogic 中添加 XpsdkPrintApi 类的引用 - 在 OrderXprinterPushSend 中修正商品名称的获取方式 - 优化代码格式和结构,提高可读性 --- app/common/logic/PayNotifyLogic.php | 2 + app/queue/redis/OrderXprinterPushSend.php | 70 +++++++++++------------ 2 files changed, 37 insertions(+), 35 deletions(-) diff --git a/app/common/logic/PayNotifyLogic.php b/app/common/logic/PayNotifyLogic.php index 52f6c5e8a..9539d11ca 100644 --- a/app/common/logic/PayNotifyLogic.php +++ b/app/common/logic/PayNotifyLogic.php @@ -32,6 +32,7 @@ use app\common\model\user_sign\UserSign; use app\common\model\vip_flow\VipFlow; use app\common\service\Curl; use app\common\service\PushService; +use app\common\service\xpyun\XpsdkPrintApi; use support\exception\BusinessException; use support\Log; use think\facade\Db; @@ -54,6 +55,7 @@ class PayNotifyLogic extends BaseLogic return true; } catch (\Exception $e) { Db::rollback(); + d($e); Log::error('支付回调处理失败' . $e->getMessage() . ',lien:' . $e->getLine() . ',file:' . $e->getFile()); throw new BusinessException($e->getMessage()); } diff --git a/app/queue/redis/OrderXprinterPushSend.php b/app/queue/redis/OrderXprinterPushSend.php index 01cb86f6d..ec6682c17 100644 --- a/app/queue/redis/OrderXprinterPushSend.php +++ b/app/queue/redis/OrderXprinterPushSend.php @@ -25,43 +25,43 @@ class OrderXprinterPushSend implements Consumer 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; - }); + $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::where('id', $item['product_id'])->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 + $product_arr = []; + foreach ($cart_info as $k => $v) { + $product_arr[] = [ + 'name' => $v['store_name'], + 'price' => $v['price'] . '元', + 'quantity' => $v['cart_num'], + 'subtotal' => $v['total_price'] . '元' ]; - ($api->printFontAlign('74AMP5N6DP21148',$order)); + } + + $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 + ]; + $res = ($api->printFontAlign('74AMP5N6DP21148', $order)); } // 消费失败时 public function onConsumeFailure(\Throwable $exception, $package) From 5727572e0d7bb01036f5cd5aafc2bb3e1861b345 Mon Sep 17 00:00:00 2001 From: mkm <727897186@qq.com> Date: Tue, 24 Dec 2024 09:07:25 +0800 Subject: [PATCH 3/4] =?UTF-8?q?fix(queue):=20=E4=BC=98=E5=8C=96=E5=B0=8F?= =?UTF-8?q?=E7=A5=A8=E6=89=93=E5=8D=B0=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 增加了 DictData 模型的引用 - 使用商店 ID 查询打印机配置,并在存在时使用该配置进行打印 --- app/queue/redis/OrderXprinterPushSend.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/queue/redis/OrderXprinterPushSend.php b/app/queue/redis/OrderXprinterPushSend.php index ec6682c17..7518e2d92 100644 --- a/app/queue/redis/OrderXprinterPushSend.php +++ b/app/queue/redis/OrderXprinterPushSend.php @@ -3,6 +3,7 @@ namespace app\queue\redis; use app\common\enum\PayEnum; +use app\common\model\dict\DictData; use app\common\model\store_order_cart_info\StoreOrderCartInfo; use app\common\model\store_product\StoreProduct; use app\common\model\system_store\SystemStore; @@ -61,7 +62,10 @@ class OrderXprinterPushSend implements Consumer 'pay_type_name' => $pay_type, 'product_arr' => $product_arr ]; - $res = ($api->printFontAlign('74AMP5N6DP21148', $order)); + $value=DictData::where('name','xprinter_'.$order['store_id'])->value('value'); + if($value){ + $res = ($api->printFontAlign($value, $order)); + } } // 消费失败时 public function onConsumeFailure(\Throwable $exception, $package) From 1f82805875dec4eb668804a29109b57488410edf Mon Sep 17 00:00:00 2001 From: mkm <727897186@qq.com> Date: Tue, 24 Dec 2024 09:27:39 +0800 Subject: [PATCH 4/4] =?UTF-8?q?fix(app):=20=E4=BF=AE=E5=A4=8D=E6=94=AF?= =?UTF-8?q?=E4=BB=98=E5=9B=9E=E8=B0=83=E5=92=8C=E8=AE=A2=E5=8D=95=E6=8E=A8?= =?UTF-8?q?=E9=80=81=E7=9A=84=E7=9B=B8=E5=85=B3=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修改 PayNotifyLogic 中异常捕获,使用 Throwable 代替 Exception - 优化 OrderXprinterPushSend 中的订单信息获取和处理 --- app/common/logic/PayNotifyLogic.php | 3 ++- app/queue/redis/OrderXprinterPushSend.php | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/common/logic/PayNotifyLogic.php b/app/common/logic/PayNotifyLogic.php index 9539d11ca..c91074eee 100644 --- a/app/common/logic/PayNotifyLogic.php +++ b/app/common/logic/PayNotifyLogic.php @@ -11,6 +11,7 @@ use app\common\enum\user\UserShipEnum; use app\common\enum\YesNoEnum; use app\common\logic\user_product_storage\UserProductStorageLogic; use app\common\model\Config; +use app\common\model\dict\DictData; use app\common\model\dict\DictType; use app\common\model\finance\CapitalFlow; use app\common\model\finance\PayNotifyLog; @@ -53,7 +54,7 @@ class PayNotifyLogic extends BaseLogic self::$action($orderSn, $extra, $type); Db::commit(); return true; - } catch (\Exception $e) { + } catch (\Throwable $e) { Db::rollback(); d($e); Log::error('支付回调处理失败' . $e->getMessage() . ',lien:' . $e->getLine() . ',file:' . $e->getFile()); diff --git a/app/queue/redis/OrderXprinterPushSend.php b/app/queue/redis/OrderXprinterPushSend.php index 7518e2d92..205f09074 100644 --- a/app/queue/redis/OrderXprinterPushSend.php +++ b/app/queue/redis/OrderXprinterPushSend.php @@ -29,6 +29,7 @@ class OrderXprinterPushSend implements Consumer $order = $data['order']; $total_price = $order['total_price']; $pay_price = $order['pay_price']; + $store_id = $order['store_id']; $deduction_price = $order['deduction_price']; $pay_time = date('Y-m-d H:i:s', $order['pay_time']); $pay_type = PayEnum::getPaySceneDesc($order['pay_type']); @@ -62,7 +63,7 @@ class OrderXprinterPushSend implements Consumer 'pay_type_name' => $pay_type, 'product_arr' => $product_arr ]; - $value=DictData::where('name','xprinter_'.$order['store_id'])->value('value'); + $value=DictData::where('name','xprinter_'.$store_id)->value('value'); if($value){ $res = ($api->printFontAlign($value, $order)); }