- 框架初始化
 - 安装插件
 - 修复PHP8.4报错
This commit is contained in:
2025-04-19 17:21:20 +08:00
commit c6a4e1f5f6
5306 changed files with 967782 additions and 0 deletions

View 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();
}
}
}

View 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');
}
}

View 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');
}
}