init
- 框架初始化 - 安装插件 - 修复PHP8.4报错
This commit is contained in:
117
addons/shopro/library/express/Express.php
Normal file
117
addons/shopro/library/express/Express.php
Normal file
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro\library\express;
|
||||
|
||||
use think\exception\HttpResponseException;
|
||||
use app\admin\model\shopro\order\Express as ExpressModel;
|
||||
|
||||
class Express
|
||||
{
|
||||
|
||||
/**
|
||||
* 快递驱动
|
||||
*/
|
||||
protected $driver = null;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->driver = sheep_config('shop.dispatch.driver');
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 提供器
|
||||
*
|
||||
* @param string $type
|
||||
* @return Base
|
||||
*/
|
||||
public function provider($driver = null)
|
||||
{
|
||||
$driver = $driver ?: $this->getDefaultDriver();
|
||||
$class = "\\addons\\shopro\\library\\express\\provider\\" . \think\helper\Str::studly($driver);
|
||||
if (class_exists($class)) {
|
||||
return new $class($this);
|
||||
}
|
||||
|
||||
error_stop('物流平台类型不支持');
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 更新订单的所有包裹
|
||||
*
|
||||
* @param mixed $orderExpress
|
||||
* @return void
|
||||
*/
|
||||
public function updateOrderExpress($orderExpress = 0)
|
||||
{
|
||||
try {
|
||||
if ($this->driver == 'thinkapi') {
|
||||
// thinkapi 才需要查询
|
||||
if (is_numeric($orderExpress)) {
|
||||
$orderExpresses = ExpressModel::where('order_id', $orderExpress)->select();
|
||||
}
|
||||
foreach ($orderExpresses as $key => $orderExpress) {
|
||||
$this->updateExpress($orderExpress);
|
||||
}
|
||||
}
|
||||
} catch (HttpResponseException $e) {
|
||||
$data = $e->getResponse()->getData();
|
||||
$message = $data ? ($data['msg'] ?? '') : $e->getMessage();
|
||||
format_log_error($e, 'updateOrderExpress.HttpResponseException', $message);
|
||||
} catch(\Exception $e) {
|
||||
format_log_error($e, 'updateOrderExpress.HttpResponseException', '获取物流信息错误');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 更新单个包裹
|
||||
*
|
||||
* @param \think\Model $orderExpress
|
||||
* @return void
|
||||
*/
|
||||
public function updateExpress($orderExpress)
|
||||
{
|
||||
if ($this->driver == 'thinkapi' && $orderExpress->status != 'signfor') {
|
||||
// thinkapi 并且是未签收的包裹才更新
|
||||
$key = 'express:' . $orderExpress->id . ':code:' . $orderExpress->express_no; // 包裹 id 拼上 运单号
|
||||
if (cache('?'.$key)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// 查询物流信息,并且更新 express_log
|
||||
$this->provider()->search([
|
||||
'order_id' => $orderExpress['order_id'],
|
||||
'express_code' => $orderExpress['express_code'],
|
||||
'express_no' => $orderExpress['express_no']
|
||||
], $orderExpress);
|
||||
|
||||
// 缓存 300 秒
|
||||
cache($key, time(), 300);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 默认快递物流驱动
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function getDefaultDriver()
|
||||
{
|
||||
return $this->driver;
|
||||
}
|
||||
|
||||
|
||||
public function __call($funcname, $arguments)
|
||||
{
|
||||
return $this->provider()->{$funcname}(...$arguments);
|
||||
}
|
||||
|
||||
}
|
||||
196
addons/shopro/library/express/adapter/Kdniao.php
Normal file
196
addons/shopro/library/express/adapter/Kdniao.php
Normal file
@@ -0,0 +1,196 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro\library\express\adapter;
|
||||
|
||||
use fast\Http;
|
||||
|
||||
class Kdniao
|
||||
{
|
||||
// 查询接口
|
||||
const REQURL = "https://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx";
|
||||
// 订阅接口
|
||||
const SUBURL = "https://api.kdniao.com/api/dist";
|
||||
// 电子面单下单接口
|
||||
const EORDER = "https://api.kdniao.com/api/EOrderService";
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 发货人信息
|
||||
*/
|
||||
protected $sender = [];
|
||||
|
||||
/**
|
||||
* 快递鸟配置参数
|
||||
*/
|
||||
protected $config = [];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->config = sheep_config('shop.dispatch.kdniao');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 物流查询
|
||||
*
|
||||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
public function search($data)
|
||||
{
|
||||
$requestParams = $this->getRequestParams($data);
|
||||
$requestData = $this->getRequestData($requestParams);
|
||||
$requestData['RequestType'] = $this->config['type'] == 'free' ? '1002' : '8001';
|
||||
|
||||
$result = Http::post(self::REQURL, $requestData);
|
||||
|
||||
$result = $this->getResponse($result, '没有物流信息');
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 物流订阅
|
||||
*
|
||||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
public function subscribe($data)
|
||||
{
|
||||
$requestParams = $this->getRequestParams($data);
|
||||
$requestData = $this->getRequestData($requestParams);
|
||||
$requestData['RequestType'] = $this->config['type'] == 'free' ? '1008' : '8008';
|
||||
|
||||
$result = Http::post(self::SUBURL, $requestData);
|
||||
|
||||
$result = $this->getResponse($result, '订阅失败');
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
public function cancel($data)
|
||||
{
|
||||
$requestData = $data;
|
||||
|
||||
$requestData = $this->getRequestData($data);
|
||||
$requestData['RequestType'] = '1147';
|
||||
|
||||
$result = Http::post(self::EORDER, $requestData);
|
||||
|
||||
$result = $this->getResponse($result, '电子面单取消失败');
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 电子面单
|
||||
*
|
||||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
public function eOrder($data)
|
||||
{
|
||||
$requestData = $this->getRequestData($data);
|
||||
$requestData['RequestType'] = '1007';
|
||||
|
||||
$result = Http::post(self::EORDER, $requestData);
|
||||
|
||||
$result = $this->getResponse($result, '电子面单下单失败');
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 快递鸟物流推送结果处理
|
||||
*
|
||||
* @param boolean $success
|
||||
* @param string $reason
|
||||
* @return string
|
||||
*/
|
||||
public function pushResult($success, $reason)
|
||||
{
|
||||
$result = [
|
||||
"EBusinessID" => $this->config['ebusiness_id'],
|
||||
"UpdateTime" => date('Y-m-d H:i:s'),
|
||||
"Success" => $success,
|
||||
'Reason' => $reason
|
||||
];
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 组装请求数据
|
||||
*
|
||||
* @param array $requestParams
|
||||
* @return array
|
||||
*/
|
||||
private function getRequestData($requestParams)
|
||||
{
|
||||
$requestParams = is_array($requestParams) ? json_encode($requestParams, JSON_UNESCAPED_UNICODE) : $requestParams;
|
||||
|
||||
$requestData = [
|
||||
'EBusinessID' => $this->config['ebusiness_id'],
|
||||
'RequestData' => urlencode($requestParams),
|
||||
'DataType' => '2',
|
||||
];
|
||||
$requestData['DataSign'] = $this->encrypt($requestParams, $this->config['app_key']);
|
||||
|
||||
return $requestData;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 组装请求参数
|
||||
*
|
||||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
private function getRequestParams($data = [])
|
||||
{
|
||||
$params = [
|
||||
'ShipperCode' => $data['express_code'],
|
||||
'LogisticCode' => $data['express_no'],
|
||||
];
|
||||
|
||||
if ($data['express_code'] == 'JD') {
|
||||
// 京东青龙配送单号 【好像不用传了,需要验证一下】
|
||||
$params['CustomerName'] = $this->config['jd_code'];
|
||||
} else if ($data['express_code'] == 'SF') {
|
||||
// 收件人手机号后四位
|
||||
$params['CustomerName'] = $data['phone'];
|
||||
}
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 处理结果
|
||||
*
|
||||
* @param object $response
|
||||
* @param string $msg
|
||||
* @return array
|
||||
*/
|
||||
private function getResponse($result, $msg = '')
|
||||
{
|
||||
$result = json_decode($result, true);
|
||||
if (!$result['Success']) {
|
||||
error_stop($result['Reason'] ?: $msg);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
// 加签
|
||||
private function encrypt($data, $app_key)
|
||||
{
|
||||
return urlencode(base64_encode(md5($data . $app_key)));
|
||||
}
|
||||
}
|
||||
45
addons/shopro/library/express/contract/ExpressInterface.php
Normal file
45
addons/shopro/library/express/contract/ExpressInterface.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro\library\express\contract;
|
||||
|
||||
interface ExpressInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* 快递查询
|
||||
*
|
||||
* @param array $data
|
||||
* @param mixed $orderExpress
|
||||
* @return array
|
||||
*/
|
||||
public function search(array $data, $orderExpress = null);
|
||||
|
||||
|
||||
/**
|
||||
* 物流信息订阅
|
||||
*
|
||||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
public function subscribe(array $data);
|
||||
|
||||
|
||||
/**
|
||||
* 物流信息推送
|
||||
*
|
||||
* @param array $data
|
||||
*/
|
||||
public function push(array $data);
|
||||
|
||||
|
||||
/**
|
||||
* 电子面单
|
||||
*
|
||||
* @param array $data
|
||||
* @param array $items
|
||||
* @return array
|
||||
*/
|
||||
public function eOrder(array $data, $items);
|
||||
|
||||
}
|
||||
121
addons/shopro/library/express/provider/Base.php
Normal file
121
addons/shopro/library/express/provider/Base.php
Normal file
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro\library\express\provider;
|
||||
|
||||
use addons\shopro\library\express\contract\ExpressInterface;
|
||||
use app\admin\model\shopro\order\Express;
|
||||
use app\admin\model\shopro\order\ExpressLog;
|
||||
|
||||
class Base implements ExpressInterface
|
||||
{
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 快递查询
|
||||
*
|
||||
* @param array $data
|
||||
* @param mixed $order_express_id
|
||||
* @return array
|
||||
*/
|
||||
public function search(array $data, $orderExpress = 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 物流信息订阅
|
||||
*
|
||||
* @param array $data
|
||||
* @return void
|
||||
*/
|
||||
public function subscribe(array $data)
|
||||
{
|
||||
error_stop('当前快递驱动不支持物流信息订阅');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 物流信息推送
|
||||
*
|
||||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
public function push(array $data)
|
||||
{
|
||||
error_stop('当前快递驱动不支持接受推送');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 电子面单
|
||||
*
|
||||
* @param array $data
|
||||
* @param array $items
|
||||
* @return array
|
||||
*/
|
||||
public function eOrder(array $data, $items)
|
||||
{
|
||||
error_stop('当前快递驱动不支持电子面单');
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 更新包裹信息
|
||||
*
|
||||
* @param array $data
|
||||
* @param mixed $orderExpress
|
||||
* @return array
|
||||
*/
|
||||
protected function updateExpress(array $data, $orderExpress)
|
||||
{
|
||||
// 更新包裹状态
|
||||
if (is_numeric($orderExpress)) {
|
||||
$orderExpress = Express::find($orderExpress);
|
||||
}
|
||||
if ($orderExpress) {
|
||||
$orderExpress->status = $data['status'];
|
||||
$orderExpress->save();
|
||||
|
||||
$this->syncTraces($data['traces'], $orderExpress);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 更新物流信息
|
||||
*
|
||||
* @param array $traces
|
||||
* @param mixed $orderExpress
|
||||
* @return void
|
||||
*/
|
||||
protected function syncTraces($traces, $orderExpress)
|
||||
{
|
||||
// 查询现有轨迹记录
|
||||
$orderExpressLog = ExpressLog::where('order_express_id', $orderExpress->id)->select();
|
||||
|
||||
$log_count = count($orderExpressLog);
|
||||
if ($log_count > 0) {
|
||||
// 移除已经存在的记录
|
||||
array_splice($traces, 0, $log_count);
|
||||
}
|
||||
|
||||
// 增加包裹记录
|
||||
foreach ($traces as $k => $trace) {
|
||||
$orderExpressLog = new ExpressLog();
|
||||
|
||||
$orderExpressLog->user_id = $orderExpress['user_id'];
|
||||
$orderExpressLog->order_id = $orderExpress['order_id'];
|
||||
$orderExpressLog->order_express_id = $orderExpress['id'];
|
||||
$orderExpressLog->content = $trace['content'];
|
||||
$orderExpressLog->change_date = $trace['change_date'];
|
||||
$orderExpressLog->status = $trace['status'];
|
||||
$orderExpressLog->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
291
addons/shopro/library/express/provider/Kdniao.php
Normal file
291
addons/shopro/library/express/provider/Kdniao.php
Normal file
@@ -0,0 +1,291 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro\library\express\provider;
|
||||
|
||||
use think\Log;
|
||||
use think\exception\HttpResponseException;
|
||||
use addons\shopro\library\express\adapter\Kdniao as KdniaoServer;
|
||||
use app\admin\model\shopro\order\Express;
|
||||
use app\admin\model\shopro\order\ExpressLog;
|
||||
|
||||
class Kdniao extends Base
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$this->server = new KdniaoServer();
|
||||
}
|
||||
|
||||
|
||||
public $status = [
|
||||
'0' => 'noinfo',
|
||||
'1' => 'collect',
|
||||
'2' => 'transport',
|
||||
'201' => 'transport',
|
||||
'202' => 'delivery',
|
||||
'211' => 'delivery',
|
||||
'3' => 'signfor',
|
||||
'301' => 'signfor',
|
||||
'302' => 'signfor',
|
||||
'311' => 'signfor',
|
||||
'4' => 'difficulty',
|
||||
'401' => 'invalid',
|
||||
'402' => 'timeout',
|
||||
'403' => 'timeout',
|
||||
'404' => 'refuse',
|
||||
'412' => 'timeout',
|
||||
];
|
||||
|
||||
|
||||
/**
|
||||
* 快递查询
|
||||
*
|
||||
* @param array $data
|
||||
* @param mixed $orderExpress
|
||||
* @return array
|
||||
*/
|
||||
public function search($data, $orderExpress = 0)
|
||||
{
|
||||
$requestData = $this->formatRequest($data);
|
||||
$result = $this->server->search($requestData);
|
||||
|
||||
$traces = $result['Traces'] ?? [];
|
||||
$status = $result['State'];
|
||||
|
||||
// 格式化结果
|
||||
$formatResult = $this->formatResult([
|
||||
'status' => $status,
|
||||
'traces' => $traces
|
||||
]);
|
||||
|
||||
if ($orderExpress) {
|
||||
$this->updateExpress($formatResult, $orderExpress);
|
||||
}
|
||||
|
||||
return $formatResult;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 物流信息订阅
|
||||
*
|
||||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
public function subscribe($data)
|
||||
{
|
||||
$requestData = $this->formatRequest($data);
|
||||
$result = $this->server->subscribe($requestData);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function cancel($data)
|
||||
{
|
||||
$kdniao = sheep_config('shop.dispatch.kdniao');
|
||||
|
||||
$this->server->cancel([
|
||||
'ShipperCode' => $data['express_code'],
|
||||
'OrderCode' => $data['order_code'],
|
||||
'ExpNo' => $data['express_no'],
|
||||
"CustomerName" => $kdniao['customer_name'],
|
||||
"CustomerPwd" => $kdniao['customer_pwd']
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 物流信息推送
|
||||
*
|
||||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
public function push(array $data)
|
||||
{
|
||||
$success = true;
|
||||
$reason = '';
|
||||
try {
|
||||
$data = json_decode(html_entity_decode($data['RequestData']), true);
|
||||
$expressData = $data['Data'];
|
||||
|
||||
foreach ($expressData as $key => $express) {
|
||||
$orderExpress = Express::where('express_no', $express['LogisticCode'])->where('express_code', $express['ShipperCode'])->find();
|
||||
|
||||
if (!$orderExpress) {
|
||||
// 包裹不存在,记录日志信息,然后继续下一个
|
||||
Log::error('order-express-notfound:' . json_encode($express));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!$express['Success']) {
|
||||
// 失败了
|
||||
if (isset($express['Reason']) && (strpos($express['Reason'], '三天无轨迹') !== false || strpos($express['Reason'], '七天内无轨迹变化') !== false)) {
|
||||
// 需要重新订阅
|
||||
$this->subscribe([
|
||||
'express_code' => $express['ShipperCode'],
|
||||
'express_no' => $express['LogisticCode']
|
||||
]);
|
||||
}
|
||||
|
||||
Log::error('order-express-resubscribe:' . json_encode($express));
|
||||
continue;
|
||||
}
|
||||
|
||||
$traces = $express['Traces'] ?? [];
|
||||
$status = $express['State'];
|
||||
|
||||
// 格式化结果
|
||||
$formatResult = $this->formatResult([
|
||||
'status' => $status,
|
||||
'traces' => $traces
|
||||
]);
|
||||
|
||||
$this->updateExpress($formatResult, $orderExpress);
|
||||
}
|
||||
} catch (HttpResponseException $e) {
|
||||
$data = $e->getResponse()->getData();
|
||||
$reason = $data ? ($data['msg'] ?? '') : $e->getMessage();
|
||||
} catch (\Exception $e) {
|
||||
$success = false;
|
||||
$reason = $e->getMessage();
|
||||
}
|
||||
|
||||
return $this->server->pushResult($success, $reason);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 电子面单
|
||||
*
|
||||
* @param array $data
|
||||
* @param array $items
|
||||
* @return array
|
||||
*/
|
||||
public function eOrder($data, $items)
|
||||
{
|
||||
$kdniao = sheep_config('shop.dispatch.kdniao');
|
||||
if ($kdniao['type'] !== 'vip') {
|
||||
error_stop('仅快递鸟标准版接口支持电子面单功能!');
|
||||
}
|
||||
$consignee = $data['consignee'];
|
||||
$order = $data['order'];
|
||||
$sender = $data['sender'] ?? sheep_config('shop.dispatch.sender');
|
||||
if (empty($sender)) {
|
||||
error_stop('请配置默认发货人信息');
|
||||
}
|
||||
// 运单基础信息
|
||||
$requestData = [
|
||||
// 下面五个参数对应快递鸟的五个参数 https://www.yuque.com/kdnjishuzhichi/dfcrg1/hrfw43
|
||||
"CustomerName" => $kdniao['customer_name'],
|
||||
"CustomerPwd" => $kdniao['customer_pwd'],
|
||||
"MonthCode" => $kdniao['month_code'],
|
||||
"SendSite" => $kdniao['send_site'],
|
||||
"SendStaff" => $kdniao['send_staff'],
|
||||
|
||||
"ShipperCode" => $kdniao['express']['code'] ?? '',
|
||||
"PayType" => $kdniao['pay_type'],
|
||||
"ExpType" => $kdniao['exp_type'],
|
||||
"IsReturnPrintTemplate" => 0, //返回打印面单模板
|
||||
"TemplateSize" => '130', // 一联单
|
||||
"Volume" => 0,
|
||||
"OrderCode" => $order['order_sn'] . '_' . time(), // 商城订单号
|
||||
"Remark" => $order['remark'] ?? '小心轻放' // 备注
|
||||
];
|
||||
|
||||
// 发货人
|
||||
$requestData['Sender'] = [
|
||||
'Name' => $sender['name'],
|
||||
'Mobile' => $sender['mobile'],
|
||||
'ProvinceName' => $sender['province_name'],
|
||||
'CityName' => $sender['city_name'],
|
||||
'ExpAreaName' => $sender['district_name'],
|
||||
'Address' => $sender['address']
|
||||
];
|
||||
|
||||
// 收货人
|
||||
$requestData['Receiver'] = [
|
||||
"Name" => $consignee['consignee'],
|
||||
"Mobile" => $consignee['mobile'],
|
||||
"ProvinceName" => $consignee['province_name'],
|
||||
"CityName" => $consignee['city_name'],
|
||||
"ExpAreaName" => $consignee['district_name'],
|
||||
"Address" => $consignee['address']
|
||||
];
|
||||
|
||||
// 包裹信息
|
||||
$totalCount = 0;
|
||||
$totalWeight = 0;
|
||||
foreach ($items as $k => $item) {
|
||||
$goodsName = $item->goods_title . ($item->goods_sku_text ? '-' . $item->goods_sku_text : '');
|
||||
|
||||
$requestData['Commodity'][] = [
|
||||
"GoodsName" => $goodsName,
|
||||
"Goodsquantity" => $item->goods_num,
|
||||
"GoodsWeight" => $item->goods_num * $item->goods_weight
|
||||
];
|
||||
$totalCount += $item->goods_num;
|
||||
$totalWeight += $item->goods_num * $item->goods_weight;
|
||||
}
|
||||
$requestData['Quantity'] = $totalCount; // 商品数量
|
||||
$requestData['Weight'] = $totalWeight;
|
||||
|
||||
$result = $this->server->eOrder($requestData);
|
||||
if ($result['Success'] === true && $result['ResultCode'] === "100") {
|
||||
return [
|
||||
'code' => $kdniao['express']['code'],
|
||||
'name' => $kdniao['express']['name'],
|
||||
'no' => $result['Order']['LogisticCode'],
|
||||
'ext' => $result,
|
||||
'driver' => 'kdniao'
|
||||
];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 处理请求接口数据
|
||||
*
|
||||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
protected function formatRequest($data)
|
||||
{
|
||||
$requestData = [
|
||||
'express_code' => $data['express_code'] ?? '',
|
||||
'express_no' => $data['express_no'],
|
||||
'phone' => (isset($data['phone']) && $data['phone']) ? substr($data['phone'], 7) : ''
|
||||
];
|
||||
|
||||
return $requestData;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 处理返回结果
|
||||
*
|
||||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
protected function formatResult($data)
|
||||
{
|
||||
$status = $this->status[$data['status']] ?? 'noinfo';
|
||||
|
||||
$traces = [];
|
||||
foreach ($data['traces'] as $trace) {
|
||||
$action = $trace['Action'] ?? '';
|
||||
if ($action !== '') {
|
||||
$currentStatus = $this->status[$action] ?? 'noinfo';
|
||||
}
|
||||
$traces[] = [
|
||||
'content' => $trace['AcceptStation'],
|
||||
'change_date' => date('Y-m-d H:i:s', strtotime(substr($trace['AcceptTime'], 0, 19))), // 快递鸟时间格式可能是 2020-08-03 16:58:272 或者 2014/06/25 01:41:06
|
||||
'status' => $currentStatus ?? 'noinfo'
|
||||
];
|
||||
}
|
||||
|
||||
return compact('status', 'traces');
|
||||
}
|
||||
}
|
||||
110
addons/shopro/library/express/provider/Thinkapi.php
Normal file
110
addons/shopro/library/express/provider/Thinkapi.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro\library\express\provider;
|
||||
|
||||
use app\admin\model\shopro\Config;
|
||||
use app\admin\model\shopro\order\Address as OrderAddress;
|
||||
use fast\Http;
|
||||
|
||||
class Thinkapi extends Base
|
||||
{
|
||||
|
||||
protected $uri = 'https://api.topthink.com';
|
||||
|
||||
protected $appCode = '';
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->appCode = Config::getConfigField('shop.dispatch.thinkapi.app_code');
|
||||
}
|
||||
|
||||
|
||||
public $status = [
|
||||
'1' => 'noinfo',
|
||||
'2' => 'transport',
|
||||
'3' => 'delivery',
|
||||
'4' => 'signfor',
|
||||
'5' => 'refuse',
|
||||
'6' => 'difficulty',
|
||||
'7' => 'invalid',
|
||||
'8' => 'timeout',
|
||||
'9' => 'fail',
|
||||
'10' => 'back'
|
||||
];
|
||||
|
||||
|
||||
/**
|
||||
* 快递查询
|
||||
*
|
||||
* @param array $data
|
||||
* @param mixed $orderExpress
|
||||
* @return array
|
||||
*/
|
||||
public function search($data, $orderExpress = 0)
|
||||
{
|
||||
$mobile = (isset($data['mobile']) && $data['mobile']) ? $data['mobile'] : '';
|
||||
if (!$mobile && stripos($data['express_no'], 'SF') === 0 && isset($data['order_id'])) {
|
||||
// 获取手机号
|
||||
$orderAddress = OrderAddress::where('order_id', $data['order_id'])->find();
|
||||
$mobile = $orderAddress ? $orderAddress->mobile : $mobile;
|
||||
}
|
||||
|
||||
$requestData = [
|
||||
'appCode' => $this->appCode,
|
||||
'com' => 'auto',
|
||||
'nu' => $data['express_no'],
|
||||
'phone' => substr($mobile, 7)
|
||||
];
|
||||
|
||||
$result = Http::get($this->uri . '/express/query', $requestData);
|
||||
$result = is_string($result) ? json_decode($result, true) : $result;
|
||||
|
||||
if (isset($result['code']) && $result['code'] != 0) {
|
||||
$msg = $result['data']['msg'] ?? ($result['message'] ?? '');
|
||||
error_stop($msg);
|
||||
}
|
||||
|
||||
$data = $result['data'] ?? [];
|
||||
$traces = $data['data'] ?? [];
|
||||
|
||||
$status = $data['status'];
|
||||
|
||||
// 格式化结果
|
||||
$formatResult = $this->formatResult([
|
||||
'status' => $status,
|
||||
'traces' => $traces
|
||||
]);
|
||||
|
||||
if ($orderExpress) {
|
||||
$this->updateExpress($formatResult, $orderExpress);
|
||||
}
|
||||
|
||||
return $formatResult;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 处理返回结果
|
||||
*
|
||||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
protected function formatResult($data)
|
||||
{
|
||||
$status = $this->status[$data['status']] ?? 'noinfo';
|
||||
|
||||
$traces = [];
|
||||
foreach ($data['traces'] as $trace) {
|
||||
$traces[] = [
|
||||
'content' => $trace['context'],
|
||||
'change_date' => $trace['time'],
|
||||
'status' => $trace['status'] ?? $status
|
||||
];
|
||||
}
|
||||
$traces = array_reverse($traces); // 调转顺序,第一条为最开始运输信息,最后一条为最新消息
|
||||
return compact('status', 'traces');
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user