DbOpenService.php 6.8 KB

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