CallBackHandler.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. <?php
  2. namespace common\components\delivery\platform\shunfeng;
  3. use bizGhs\express\classes\DeliveryAuthTokenClass;
  4. use bizGhs\express\classes\GhsDeliveryOrderClass;
  5. use bizGhs\order\classes\OrderClass;
  6. use Yii;
  7. /**
  8. * 顺丰同城回调处理类
  9. * 处理顺丰平台的各种回调通知
  10. *
  11. * Class CallBackHandler
  12. * @package common\components\delivery\platform\shunfeng
  13. */
  14. class CallBackHandler
  15. {
  16. /**
  17. * 处理顺丰回调
  18. * 根据 url_index 参数判断回调类型并分发到对应的处理方法
  19. *
  20. * @param array $data 回调数据
  21. * @return bool 处理结果
  22. */
  23. public function handle($data)
  24. {
  25. try {
  26. // 记录回调原始数据
  27. Yii::info('顺丰同城回调 - 原始数据: ' . json_encode($data, JSON_UNESCAPED_UNICODE), 'shunfeng-callback');
  28. // 验证必要参数
  29. if (empty($data['url_index'])) {
  30. Yii::error('顺丰回调缺少 url_index 参数: ' . json_encode($data, JSON_UNESCAPED_UNICODE), 'shunfeng-callback');
  31. return false;
  32. }
  33. $urlIndex = $data['url_index'];
  34. // 根据回调类型分发处理
  35. switch ($urlIndex) {
  36. case 'rider_status':
  37. // 配送状态更改回调
  38. return $this->handleRiderStatus($data);
  39. case 'order_complete':
  40. // 订单完成回调
  41. return $this->handleOrderComplete($data);
  42. case 'sf_cancel':
  43. // 顺丰原因取消回调
  44. return $this->handleSfCancel($data);
  45. case 'rider_exception':
  46. // 订单异常回调
  47. return $this->handleRiderException($data);
  48. case 'rider_recall':
  49. // 骑士撤单回调
  50. return $this->handleRiderRecall($data);
  51. case 'bindnotify':
  52. // 授权状态回调
  53. return $this->handleBindNotify($data);
  54. case 'cancelbindnotify':
  55. // 取消授权状态回调
  56. return $this->handleCancelBindNotify($data);
  57. default:
  58. Yii::warning('未知的顺丰回调类型: ' . $urlIndex, 'shunfeng-callback');
  59. return false;
  60. }
  61. } catch (\Exception $e) {
  62. Yii::error('顺丰回调处理异常: ' . $e->getMessage() . ' | ' . $e->getTraceAsString(), 'shunfeng-callback');
  63. return false;
  64. }
  65. }
  66. /**
  67. * 处理配送状态更改回调
  68. * 状态包括:10-配送员接单/改派;12-配送员到店;15-配送员配送中
  69. *
  70. * @param array $data 回调数据
  71. * @return bool 处理结果
  72. */
  73. private function handleRiderStatus($data)
  74. {
  75. Yii::info('顺丰配送状态更改回调: ' . json_encode($data, JSON_UNESCAPED_UNICODE), 'shunfeng-callback');
  76. // 验证必要参数
  77. if (empty($data['sf_order_id']) || !isset($data['order_status'])) {
  78. Yii::error('顺丰配送状态回调参数不完整: ' . json_encode($data, JSON_UNESCAPED_UNICODE), 'shunfeng-callback');
  79. return false;
  80. }
  81. $orderId = $data['sf_order_id'];
  82. // 查找配送订单
  83. $deliveryOrder = GhsDeliveryOrderClass::getByCondition([
  84. 'deliveryId' => 'shunfeng',
  85. 'orderId' => $orderId
  86. ], true);
  87. if (!$deliveryOrder) {
  88. Yii::error('顺丰配送订单不存在: sf_order_id=' . $orderId, 'shunfeng-callback');
  89. return false;
  90. }
  91. // 更新配送订单信息
  92. $updateData = [];
  93. // 更新配送员信息
  94. $updateData['riderName'] = $data['operator_name'];
  95. $updateData['riderPhone'] = $data['operator_phone'];
  96. $updateData['riderLng'] = $data['rider_lng'];
  97. $updateData['riderLat'] = $data['rider_lat'];
  98. $updateData['statusDesc'] = $data['status_desc'];
  99. $updateData['pushTime'] = date('Y-m-d H:i:s', $data['push_time']);
  100. $orderStatus = $data['order_status'];
  101. // 转换顺丰订单状态到系统状态
  102. $sendStatus = $this->convertOrderStatus($orderStatus);
  103. if ($sendStatus !== null) {
  104. // 更新配送订单
  105. $deliveryOrder->orderStatus = $sendStatus;
  106. $deliveryOrder->save();
  107. // 更新主订单的配送状态
  108. $order = OrderClass::getById($deliveryOrder->ghsOrderId, true);
  109. if ($order) {
  110. $order->sendStatus = $sendStatus;
  111. $order->save();
  112. }
  113. } else {
  114. return false;
  115. }
  116. return true;
  117. }
  118. /**
  119. * 处理订单完成回调
  120. * 订单状态:17-配送员点击完成
  121. *
  122. * @param array $data 回调数据
  123. * @return bool 处理结果
  124. */
  125. private function handleOrderComplete($data)
  126. {
  127. Yii::info('顺丰订单完成回调: ' . json_encode($data, JSON_UNESCAPED_UNICODE), 'shunfeng-callback');
  128. // 验证必要参数
  129. if (empty($data['sf_order_id'])) {
  130. Yii::error('顺丰订单完成回调参数不完整: ' . json_encode($data, JSON_UNESCAPED_UNICODE), 'shunfeng-callback');
  131. return false;
  132. }
  133. $orderId = $data['sf_order_id'];
  134. // 查找配送订单
  135. $deliveryOrder = GhsDeliveryOrderClass::getByCondition([
  136. 'deliveryId' => 'shunfeng',
  137. 'orderId' => $orderId
  138. ], true);
  139. if (!$deliveryOrder) {
  140. Yii::error('顺丰配送订单不存在: sf_order_id=' . $orderId, 'shunfeng-callback');
  141. return false;
  142. }
  143. $sendStatus = $this->convertOrderStatus($data['order_status']);
  144. if ($sendStatus === null) {
  145. Yii::error('顺丰订单状态转换失败: ' . $data['order_status'], 'shunfeng-callback');
  146. return false;
  147. }
  148. $deliveryOrder->orderStatus = $sendStatus;
  149. $deliveryOrder->save();
  150. // 更新主订单状态为已送达
  151. $order = OrderClass::getById($deliveryOrder->ghsOrderId, true, 'id,sendStatus,deliveryId,purchaseId,customId,status');
  152. if ($order) {
  153. $order->sendStatus = $sendStatus;
  154. $order->save();
  155. // 确认送达,更新主订单状态
  156. if ($sendStatus == 5) {
  157. OrderClass::confirmReach($order, ['staffId' => 0, 'staffName' => '']);
  158. }
  159. }
  160. return true;
  161. }
  162. /**
  163. * 处理顺丰原因取消回调
  164. * 订单状态:2-订单取消
  165. *
  166. * @param array $data 回调数据
  167. * @return bool 处理结果
  168. */
  169. private function handleSfCancel($data)
  170. {
  171. Yii::info('顺丰取消订单回调: ' . json_encode($data, JSON_UNESCAPED_UNICODE), 'shunfeng-callback');
  172. // 验证必要参数
  173. if (empty($data['sf_order_id'])) {
  174. Yii::error('顺丰取消回调参数不完整: ' . json_encode($data, JSON_UNESCAPED_UNICODE), 'shunfeng-callback');
  175. return false;
  176. }
  177. $orderId = $data['sf_order_id'];
  178. // 查找配送订单
  179. $deliveryOrder = GhsDeliveryOrderClass::getByCondition([
  180. 'deliveryId' => 'shunfeng',
  181. 'orderId' => $orderId
  182. ], true);
  183. if (!$deliveryOrder) {
  184. Yii::error('顺丰配送订单不存在: sf_order_id=' . $orderId, 'shunfeng-callback');
  185. return false;
  186. }
  187. $sendStatus = $this->convertOrderStatus($data['order_status']);
  188. if ($sendStatus === null) {
  189. Yii::error('顺丰订单状态转换失败: ' . $data['order_status'], 'shunfeng-callback');
  190. return false;
  191. }
  192. $deliveryOrder->orderStatus = $sendStatus;
  193. $deliveryOrder->save();
  194. // 更新主订单状态为取消
  195. $order = OrderClass::getById($deliveryOrder->ghsOrderId, true, 'id,sendStatus,deliveryId,purchaseId,customId,status');
  196. if ($order) {
  197. $order->sendStatus = $sendStatus;
  198. $order->save();
  199. // 更新主订单状态为取消
  200. if ($sendStatus == -1) {
  201. OrderClass::cancelOrderSend($order, ['staffId' => 0, 'staffName' => '']);
  202. }
  203. }
  204. return true;
  205. }
  206. /**
  207. * 处理订单异常回调
  208. * 订单状态:91-骑士上报异常
  209. *
  210. * @param array $data 回调数据
  211. * @return bool 处理结果
  212. */
  213. private function handleRiderException($data)
  214. {
  215. Yii::info('顺丰订单异常回调: ' . json_encode($data, JSON_UNESCAPED_UNICODE), 'shunfeng-callback');
  216. // 验证必要参数
  217. if (empty($data['sf_order_id'])) {
  218. Yii::error('顺丰异常回调参数不完整: ' . json_encode($data, JSON_UNESCAPED_UNICODE), 'shunfeng-callback');
  219. return false;
  220. }
  221. $orderId = $data['sf_order_id'];
  222. // 查找配送订单
  223. $deliveryOrder = GhsDeliveryOrderClass::getByCondition([
  224. 'deliveryId' => 'shunfeng',
  225. 'orderId' => $orderId
  226. ], true);
  227. if (!$deliveryOrder) {
  228. Yii::error('顺丰配送订单不存在: sf_order_id=' . $orderId, 'shunfeng-callback');
  229. return false;
  230. }
  231. $sendStatus = $this->convertOrderStatus($data['order_status']);
  232. if ($sendStatus === null) {
  233. Yii::error('顺丰订单状态转换失败: ' . $data['order_status'], 'shunfeng-callback');
  234. return false;
  235. }
  236. $deliveryOrder->orderStatus = $sendStatus;
  237. $deliveryOrder->save();
  238. // 更新主订单状态为异常
  239. $order = OrderClass::getById($deliveryOrder->ghsOrderId, true, 'id,sendStatus,deliveryId,purchaseId,customId,status');
  240. if ($order) {
  241. $order->sendStatus = $sendStatus;
  242. $order->save();
  243. // 更新主订单状态为异常
  244. if ($sendStatus == -2) {
  245. OrderClass::cancelOrderSend($order, ['staffId' => 0, 'staffName' => '']);
  246. }
  247. }
  248. return true;
  249. }
  250. /**
  251. * 处理骑士撤单回调
  252. * 订单状态:22-配送员撤单
  253. *
  254. * @param array $data 回调数据
  255. * @return bool 处理结果
  256. */
  257. private function handleRiderRecall($data)
  258. {
  259. Yii::info('顺丰骑士撤单回调: ' . json_encode($data, JSON_UNESCAPED_UNICODE), 'shunfeng-callback');
  260. // 验证必要参数
  261. if (empty($data['shop_order_id'])) {
  262. Yii::error('顺丰撤单回调参数不完整: ' . json_encode($data, JSON_UNESCAPED_UNICODE), 'shunfeng-callback');
  263. return false;
  264. }
  265. $shopOrderId = $data['shop_order_id'];
  266. // 查找配送订单
  267. $deliveryOrder = GhsDeliveryOrderClass::getByCondition([
  268. 'deliveryId' => 'shunfeng',
  269. 'orderId' => $shopOrderId
  270. ], true);
  271. if (!$deliveryOrder) {
  272. Yii::error('顺丰配送订单不存在: shop_order_id=' . $shopOrderId, 'shunfeng-callback');
  273. return false;
  274. }
  275. $sendStatus = $this->convertOrderStatus($data['order_status']);
  276. if ($sendStatus === null) {
  277. Yii::error('顺丰订单状态转换失败: ' . $data['order_status'], 'shunfeng-callback');
  278. return false;
  279. }
  280. $deliveryOrder->orderStatus = $sendStatus;
  281. $deliveryOrder->save();
  282. // 更新主订单状态为派单中
  283. $order = OrderClass::getById($deliveryOrder->ghsOrderId, true);
  284. if ($order) {
  285. $order->sendStatus = $sendStatus;
  286. $order->save();
  287. }
  288. return true;
  289. }
  290. /**
  291. * 处理顺丰授权成功回调
  292. *
  293. * @param array $data
  294. * @return bool
  295. */
  296. private function handleBindNotify($data)
  297. {
  298. $sign = Yii::$app->request->get('sign');
  299. Yii::info('顺丰授权状态回调(sign=' . ($sign ?? '') . '): ' . json_encode($data, JSON_UNESCAPED_UNICODE), 'shunfeng-callback');
  300. $mainId = isset($data['out_shop_id']) ? (int)$data['out_shop_id'] : 0;
  301. $shopId = isset($data['shop_id']) ? $data['shop_id'] : '';
  302. if ($shopId === '') {
  303. Yii::error('顺丰授权状态回调参数缺失: ' . json_encode($data, JSON_UNESCAPED_UNICODE), 'shunfeng-callback');
  304. return false;
  305. }
  306. $payload = [
  307. 'mainId' => $mainId,
  308. 'platform' => 'shunfeng',
  309. 'shopId' => $shopId,
  310. 'authType' => 'all_store',
  311. 'accessToken' => $data['access_token'] ?? '',
  312. 'refreshToken' => $data['refresh_token'] ?? '',
  313. 'expiresAt' => time() + (100 * 86400 * 365),
  314. ];
  315. $existing = DeliveryAuthTokenClass::getByCondition(
  316. ['mainId' => $mainId, 'platform' => 'shunfeng'],
  317. false,
  318. false,
  319. 'id'
  320. );
  321. if ($existing && isset($existing['id'])) {
  322. DeliveryAuthTokenClass::updateById($existing['id'], [
  323. 'shopId' => $payload['shopId'],
  324. 'authType' => $payload['authType'],
  325. 'accessToken' => $payload['accessToken'],
  326. 'refreshToken' => $payload['refreshToken'],
  327. 'expiresAt' => $payload['expiresAt'],
  328. ]);
  329. } else {
  330. DeliveryAuthTokenClass::add($payload);
  331. }
  332. return true;
  333. }
  334. /**
  335. * 处理顺丰取消授权回调
  336. *
  337. * @param array $data
  338. * @return bool
  339. */
  340. private function handleCancelBindNotify($data)
  341. {
  342. $sign = Yii::$app->request->get('sign');
  343. Yii::info('顺丰取消授权状态回调(sign=' . ($sign ?? '') . '): ' . json_encode($data, JSON_UNESCAPED_UNICODE), 'shunfeng-callback');
  344. $shopId = isset($data['shop_id']) ? $data['shop_id'] : '';
  345. if ($shopId === '') {
  346. Yii::error('顺丰授权状态回调参数缺失: ' . json_encode($data, JSON_UNESCAPED_UNICODE), 'shunfeng-callback');
  347. return false;
  348. }
  349. if (!empty($data['app_id'])) {
  350. $auth = new Auth();
  351. if ((string)$auth->getAppKey() !== (string)$data['app_id']) {
  352. Yii::warning('顺丰取消授权回调 app_id 不匹配: ' . json_encode($data, JSON_UNESCAPED_UNICODE), 'shunfeng-callback');
  353. return false;
  354. }
  355. }
  356. $condition = [
  357. 'platform' => 'shunfeng',
  358. 'shopId' => $shopId
  359. ];
  360. $deleted = DeliveryAuthTokenClass::deleteByCondition($condition);
  361. if ($deleted === 0) {
  362. Yii::warning('顺丰取消授权回调未删除任何记录: ' . json_encode($data, JSON_UNESCAPED_UNICODE), 'shunfeng-callback');
  363. }
  364. return true;
  365. }
  366. /**
  367. * 转换顺丰订单状态到系统配送状态
  368. *
  369. * 顺丰状态说明:
  370. * - 10: 配送员接单/配送员改派
  371. * - 12: 配送员到店
  372. * - 15: 配送员配送中
  373. * - 17: 配送员点击完成
  374. * - 2: 订单取消
  375. * - 22: 配送员撤单
  376. * - 91: 骑士上报异常
  377. *
  378. * 系统状态说明:
  379. * - 0: 订单生成
  380. * - 1: 系统已接单
  381. * - 2: 派单中
  382. * - 3: 待取货
  383. * - 4: 配送中
  384. * - 5: 已送达
  385. * - -1: 已取消
  386. * - -2: 异常
  387. * - 10: 改派中
  388. * - 20: 已分配骑手
  389. * - 30: 骑手已到店
  390. * - 40: 申请取消中
  391. * - 50: 客服介入处理中
  392. *
  393. * @param int $sfOrderStatus 顺丰订单状态
  394. * @return int|null 系统配送状态,null表示不需要更新主订单状态
  395. */
  396. private function convertOrderStatus($sfOrderStatus)
  397. {
  398. // 顺丰状态映射到系统配送状态
  399. $statusMap = [
  400. 10 => 20, // 配送员接单/改派 -> 已分配骑手
  401. 12 => 30, // 配送员到店 -> 骑手已到店
  402. 15 => 4, // 配送员配送中 -> 配送中
  403. 17 => 5, // 配送完成 -> 已送达
  404. 2 => -1, // 订单取消 -> 已取消
  405. 22 => 2, // 配送员撤单 -> 派单中(重新派单)
  406. 91 => -2, // 骑士上报异常 -> 异常
  407. ];
  408. return $statusMap[$sfOrderStatus] ?? null;
  409. }
  410. }