Logistics.php 13 KB

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