Logistics.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. namespace common\helpers;
  3. use common\models\Region;
  4. use Yii;
  5. class Logistics
  6. {
  7. // 1.获取bearer token,此token是其他api调用时的必传参数.
  8. const authenticationUrl = 'http://warehouse.worldsyntech.com/index.php?route=rest/admin_security/api_login&grant_type=client_credentials';
  9. // 2.创建订单和产品(以前不存在/已提交的产品).
  10. const createOrderUrl = 'http://warehouse.worldsyntech.com/index.php?route=rest/warehouse_api/add_order';
  11. // 3.获取产品/包裹的重量和状态.
  12. const getProductUrl = 'http://warehouse.worldsyntech.com/index.php?route=rest/warehouse_api/get_order_product';
  13. // 4.获取订单重量,m3,包装的包裹数量,费用跟踪号码和状态.
  14. const getOrderUrl = 'http://warehouse.worldsyntech.com/index.php?route=rest/warehouse_api/get_order';
  15. // 5.通知仓库已经付款,包裹可以投递.
  16. const notifyDeliveryUrl = 'http://warehouse.worldsyntech.com/index.php?route=rest/ warehouse_api/notify_delivery';
  17. // 6.创建产品/包裹.
  18. const addProductUrl = 'http://warehouse.worldsyntech.com/index.php?route=rest/warehouse_api/add_order_product';
  19. // 7.创建交付订单并将现有产品绑定到订单.需要先调用接口6创建商品.
  20. const createOrderSimpleUrl = 'http://warehouse.worldsyntech.com/index.php?route=rest/warehouse_api/add_order_simple';
  21. // 8.计算订单费用,并将订单状态改为待付款.
  22. const stockOutUrl = 'http://warehouse.worldsyntech.com/index.php?route=rest/warehouse_api/stock_out';
  23. // 9.通知物流系统订单不可删除.error包含错误的订单ID.
  24. const notifyOrderUndeletableUrl = 'http://warehouse.worldsyntech.com/index.php?route=rest/warehouse_api/notify_order_undeletable';
  25. // 10.取消订单产品/包裹。它将状态更改为无效,并保留订单产品/包裹.
  26. const cancelOrderProductUrl = ' http://warehouse.worldsyntech.com/index.php?route=rest/warehouse_api/cancel_order_product';
  27. // 11.从数据库中删除订购产品/包裹.
  28. const deleteOrderProductUrl = 'http://warehouse.worldsyntech.com/index.php?route=rest/warehouse_api/delete_order_product';
  29. // 12.取消订单,状态改为无效,保留订单不删除.
  30. const cancelOrderUrl = 'http://warehouse.worldsyntech.com/index.php?route=rest/warehouse_api/cancel_order';
  31. // 13.从数据库中删除订单.
  32. const deleteOrderUrl = 'http://warehouse.worldsyntech.com/index.php?route=rest/warehouse_api/delete_order';
  33. // 14.运单追踪.
  34. const trackOrderUrl = 'https://taoplus.com.my/index.php?route=information/order_tracking/json&order_tracking_search=';
  35. // accessToken的缓存key
  36. const wstLogisticsBearerTokenKey = 'wstLogisticsBearerToken';
  37. // 国家
  38. const country = 'China';
  39. function __construct()
  40. {
  41. if (!\Yii::$app->redis->get(self::wstLogisticsBearerTokenKey)) {
  42. $this->getAuthentication();
  43. }
  44. }
  45. /**
  46. * 通用curl接口.
  47. * @param string $url api接口url
  48. * @param array $request_body 参数
  49. * @return mixed
  50. */
  51. function curl($url, $request_body)
  52. {
  53. $accessToken = \Yii::$app->redis->get(self::wstLogisticsBearerTokenKey);
  54. $ch = curl_init($url);
  55. header('Content-Type: application/json');
  56. $authorization = "Authorization: Bearer " . $accessToken;
  57. curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json', $authorization]);
  58. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  59. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  60. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request_body));
  61. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  62. $response = curl_exec($ch);
  63. curl_close($ch);
  64. return json_decode($response, true);
  65. }
  66. // 1.获取bearer token,此token是其他api调用时的必传参数.
  67. function getAuthentication()
  68. {
  69. $request = [
  70. 'user_name' => Yii::$app->params['wst']['userName'],
  71. 'password' => Yii::$app->params['wst']['password'],
  72. 'agent_id' => Yii::$app->params['wst']['agentId'],
  73. ];
  74. $ch = curl_init(self::authenticationUrl);
  75. header('Content-Type: application/json');
  76. $authorization = "Authorization: Basic " . Yii::$app->params['wst']['baseToken'];
  77. curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json', $authorization]);
  78. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  79. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  80. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request));
  81. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  82. $response = curl_exec($ch);
  83. curl_close($ch);
  84. $result = json_decode($response, true);
  85. if ($result['success']) {
  86. // token和过期时间写入redis
  87. \Yii::$app->redis->set(self::wstLogisticsBearerTokenKey, $result['data']['access_token']);
  88. \Yii::$app->redis->expire(self::wstLogisticsBearerTokenKey, $result['data']['expires_in']);
  89. }
  90. }
  91. // 2.创建订单和产品(以前不存在/已提交的产品).
  92. function createOrder($order)
  93. {
  94. $request = [
  95. 'order_no' => $order['SN'], // 客户系统中的订单号
  96. 'delivery_method_id' => '133', // 快递方式ID. TODO:
  97. 'warehouse_id' => '1', // 仓库ID. TODO:
  98. 'country' => 'China', // 收件人国家 Malaysia
  99. 'state' => Region::getCnName($order['PROVINCE']), // , // 收件人省 Johor(test)
  100. 'city' => Region::getCnName($order['CITY']) . Region::getCnName($order['COUNTY']), // 收件市县
  101. 'post_code' => $order['ZIP_CODE'], // 收件人邮政编码 14000(test)
  102. 'address' => $order['ADDRESS'], // 送货地址
  103. 'consignee' => $order['CONSIGNEE_REAL_NAME'], // 收货人姓名,使用订单中的收货人真实姓名
  104. 'consignee_ic_number' => $order['CONSIGNEE_ID_NO'],
  105. 'telephone' => $order['MOBILE'], // 收件人电话号码
  106. 'sender' => $order['CONSIGNEE_REAL_NAME'],
  107. 'products' => [
  108. [
  109. 'product_no' => $order['SKU_CODE'], // 客户系统中的产品编号
  110. 'product_name' => $order['GOODS_TITLE'], // 产品名称
  111. 'tracking_number' => Date::today('Ymd') . $this->_random(10, 1),
  112. 'quantity' => $order['BUY_NUMS'], // 产品数量
  113. 'total_price' => $order['PAY_AMOUNT'], // 订单总金额,Decimal
  114. 'currency_code' => 'CNY', // 产品的货币代码. 如USD(美元),MYR(马来西亚林吉特),SGD(新加坡元),CNY(人民币)
  115. ],
  116. ],
  117. ];
  118. return $this->curl(self::createOrderUrl, $request);
  119. }
  120. // 3.获取产品/包裹的重量和状态.
  121. function getProduct($order)
  122. {
  123. $request = [
  124. 'tracking_number' => '027300027300',
  125. ];
  126. $response = $this->curl(self::getProductUrl, $request);
  127. LoggerTool::info($response);
  128. return $response;
  129. }
  130. // 4.获取订单重量,m3,包装的包裹数量,费用跟踪号码和状态
  131. function getOrder($order)
  132. {
  133. $request = [
  134. 'order_id' => 'elg8-785',
  135. ];
  136. $response = $this->curl(self::getOrderUrl, $request);
  137. LoggerTool::info($response);
  138. return $response;
  139. }
  140. // 5.通知仓库已经付款,包裹可以投递.
  141. function notifyDelivery($order)
  142. {
  143. $request = [
  144. 'order_id' => 'elg8-785',
  145. ];
  146. $response = $this->curl(self::notifyDeliveryUrl, $request);
  147. LoggerTool::info($response);
  148. return $response;
  149. }
  150. // 6.创建产品/包裹.
  151. function addProduct($orderProducts)
  152. {
  153. $products = [];
  154. foreach ($orderProducts as $item) {
  155. $products[] = [
  156. "product_no" => $item['SKU_CODE'],
  157. "product_name" => $item['GOODS_TITLE'],
  158. "tracking_number" => '',
  159. "quantity" => $item['BUY_NUMS'],
  160. "total_price" => $item['REAL_PV'],
  161. "currency_code" => "CNY",
  162. ];
  163. }
  164. $request = [
  165. 'warehouse_id' => '1',
  166. "products" => $products
  167. ];
  168. $response = $this->curl(self::addProductUrl, $request);
  169. LoggerTool::info($response);
  170. return $response;
  171. }
  172. // 7.创建交付订单并将现有产品绑定到订单.需要先调用接口6创建商品.
  173. function createOrderSimple($order)
  174. {
  175. $request = [
  176. "order_no" => "T-1000",
  177. "delivery_method_id" => "39",
  178. "warehouse_id" => "1",
  179. "country" => "Malaysia",
  180. "state" => "Pulau Pinang",
  181. "city" => "Bukit Mertajam",
  182. "post_code" => "14000",
  183. "address" => "1584,JALAN NANGKA,TAMAN JAMBU MAWAR,14000 BUKIT MERTAJAM",
  184. "consignee" => "TSA",
  185. "consignee_ic_number" => "111111-11-1111",
  186. "telephone" => "017-5423223",
  187. "cod" => "CNY",
  188. "cod_currency_code" => "100",
  189. "address_area" => "",
  190. "address_subdivision" => "",
  191. "products" => [
  192. [
  193. "product_no" => "12345",
  194. "tracking_number" => "2019061112A1",
  195. ],
  196. [
  197. "product_no" => "23456",
  198. "tracking_number" => "20190604_A12",
  199. ]
  200. ]
  201. ];
  202. $response = $this->curl(self::createOrderSimpleUrl, $request);
  203. LoggerTool::info($response);
  204. return $response;
  205. }
  206. // 8.根据货物重量计算订单费用,并更改订单状态为待付款
  207. function stockOut($order)
  208. {
  209. $request = [
  210. 'order_id' => 'elg8-785',
  211. 'weight' => '2.5', // 订单重量(KG)
  212. ];
  213. $response = $this->curl(self::stockOutUrl, $request);
  214. LoggerTool::info($response);
  215. return $response;
  216. }
  217. // 9.通知物流系统订单不可删除. error包含错误的订单ID.
  218. function notifyOrderUndeletable($order)
  219. {
  220. $request = [
  221. 'order_ids' => ['elg8-785']
  222. ];
  223. $response = $this->curl(self::notifyOrderUndeletableUrl, $request);
  224. LoggerTool::info($response);
  225. return $response;
  226. }
  227. // 10.取消订单产品/包裹。它将状态更改为无效,并保留订单产品/包裹.
  228. function cancelOrderProduct($order)
  229. {
  230. $request = [
  231. 'order_product_ids' => ['15895', '15896', '15897']
  232. ];
  233. $response = $this->curl(self::cancelOrderProductUrl, $request);
  234. LoggerTool::info($response);
  235. return $response;
  236. }
  237. // 11.从数据库中删除订购产品/包裹.
  238. function deleteOrderProduct($order)
  239. {
  240. $request = [
  241. 'order_product_ids' => ['15895', '15896', '15897']
  242. ];
  243. $response = $this->curl(self::deleteOrderProductUrl, $request);
  244. LoggerTool::info($response);
  245. return $response;
  246. }
  247. // 12.取消订单,状态改为无效,保留订单不删除.
  248. function cancelOrder($order)
  249. {
  250. $request = [
  251. 'order_ids' => ['elg8-787', 'elg8-786']
  252. ];
  253. $response = $this->curl(self::cancelOrderUrl, $request);
  254. LoggerTool::info($response);
  255. return $response;
  256. }
  257. // 13.从数据库中删除订单.
  258. function deleteOrder($order)
  259. {
  260. $request = [
  261. 'order_ids' => ['elg8-787', 'elg8-786']
  262. ];
  263. $response = $this->curl(self::deleteOrderUrl, $request);
  264. LoggerTool::info($response);
  265. return $response;
  266. }
  267. // 13.从数据库中删除订单.
  268. function trackOrder($trackNo)
  269. {
  270. // 运单号
  271. $trackNo = 'TBS10182495KJ2430';
  272. $curl = curl_init();
  273. curl_setopt($curl,CURLOPT_TIMEOUT,5000);
  274. curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
  275. curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,false);
  276. curl_setopt($curl,CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
  277. curl_setopt($curl,CURLOPT_URL, self::trackOrderUrl . $trackNo);
  278. curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
  279. $response = curl_exec($curl);
  280. if (!$response) {
  281. $error = curl_errno($curl);
  282. curl_close($curl);
  283. LoggerTool::error($error);
  284. return false;
  285. }
  286. curl_close($curl);
  287. $result = json_decode($response, true);
  288. LoggerTool::info($result);
  289. return $result['data'];
  290. }
  291. /**
  292. * 生成随机数
  293. * @param $length
  294. * @param int $numeric
  295. * @return string
  296. */
  297. private function _random($length, $numeric = 0) {
  298. $seed = base_convert(md5(microtime() . $_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);
  299. $seed = $numeric ? (str_replace('0', '', $seed) . '012340567890') : ($seed . 'zZ' . strtoupper($seed));
  300. $hash = '';
  301. $max = strlen($seed) - 1;
  302. for ($i = 0; $i < $length; $i++) {
  303. $hash .= $seed[mt_rand(0, $max)];
  304. }
  305. return $hash;
  306. }
  307. }