// +---------------------------------------------------------------------- namespace crmeb\jobs; use app\common\repositories\store\order\StoreOrderRepository; use crmeb\interfaces\JobInterface; use think\facade\Log; use think\facade\Db; use think\queue\Job; class SendGoodsCodeJob implements JobInterface { public $event; public function fire($job, $data) { $this->event = $data; Log::info("sendGoodsCodeJob" . json_encode($this->event)); Log::info("sendGoodsCodeJob ============= handle监听order_id " . $this->event['order_id']); try { if ($this->event['activity_type'] == 0) { //发起物流信息返回快递员手机 $logisticsPhone = $this->sendLogistics($this->event['order_id'], $this->event['order_sn']); //生成用户的收货码 $this->generateLogisticsCode($this->event['uid'], $this->event['order_id'], $this->event['order_sn'], $logisticsPhone); //记录订单收货地址记录 $this->recordOrderAddr($this->event); } } catch (\Exception $e) { Log::info('sendGoodsCodeJob 异常:' . $e->getMessage()); } $job->delete(); } //订单收货记录 public function recordOrderAddr($order) { //province_code . city_code . district_code . street_code . village_code . brigade_id; //设置地址信息 $addressInfo = explode(',', $order['user_address_code'] ?? ''); $productOrder = [ 'uid' => $order['uid'] ?? 0, 'order_id' => $order['order_id'] ?? 0, 'province_code' => $addressInfo[0] ?? '', 'city_code' => $addressInfo[1] ?? '', 'district_code' => $addressInfo[2] ?? '', 'street_code' => $addressInfo[3] ?? '', 'village_code' => $addressInfo[4] ?? '', 'brigade_id' => $addressInfo[5] ?? 0, 'status' => 1, 'create_time' => date('Y-m-d H:i:s') ]; //商品信息 $productInfo = Db::name('store_order_product')->where('order_id', $order['order_id'] ?? 0)->find(); if ($productInfo) { $productOrder['product_id'] = $productInfo['product_id'] ?? 0; $productOrder['product_price'] = $productInfo['product_price'] ?? 0; $productOrder['total_price'] = $productInfo['total_price'] ?? 0; $productOrder['product_num'] = $productInfo['product_num'] ?? 0; } //商户信息 $merchantInfo = Db::name('merchant')->where('mer_id', $order['mer_id'] ?? 0)->find(); if ($merchantInfo) { $productOrder['mer_id'] = $merchantInfo['mer_id'] ?? 0; $productOrder['mer_category_id'] = $merchantInfo['category_id'] ?? 0; $productOrder['mer_type_id'] = $merchantInfo['type_id'] ?? 0; $productOrder['is_trader'] = $merchantInfo['is_trader'] ?? 0; } Db::name('ProductOrderLog')->insert($productOrder); } //用户收货码 public function generateLogisticsCode($uid, $orderId, $orderSn, $logisticsPhone) { $code = random_int(1000, 9999); app()->make(StoreOrderRepository::class)->update($orderId, [ 'logistics_code' => $code, 'logistics_phone' => $logisticsPhone ]); } //发送物流 public function sendLogistics($orderId, $orderSn) { $postUrl = env('LOGISTICS_HOST_URL') . '/api/lstSet'; Log::info("物流HOST: {$postUrl}"); Log::info("发送物流信息 orderId: {$orderId}, orderSn: {$orderSn}"); $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; } public function failed($data) { // TODO: Implement failed() method. } }