From 7febb80b9decc45ef8d00c35d9c72352409b003e Mon Sep 17 00:00:00 2001 From: mkm <727897186@qq.com> Date: Sat, 8 Jun 2024 17:37:18 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=E4=BF=AE=E6=94=B9=E4=BA=86?= =?UTF-8?q?=E8=AE=A2=E5=8D=95=E5=A4=84=E7=90=86=E7=9A=84=E9=80=BB=E8=BE=91?= =?UTF-8?q?=EF=BC=8C=E5=BC=95=E5=85=A5=E4=BA=86=E6=A0=B8=E9=94=80=E7=A0=81?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=EF=BC=8C=E5=B9=B6=E4=BC=98=E5=8C=96=E4=BA=86?= =?UTF-8?q?=E6=94=AF=E4=BB=98=E9=80=9A=E7=9F=A5=E7=9A=84=E6=8E=A8=E9=80=81?= =?UTF-8?q?=E6=9C=BA=E5=88=B6=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/IndexController.php | 6 +- app/common/logic/PayNotifyLogic.php | 6 +- app/common/logic/PaymentLogic.php | 10 +-- app/queue/redis/CodePaySend.php | 57 +++++++++++++++ app/queue/redis/PushPlatformPrintSend.php | 70 +++++++++++++++++++ .../store_order/StoreOrderController.php | 5 +- .../store_order/StoreOrderValidate.php | 6 +- config/plugin/webman/redis-queue/log.php | 2 +- 8 files changed, 147 insertions(+), 15 deletions(-) create mode 100644 app/queue/redis/CodePaySend.php create mode 100644 app/queue/redis/PushPlatformPrintSend.php diff --git a/app/api/controller/IndexController.php b/app/api/controller/IndexController.php index d74c7721..1205aadb 100644 --- a/app/api/controller/IndexController.php +++ b/app/api/controller/IndexController.php @@ -15,6 +15,8 @@ use hg\apidoc\annotation as ApiDoc; use support\Log; use Yansongda\Pay\Exception\InvalidSignException; use Monolog\Handler\RotatingFileHandler; +use Webman\RedisQueue\Redis; + #[ApiDoc\NotParse()] class IndexController extends BaseApiController @@ -23,8 +25,8 @@ class IndexController extends BaseApiController public function index() { - Log::error(222); -d(2); + Redis::send('push-platform-print', ['order_id' => 119]); + d(2); try { $wechat = new PayService(1); diff --git a/app/common/logic/PayNotifyLogic.php b/app/common/logic/PayNotifyLogic.php index 8d40b16e..633256af 100644 --- a/app/common/logic/PayNotifyLogic.php +++ b/app/common/logic/PayNotifyLogic.php @@ -114,11 +114,11 @@ class PayNotifyLogic extends BaseLogic if ($order->pay_type == 9) { $extra['create_time'] = $order['create_time']; - PushService::push('store_merchant_' . $order['id'], $order['id'], ['type' => 'cash_register', 'msg' => '您有一笔订单已支付', 'data' => $extra]); + PushService::push('store_merchant_' . $order['store_id'], $order['store_id'], ['type' => 'cash_register', 'msg' => '您有一笔订单已支付', 'data' => $extra]); } else { - PushService::push('store_merchant_' . $order['id'], $order['id'], ['type' => 'store_merchant', 'msg' => '您有一笔新的订单']); + PushService::push('store_merchant_' . $order['store_id'], $order['store_id'], ['type' => 'store_merchant', 'msg' => '您有一笔新的订单']); Redis::send('push-platform-print', ['order_id' => $order['id']], 60); -// Db::name('order_middle')->insert(['c_order_id' => $order['id']]); + // Db::name('order_middle')->insert(['c_order_id' => $order['id']]); } if (!empty($extra['payer']['openid']) && $order->pay_type != 9) { Redis::send('push-delivery', ['order_sn' => $order['order_id'], 'openid' => $extra['payer']['openid']], 5); diff --git a/app/common/logic/PaymentLogic.php b/app/common/logic/PaymentLogic.php index 7f8ac059..2f282aa0 100644 --- a/app/common/logic/PaymentLogic.php +++ b/app/common/logic/PaymentLogic.php @@ -84,16 +84,16 @@ class PaymentLogic extends BaseLogic } $order = [ 'description' => '条码商品', - 'out_trade_no' => $order['number'], + 'out_trade_no' => (string)$order['order_id'], 'payer' => [ 'auth_code' => (string)$auth_code ], 'amount' => [ - 'total' => intval($order['actual'] * 100), + 'total' => intval($order['pay_price'] * 100), ], 'scene_info' => [ "store_info" => [ - 'id' => (string)$order['merchant'] + 'id' => (string)$order['store_id']??1 ] ], ]; @@ -124,9 +124,9 @@ class PaymentLogic extends BaseLogic } $order = [ 'subject' => '条码商品', - 'out_trade_no' => $order['number'], + 'out_trade_no' => (string)$order['order_id'], 'auth_code' => (string)$auth_code, - 'total_amount' => $order['actual'], + 'total_amount' => $order['pay_price'], 'extend_params'=>['attach'=>'alipay_cashier'] ]; $wechat = new PayService(); diff --git a/app/queue/redis/CodePaySend.php b/app/queue/redis/CodePaySend.php new file mode 100644 index 00000000..c3b33c55 --- /dev/null +++ b/app/queue/redis/CodePaySend.php @@ -0,0 +1,57 @@ + $data['order_id'], + ]; + $res = $pay->wechat->query($order); + if ($res['trade_state'] == 'SUCCESS' && $res['trade_state_desc'] == '支付成功') { + PayNotifyLogic::handle('wechat_common', $res['out_trade_no'], $res); + }else{ + throw new BusinessException('订单支付中', 200); + } + } + // 消费失败时 + public function onConsumeFailure(\Throwable $exception, $package) + { + // 直接更改消息队列数据结构,将最大重试次数max_attempts字段设置为0,即不再重试。 + if($package['attempts'] ==$exception['max_attempts']){ + $data = [ + 'order_id' => $package['data']['order_id'], + 'paid' => 0, + ]; + $find=StoreOrder::where($data)->find(); + if($find){ + $order = StoreOrder::update($data); + if($order){ + PushService::push('store_merchant_' . $order['store_id'], $order['store_id'], ['type'=>'cash_register','msg'=>'支付超时,订单已被取消,请重新提交订单']); + } + } + } + return $package; + } +} diff --git a/app/queue/redis/PushPlatformPrintSend.php b/app/queue/redis/PushPlatformPrintSend.php new file mode 100644 index 00000000..2ed2175b --- /dev/null +++ b/app/queue/redis/PushPlatformPrintSend.php @@ -0,0 +1,70 @@ +find(); + if ($find) { + $merchant = SystemStore::where('id', $find['store_id'])->field('name,phone')->find(); + $mer_user_info = SystemStoreStaff::where('store_id', $find['store_id'])->where('is_admin',1)->field('staff_name,phone')->find(); + $user = User::where('id', $find['uid'])->field('nickname,mobile')->find(); + $find['system_store_name'] = $merchant['name']; + $find['system_store_phone'] = $merchant['phone']; + $find['staff_name'] = $mer_user_info['staff_name']; + $find['staff_phone'] = $mer_user_info['phone']; + $find['nickname'] = $user['nickname']??''; + $find['user_mobile'] = $user['mobile']??''; + $find['info'] = StoreOrderCartInfo::where('oid', $find['id'])->field('store_id,product_id,cart_num,price,total_price')->select()->each(function ($item) { + $goods = StoreBranchProduct::where(['store_id'=>$item['store_id'],'product_id'=>$item['product_id']])->field('store_name,unit')->find(); + $item['unit_name'] = StoreProductUnit::where('id', $goods['unit'])->value('name'); + $item['store_name'] = $goods['store_name']; + return $item; + }); + PushService::push('store_merchant_'.$find['store_id'], 1, ['type'=>'platform_print','msg'=>'打印队列','data'=>$find]); + Log::info('打印推送结束'.$id); + + } + } + public function onConsumeFailure(\Throwable $exception, $package) + { + Log::error('打印队列推送失败。order_id:'. $package['data']['order_id'].',msg:'.$exception->getMessage()); + return $package; + } +} \ No newline at end of file diff --git a/app/store/controller/store_order/StoreOrderController.php b/app/store/controller/store_order/StoreOrderController.php index a9cdc2be..852eea64 100644 --- a/app/store/controller/store_order/StoreOrderController.php +++ b/app/store/controller/store_order/StoreOrderController.php @@ -96,7 +96,8 @@ class StoreOrderController extends BaseAdminController ApiDoc\NotHeaders(), ApiDoc\Author('中国队长'), ApiDoc\Header(ref: [Definitions::class, "token"]), - ApiDoc\Query(name: 'id', type: 'int', require: true, desc: '订单id'), + ApiDoc\Query(name: 'id', type: 'int', require: false, desc: '订单id'), + ApiDoc\Query(name: 'verify_code', type: 'string', require: false, desc: '核销码'), ApiDoc\ResponseSuccess("data", type: "array"), ] public function detail(StoreOrderLogic $orderLogic) @@ -151,7 +152,7 @@ class StoreOrderController extends BaseAdminController return $this->fail(PaymentLogic::getError(), $params); } if (isset($result['trade_state_desc']) && $result['trade_state_desc'] == '支付成功') { - PayNotifyLogic::handle('StoreOrder', $result['out_trade_no'], $result); + PayNotifyLogic::handle('wechat_common', $result['out_trade_no'], $result); } else { Redis::send('send-code-pay', ['number' => $order['number']]); return $this->success('用户支付中'); diff --git a/app/store/validate/store_order/StoreOrderValidate.php b/app/store/validate/store_order/StoreOrderValidate.php index 3e7eeec4..f7b4eeb8 100644 --- a/app/store/validate/store_order/StoreOrderValidate.php +++ b/app/store/validate/store_order/StoreOrderValidate.php @@ -19,7 +19,8 @@ class StoreOrderValidate extends BaseValidate * @var string[] */ protected $rule = [ - 'id' => 'require', + 'id' => 'requireWithout:verify_code', + 'verify_code' => 'requireWithout:id', ]; @@ -29,6 +30,7 @@ class StoreOrderValidate extends BaseValidate */ protected $field = [ 'id' => 'id', + 'verify_code' => '核销码', ]; @@ -76,7 +78,7 @@ class StoreOrderValidate extends BaseValidate */ public function sceneDetail() { - return $this->only(['id']); + return $this->only(['id','verify_code']); } } diff --git a/config/plugin/webman/redis-queue/log.php b/config/plugin/webman/redis-queue/log.php index 4a60c18f..6be46eb2 100644 --- a/config/plugin/webman/redis-queue/log.php +++ b/config/plugin/webman/redis-queue/log.php @@ -18,7 +18,7 @@ return [ [ 'class' => \support\log\MonologExtendHandler::class, 'constructor' => [ - '/logs/redis-queue/queue.log', + 'redis-queue', 7, //$maxFiles Monolog\Logger::DEBUG, ], From b9c341333683cc7f32bde5eac0a907823ead6dbf Mon Sep 17 00:00:00 2001 From: luofei <604446095@qq.com> Date: Sat, 8 Jun 2024 17:37:59 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=A6=96=E9=A1=B5?= =?UTF-8?q?=E9=85=8D=E9=80=81=E7=BB=9F=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/store/controller/WorkbenchController.php | 64 +++++-------- app/store/logic/WorkbenchLogic.php | 98 ++++++++++++++++++++ 2 files changed, 123 insertions(+), 39 deletions(-) diff --git a/app/store/controller/WorkbenchController.php b/app/store/controller/WorkbenchController.php index 64484173..267a13ec 100644 --- a/app/store/controller/WorkbenchController.php +++ b/app/store/controller/WorkbenchController.php @@ -3,6 +3,7 @@ namespace app\store\controller; use app\common\controller\Definitions; +use app\store\lists\store_order\StoreOrderLists; use app\store\logic\WorkbenchLogic; use hg\apidoc\annotation as ApiDoc; @@ -49,7 +50,7 @@ class WorkbenchController extends BaseAdminController } #[ - ApiDoc\Title('配送统计(暂时不用)'), + ApiDoc\Title('配送统计'), ApiDoc\url('/store/workbench/delivery'), ApiDoc\Method('GET'), ApiDoc\NotHeaders(), @@ -61,8 +62,29 @@ class WorkbenchController extends BaseAdminController ] public function delivery() { - $storeId = $this->request->adminInfo['store_id']; - $result = WorkbenchLogic::delivery($storeId); + $params = $this->request->get(); + $params['store_id'] = $this->request->adminInfo['store_id']; + $result = WorkbenchLogic::delivery($params); + return $this->data($result); + } + + #[ + ApiDoc\Title('配送订单统计'), + ApiDoc\url('/store/workbench/deliveryOrder'), + ApiDoc\Method('GET'), + ApiDoc\NotHeaders(), + ApiDoc\Author('中国队长'), + ApiDoc\Query(name: 'start_time', type: 'string', require: true, desc: '开始时间'), + ApiDoc\Query(name: 'end_time', type: 'string', require: true, desc: '结束时间'), + ApiDoc\Header(ref: [Definitions::class, "token"]), + ApiDoc\Query(ref: [Definitions::class, "page"]), + ApiDoc\ResponseSuccess("data", type: "array"), + ] + public function deliveryOrder() + { + $params = $this->request->get(); + $params['store_id'] = $this->request->adminInfo['store_id']; + $result = WorkbenchLogic::deliveryOrder($params); return $this->data($result); } @@ -120,40 +142,4 @@ class WorkbenchController extends BaseAdminController return $workbench->get_product_ranking(); } - #[ - ApiDoc\Title('用户统计-概况'), - ApiDoc\url('/store/workbench/get_user_basic'), - ApiDoc\Method('GET'), - ApiDoc\NotHeaders(), - ApiDoc\Author('中国队长'), - ApiDoc\Query(name: 'start_time', type: 'string', require: true, desc: '开始时间'), - ApiDoc\Query(name: 'end_time', type: 'string', require: true, desc: '结束时间'), - ApiDoc\Header(ref: [Definitions::class, "token"]), - ApiDoc\ResponseSuccess("data", type: "array"), - ] - public function get_user_basic(\app\admin\controller\WorkbenchController $workbench) - { - $params = $this->request->get(); - $params['store_id'] = $this->request->adminInfo['store_id']; - return $workbench->get_user_basic(); - } - - #[ - ApiDoc\Title('用户统计-图表'), - ApiDoc\url('/store/workbench/get_user_trend'), - ApiDoc\Method('GET'), - ApiDoc\NotHeaders(), - ApiDoc\Author('中国队长'), - ApiDoc\Query(name: 'start_time', type: 'string', require: true, desc: '开始时间'), - ApiDoc\Query(name: 'end_time', type: 'string', require: true, desc: '结束时间'), - ApiDoc\Header(ref: [Definitions::class, "token"]), - ApiDoc\ResponseSuccess("data", type: "array"), - ] - public function get_user_trend(\app\admin\controller\WorkbenchController $workbench) - { - $params = $this->request->get(); - $params['store_id'] = $this->request->adminInfo['store_id']; - return $workbench->get_user_trend(); - } - } diff --git a/app/store/logic/WorkbenchLogic.php b/app/store/logic/WorkbenchLogic.php index 8084b86f..01fb12a1 100644 --- a/app/store/logic/WorkbenchLogic.php +++ b/app/store/logic/WorkbenchLogic.php @@ -431,4 +431,102 @@ class WorkbenchLogic extends BaseLogic return $data; } + public static function delivery($params) + { + $data = []; + $storeId = $params['store_id']; + $startTime = $params['start_time']; + $endTime = $params['end_time']; + $endTime = date('Y-m-d', strtotime($endTime) + 86400); + $dateDiff = (new \DateTime($endTime))->diff(new \DateTime($startTime)); + $timeRange = []; + if ($dateDiff->days == 1) { + $group = 'HOUR(pay_time)'; + $i = 0; + while ($i < 24) { + $timeRange[] = date('H', strtotime("+$i hours", strtotime($startTime))); + $i++; + } + $field = 'from_unixtime(pay_time,"%H") as pay_time,sum(pay_price) as pay_price,count(id) as order_num'; + } elseif ($dateDiff->days <= 31) { + $group = 'DAY(pay_time)'; + $i = 0; + while ($i < $dateDiff->days) { + $timeRange[] = date('m-d', strtotime("+$i days", strtotime($startTime))); + $i++; + } + $field = 'from_unixtime(pay_time,"%m-%d") as pay_time,sum(pay_price) as pay_price,count(id) as order_num'; + } else { + $group = 'MONTH(pay_time)'; + $i = 0; + $month = 0; + if ($dateDiff->y > 0) { + $month = $dateDiff->y * 12; + } + if ($dateDiff->m > 0) { + $month += $dateDiff->m; + } + if ($dateDiff->d > 0) { + $month += 1; + } + while ($i < $month) { + $timeRange[] = date('Y-m', strtotime("+$i months", strtotime($startTime))); + $i++; + } + $field = 'from_unixtime(pay_time,"%Y-%m") as pay_time,sum(pay_price) as pay_price,count(id) as order_num'; + } + $amountList = StoreOrder::field($field) + ->where('store_id', $storeId) + ->where('paid', 1) + ->where('shipping_type', 1) + ->whereBetweenTime('pay_time', $startTime, $endTime) + ->group($group) + ->select() + ->toArray(); + $amountList = reset_index($amountList, 'pay_time'); + $amountListTmp = []; + $countListTmp = []; + $range = []; + foreach ($timeRange as $item) { + $range[] = $item; + if (!isset($amountList[$item])) { + $amountListTmp[$item] = 0; + } else { + $amountListTmp[$item] = $amountList[$item]['pay_price']; + } + if (!isset($amountList[$item])) { + $countListTmp[$item] = 0; + } else { + $countListTmp[$item] = $amountList[$item]['order_num']; + } + } + $data['statistics'] = [ + 'range' => $range, + 'data' => [ + 'order_amount' => array_values($amountListTmp), + 'order_count' => array_values($countListTmp) + ] + ]; + return $data; + } + + public static function deliveryOrder($params) + { + $startTime = $params['start_time']; + $endTime = $params['end_time']; + $endTime = date('Y-m-d', strtotime($endTime) + 86400); + $query = StoreOrder::with('user')->where('store_id', $params['store_id']) + ->where('paid', 1) + ->where('shipping_type', 1) + ->whereBetweenTime('create_time', $startTime, $endTime); + $data['count'] = $query->count(); + $data['page_no'] = $params['page_no']; + $data['page_size'] = $params['page_size']; + $data['extend'] = []; + $data['lists'] = $query->order('create_time', 'desc') + ->page($params['page_no'], $params['page_size']) + ->select()->toArray(); + return $data; + } + }