GongApiRequest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. <?php
  2. namespace app\traits;
  3. use think\facade\Cache;
  4. use think\facade\Db;
  5. use think\facade\Log;
  6. trait GongApiRequest
  7. {
  8. //接口文档:https://kdocs.cn/l/cov4HdLPYVgV
  9. private static $path = 'https://www.douhuomall.com';
  10. // private static $app_id = 'YS112May13bb673b251a';
  11. // private static $app_secret = '38971362a8965c9135cb147d53e52706';
  12. // private static $mobile = '13911251783';
  13. private static $app_id = 'YS3a8ab661406e47bb86';
  14. private static $app_secret = '41d8b8e1dd9a454a1abc4534297e33c3';
  15. private static $mobile = '17855366921';
  16. public static function get_access_token(){
  17. $url = '/api_v2/noAuth/getAccessToken';
  18. $time = time();
  19. $body = [
  20. 'app_id' => self::$app_id,
  21. 'sign' => self::getSign($time),
  22. 'mobile' => self::$mobile,
  23. 'timestamp' => $time
  24. ];
  25. $res = self::curl_post(self::$path.$url,$body);
  26. if($res['error_code'] == 0){
  27. Cache::set('gong_access_token', $res['result']['access_token'], 23*60*60);// 将取到的token存入redis方便下次使用
  28. return $res['result']['access_token'];
  29. }
  30. return false;
  31. }
  32. public static function access_token(){
  33. $gong_access_token = Cache::get('gong_access_token');
  34. $url = '/api_v2/Goods/getGoodsList';
  35. $body = ['access_token' => $gong_access_token];
  36. $res = self::curl_post(self::$path.$url,$body);
  37. if(!$gong_access_token || ($res['error_code'] == 1001)){
  38. Cache::delete('gong_access_token');
  39. $gong_access_token = self::get_access_token();
  40. }
  41. return $gong_access_token;
  42. }
  43. //分类(未更改)
  44. public static function get_category(){
  45. $url = '/api/category/get_list';
  46. $body = [
  47. 'access_token' => self::access_token(),
  48. ];
  49. $res = self::curl_post(self::$path.$url,$body);
  50. if($res['error_code'] == 1002 && $res['error_msg'] == '会话不存在'){
  51. return 1002;
  52. }
  53. if($res['error_code'] == 0){
  54. return $res['result'];
  55. }
  56. return false;
  57. }
  58. /**
  59. * 商品列表
  60. * @param int $page 页数
  61. * @param int $limit 每页数量
  62. * @return array|boolean
  63. * @author lin
  64. * @created 2022年5月19日11:31:59
  65. */
  66. public static function get_goods_list($page=1,$limit=50){
  67. $url = '/api_v2/Goods/getGoodsList';
  68. $body = [
  69. 'access_token' => self::access_token(),
  70. 'page' => $page,
  71. 'limit' => $limit
  72. ];
  73. $res = self::curl_post(self::$path.$url,$body);
  74. if($res['error_code'] == 0){
  75. return $res['result'];
  76. }
  77. return false;
  78. }
  79. /**
  80. * 商品详情
  81. * @param array $goods_id 商品goods_id
  82. * @return array|boolean
  83. * @author lin
  84. * @created 2022年5月19日11:31:59
  85. */
  86. public static function get_goods_info($goods_id){
  87. $url = '/api_v2/Goods/getGoodsDetail';
  88. $body = [
  89. 'access_token' => self::access_token(),
  90. 'goods_id' => $goods_id,
  91. ];
  92. $res = self::curl_post(self::$path.$url,$body);
  93. if ($res['error_code'] == 2001) {
  94. return $res;
  95. }
  96. if($res['error_code'] == 0){
  97. return $res['result'];
  98. }
  99. return false;
  100. }
  101. /**
  102. * 规格详情
  103. * @param array $sku_id 商品sku_id
  104. * @return array|boolean
  105. * @author lin
  106. * @created 2022年5月19日11:31:59
  107. */
  108. public static function get_sku_detail($sku_id){
  109. $url = '/api_v2/Goods/getSkuDetail';
  110. $body = [
  111. 'access_token' => self::access_token(),
  112. 'sku_id' => $sku_id,
  113. ];
  114. $res = self::curl_post(self::$path.$url,$body);
  115. echo json_encode($res);
  116. if($res['error_code'] == 0){
  117. return $res['result'];
  118. }
  119. return false;
  120. }
  121. /**
  122. * 商品规格
  123. * @param array $sku_sn 商品sku_sn
  124. * @return array|boolean
  125. * @author lin
  126. * @created 2022年5月19日11:31:59
  127. */
  128. public static function get_sku($sku_sn){
  129. $url = '/api/Goods/getSkuDetail';
  130. $body = [
  131. 'access_token' => self::access_token(),
  132. 'sku_sn' => $sku_sn
  133. ];
  134. $res = self::curl_post(self::$path.$url,$body);
  135. if($res['error_code'] == 0){
  136. return $res['result'];
  137. }
  138. return false;
  139. }
  140. /**
  141. * 商品价格(未更改)
  142. * @param string $sku_id 商品skuid,[‘984783’,’984784’]
  143. * @return array|boolean
  144. * @author lin
  145. * @created 2022年5月19日11:31:59
  146. */
  147. public static function get_price($sku_id){
  148. $url = '/api/goods/sell_price';
  149. $body = [
  150. 'access_token' => self::access_token(),
  151. 'skuid' => $sku_id
  152. ];
  153. $res = self::curl_post(self::$path.$url,$body);
  154. if($res['error_code'] == 0){
  155. return $res['result'];
  156. }
  157. return false;
  158. }
  159. /**
  160. * 商品是否有货(未更改)
  161. * @param int $sku_id 商品sku_id
  162. * @param int $address_id 地址id(区id或者接到id)
  163. * @param int $goods_num 商品数量
  164. * @return array|boolean
  165. * @author lin
  166. * @created 2022年5月19日11:31:59
  167. */
  168. public static function stock($sku_id,$address_id,$goods_num){
  169. $url = '/api/goods/stock';
  170. $body = [
  171. 'access_token' => self::access_token(),
  172. 'sku_id' => $sku_id,
  173. 'address_id' => $address_id,
  174. 'goods_num' => $goods_num
  175. ];
  176. $res = self::curl_post(self::$path.$url,$body);
  177. if($res['error_code'] == 0){
  178. return $res['result'];
  179. }
  180. return false;
  181. }
  182. /**
  183. * 查询商品上下架(未更改)
  184. * @param string $spu_id 商品spu_id
  185. * @return array|boolean
  186. * @author lin
  187. * @created 2022年5月19日11:46:54
  188. * */
  189. public static function goods_status($spu_id){
  190. $url = '/api/goods/status';
  191. $body = [
  192. 'access_token' => self::access_token(),
  193. 'spu_id' => $spu_id
  194. ];
  195. $res = self::curl_post(self::$path.$url,$body);
  196. if($res['error_code'] == 0){
  197. return $res['result'];
  198. }
  199. return false;
  200. }
  201. /**
  202. * 查询商品库存
  203. * @param string $sku_list 商品sku和数量[{"sku_id":123123,"num":1},{"sku_id":123124,"num":1}]
  204. * @param int $address_id 地址id(区id或街道id)
  205. * @return array|boolean
  206. * @author lin
  207. * @created 2022年5月19日11:46:54
  208. */
  209. public static function sku_stock(string $sku_list, int $address_id){
  210. $url = '/api_v2/Goods/stock';
  211. $body = [
  212. 'access_token' => self::access_token(),
  213. 'sku_list' => $sku_list,
  214. 'address_id' => $address_id,
  215. ];
  216. $res = self::curl_post(self::$path.$url,$body);
  217. if($res['error_code'] == 0){
  218. return $res['result'];
  219. }
  220. return false;
  221. }
  222. /**
  223. * 运费查询(下单前调用,确定可售) (未对接)
  224. * @param string $address_id 地址id
  225. * @param array $sku_list sku列表 [{‘sku_id’:80,‘num’:5},{‘sku_id’:81,‘num’:2}]
  226. * @return array|boolean
  227. * @author lin
  228. * @created 2022年5月19日11:46:54
  229. */
  230. public static function freight(string $address_id, array $sku_list){
  231. $url = '/api_v2/order/getFreightPrice';
  232. $body = [
  233. 'access_token' => self::access_token(),
  234. 'address_id' => $address_id,
  235. 'sku_list' => $sku_list
  236. ];
  237. $res = self::curl_post(self::$path.$url,$body);
  238. if($res['error_code'] == 0){
  239. return $res['result'];
  240. }
  241. return false;
  242. }
  243. /**
  244. * 正式下单
  245. * @param string $address_id 地址id
  246. * @param array $sku_list sku列表 [{‘sku_id’:80,‘num’:5},{‘sku_id’:81,‘num’:2}]
  247. * @param string $address 详细地址
  248. * @param string $third_order 订单id
  249. * @param string $other 备注
  250. * @param string $receiver 收货人
  251. * @param string $receiver_phone 收货人手机号
  252. * @return array|boolean
  253. * @author lin
  254. * @created 2022年5月19日13:22:09
  255. */
  256. public static function submit(string $address_id, array $sku_list, string $address, string $third_order, string $other, string $receiver, string $receiver_phone){
  257. $url = '/api_v2/order/submit';
  258. $body = [
  259. 'access_token' => self::access_token(),
  260. 'address_id' => $address_id,
  261. 'sku_list' => $sku_list,
  262. 'receiver' => $receiver,
  263. 'receiver_phone' => $receiver_phone,
  264. 'address' => $address,
  265. 'other' => $other,
  266. 'third_order' => $third_order
  267. ];
  268. // Db::name('log')->insert(['data'=>'供应链发货-------2:']);
  269. $res = self::curl_post(self::$path.$url,$body);
  270. return $res;
  271. }
  272. /**
  273. * 手动发货(未对接)
  274. * @param string $out_order_no 订单编号
  275. * @param string $logistics_number 物流单号
  276. * @param string $logistics_company 物流名称
  277. * @return string|boolean 订单状态值 0失败 1待付款 2待发货 3待收货 4已完成 5已取消 6售后中 7售后完成8拒收
  278. * @author lin
  279. * @created 2022年5月19日11:46:54
  280. */
  281. public static function order_send(string $out_order_no, string $logistics_company, string $logistics_number){
  282. $url = '/api/order/send';
  283. $body = [
  284. 'access_token' => self::access_token(),
  285. 'out_order_no' => $out_order_no,
  286. 'logistics_number' => $logistics_number,
  287. 'logistics_company' => $logistics_company
  288. ];
  289. $res = self::curl_post(self::$path.$url,$body);
  290. if($res['error_code'] == 0){
  291. return $res['result'];
  292. }
  293. return false;
  294. }
  295. /**
  296. * 查询订单状态(未对接)
  297. * @param string $out_order_no 订单编号
  298. * @return string|boolean 订单状态值 0失败 1待付款 2待发货 3待收货 4已完成 5已取消 6售后中 7售后完成8拒收
  299. * @author lin
  300. * @created 2022年5月19日11:46:54
  301. */
  302. public static function order_status(string $out_order_no){
  303. $url = '/api/order/status';
  304. $body = [
  305. 'access_token' => self::access_token(),
  306. 'out_order_no' => $out_order_no,
  307. ];
  308. $res = self::curl_post(self::$path.$url,$body);
  309. if($res['error_code'] == 0){
  310. return $res['result'];
  311. }
  312. return false;
  313. }
  314. /**
  315. * 查询订单详情
  316. * @param string $out_order_no 订单编号
  317. * @return array|boolean
  318. * @author lin
  319. * @created 2022年5月19日11:46:54
  320. */
  321. public static function order_details(string $out_order_no){
  322. $url = '/api_v2/order/getOrderDetail';
  323. $body = [
  324. 'access_token' => self::access_token(),
  325. 'out_order_no' => $out_order_no,
  326. ];
  327. $res = self::curl_post(self::$path.$url,$body);
  328. if($res['error_code'] == 0){
  329. return $res['result'];
  330. }
  331. return false;
  332. }
  333. /**
  334. * 查询配送信息
  335. * @param string $out_order_no 订单编号
  336. * @return array|boolean
  337. * @author lin
  338. * @created 2022年5月19日11:46:54
  339. */
  340. public static function order_track(string $out_order_no){
  341. $url = '/api_v2/order/track';
  342. $body = [
  343. 'access_token' => self::access_token(),
  344. 'out_order_no' => $out_order_no,
  345. ];
  346. $res = self::curl_post(self::$path.$url,$body);
  347. if($res['error_code'] == 0){
  348. return $res['result'];
  349. }
  350. return false;
  351. }
  352. /**
  353. * 确认收货(未对接)
  354. * @param string $out_order_no 订单编号
  355. * @return array|boolean
  356. * @author lin
  357. * @created 2022年5月19日11:46:54
  358. */
  359. public static function confirm_received(string $out_order_no){
  360. $url = '/api/order/confirm_received';
  361. $body = [
  362. 'access_token' => self::access_token(),
  363. 'out_order_no' => $out_order_no,
  364. ];
  365. $res = self::curl_post(self::$path.$url,$body);
  366. if($res['error_code'] == 0){
  367. return $res['result'];
  368. }
  369. return false;
  370. }
  371. /**
  372. * 查询商品是否可售后属性(未对接)
  373. * @param string $out_order_no 订单编号
  374. * @return array|boolean
  375. * @author lin
  376. * @created 2022年5月19日11:46:54
  377. */
  378. public static function after_is_can(string $out_order_no){
  379. $url = '/api/after/is_can';
  380. $body = [
  381. 'access_token' => self::access_token(),
  382. 'out_order_no' => $out_order_no,
  383. ];
  384. $res = self::curl_post(self::$path.$url,$body);
  385. if($res['error_code'] == 0){
  386. return $res['result'];
  387. }
  388. return false;
  389. }
  390. /**
  391. * 申请售后(未对接)
  392. * @param string $out_order_no 订单编号
  393. * @param string $customer_expect 1-退货。2-换货
  394. * @param string $question_desc 申请理由
  395. * @param array $sku_list 多商品规格
  396. * @param string $img 退货图片json['',''],链接
  397. * @return array|boolean
  398. * @author lin
  399. * @created 2022年5月19日11:46:54
  400. */
  401. public static function after_create_apply(string $out_order_no, string $customer_expect, string $question_desc, array $sku_list, string $img){
  402. $url = '/api/after/create_apply';
  403. $body = [
  404. 'access_token' => self::access_token(),
  405. 'plat_order_no' => $out_order_no,
  406. 'after_type' => $customer_expect,
  407. 'explain' => $question_desc,
  408. 'sku_list' => $sku_list,
  409. 'img' => $img
  410. ];
  411. $res = self::curl_post(self::$path.$url,$body);
  412. return $res;
  413. }
  414. /**
  415. * 申请售后(未对接)
  416. * @param string $order_goods_id 订单商品id
  417. * @param string $out_order_no 系统订单号
  418. * @param string $after_type 1=未收货退款,2=退货退款,3=换货补发,4=不退货仅退款
  419. * @param string $explain 售后原因
  420. * @param string $after_num 售后数量
  421. * @return array|boolean
  422. * @author lin
  423. * @created 2022年5月19日11:46:54
  424. */
  425. public static function apply_after_sales(string $order_goods_id,string $out_order_no, string $after_type, string $explain, string $after_num){
  426. $url = '/api/After/applyAfterSales';
  427. $body = [
  428. 'access_token' => self::access_token(),
  429. 'order_goods_id' => $order_goods_id,
  430. 'after_type' => $after_type,
  431. 'after_num' => $after_num,
  432. 'explain' => $explain,
  433. 'plat_order_no' => $out_order_no,
  434. ];
  435. $res = self::curl_post(self::$path.$url,$body);
  436. return $res;
  437. }
  438. /**
  439. * 查询订单是否可售后(未对接)
  440. * @param string $out_order_no 系统订单号
  441. * @return mixed|string
  442. */
  443. public static function is_can_after(string $out_order_no){
  444. $url = '/api/After/isCanAfter';
  445. $body = [
  446. 'access_token' => self::access_token(),
  447. 'order_goods_id' => '',
  448. 'plat_order_no' => $out_order_no,
  449. ];
  450. $res = self::curl_post(self::$path.$url,$body);
  451. return $res;
  452. }
  453. /**
  454. * 售后订单状态(未对接)
  455. * @param string $after_sn 售后订单号
  456. * @return array|boolean
  457. */
  458. public static function after_status_new(string $after_sn){
  459. $url = '/api/after/afterStatus';
  460. $body = [
  461. 'access_token' => self::access_token(),
  462. 'plat_after_no' => $after_sn
  463. ];
  464. return self::curl_post(self::$path.$url,$body);
  465. }
  466. /**
  467. * 售后订单状态(未对接)
  468. * @param string $after_sn 售后订单号
  469. * @return array|boolean
  470. */
  471. public static function after_status(string $after_sn){
  472. $url = '/api/after/status';
  473. $body = [
  474. 'access_token' => self::access_token(),
  475. 'after_sn' => $after_sn
  476. ];
  477. return self::curl_post(self::$path.$url,$body);
  478. }
  479. /**
  480. * 退换货发货信息(未对接)
  481. * @param string $after_sn 售后订单编号。申请售后接口返回
  482. * @param string $logistics_company 1-退货。2-换货
  483. * @param string $logistics_number 申请理由
  484. * @return string|boolean
  485. * @author lin
  486. * @created 2022年5月19日13:40:52
  487. */
  488. public static function after_update_send_info(string $after_sn, string $logistics_number, string $logistics_company){
  489. $url = '/api/after/update_send_info';
  490. $body = [
  491. 'access_token' => self::access_token(),
  492. 'after_sn' => $after_sn,
  493. 'logistics_company' => $logistics_company,
  494. 'logistics_number' => $logistics_number
  495. ];
  496. $res = self::curl_post(self::$path.$url,$body);
  497. if($res['error_code'] == 0){
  498. return $res['result'];
  499. }
  500. return false;
  501. }
  502. /**
  503. * 取消申请售后(未对接)
  504. * @param string $after_sn 售后订单编号。申请售后接口返回
  505. * @param string $out_order_no 订单编号
  506. * @return string|boolean
  507. * @author lin
  508. * @created 2022年5月19日13:40:52
  509. */
  510. public static function after_cancel_apply(string $after_sn, string $out_order_no){
  511. $url = '/api/after/cancel_apply';
  512. $body = [
  513. 'access_token' => self::access_token(),
  514. 'after_sn' => $after_sn,
  515. 'out_order_no' => $out_order_no
  516. ];
  517. $res = self::curl_post(self::$path.$url,$body);
  518. if($res['error_code'] == 0){
  519. return $res['result'];
  520. }
  521. return false;
  522. }
  523. /**
  524. * 获取地区(未对接)
  525. * @param string $code 地区代码,为空查询省级信息,否则查询code的下级
  526. * @return string|boolean
  527. * @author lin
  528. * @created 2022年5月19日13:40:52
  529. */
  530. public static function get_region_code(string $code = null){
  531. $url = '/api/regions/get_region_code';
  532. $body = [
  533. 'access_token' => self::access_token(),
  534. ];
  535. if($code != null) $body['code'] = $code;
  536. $res = self::curl_post(self::$path.$url,$body);
  537. if($res['error_code'] == 0){
  538. return $res['result'];
  539. }
  540. return false;
  541. }
  542. /**
  543. * 获取消息池消息(未对接)
  544. * @param int $type 1-订单状态变动, 2-商品详情, 3-sku价格, 4-商品删除, 5-规格状态变更, 6-规格删除, 7-规格详情, 8-商品状态变更
  545. * @return array|boolean
  546. * @author lin
  547. * @created 2022年5月19日13:55:07
  548. */
  549. public static function MessagePool(int $type){
  550. $url = '/api/MessagePool/get';
  551. $body = [
  552. 'access_token' => self::access_token(),
  553. 'type' => $type,
  554. ];
  555. $res = self::curl_post(self::$path.$url,$body);
  556. if($res['error_code'] == 0){
  557. return $res['result'];
  558. }
  559. return false;
  560. }
  561. public static function getSign($time){
  562. //按照以下顺序将字符串拼接起来 app_id+mobile+timestamp+app_secret 再用md5加密
  563. return md5(self::$app_id.self::$mobile.$time.self::$app_secret);
  564. }
  565. //通过curl发送post请求带body传参
  566. public static function curl_post($url, $postFields = null, $header = array())
  567. {
  568. // Db::name('log')->insert(['data'=>'供应链发货-------3:']);
  569. if ($url != 'https://www.douhuomall.com/api/category/get_list') {
  570. Db::name("log")->insert(array('data' => '供应链【' . $url . '】body:' . json_encode($postFields)));//日志记录
  571. }
  572. $curl = curl_init();
  573. //设置抓取的url
  574. curl_setopt($curl, CURLOPT_URL, $url);
  575. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
  576. curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  577. curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postFields));
  578. // curl_setopt($curl, CURLOPT_TIMEOUT_MS, 500);
  579. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  580. // curl_setopt($curl, CURLOPT_HEADER, 1);
  581. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  582. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  583. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  584. //执行命令
  585. $data = curl_exec($curl);
  586. if(is_null(json_decode($data))){
  587. $data = strstr($data,'{');
  588. }
  589. if ($url != 'https://www.douhuomall.com/api/category/get_list'){
  590. log::info('供应链【'.$url.'】body:'.json_encode($postFields,JSON_UNESCAPED_UNICODE));
  591. log::info('供应链【'.$url.'】headerArray:'.json_encode($header,JSON_UNESCAPED_UNICODE));
  592. log::info('供应链【'.$url.'】data:'.$data);
  593. // Db::name("log")->insert(array('data'=>'供应链【'.$url.'】body:'.json_encode($postFields)));//日志记录
  594. // Db::name("log")->insert(array('data'=>'供应链【'.$url.'】headerArray:'.json_encode($header)));//日志记录
  595. // Db::name("log")->insert(array('data'=>'供应链【'.$url.'】data:'.$data));//日志记录
  596. // dump($data);
  597. }
  598. // 显示错误信息
  599. if (curl_error($curl)) {
  600. return curl_error($curl);
  601. } else {
  602. curl_close($curl);
  603. return json_decode($data, true);
  604. }
  605. }
  606. }