Kdniao.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. namespace addons\shopro\library\express\provider;
  3. use think\Log;
  4. use think\exception\HttpResponseException;
  5. use addons\shopro\library\express\adapter\Kdniao as KdniaoServer;
  6. use app\admin\model\shopro\order\Express;
  7. use app\admin\model\shopro\order\Aftersale;
  8. class Kdniao extends Base
  9. {
  10. public function __construct()
  11. {
  12. $this->server = new KdniaoServer();
  13. }
  14. public $status = [
  15. '0' => 'noinfo',
  16. '1' => 'collect',
  17. '2' => 'transport',
  18. '201' => 'transport',
  19. '202' => 'delivery',
  20. '211' => 'delivery',
  21. '3' => 'signfor',
  22. '301' => 'signfor',
  23. '302' => 'signfor',
  24. '311' => 'signfor',
  25. '4' => 'difficulty',
  26. '401' => 'invalid',
  27. '402' => 'timeout',
  28. '403' => 'timeout',
  29. '404' => 'refuse',
  30. '412' => 'timeout',
  31. ];
  32. /**
  33. * 快递查询
  34. *
  35. * @param array $data
  36. * @param mixed $orderExpress
  37. * @return array
  38. */
  39. public function search($data, $orderExpress = 0)
  40. {
  41. $requestData = $this->formatRequest($data);
  42. $result = $this->server->search($requestData);
  43. $traces = $result['Traces'] ?? [];
  44. $status = $result['State'];
  45. // 格式化结果
  46. $formatResult = $this->formatResult([
  47. 'status' => $status,
  48. 'traces' => $traces
  49. ]);
  50. if ($orderExpress) {
  51. $this->updateExpress($formatResult, $orderExpress);
  52. }
  53. return $formatResult;
  54. }
  55. /**
  56. * 物流信息订阅
  57. *
  58. * @param array $data
  59. * @return array
  60. */
  61. public function subscribe($data)
  62. {
  63. $requestData = $this->formatRequest($data);
  64. $result = $this->server->subscribe($requestData);
  65. return $result;
  66. }
  67. public function cancel($data)
  68. {
  69. $kdniao = sheep_config('shop.dispatch.kdniao');
  70. $this->server->cancel([
  71. 'ShipperCode' => $data['express_code'],
  72. 'OrderCode' => $data['order_code'],
  73. 'ExpNo' => $data['express_no'],
  74. "CustomerName" => $kdniao['customer_name'],
  75. "CustomerPwd" => $kdniao['customer_pwd']
  76. ]);
  77. }
  78. /**
  79. * 物流信息推送
  80. *
  81. * @param array $data
  82. * @return array
  83. */
  84. public function push(array $data)
  85. {
  86. $success = true;
  87. $reason = '';
  88. try {
  89. $data = json_decode(html_entity_decode($data['RequestData']), true);
  90. $expressData = $data['Data'];
  91. foreach ($expressData as $key => $express) {
  92. $Callback = (isset($express['CallBack']) && !empty($express['CallBack'])) ? $express['CallBack'] : 'Express';
  93. $Callback = $Callback == 'Express' ? 'Express' : 'Aftersale';
  94. // dump($Callback);exit;
  95. if($Callback == 'Express'){
  96. $orderExpress = Express::where('express_no', $express['LogisticCode'])->where('express_code', $express['ShipperCode'])->find();
  97. }else{
  98. $orderExpress = Aftersale::where('express_no', $express['LogisticCode'])->where('express_code', $express['ShipperCode'])->find();
  99. }
  100. if (!$orderExpress) {
  101. // 包裹不存在,记录日志信息,然后继续下一个
  102. Log::error('order-express-notfound:' . json_encode($express));
  103. continue;
  104. }
  105. if (!$express['Success']) {
  106. // 失败了
  107. if (isset($express['Reason']) && (strpos($express['Reason'], '三天无轨迹') !== false || strpos($express['Reason'], '七天内无轨迹变化') !== false)) {
  108. // 需要重新订阅
  109. $this->subscribe([
  110. 'express_code' => $express['ShipperCode'],
  111. 'express_no' => $express['LogisticCode'],
  112. 'Callback' => $Callback,
  113. ]);
  114. }
  115. Log::error('order-express-resubscribe:' . json_encode($express));
  116. continue;
  117. }
  118. $traces = $express['Traces'] ?? [];
  119. $status = $express['State'];
  120. // 格式化结果
  121. $formatResult = $this->formatResult([
  122. 'status' => $status,
  123. 'traces' => $traces
  124. ]);
  125. if($Callback == 'Express'){
  126. $this->updateExpress($formatResult, $orderExpress);
  127. }else{
  128. $this->updateAftersale($formatResult, $orderExpress);
  129. }
  130. }
  131. } catch (HttpResponseException $e) {
  132. $data = $e->getResponse()->getData();
  133. $reason = $data ? ($data['msg'] ?? '') : $e->getMessage();
  134. } catch (\Exception $e) {
  135. $success = false;
  136. $reason = $e->getMessage();
  137. }
  138. return $this->server->pushResult($success, $reason);
  139. }
  140. /**
  141. * 电子面单
  142. *
  143. * @param array $data
  144. * @param array $items
  145. * @return array
  146. */
  147. public function eOrder($data, $items)
  148. {
  149. $kdniao = sheep_config('shop.dispatch.kdniao');
  150. if ($kdniao['type'] !== 'vip') {
  151. error_stop('仅快递鸟标准版接口支持电子面单功能!');
  152. }
  153. $consignee = $data['consignee'];
  154. $order = $data['order'];
  155. $sender = $data['sender'] ?? sheep_config('shop.dispatch.sender');
  156. if (empty($sender)) {
  157. error_stop('请配置默认发货人信息');
  158. }
  159. // 运单基础信息
  160. $requestData = [
  161. "CustomerName" => $kdniao['customer_name'],
  162. "CustomerPwd" => $kdniao['customer_pwd'],
  163. "ShipperCode" => $kdniao['express']['code'] ?? '',
  164. "PayType" => $kdniao['pay_type'],
  165. "ExpType" => $kdniao['exp_type'],
  166. "IsReturnPrintTemplate" => 0, //返回打印面单模板
  167. "TemplateSize" => '130', // 一联单
  168. "Volume" => 0,
  169. "OrderCode" => $order['order_sn'] . '_' . time(), // 商城订单号
  170. "Remark" => $order['remark'] ?? '小心轻放' // 备注
  171. ];
  172. // 发货人
  173. $requestData['Sender'] = [
  174. 'Name' => $sender['name'],
  175. 'Mobile' => $sender['mobile'],
  176. 'ProvinceName' => $sender['province_name'],
  177. 'CityName' => $sender['city_name'],
  178. 'ExpAreaName' => $sender['district_name'],
  179. 'Address' => $sender['address']
  180. ];
  181. // 收货人
  182. $requestData['Receiver'] = [
  183. "Name" => $consignee['consignee'],
  184. "Mobile" => $consignee['mobile'],
  185. "ProvinceName" => $consignee['province_name'],
  186. "CityName" => $consignee['city_name'],
  187. "ExpAreaName" => $consignee['district_name'],
  188. "Address" => $consignee['address']
  189. ];
  190. // 包裹信息
  191. $totalCount = 0;
  192. $totalWeight = 0;
  193. foreach ($items as $k => $item) {
  194. $goodsName = $item->goods_title . ($item->goods_sku_text ? '-' . $item->goods_sku_text : '');
  195. $requestData['Commodity'][] = [
  196. "GoodsName" => $goodsName,
  197. "Goodsquantity" => $item->goods_num,
  198. "GoodsWeight" => $item->goods_num * $item->goods_weight
  199. ];
  200. $totalCount += $item->goods_num;
  201. $totalWeight += $item->goods_num * $item->goods_weight;
  202. }
  203. $requestData['Quantity'] = $totalCount; // 商品数量
  204. $requestData['Weight'] = $totalWeight;
  205. $result = $this->server->eOrder($requestData);
  206. if ($result['Success'] === true && $result['ResultCode'] === "100") {
  207. return [
  208. 'code' => $kdniao['express']['code'],
  209. 'name' => $kdniao['express']['name'],
  210. 'no' => $result['Order']['LogisticCode'],
  211. 'ext' => $result,
  212. 'driver' => 'kdniao'
  213. ];
  214. }
  215. return false;
  216. }
  217. /**
  218. * 处理请求接口数据
  219. *
  220. * @param array $data
  221. * @return array
  222. */
  223. protected function formatRequest($data)
  224. {
  225. $requestData = [
  226. 'express_code' => $data['express_code'] ?? '',
  227. 'express_no' => $data['express_no'],
  228. 'phone' => (isset($data['phone']) && $data['phone']) ? substr($data['phone'], 7) : '',
  229. ];
  230. if(isset($data['Callback']) && !empty($data['Callback'])){
  231. $requestData['Callback'] = $data['Callback'];
  232. }
  233. return $requestData;
  234. }
  235. /**
  236. * 处理返回结果
  237. *
  238. * @param array $data
  239. * @return array
  240. */
  241. protected function formatResult($data)
  242. {
  243. $status = $this->status[$data['status']] ?? 'noinfo';
  244. $traces = [];
  245. foreach ($data['traces'] as $trace) {
  246. $action = $trace['Action'] ?? '';
  247. if ($action !== '') {
  248. $currentStatus = $this->status[$action] ?? 'noinfo';
  249. }
  250. $traces[] = [
  251. 'content' => $trace['AcceptStation'],
  252. 'change_date' => date('Y-m-d H:i:s', strtotime(substr($trace['AcceptTime'], 0, 19))), // 快递鸟时间格式可能是 2020-08-03 16:58:272 或者 2014/06/25 01:41:06
  253. 'status' => $currentStatus ?? 'noinfo'
  254. ];
  255. }
  256. return compact('status', 'traces');
  257. }
  258. }