// +---------------------------------------------------------------------- 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 || in_array($this->event['source'], [0,2,103])) { //发起物流信息返回快递员手机 $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); } } catch (\Exception $e) { Log::info('sendGoodsCodeJob 异常:' . $e->getMessage()); } $job->delete(); } //用户收货码 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. } }