6
0
Fork 0

调整物流订阅推送

release
vine_liutk 2021-12-18 13:06:48 +08:00
parent 8135c6942e
commit fb366523f4
9 changed files with 1463 additions and 1324 deletions

View File

@ -5,6 +5,8 @@ namespace App\Endpoint\Callback\Http\Controllers;
use App\Services\Kuaidi100Service;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Throwable;
class Kuaidi100Controller extends Controller
{
@ -13,14 +15,21 @@ class Kuaidi100Controller extends Controller
* @param \Illuminate\Http\Request $request
* @return void
*/
public function callback(Request $request, Kuaidi100Service $kuaidi100Service)
public function notify(Request $request, Kuaidi100Service $kuaidi100Service)
{
$data = $request->all();
//校验回调签名
if ($kuaidi100Service->unPollSign(Arr::get($data, 'sign', ''), Arr::get($data, 'param', []))) {
dd(132465);
// if ($kuaidi100Service->unPollSign(Arr::get($data, 'sign', ''), Arr::get($data, 'param', []))) {
try {
DB::beginTransaction();
$kuaidi100Service->callback($request->input('param', []));
DB::commit();
} catch (Throwable $th) {
DB::rollBack();
report($th);
}
// }
return response()->json([
'result'=>true,
'returnCode'=>'200',

View File

@ -4,6 +4,7 @@ use App\Endpoint\Callback\Http\Controllers\Kuaidi100Controller;
use App\Endpoint\Callback\Http\Controllers\WeChatPayController;
use Illuminate\Support\Facades\Route;
Route::post('kuaidi100', [Kuaidi100Controller::class, 'callback']);
//快递100物流推送
Route::post('kuaidi100', [Kuaidi100Controller::class, 'notify']);
// 微信支付通知
Route::post('wxpay/paid-notify', [WeChatPayController::class, 'paidNotify'])->name('wxpay.paid_notify');

View File

@ -2,10 +2,24 @@
namespace App\Models;
use App\Casts\JsonArray;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class KuaidiLog extends Model
{
use HasFactory;
use HasDateTimeFormatter;
protected $casts = [
'info'=>JsonArray::class,
];
protected $fillable = [
'number',
'company',
'code',
'info',
];
}

View File

@ -2,6 +2,7 @@
namespace App\Models;
use Carbon\Carbon;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
@ -11,10 +12,39 @@ class OrderPackage extends Model
use HasFactory;
use HasDateTimeFormatter;
public const STATUS_WAIT = 1;//揽收
public const STATUS_ONTHEWAY = 0;//途中
public const STATUS_DISTRIBUTE = 5;//派送
public const STATUS_CHECK = 3;//签收
public const STATUS_QUESTION = 2;//疑难:问题包裹
public const STATUS_REFUND = 4;//退签
public const STATUS_REFUSE = 6;//拒签
public const STATUS_OTHER = 10;//其他
public const STATUS_AUTOCHECK = 11;//自动签收
public static $kuaidi100StatusMap = [
'wait' => self::STATUS_WAIT,
'on_the_way' => self::STATUS_ONTHEWAY,
'distribute' => self::STATUS_DISTRIBUTE,
'check' => self::STATUS_CHECK,
'refund' => self::STATUS_REFUND,
'refuse' => self::STATUS_REFUSE,
'questions'=> self::STATUS_QUESTION,
];
protected $attributes = [
'status' => self::STATUS_WAIT,
'is_failed'=> false,
];
protected $casts = [
'is_failed' => 'bool',
];
protected $fillable = [
'remarks', 'status', 'is_failed', 'checked_at',
];
/**
* 订单
*
@ -33,4 +63,26 @@ class OrderPackage extends Model
{
return $this->hasMany(OrderPackageProduct::class, 'order_package_id');
}
public function orderProducts()
{
return $this->belongsToMany(OrderProduct::class, 'order_package_products', 'order_package_id', 'order_product_id');
}
/**
* 更新包裹物流状态
*/
public function updatePackageStatus(int $status, ?Carbon $time = null)
{
//如果是签收或者自动签收,填入签收时间; 并填入包裹商品里面的售后有效期;
$this->status = $status;
if ($status == self::STATUS_CHECK || $status == self::STATUS_AUTOCHECK) {
$this->checked_at = $time ?? now();
$this->orderProducts()->update([
'after_expire_at'=>$this->checked_at->addDays(7),
]);
}
$this->save();
}
}

View File

@ -3,6 +3,9 @@
namespace App\Services;
use App\Exceptions\BizException;
use App\Models\KuaidiLog;
use App\Models\OrderPackage;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Http;
class Kuaidi100Service
@ -13,172 +16,7 @@ class Kuaidi100Service
protected $basePath;
public function __construct()
{
$this->setAppKey();
$this->setCustomer();
$this->basePath = 'https://poll.kuaidi100.com';
}
public function setAppKey($appKey = null)
{
$this->appKey = $appKey ??config('settings.kuaidi100_app_key');
}
public function setCustomer($customer = null)
{
$this->customer = $customer ??config('settings.kuaidi100_customer');
}
/**
* 订阅推送
*
* @return void
*/
public function poll(string $company, string $number, string $phone)
{
$uri = 'poll';
$callbackUrl = config('settings.kuaidi100_callback');
if (is_null($callbackUrl)) {
throw new BizException('未配置推送回调地址');
}
$code = $this->getCode($company);
$params = [
'schema' => 'json',
'param' => [
'company' => $code,
'number' => $number,
'key' => $this->appKey,
'parameters'=> [
'callbackurl'=> $callbackUrl,
'salt' => config('settings.kuaidi100_secret'),
'phone' => $phone,
'resultv2' => '1',
'autoCom' => empty($code) ? 1 : 0,
],
],
];
$response = $this->post($uri, $params);
return $response;
}
/**
* 实时查询
*
* @param string $company
* @param string $number
* @param string $phone
* @return void
*/
public function getPollQuery(string $company, string $number, string $phone)
{
$uri = '/poll/query.do';
$code = $this->getCode($company);
$params = [
'com' => $code, //快递公司编码
'num' => $number, //快递单号
'phone' => $phone,
'order' => 'desc',
'resultv2' => '4', //开启行政区域解析
];
$postData['customer'] = $this->customer;
$postData['param'] = json_encode($params);
$postData['sign'] = $this->makeSign($params);
return $this->post($uri, $postData);
}
/**
* post请求
*
* @param string $uri
* @param array $params
* @return void
*/
public function post(string $uri, array $params)
{
$postData = $params;
$str = '';
foreach ($postData as $k=>$v) {
$str .= "$k=".$v.'&'; //默认UTF-8编码格式
}
$postData = substr($str, 0, -1);
$response = Http::withHeaders([
'Accept' => 'application/json',
])->post($this->basePath.$uri.'?'.$postData);
return $response;
}
private function makeSign($param)
{
$sign = md5(json_encode($param).$this->appKey.$this->customer);
return strtoupper($sign);
}
/**
* 解密推送回调sign
*
* @param string $sign
* @param array $params
* @return void
*/
public function unPollSign(string $sign, array $params)
{
return $sign === md5(json_encode($params).config('settings.kuaidi100_secret'));
}
public function getStatusName($state = 0)
{
$nameArr = [
1 =>'揽收',
101 =>'已下单',
102 =>'待揽收',
103 =>'已揽收',
0 =>'在途',
1001=>'到达派件城市',
1002=>'干线',
5 =>'派件',
501 =>'投柜或驿站',
3 =>'签收',
301 =>'本人签收',
302 =>'派件异常后签收',
303 =>'代签',
304 =>'投柜或站签收',
6 =>'退回',
4 =>'退签',
401 =>'已销单',
14 =>'拒签',
7 =>'转投',
2 =>'疑难',
201 =>'超时未签收',
202 =>'超时未更新',
203 =>'拒收',
204 =>'派件异常',
205 =>'柜或驿站超时未取',
206 =>'无法联系',
207 =>'超区',
208 =>'滞留',
209 =>'破损',
8 =>'清关',
10 =>'待清关',
11 =>'清关中',
12 =>'已清关',
13 =>'清关异常',
14 =>'拒签',
];
return isset($nameArr[$state]) ? $nameArr[$state] : '未知状态';
}
public function getCode($company = '')
{
if (empty($company)) {
return '';
}
$codeArr = [
public static $codeArr = [
'圆通速递'=>'yuantong',
'韵达快递'=>'yunda',
'韵达速递'=>'yunda',
@ -1479,6 +1317,219 @@ class Kuaidi100Service
'转瞬达集运'=>'zsda56',
'准实快运'=>'zsky123',
];
public function __construct()
{
$this->setAppKey();
$this->setCustomer();
$this->basePath = 'https://poll.kuaidi100.com';
}
public function setAppKey($appKey = null)
{
$this->appKey = $appKey ??config('settings.kuaidi100_app_key');
}
public function setCustomer($customer = null)
{
$this->customer = $customer ??config('settings.kuaidi100_customer');
}
/**
* 订阅推送
*
* @return void
*/
public function poll(string $number, string $code, string $phone)
{
$uri = '/poll';
$callbackUrl = config('settings.kuaidi100_callback');
if (empty($callbackUrl)) {
throw new BizException('未配置推送回调地址');
}
$params = [
'schema' => 'json',
'param' => json_encode([
'company' => $code,
'number' => $number,
'key' => $this->appKey,
'parameters'=> [
'callbackurl'=> $callbackUrl,
'salt' => config('settings.kuaidi100_secret'),
'phone' => $phone,
'resultv2' => '1',
'autoCom' => empty($code) ? 1 : 0,
],
]),
];
$response = $this->post($uri, $params);
$resData = $response->json();
if (!($resData['returnCode'] == 200 || $resData['returnCode'] == 501)) {
throw new BizException('订阅物流消息失败:'.$resData['message']);
}
return $response;
}
/**
* 处理回调推送
*
* @param array $params
* @return void
*/
public function callback(array $params)
{
//最新的物流结果
$lastRsult = Arr::get($params, 'lastResult');
$shippingNumber = Arr::get($lastRsult, 'nu');
$shippingCode = Arr::get($lastRsult, 'com', '');
if ($shippingNumber) {
//找到有这个运单号的包裹
$packages = OrderPackage::where('shipping_number', $shippingNumber)->get();
$status = Arr::get($lastRsult, 'state', null);
if (!is_null($status)) {
//处理非预期内的快递100的物流状态
if (!in_array($status, OrderPackage::$kuaidi100StatusMap)) {
$status = OrderPackage::STATUS_OTHER;
}
//更新状态
foreach ($packages as $package) {
$package->updatePackageStatus($status);
}
}
//记录物流信息
KuaidiLog::updateOrCreate([
'number' => $shippingNumber,
'code' => $shippingCode,
], [
'info'=>Arr::get($lastRsult, 'data', []),
]);
}
}
/**
* 实时查询
*
* @param string $company
* @param string $number
* @param string $phone
* @return void
*/
public function getPollQuery(string $company, string $number, string $phone)
{
$uri = '/poll/query.do';
$code = $this->getCode($company);
$params = [
'com' => $code, //快递公司编码
'num' => $number, //快递单号
'phone' => $phone,
'order' => 'desc',
'resultv2' => '4', //开启行政区域解析
];
$postData['customer'] = $this->customer;
$postData['param'] = json_encode($params);
$postData['sign'] = $this->makeSign($params);
return $this->post($uri, $postData);
}
/**
* post请求
*
* @param string $uri
* @param array $params
* @return
*/
public function post(string $uri, array $params)
{
$postData = $params;
$str = '';
foreach ($postData as $k=>$v) {
$str .= "$k=".$v.'&'; //默认UTF-8编码格式
}
$postData = substr($str, 0, -1);
$response = Http::withHeaders([
'Accept' => 'application/json',
])->post($this->basePath.$uri.'?'.$postData);
if (!$response->successful()) {
throw new BizException('第三方快递请求失败');
}
return $response;
}
private function makeSign($param)
{
$sign = md5(json_encode($param).$this->appKey.$this->customer);
return strtoupper($sign);
}
/**
* 解密推送回调sign
*
* @param string $sign
* @param array $params
* @return void
*/
public function unPollSign(string $sign, array $params)
{
return $sign === md5(json_encode($params).config('settings.kuaidi100_secret'));
}
public function getStatusName($state = 0)
{
$nameArr = [
1 =>'揽收',
101 =>'已下单',
102 =>'待揽收',
103 =>'已揽收',
0 =>'在途',
1001=>'到达派件城市',
1002=>'干线',
5 =>'派件',
501 =>'投柜或驿站',
3 =>'签收',
301 =>'本人签收',
302 =>'派件异常后签收',
303 =>'代签',
304 =>'投柜或站签收',
6 =>'退回',
4 =>'退签',
401 =>'已销单',
14 =>'拒签',
7 =>'转投',
2 =>'疑难',
201 =>'超时未签收',
202 =>'超时未更新',
203 =>'拒收',
204 =>'派件异常',
205 =>'柜或驿站超时未取',
206 =>'无法联系',
207 =>'超区',
208 =>'滞留',
209 =>'破损',
8 =>'清关',
10 =>'待清关',
11 =>'清关中',
12 =>'已清关',
13 =>'清关异常',
14 =>'拒签',
];
return isset($nameArr[$state]) ? $nameArr[$state] : '未知状态';
}
public function getCode($company = '')
{
if (empty($company)) {
return '';
}
$codeArr = self::$codeArr;
return isset($codeArr[$company]) ? $codeArr[$company] : '';
}
}

View File

@ -18,6 +18,7 @@ use App\Models\ProductSku;
use App\Models\ShippingAddress;
use App\Models\User;
use App\Models\UserCoupon;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
class OrderService
@ -697,13 +698,23 @@ class OrderService
$package->consignee_address = $order->consignee_address;
$package->shipping_company = $params['shipping_company'];
$package->shipping_code = Arr::get(Kuaidi100Service::$codeArr, $package->shipping_company, '');
$package->shipping_number = $params['shipping_number'];
//保存发货单
$package->save();
//保存发货单商品
$package->packageProducts()->saveMany(array_map(function ($value) {
return new OrderPackageProduct($value);
OrderPackageProduct::insert(array_map(function ($value) use ($package) {
return array_merge($value, [
'order_package_id' => $package->id,
'created_at' => $package->created_at,
'updated_at' => $package->created_at,
]);
}, $packageProducts));
if (config('settings.kuaidi100_is_use')) {
$kuaidi100Service = new Kuaidi100Service();
$kuaidi100Service->poll($package->shipping_number, $package->shipping_code, $package->consignee_telephone);
}
//更新订单状态
if (!OrderProduct::where('order_id', $order->id)->where('remain_quantity', '>', 0)->exists()) {

View File

@ -22,11 +22,10 @@ return [
// 快递100是否开启
'kuaidi100_is_use' => true,
'kuaidi100_callback'=> '',
'kuaidi100_callback'=> env('APP_URL', '').'/callback/kuaidi100',
'kuaidi100_app_key'=> 'BTvgbjti4727',
'kuaidi100_customer'=> '064109188EC4D85DA655DFC342144C6A',
'kuaidi100_secret'=> '1bd287d1981749f2a30ea74cac0ab99c',
'kuaidi100_userid'=> 'ec0b6ec7729d4f22824cfd3c519dd45b',
];

View File

@ -21,12 +21,15 @@ class CreateOrderPackagesTable extends Migration
$table->string('consignee_zone')->nullable()->comment('收货人所在地区');
$table->string('consignee_address')->nullable()->comment('收货人详细地址');
$table->string('shipping_company')->nullable()->comment('快递公司');
$table->string('shipping_code')->nullable()->comment('快递编码');
$table->string('shipping_number')->nullable()->comment('快递单号');
$table->unsignedTinyInteger('is_failed')->default(0)->comment('是否作废');
$table->unsignedTinyInteger('status')->default(0)->comment('快递状态:0在途1揽收2疑难3签收4退签5派件6退回');
$table->timestamp('inspected_at')->nullable()->comment('签收时间');
$table->unsignedTinyInteger('status')->default(0)->comment('快递状态:0在途1揽收2疑难3签收4退签5派件14拒签');
$table->timestamp('checked_at')->nullable()->comment('签收时间');
$table->string('remarks')->nullable()->comment('备注');
$table->timestamps();
$table->index('shipping_number');
});
}

View File

@ -15,13 +15,12 @@ class CreateKuaidiLogsTable extends Migration
{
Schema::create('kuaidi_logs', function (Blueprint $table) {
$table->id();
$table->string('shipping_number')->comment('快递单号');
$table->string('shipping_company')->comment('快递公司');
$table->string('shipping_code')->nullable()->comment('快递公司编号');
$table->json('shipping_info')->nullable()->comment('物流情况明细');
$table->string('number')->comment('快递单号');
$table->string('code')->nullable()->comment('快递编码');
$table->json('info')->nullable()->comment('物流情况明细');
$table->timestamps();
$table->index('shipping_number');
$table->index('number');
});
}