Logistics.php 14 KB

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