SfExpress.php 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. /**
  22. *
  23. * @param Request $request
  24. * @param $id
  25. * @return Response
  26. */
  27. #[Route(path: "hook/{id}",methods: ['get','post'])]
  28. public function getHook(Request $request,$id): Response
  29. {
  30. try {
  31. if ($request->method() == "GET") {
  32. $data = $request->get();
  33. } else {
  34. $data = $request->post();
  35. }
  36. if (!empty($data['orderState'])){
  37. foreach ($data['orderState'] as $val) {
  38. $order = (new SaasOrder)->where("express_id",$val['waybillNo'])->findOrEmpty();
  39. if ($order->isEmpty()) return json(['code' => 0,"success" => true,"msg" => ""]);
  40. if ($val['empCode'] == 500963) {
  41. (new SaasOrderExpress)->insertGetId(['order_id' => $order['order_sn'],'express_id' => $val['waybillNo'],'content' => "顺丰快递",'title' => "系统已接收"]);
  42. (new SaasOrderLog)->insertGetId(['order_id' => $order['order_sn'],'remark' => "顺丰快递",'title' => "系统已接收"]);
  43. }
  44. }
  45. }
  46. if (!empty($data['Body']['WaybillRoute'])){
  47. foreach ($data['Body']['WaybillRoute'] as $val) {
  48. $order = (new SaasOrder)->where("express_id",$val['mailno'])->with(['store' => function($query){
  49. $query->field('store_id,store_name,appid,secret,express_time,order_end');
  50. }])->findOrEmpty();
  51. if ($order->isEmpty()) return json(['code' => 0,"success" => true,"msg" => ""]);
  52. if ($order['status'] >= 2) return json(['code' => 0,"success" => true,"msg" => ""]);
  53. (new SaasOrderExpress)->insertGetId(['order_id' => $order['order_sn'],'express_id' => $val['mailno'],'content' => $val['remark'],'title' => $this->opCode[$val['opCode']]??'']);
  54. (new SaasOrderLog)->insertGetId(['order_id' => $order['order_sn'],'remark' => $val['remark'],'title' => $this->opCode[$val['opCode']]??'' ]);
  55. if ($val['opCode'] == 50 && $order['store']['order_end'] == "2") { // 已揽收,发起核销
  56. // $val['acceptAddress'] 发货地址
  57. $this->orderDone($order->toArray(),1);
  58. $order->status = 2;
  59. $order->save();
  60. }
  61. if ($val['opCode'] == 80 && $order['store']['order_end'] == "3") { // 已签收
  62. // $val['acceptAddress'] 发货地址
  63. $this->orderDone($order->toArray(),2);
  64. $order->status = 2;
  65. $order->save();
  66. }
  67. }
  68. }
  69. return json(['code' => 0,"success" => true,"msg" => ""]);
  70. } catch (\Throwable $throwable) {
  71. return error($throwable->getMessage());
  72. }
  73. }
  74. protected function orderDone(array $data = [],int $type = 1)
  75. {
  76. }
  77. }