From 2e1b2344bbf7d2c33f44047f9231eb79d651522a Mon Sep 17 00:00:00 2001 From: yaooo <272523191@qq.com> Date: Fri, 11 Aug 2023 21:29:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=BF=AB=E9=80=92=E5=91=98?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../store/order/StoreOrderRepository.php | 23 ++++-- app/controller/api/Auth.php | 11 ++- app/event.php | 1 + app/listener/DeliveryGoods.php | 70 +++++++++++++++++++ app/listener/paySuccessOrder.php | 46 ------------ 5 files changed, 97 insertions(+), 54 deletions(-) create mode 100644 app/listener/DeliveryGoods.php diff --git a/app/common/repositories/store/order/StoreOrderRepository.php b/app/common/repositories/store/order/StoreOrderRepository.php index 81fa826f..8099cfa4 100644 --- a/app/common/repositories/store/order/StoreOrderRepository.php +++ b/app/common/repositories/store/order/StoreOrderRepository.php @@ -2642,15 +2642,26 @@ class StoreOrderRepository extends BaseRepository * @param $orderSn * 更新扫描发货状态 */ - public function deliveryGoods($orderId, $orderSn) + public function deliveryGoods($orderId, $orderSn, $logisticsCode='') { - $order = $this->dao->search([], null)->where('order_sn', $orderSn)->where('order_id', $orderId)->where('StoreOrder.is_del', 0)->find(); + if ($logisticsCode) { + $order = $this->dao->search([], null)->where('order_sn', $orderSn)->where('order_id', $orderId)->where('logistics_code', $logisticsCode)->where('StoreOrder.status', 1)->where('StoreOrder.is_del', 0)->find(); + } else { + $order = $this->dao->search([], null)->where('order_sn', $orderSn)->where('order_id', $orderId)->where('StoreOrder.status', 0)->where('StoreOrder.is_del', 0)->find(); + } if (!$order) - throw new ValidateException('订单不存在'); - if ($order['status'] != 0 || $order['order_type']) + throw new ValidateException('订单不存在或编码code错误'); + if ($order['order_type']) throw new ValidateException('订单状态有误'); - $order->status = 1; - $order->mark = '快递员扫描取件'; + + if ($logisticsCode) { + $order->status = 2; + $order->mark = '快递员已完成送货'; + } else { + $order->status = 1; + $order->mark = '快递员已扫描取件'; + event('product.delivery', compact('order')); + } $order->save(); } diff --git a/app/controller/api/Auth.php b/app/controller/api/Auth.php index fea0c4de..02b7941c 100644 --- a/app/controller/api/Auth.php +++ b/app/controller/api/Auth.php @@ -885,11 +885,18 @@ class Auth extends BaseController public function deliveryGoods($id) { $orderSn = $this->request->param('order_sn'); + $logisticsCode = $this->request->param('logistics_code') ?? ''; if (empty($orderSn)) { return app('json')->fail('参数order_sn不能为空'); } - app()->make(StoreOrderRepository::class)->deliveryGoods($id, $orderSn); - return app('json')->success('快递员扫描取件成功'); + if (empty($logisticsCode)) { + app()->make(StoreOrderRepository::class)->deliveryGoods($id, $orderSn); + return app('json')->success('快递员扫描取件成功'); + } else { + app()->make(StoreOrderRepository::class)->deliveryGoods($id, $orderSn, $logisticsCode); + return app('json')->success('快递员已完成送货'); + } } + } diff --git a/app/event.php b/app/event.php index 67765853..9f9b7d12 100644 --- a/app/event.php +++ b/app/event.php @@ -67,6 +67,7 @@ return [ // 'community_address'=>[\app\listener\CommunityAddress::class], 'order.paySuccessOrder'=>[\app\listener\paySuccessOrder::class], 'product.create'=>[\app\listener\ProductCreate::class], + 'product.delivery'=>[\app\listener\DeliveryGoods::class], 'product.sell'=>[\app\listener\CloudProduct::class], //商品上下架 'refund.after'=>[\app\listener\AfterRefund::class], 'order.create'=>[\app\listener\OrderCreate::class], diff --git a/app/listener/DeliveryGoods.php b/app/listener/DeliveryGoods.php new file mode 100644 index 00000000..e7072977 --- /dev/null +++ b/app/listener/DeliveryGoods.php @@ -0,0 +1,70 @@ +event = $event; + Log::info("============= handle监听order_id " . $this->event['order']['order_id']); + $this->event = $event; + //发起物流信息返回快递员手机 + $logisticsPhone = $this->sendLogistics($this->event['order']['order_id'], $this->event['order']['order_sn']); + //生成用户的收货码、发生短信包含收货码 + $this->generateLogisticsCode($this->event['order']['uid'], $this->event['order']['order_id'], $this->event['order']['order_sn'], $logisticsPhone); + } + + //用户收货码 + public function generateLogisticsCode($uid, $orderId, $orderSn, $logisticsPhone) { + $code = random_int(1000, 9999); + $res = app()->make(StoreOrderRepository::class)->update($orderId, [ + 'logistics_code' => $code + ]); + if ($res) { + //收货人短信 + $phone = StoreOrder::where('order_id', $orderId)->value('user_phone'); + if ($phone) { + Log::info("发送短信 {$phone}, orderId: {$orderId}"); + SmsService::create()->send($phone, 'TAKEGOOD_CODE', ['number' => substr($orderSn, -6), 'number2' => $code, 'phone' => $logisticsPhone]); + } + } + } + + //发送物流 + public function sendLogistics($orderId, $orderSn) + { + Log::info("发送物流信息 orderId: {$orderId}, orderSn: {$orderSn}"); + $postUrl = 'http://logistics.lihaink.cn/api/lstSet'; + $curlPost = [ + 'order_id' => $orderId, + 'order_sn' => $orderSn, + ]; + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $postUrl); + curl_setopt($ch, CURLOPT_HEADER, 0); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost); + $data = curl_exec($ch); + curl_close($ch); + $phone = ''; + if (!empty($data) && is_string($data)) { + $logisticsInfo = json_decode($data, true); + $phone = $logisticsInfo['data']['phone']; + Log::info("物流联系信息" . json_encode($logisticsInfo)); + } + return $phone; + } + +} \ No newline at end of file diff --git a/app/listener/paySuccessOrder.php b/app/listener/paySuccessOrder.php index a11d2b11..702ca14c 100644 --- a/app/listener/paySuccessOrder.php +++ b/app/listener/paySuccessOrder.php @@ -9,8 +9,6 @@ use app\common\model\system\merchant\Merchant; use app\common\repositories\store\order\StoreOrderRepository; use app\common\repositories\system\merchant\FinancialRecordRepository; use app\common\repositories\system\merchant\MerchantRepository; -use app\common\model\store\order\StoreOrder; -use crmeb\services\SmsService; use crmeb\utils\DingTalk; use think\facade\Db; use think\facade\Log; @@ -28,7 +26,6 @@ class paySuccessOrder public function handle($event) { - Log::info("============= handle监听"); $this->event = $event; $this->finance = []; $this->index = 1; @@ -140,49 +137,6 @@ class paySuccessOrder Log::error('订单分润出错', ['code' => $e->getCode(), 'message' => $e->getMessage(), 'trace' => $e->getTraceAsString()]); DingTalk::exception($e, '订单分润出错'); } - //发起物流信息返回快递员手机 - $logisticsPhone = $this->sendLogistics($this->event['order']['order_id'], $this->event['order']['order_sn']); - //生成用户的收货码、发生短信包含收货码 - $this->generateLogisticsCode($this->event['order']['uid'], $this->event['order']['order_id'], $this->event['order']['order_sn'], $logisticsPhone); - } - - //用户收货码 - public function generateLogisticsCode($uid, $orderId, $orderSn, $logisticsPhone) { - $code = random_int(1000, 9999); - $res = app()->make(StoreOrderRepository::class)->update($orderId, [ - 'logistics_code' => $code - ]); - if ($res) { - //收货人短信 - $phone = StoreOrder::where('order_id', $orderId)->value('user_phone'); - if ($phone) { - Log::info("发送短信 {$phone}, orderId: {$orderId}"); - SmsService::create()->send($phone, 'TAKEGOOD_CODE', ['number' => substr($orderSn, -6), 'number2' => $code, 'phone' => $logisticsPhone]); - } - } - } - - //发送物流 - public function sendLogistics($orderId, $orderSn) - { - $postUrl = 'http://logistics.lihaink.cn/api/lstSet'; - $curlPost = [ - 'order_id' => $orderId, - 'order_sn' => $orderSn, - ]; - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $postUrl); - curl_setopt($ch, CURLOPT_HEADER, 0); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($ch, CURLOPT_POST, 1); - curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost); - $data = curl_exec($ch); - curl_close($ch); - $logisticsInfo = json_decode($data, true); - $phone = $logisticsInfo['data']['phone'] ?? ''; - Log::info("发送物流信息 orderId: {$orderId}, orderSn: {$orderSn}"); - Log::info("物流联系信息" . json_encode($logisticsInfo)); - return $phone; } public function calculate($type, $field)