SfExpress.php 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace app\controller\notify;
  3. use app\extra\basic\Base;
  4. use app\model\saas\SaasOrder;
  5. use app\model\saas\SaasOrderExpress;
  6. use app\model\saas\SaasOrderLog;
  7. use LinFly\Annotation\Route\Controller;
  8. use LinFly\Annotation\Route\Route;
  9. use support\Request;
  10. use support\Response;
  11. #[Controller(prefix: "/sf")]
  12. class SfExpress extends Base
  13. {
  14. protected array $opCode = [
  15. 50 => "已揽件",
  16. 30 => "运输中",
  17. 31 => "运输中",
  18. 44 => "运输中",
  19. 80 => "已签收"
  20. ];
  21. protected array $whiteCode = [50,30,31,44,80];
  22. /**
  23. *
  24. * @param Request $request
  25. * @param $id
  26. * @return Response
  27. */
  28. #[Route(path: "hook/{id}",methods: ['get','post'])]
  29. public function getHook(Request $request,$id): Response
  30. {
  31. try {
  32. if ($request->method() == "GET") {
  33. $data = $request->get();
  34. } else {
  35. $data = $request->post();
  36. }
  37. if (!empty($data['orderState'])){
  38. foreach ($data['orderState'] as $val) {
  39. $order = (new SaasOrder)->where("express_id",$val['waybillNo'])->findOrEmpty();
  40. if ($order->isEmpty()) return json(['code' => 0,"success" => true,"msg" => ""]);
  41. if ($val['empCode'] == 500963) {
  42. (new SaasOrderExpress)->insertGetId(['order_id' => $order['order_sn'],'express_id' => $val['waybillNo'],'content' => "顺丰快递",'title' => "系统已接收"]);
  43. (new SaasOrderLog)->insertGetId(['order_id' => $order['order_sn'],'remark' => "顺丰快递",'title' => "系统已接收"]);
  44. }
  45. }
  46. }
  47. if (!empty($data['Body']['WaybillRoute'])){
  48. foreach ($data['Body']['WaybillRoute'] as $val) {
  49. if (in_array($val['opCode'],$this->whiteCode)) {
  50. $order = (new SaasOrder)->where("express_id",$val['mailno'])->with(['store' => function($query){
  51. $query->field('store_id,store_name,appid,secret,express_time,order_end');
  52. }])->findOrEmpty();
  53. if ($order->isEmpty()) return json(['code' => 0,"success" => true,"msg" => ""]);
  54. if ($order['status'] >= 2) return json(['code' => 0,"success" => true,"msg" => ""]);
  55. (new SaasOrderExpress)->insertGetId(['order_id' => $order['order_sn'],'express_id' => $val['mailno'],'content' => $val['remark'],'title' => $this->opCode[$val['opCode']]??'']);
  56. (new SaasOrderLog)->insertGetId(['order_id' => $order['order_sn'],'remark' => $val['remark'],'title' => $this->opCode[$val['opCode']]??'' ]);
  57. if ($val['opCode'] == 50 && $order['store']['order_end'] == "2") { // 已揽收,发起核销
  58. // $val['acceptAddress'] 发货地址
  59. $this->orderDone($order->toArray(),1);
  60. $order->status = 2;
  61. $order->save();
  62. }
  63. if ($val['opCode'] == 80 && $order['store']['order_end'] == "3") { // 已签收
  64. // $val['acceptAddress'] 发货地址
  65. $this->orderDone($order->toArray(),2);
  66. $order->status = 2;
  67. $order->save();
  68. }
  69. }
  70. }
  71. }
  72. return json(['code' => 0,"success" => true,"msg" => ""]);
  73. } catch (\Throwable $throwable) {
  74. return error($throwable->getMessage());
  75. }
  76. }
  77. protected function orderDone(array $data = [],int $type = 1)
  78. {
  79. }
  80. }