DbOpenService.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace App\Services;
  3. use App\Components\ErrorPush;
  4. use App\Traits\ServiceAppAop;
  5. use Illuminate\Database\Eloquent\Model;
  6. class DbOpenService
  7. {
  8. use ServiceAppAop, ErrorPush;
  9. protected $modelClass = DbOpenService::class;
  10. /**
  11. * 创建德邦订单,生成快递单号
  12. * @param Model|\stdClass $waybill
  13. *
  14. * @return string|null
  15. * @throws \Throwable
  16. */
  17. public function getDbOrderNo($waybill):?array
  18. {
  19. $waybill->loadCount(["waybillAuditLogs"=>function($query){
  20. $query->where("audit_stage","发起德邦调度");
  21. }]);
  22. if ($waybill->waybill_audit_logs_count>0)return null;
  23. $header = [
  24. 'Content-Type' => 'application/x-www-form-urlencoded;charset=utf-8',
  25. "Accept" => "application/json"
  26. ];
  27. $body = $this->formatWaybillData($waybill);
  28. try {
  29. $response = httpPost(config('api_logistic.DB.prod.uri')['create_order'], $body, $header);
  30. if (!$response["result"])return null;
  31. return $response ?? '';
  32. }catch (\Exception $e){
  33. $this->push(__METHOD__."->".__LINE__,"德邦接口请求失败",$e->getMessage() . ' | '.json_encode($response ?? ''));
  34. return null;
  35. }
  36. }
  37. /**
  38. * 根据 德邦运单号 获取 订单物流轨迹
  39. * @param array $params
  40. * @return array
  41. */
  42. public function getOrderStatus(array $params = []):array
  43. {
  44. if ( ($params['mailNo']??'') == '') return ['code' => 0 , 'msg' => '德邦运单号不能为空'];
  45. $header = [
  46. 'Content-Type' => 'application/x-www-form-urlencoded;charset=utf-8',
  47. "Accept" => "application/json"
  48. ];
  49. $data = [
  50. 'mailNo' => $params['mailNo']??''
  51. ];
  52. $param = json_encode($data, 1);
  53. $dd["params"] = $param;
  54. $dd["timestamp"] = (integer)getMillisecond();
  55. $dd["digest"] = base64_encode(md5($param . config('api_logistic.DB.prod.app_key') . $dd['timestamp']));
  56. $dd["companyCode"] = config('api_logistic.DB.prod.company_code');
  57. $return = httpPost(config('api_logistic.DB.prod.uri')['order_locus'], $dd, $header);
  58. if (array_key_exists('result', $return) && $return['result'] == 'true' && array_key_exists('resultCode', $return) && $return['resultCode'] == '1000'){
  59. return ['code'=> 1, 'msg'=> '正在加载中。。。', 'data' => $return['responseParam']];
  60. }
  61. return ['code' => 0, 'msg' => '暂无物流信息'];
  62. }
  63. /**
  64. * @param Model|\stdClass $waybill
  65. */
  66. private function formatWaybillData($waybill):array
  67. {
  68. $waybill->loadMissing([
  69. "order.shop","owner","order.warehouse.province","order.warehouse.city","order.warehouse.county","deliveryType"
  70. ]);
  71. $date = date('Y-m-d H:i:s', $waybill->deliver_at ? strtotime($waybill->deliver_at) : now()->getTimestamp());
  72. $data = [
  73. 'logisticID' => config('api_logistic.DB.prod.sign').date("YmdHis").mt_rand(1000, 9999).$waybill->order->id,
  74. 'custOrderNo' => $waybill->order->client_code ?? '',
  75. 'needTraceInfo' => config('api_logistic.DB.prod.needTraceInfo'),
  76. 'companyCode' => config('api_logistic.DB.prod.company_code'),
  77. 'orderType' => $waybill->order_type,
  78. 'transportType' => $waybill->transport_type,
  79. 'customerCode' => config('api_logistic.DB.prod.customer_Code'),
  80. 'sender' => [
  81. 'companyName' => '宝时物流',
  82. 'businessNetworkNo' => '',
  83. 'name' => '宝时物流',
  84. 'mobile' => '',
  85. 'phone' => '021-6316561',
  86. 'province' => $waybill->order->warehouse->province->name ?? '',
  87. 'city' => $waybill->order->warehouse->city->name ?? '',
  88. 'country' => $waybill->order->warehouse->county->name ?? '',
  89. 'town' => '',
  90. 'address' => $waybill->order->warehouse->address ?? '',
  91. ],
  92. 'receiver' => [
  93. 'toNetworkNo' => '',
  94. 'name' => $waybill->order->consignee_name ?? '',
  95. 'phone' => $waybill->order->consignee_phone ?? '',
  96. 'mobile' => $waybill->order->consignee_phone ?? '',
  97. 'province' => $waybill->order->province ?? '',
  98. 'city' => $waybill->order->city ?? "",
  99. 'county' => $waybill->order->district ?? '',
  100. 'town' => '',
  101. 'address' => $waybill->order->address ?? '',
  102. 'companyName' => ''
  103. ],
  104. 'packageInfo' => [
  105. 'cargoName' => $waybill->cargo_name ?? '',
  106. 'totalNumber' => $waybill->total_number ?? '',
  107. 'totalWeight' => $waybill->total_weight ?? '',
  108. 'totalVolume' => '',
  109. 'packageService' => $waybill->package_service ?? '',
  110. 'deliveryType' => $waybill->deliveryType->id ?? ''
  111. ],
  112. 'gmtCommit' => $date,
  113. 'payType' => $waybill->pay_type,
  114. 'addServices' => [
  115. 'insuranceValue' => '',
  116. 'codType' => '',
  117. 'reciveLoanAccount' => '',
  118. 'accountName' => '',
  119. 'codValue' => '',
  120. 'backSignBill' => $waybill->back_sign_bill
  121. ],
  122. 'smsNotify' => config('api_logistic.DB.prod.smsNotify'),
  123. 'sendStartTime' => $date,
  124. 'sendEndTime' => date('Y-m-d ', $waybill->deliver_at ? strtotime($waybill->deliver_at) : now()->getTimestamp()).'23:59:59',
  125. 'originalWaybillNumber' => $waybill->wms_bill_number ?? '',
  126. 'remark' => $waybill->dispatch_remark ?? '',
  127. 'isOut' => 'N',
  128. 'passwordSigning' => config('api_logistic.DB.prod.passwordSigning'),
  129. 'isdispatched' => '',
  130. 'ispresaleorder'=> '',
  131. 'isCenterDelivery' => '',
  132. // 'orderExtendFields' => [
  133. // 'value' => '',
  134. // 'key' => ''
  135. // ]
  136. ];
  137. $param = json_encode($data);
  138. $timestamp = (integer)getMillisecond();
  139. return ["params"=>$param,"timestamp"=>$timestamp,
  140. "digest"=>base64_encode(md5($param.config('api_logistic.DB.prod.app_key').$timestamp)),
  141. "companyCode" => config('api_logistic.DB.prod.company_code')];
  142. }
  143. }