Kdniao.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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\ExpressLog;
  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. $orderExpress = Express::where('express_no', $express['LogisticCode'])->where('express_code', $express['ShipperCode'])->find();
  93. if (!$orderExpress) {
  94. // 包裹不存在,记录日志信息,然后继续下一个
  95. Log::error('order-express-notfound:' . json_encode($express));
  96. continue;
  97. }
  98. if (!$express['Success']) {
  99. // 失败了
  100. if (isset($express['Reason']) && (strpos($express['Reason'], '三天无轨迹') !== false || strpos($express['Reason'], '七天内无轨迹变化') !== false)) {
  101. // 需要重新订阅
  102. $this->subscribe([
  103. 'express_code' => $express['ShipperCode'],
  104. 'express_no' => $express['LogisticCode']
  105. ]);
  106. }
  107. Log::error('order-express-resubscribe:' . json_encode($express));
  108. continue;
  109. }
  110. $traces = $express['Traces'] ?? [];
  111. $status = $express['State'];
  112. // 格式化结果
  113. $formatResult = $this->formatResult([
  114. 'status' => $status,
  115. 'traces' => $traces
  116. ]);
  117. $this->updateExpress($formatResult, $orderExpress);
  118. }
  119. } catch (HttpResponseException $e) {
  120. $data = $e->getResponse()->getData();
  121. $reason = $data ? ($data['msg'] ?? '') : $e->getMessage();
  122. } catch (\Exception $e) {
  123. $success = false;
  124. $reason = $e->getMessage();
  125. }
  126. return $this->server->pushResult($success, $reason);
  127. }
  128. /**
  129. * 电子面单
  130. *
  131. * @param array $data
  132. * @param array $items
  133. * @return array
  134. */
  135. public function eOrder($data, $items)
  136. {
  137. $kdniao = sheep_config('shop.dispatch.kdniao');
  138. if ($kdniao['type'] !== 'vip') {
  139. error_stop('仅快递鸟标准版接口支持电子面单功能!');
  140. }
  141. $consignee = $data['consignee'];
  142. $order = $data['order'];
  143. $sender = $data['sender'] ?? sheep_config('shop.dispatch.sender');
  144. if (empty($sender)) {
  145. error_stop('请配置默认发货人信息');
  146. }
  147. // 运单基础信息
  148. $requestData = [
  149. "CustomerName" => $kdniao['customer_name'],
  150. "CustomerPwd" => $kdniao['customer_pwd'],
  151. "ShipperCode" => $kdniao['express']['code'] ?? '',
  152. "PayType" => $kdniao['pay_type'],
  153. "ExpType" => $kdniao['exp_type'],
  154. "IsReturnPrintTemplate" => 0, //返回打印面单模板
  155. "TemplateSize" => '130', // 一联单
  156. "Volume" => 0,
  157. "OrderCode" => $order['order_sn'] . '_' . time(), // 商城订单号
  158. "Remark" => $order['remark'] ?? '小心轻放' // 备注
  159. ];
  160. // 发货人
  161. $requestData['Sender'] = [
  162. 'Name' => $sender['name'],
  163. 'Mobile' => $sender['mobile'],
  164. 'ProvinceName' => $sender['province_name'],
  165. 'CityName' => $sender['city_name'],
  166. 'ExpAreaName' => $sender['district_name'],
  167. 'Address' => $sender['address']
  168. ];
  169. // 收货人
  170. $requestData['Receiver'] = [
  171. "Name" => $consignee['consignee'],
  172. "Mobile" => $consignee['mobile'],
  173. "ProvinceName" => $consignee['province_name'],
  174. "CityName" => $consignee['city_name'],
  175. "ExpAreaName" => $consignee['district_name'],
  176. "Address" => $consignee['address']
  177. ];
  178. // 包裹信息
  179. $totalCount = 0;
  180. $totalWeight = 0;
  181. foreach ($items as $k => $item) {
  182. $goodsName = $item->goods_title . ($item->goods_sku_text ? '-' . $item->goods_sku_text : '');
  183. $requestData['Commodity'][] = [
  184. "GoodsName" => $goodsName,
  185. "Goodsquantity" => $item->goods_num,
  186. "GoodsWeight" => $item->goods_num * $item->goods_weight
  187. ];
  188. $totalCount += $item->goods_num;
  189. $totalWeight += $item->goods_num * $item->goods_weight;
  190. }
  191. $requestData['Quantity'] = $totalCount; // 商品数量
  192. $requestData['Weight'] = $totalWeight;
  193. $result = $this->server->eOrder($requestData);
  194. if ($result['Success'] === true && $result['ResultCode'] === "100") {
  195. return [
  196. 'code' => $kdniao['express']['code'],
  197. 'name' => $kdniao['express']['name'],
  198. 'no' => $result['Order']['LogisticCode'],
  199. 'ext' => $result,
  200. 'driver' => 'kdniao'
  201. ];
  202. }
  203. return false;
  204. }
  205. /**
  206. * 处理请求接口数据
  207. *
  208. * @param array $data
  209. * @return array
  210. */
  211. protected function formatRequest($data)
  212. {
  213. $requestData = [
  214. 'express_code' => $data['express_code'] ?? '',
  215. 'express_no' => $data['express_no'],
  216. 'phone' => (isset($data['phone']) && $data['phone']) ? substr($data['phone'], 7) : ''
  217. ];
  218. return $requestData;
  219. }
  220. /**
  221. * 处理返回结果
  222. *
  223. * @param array $data
  224. * @return array
  225. */
  226. protected function formatResult($data)
  227. {
  228. $status = $this->status[$data['status']] ?? 'noinfo';
  229. $traces = [];
  230. foreach ($data['traces'] as $trace) {
  231. $action = $trace['Action'] ?? '';
  232. if ($action !== '') {
  233. $currentStatus = $this->status[$action] ?? 'noinfo';
  234. }
  235. $traces[] = [
  236. 'content' => $trace['AcceptStation'],
  237. '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
  238. 'status' => $currentStatus ?? 'noinfo'
  239. ];
  240. }
  241. return compact('status', 'traces');
  242. }
  243. }