|
|
<?php
namespace App\Controller\v3;use App\Constants\v3\OrderState;use App\Controller\BaseController;use App\Model\v3\Order;use App\Model\v3\OrderMain;use Hyperf\DbConnection\Db;use Psr\Http\Message\ResponseInterface;
/** * 顺丰同城回调 */class SfExpressController extends BaseController{ private string $dev_id; private string $dev_key;// private string $shop_id;
public function __construct() { parent::__construct(); //此句不能少,否则$this->request和$this->response获取不到
$this->dev_id = env('SF_EXPRESS_DEV_ID'); $this->dev_key = env('SF_EXPRESS_DEV_KEY');// $this->shop_id = env('SF_EXPRESS_SHOP_ID');
}
/** * 配送状态更改回调 */ public function riderStatus(): ResponseInterface { if (!$this->checkSign($this->request->query('sign'))) { return $this->response->json([ 'error_code' => 500, 'error_msg' => '签名错误', ]); }
$formData = $this->request->all(); if (empty($formData['shop_order_id'])) { return $this->response->json([ 'error_code' => 500, 'error_msg' => '无效的shop_order_id', ]); }
# 主订单表
$orderMain = OrderMain::whereIn('state', [OrderState::PAID, OrderState::DELIVERY]) ->where('global_order_id', $formData['shop_order_id']) ->first();
if (!$orderMain) { return $this->response->json([ 'error_code' => 500, 'error_msg' => '订单不存在', ]); }
DB::beginTransaction(); try { # 主订单表
$orderMain->state = OrderState::DELIVERY; //order_status 订单状态 10-配送员确认;12:配送员到店;15:配送员配送中
if (!empty($formData['order_status'])) { if ($formData['order_status'] == 10 && $orderMain->receive_time == 0) { $orderMain->receive_time = time(); // 接单时间
} else if ($formData['order_status'] == 12 && $orderMain->delivery_start_time == 0) { $orderMain->delivery_start_time = time(); // 开始配送时间
} } $orderMain->save();
# 子订单表
Order::where('order_main_id', $formData['shop_order_id']) ->update(['state' => $orderMain->state]);
DB::commit(); } catch (\Exception $exception) { DB::rollBack(); return $this->response->json([ 'error_code' => 500, 'error_msg' => $exception->getMessage(), ]); }
return $this->response->json([ 'error_code' => 0, 'error_msg' => 'success', ]); }
/** * 骑士撤单状态回调 */ public function riderRecall(): ResponseInterface { if (!$this->checkSign($this->request->query('sign'))) { return $this->response->json([ 'error_code' => 500, 'error_msg' => '签名错误', ]); }
$formData = $this->request->all(); if (empty($formData['shop_order_id'])) { return $this->response->json([ 'error_code' => 500, 'error_msg' => '无效的shop_order_id', ]); }
//order_status 22-配送员撤单
//TODO 因目前订单表没有相关撤单的字段,故目前不做处理,仅仅返回成功给顺丰
return $this->response->json([ 'error_code' => 0, 'error_msg' => 'success', ]); }
/** * 订单完成回调 */ public function orderComplete(): ResponseInterface { if (!$this->checkSign($this->request->query('sign'))) { return $this->response->json([ 'error_code' => 500, 'error_msg' => '签名错误', ]); }
$formData = $this->request->all(); if (empty($formData['shop_order_id'])) { return $this->response->json([ 'error_code' => 500, 'error_msg' => '无效的shop_order_id', ]); }
# 主订单表
$orderMain = OrderMain::where('state', '>', OrderState::UNPAID) ->where('global_order_id', $formData['shop_order_id']) ->first();
if (!$orderMain) { return $this->response->json([ 'error_code' => 500, 'error_msg' => '订单不存在', ]); }
DB::beginTransaction(); try { # 主订单表
$orderMain->state = OrderState::COMPLETED; $orderMain->complete_time = time(); $orderMain->delivery_time = time(); $orderMain->save();
# 子订单表
Order::where('order_main_id', $formData['shop_order_id']) ->update(['state' => $orderMain->state]);
DB::commit(); } catch (\Exception $exception) { DB::rollBack(); return $this->response->json([ 'error_code' => 500, 'error_msg' => $exception->getMessage(), ]); }
return $this->response->json([ 'error_code' => 0, 'error_msg' => 'success', ]); }
/** * 顺丰原因订单取消回调 */ public function sfCancel(): ResponseInterface { //TODO 因目前订单表没有相关撤单的字段,故目前不做处理,仅仅返回成功给顺丰
return $this->riderRecall(); }
/** * 订单异常回调 */ public function riderException(): ResponseInterface { /** ex_id枚举值: 4003:托寄物丢失或损坏 1001:商家出货慢 2010:顾客拒绝实名认证 3004:实名认证校验失败 1007:更改取货地址 2001:顾客电话无法接通 2004:更改期望送达时间 2005:顾客拒收 2008:顾客不在家 2009:更改送货地址 4001:配送地址错误 4002:其他 */ //TODO 因目前订单表没有相关撤单的字段,故目前不做处理,仅仅返回成功给顺丰
return $this->riderRecall(); }
/** * 回调签名校验 */ private function checkSign(?string $checkSign): bool { $post_data = $this->request->getBody()->getContents(); echo PHP_EOL, '$post_data', PHP_EOL; print_r($post_data); $sign_char = $post_data . "&{$this->dev_id}&{$this->dev_key}"; return $checkSign == base64_encode(md5($sign_char)); }}
|