shop-php/crmeb/jobs/SendGoodsCodeJob.php

89 lines
3.2 KiB
PHP
Raw Normal View History

2023-09-08 10:55:26 +08:00
<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
namespace crmeb\jobs;
2023-09-08 12:05:27 +08:00
use app\common\repositories\store\order\StoreOrderRepository;
2023-09-08 10:55:26 +08:00
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;
2023-09-08 12:11:01 +08:00
Log::info("sendGoodsCodeJob" . json_encode($this->event));
2023-09-08 10:57:57 +08:00
Log::info("sendGoodsCodeJob ============= handle监听order_id " . $this->event['order_id']);
2023-09-08 12:02:18 +08:00
try {
2023-11-10 16:21:32 +08:00
if ($this->event['activity_type'] == 0 || in_array($this->event['source'], [0,2,103])) {
2023-09-08 12:02:18 +08:00
//发起物流信息返回快递员手机
2024-01-13 15:35:45 +08:00
if($this->event['activity_type'] !=98){
$logisticsPhone = $this->sendLogistics($this->event['order_id'], $this->event['order_sn']);
}
2023-09-08 12:02:18 +08:00
//生成用户的收货码
$this->generateLogisticsCode($this->event['uid'], $this->event['order_id'], $this->event['order_sn'], $logisticsPhone);
}
} catch (\Exception $e) {
Log::info('sendGoodsCodeJob 异常:' . $e->getMessage());
2023-09-08 10:55:26 +08:00
}
$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.
}
}