DbOpenService.php 6.5 KB

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