DbOpenService.php 6.5 KB

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