110 lines
3.3 KiB
PHP
110 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace app\common\library\app\physical\express;
|
|
|
|
use think\exception\HttpResponseException;
|
|
use app\common\model\app\physical\OrderExpress;
|
|
|
|
class Express
|
|
{
|
|
|
|
protected $driver = null;
|
|
protected $config = [];
|
|
|
|
public function __construct($driver = 'aliyun', $config = [])
|
|
{
|
|
$this->driver = $driver;
|
|
$this->config = $config;
|
|
}
|
|
|
|
public function provider($driver = null)
|
|
{
|
|
$driver = $driver ?: $this->getDefaultDriver();
|
|
$class = "\\app\\common\\library\\app\\physical\\express\\provider\\" . \think\helper\Str::studly($driver);
|
|
if (class_exists($class)) {
|
|
return new $class($this->config);
|
|
}
|
|
|
|
throw new \Exception('物流平台类型不支持');
|
|
}
|
|
|
|
/**
|
|
* 更新订单的所有包裹物流信息
|
|
*
|
|
* @param mixed $orderExpress 订单ID或发货单ID
|
|
* @return void
|
|
*/
|
|
public function updateOrderExpress($orderExpress = 0)
|
|
{
|
|
try {
|
|
// 如果传入的是订单ID,获取该订单的所有发货单
|
|
if (is_numeric($orderExpress)) {
|
|
$orderExpresses = OrderExpress::where('order_id', $orderExpress)
|
|
->where('uniacid', UNIACID)
|
|
->select();
|
|
} else {
|
|
$orderExpresses = [$orderExpress];
|
|
}
|
|
|
|
// 遍历更新每个发货单的物流信息
|
|
foreach ($orderExpresses as $orderExpress) {
|
|
$this->updateExpress($orderExpress);
|
|
}
|
|
} catch (HttpResponseException $e) {
|
|
$data = $e->getResponse()->getData();
|
|
$message = $data ? ($data['msg'] ?? '') : $e->getMessage();
|
|
\think\Log::error('updateOrderExpress.HttpResponseException: ' . $message);
|
|
} catch(\Exception $e) {
|
|
\think\Log::error('updateOrderExpress.Exception: 获取物流信息错误 - ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 更新单个包裹的物流信息
|
|
*
|
|
* @param \think\Model $orderExpress 发货单模型
|
|
* @return bool
|
|
*/
|
|
public function updateExpress($orderExpress)
|
|
{
|
|
// 只有未签收的包裹才更新物流
|
|
if ($orderExpress->status == 'signfor') {
|
|
return true;
|
|
}
|
|
|
|
// 检查缓存,避免频繁查询(5分钟缓存)
|
|
$key = 'express:' . $orderExpress->id . ':code:' . $orderExpress->express_no;
|
|
if (cache('?'.$key)) {
|
|
return true;
|
|
}
|
|
|
|
try {
|
|
// 查询物流信息
|
|
$this->provider()->search([
|
|
'order_id' => $orderExpress['order_id'],
|
|
'express_code' => $orderExpress['express_code'],
|
|
'express_no' => $orderExpress['express_no'],
|
|
'mobile' => $orderExpress['sender_mobile'] ?? ''
|
|
], $orderExpress);
|
|
|
|
// 缓存 300 秒(5分钟)
|
|
cache($key, time(), 300);
|
|
|
|
return true;
|
|
} catch (\Exception $e) {
|
|
\think\Log::error('updateExpress.Exception: ' . $e->getMessage());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public function getDefaultDriver()
|
|
{
|
|
return $this->driver;
|
|
}
|
|
|
|
public function __call($funcname, $arguments)
|
|
{
|
|
return $this->provider()->{$funcname}(...$arguments);
|
|
}
|
|
}
|