KdApiExpOrder.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. namespace app\common\library;
  3. use app\common\model\Order;
  4. use app\common\model\ElectronicsOrder;
  5. use app\common\model\OrderElectronics;
  6. use fast\Http;
  7. /**
  8. * 电子面单
  9. * @ DateTime 2021-06-11
  10. * @
  11. */
  12. class KdApiExpOrder
  13. {
  14. protected static $reqUrl = '';
  15. /**
  16. * @ DateTime 2021-06-11
  17. * @
  18. * @electronics [type] $electronics
  19. * @electronics [type] $sender
  20. * @return void
  21. */
  22. public static function create($order_id, $electronics_id)
  23. {
  24. $order = Order::with(['OrderGoods'])
  25. ->field('o.*,p.name province_name,c.name city_name,a.name area_name')
  26. ->alias('o')
  27. ->join('shop_address d', 'o.address_id=d.id', 'LEFT')
  28. ->join('shop_area p', 'd.province_id=p.id', 'LEFT')
  29. ->join('shop_area c', 'd.city_id=c.id', 'LEFT')
  30. ->join('shop_area a', 'd.area_id=a.id', 'LEFT')
  31. ->where('o.id', $order_id)
  32. ->find();
  33. if ($order->orderstate == 1) {
  34. throw new \Exception('订单已取消,不支持生成电子面单');
  35. }
  36. if ($order->orderstate == 2) {
  37. throw new \Exception('订单已失效,不支持生成电子面单');
  38. }
  39. if ($order->orderstate == 3) {
  40. throw new \Exception('订单已完成,不支持生成电子面单');
  41. }
  42. if (!$order->paystate) {
  43. throw new \Exception('订单未支付,不支持生成电子面单');
  44. }
  45. if ($order->shippingstate) {
  46. throw new \Exception('订单已发货,不支持生成电子面单');
  47. }
  48. $electronics = ElectronicsOrder::with(['Shipper'])->where('id', $electronics_id)->find();
  49. if (empty($order)) {
  50. throw new \Exception('未找到订单');
  51. }
  52. if (empty($electronics)) {
  53. throw new \Exception("电子面单模板不存在");
  54. }
  55. if (empty($electronics->shipper)) {
  56. throw new \Exception("快递公司不存在");
  57. }
  58. if (empty($order->order_goods)) {
  59. throw new \Exception("订单商品不存在");
  60. }
  61. $Commodity = [];
  62. $quantity = 0;
  63. $Weight = 0;
  64. foreach ($order->order_goods as $item) {
  65. $Commodity[] = [
  66. 'GoodsName' => $item['title'],
  67. 'GoodsCode' => $item['goods_id'] . '_' . $item['goods_sku_id'],
  68. 'Goodsquantity' => $item['nums'],
  69. 'GoodsPrice' => $item['price'],
  70. 'GoodsWeight' => $item['weight'],
  71. // 'GoodsVol' => 0,
  72. 'GoodsDesc' => $item['attrdata']
  73. ];
  74. $quantity = bcadd($quantity, $item['nums']);
  75. $Weight = bcadd($Weight, $item['weight']);
  76. }
  77. // 组装应用级参数
  78. $requestData = [
  79. 'MemberID' => $order->user_id,
  80. 'OrderCode' => $order->order_sn,
  81. 'ShipperCode' => $electronics->shipper->shipper_code,
  82. 'LogisticCode' => $electronics->logistic_code,
  83. 'CustomerName' => $electronics->customer_name,
  84. 'CustomerPwd' => $electronics->customer_pwd,
  85. // 'ThrOrderCode' => '1234567890',
  86. 'SendSite' => $electronics->send_site,
  87. 'PayType' => $electronics->paytype,
  88. 'MonthCode' => $electronics->month_code,
  89. 'IsReturnSignBill' => $electronics->is_return_sign_bill,
  90. 'OperateRequire' => $electronics->operate_require,
  91. 'ExpType' => $electronics->exp_type,
  92. 'Cost' => $order->shippingfee,
  93. 'OtherCost' => 0,
  94. 'Sender' => [
  95. 'Company' => $electronics->company,
  96. 'Name' => $electronics->name,
  97. 'Tel' => $electronics->tel,
  98. 'Mobile' => $electronics->mobile,
  99. 'PostCode' => $electronics->post_code,
  100. 'ProvinceName' => $electronics->province_name,
  101. 'CityName' => $electronics->city_name,
  102. 'ExpAreaName' => $electronics->exp_area_name,
  103. 'Address' => $electronics->address
  104. ],
  105. 'Receiver' => [
  106. 'Company' => '',
  107. 'Name' => $order->receiver,
  108. 'Tel' => '',
  109. 'Mobile' => $order->mobile,
  110. 'PostCode' => $order->zipcode,
  111. 'ProvinceName' => $order->province_name,
  112. 'CityName' => $order->city_name,
  113. 'ExpAreaName' => $order->area_name,
  114. 'Address' => $order->address
  115. ],
  116. 'Commodity' => $Commodity,
  117. 'IsNotice' => $electronics->is_notice,
  118. 'StartDate' => '',
  119. 'EndDate' => '',
  120. 'AddService' => [],
  121. 'Weight' => $Weight,
  122. 'Quantity' => $quantity,
  123. // 'Volume' => 0,
  124. 'IsReturnPrintTemplate' => $electronics->is_return_temp,
  125. 'Remark' => $electronics->remark
  126. ];
  127. self::setReqUrl();
  128. $res = self::sendPost($requestData, 1007);
  129. //生成电子面单后,替换快递单号
  130. if (isset($res['Success']) && $res['Success']) {
  131. $order->expressno = $res['Order']['LogisticCode'];
  132. $order->expressname = $electronics['shipper']['name'];
  133. $order->save();
  134. }
  135. //入库
  136. OrderElectronics::push($res, $order->order_sn, $electronics->customer_name, $electronics->customer_pwd);
  137. return $res;
  138. }
  139. /**
  140. * @ DateTime 2021-06-11
  141. * @ 电子面单取消
  142. * @return void
  143. */
  144. public static function cancel($param)
  145. {
  146. // 组装应用级参数
  147. $requestData = [
  148. 'ShipperCode' => $param['shipper_code'],
  149. 'OrderCode' => $param['order_sn'],
  150. 'ExpNo' => $param['logistic_code'],
  151. 'CustomerName' => $param['customer_name'],
  152. 'CustomerPwd' => $param['customer_pwd']
  153. ];
  154. self::setReqUrl();
  155. return self::sendPost($requestData, 1147);
  156. }
  157. /**
  158. * @ DateTime 2021-06-11
  159. * @ 单号余量查询
  160. * @return void
  161. */
  162. public static function getOrderTraces($param)
  163. {
  164. $requestData = [
  165. 'ShipperCode' => $param['shipper_code'],
  166. 'StationCode' => '',
  167. 'StationName' => '',
  168. 'CustomerName' => $param['customer_name'],
  169. 'CustomerPwd' => $param['customer_pwd']
  170. ];
  171. self::setReqUrl();
  172. return self::sendPost($requestData, 1127);
  173. }
  174. /**
  175. * @ 物流查询
  176. * @param [type] $param
  177. * @return void
  178. */
  179. public static function getLogisticsQuery($param)
  180. {
  181. $requestData = [
  182. 'OrderCode' => $param['order_sn'],
  183. 'ShipperCode' => $param['shipper_code'],
  184. 'LogisticCode' => $param['logistic_code'],
  185. ];
  186. self::setReqUrl(1);
  187. return self::sendPost($requestData, 1002);
  188. }
  189. //设置url
  190. protected static function setReqUrl($type = 0)
  191. {
  192. $config = get_addon_config('shop');
  193. if ($config['api_mode'] == 'sandbox') {
  194. self::$reqUrl = 'http://sandboxapi.kdniao.com:8080/kdniaosandbox/gateway/exterfaceInvoke.json';
  195. } else {
  196. switch ($type) {
  197. case 1: //即时物流
  198. self::$reqUrl = 'https://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx';
  199. break;
  200. default: //电子面单
  201. self::$reqUrl = 'https://api.kdniao.com/api/EOrderService';
  202. }
  203. }
  204. }
  205. /**
  206. * @ DateTime 2021-06-11
  207. * @ 请求
  208. * @return void
  209. */
  210. protected static function sendPost($requestData, $requestType)
  211. {
  212. $requestData = json_encode($requestData, JSON_UNESCAPED_UNICODE);
  213. $config = get_addon_config('shop');
  214. // 组装系统级参数
  215. $data = array(
  216. 'EBusinessID' => $config['EBusinessID'],
  217. 'RequestType' => $requestType,
  218. 'RequestData' => urlencode($requestData),
  219. 'DataType' => '2',
  220. );
  221. $data['DataSign'] = self::encrypt($requestData, $config['kdNiaoApiKey']);
  222. //以form表单形式提交post请求,post请求体中包含了应用级参数和系统级参数
  223. $result = Http::post(self::$reqUrl, $data);
  224. return (array)json_decode($result, true);
  225. }
  226. /**
  227. * Sign签名生成
  228. * @electronics data 内容
  229. * @electronics ApiKey ApiKey
  230. * @return DataSign签名
  231. */
  232. protected static function encrypt($data, $apiKey)
  233. {
  234. return urlencode(base64_encode(md5($data . $apiKey)));
  235. }
  236. }