GongApiRequest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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_v2/Goods/getGoodsDetail';
  191. $body = [
  192. 'access_token' => self::access_token(),
  193. 'goods_id' => $spu_id,
  194. ];
  195. $res = self::curl_post(self::$path.$url,$body);
  196. if ($res['error_code'] == 2001) {
  197. return $res;
  198. }
  199. if($res['error_code'] == 0){
  200. return $res['result'];
  201. }
  202. return false;
  203. }
  204. /**
  205. * 查询商品库存
  206. * @param string $sku_list 商品sku和数量[{"sku_id":123123,"num":1},{"sku_id":123124,"num":1}]
  207. * @param int $address_id 地址id(区id或街道id)
  208. * @return array|boolean
  209. * @author lin
  210. * @created 2022年5月19日11:46:54
  211. */
  212. public static function sku_stock(string $sku_list, int $address_id){
  213. $url = '/api_v2/Goods/stock';
  214. $body = [
  215. 'access_token' => self::access_token(),
  216. 'sku_list' => $sku_list,
  217. 'address_id' => $address_id,
  218. ];
  219. $res = self::curl_post(self::$path.$url,$body);
  220. if($res['error_code'] == 0){
  221. return $res['result'];
  222. }
  223. return false;
  224. }
  225. /**
  226. * 运费查询(下单前调用,确定可售) (未对接)
  227. * @param string $address_id 地址id
  228. * @param array $sku_list sku列表 [{‘sku_id’:80,‘num’:5},{‘sku_id’:81,‘num’:2}]
  229. * @return array|boolean
  230. * @author lin
  231. * @created 2022年5月19日11:46:54
  232. */
  233. public static function freight(string $address_id, array $sku_list){
  234. $url = '/api_v2/order/getFreightPrice';
  235. $body = [
  236. 'access_token' => self::access_token(),
  237. 'address_id' => $address_id,
  238. 'sku_list' => $sku_list
  239. ];
  240. $res = self::curl_post(self::$path.$url,$body);
  241. if($res['error_code'] == 0){
  242. return $res['result'];
  243. }
  244. return false;
  245. }
  246. /**
  247. * 正式下单
  248. * @param string $address_id 地址id
  249. * @param array $sku_list sku列表 [{‘sku_id’:80,‘num’:5},{‘sku_id’:81,‘num’:2}]
  250. * @param string $address 详细地址
  251. * @param string $third_order 订单id
  252. * @param string $other 备注
  253. * @param string $receiver 收货人
  254. * @param string $receiver_phone 收货人手机号
  255. * @return array|boolean
  256. * @author lin
  257. * @created 2022年5月19日13:22:09
  258. */
  259. public static function submit(string $address_id, array $sku_list, string $address, string $third_order, string $other, string $receiver, string $receiver_phone){
  260. $url = '/api_v2/order/submit';
  261. $body = [
  262. 'access_token' => self::access_token(),
  263. 'address_id' => $address_id,
  264. 'sku_list' => $sku_list,
  265. 'receiver' => $receiver,
  266. 'receiver_phone' => $receiver_phone,
  267. 'address' => $address,
  268. 'other' => $other,
  269. 'third_order' => $third_order
  270. ];
  271. // Db::name('log')->insert(['data'=>'供应链发货-------2:']);
  272. $res = self::curl_post(self::$path.$url,$body);
  273. return $res;
  274. }
  275. /**
  276. * 手动发货(未对接)
  277. * @param string $out_order_no 订单编号
  278. * @param string $logistics_number 物流单号
  279. * @param string $logistics_company 物流名称
  280. * @return string|boolean 订单状态值 0失败 1待付款 2待发货 3待收货 4已完成 5已取消 6售后中 7售后完成8拒收
  281. * @author lin
  282. * @created 2022年5月19日11:46:54
  283. */
  284. public static function order_send(string $out_order_no, string $logistics_company, string $logistics_number){
  285. $url = '/api/order/send';
  286. $body = [
  287. 'access_token' => self::access_token(),
  288. 'out_order_no' => $out_order_no,
  289. 'logistics_number' => $logistics_number,
  290. 'logistics_company' => $logistics_company
  291. ];
  292. $res = self::curl_post(self::$path.$url,$body);
  293. if($res['error_code'] == 0){
  294. return $res['result'];
  295. }
  296. return false;
  297. }
  298. /**
  299. * 查询订单状态(未对接)
  300. * @param string $out_order_no 订单编号
  301. * @return string|boolean 订单状态值 0失败 1待付款 2待发货 3待收货 4已完成 5已取消 6售后中 7售后完成8拒收
  302. * @author lin
  303. * @created 2022年5月19日11:46:54
  304. */
  305. public static function order_status(string $out_order_no){
  306. $url = '/api/order/status';
  307. $body = [
  308. 'access_token' => self::access_token(),
  309. 'out_order_no' => $out_order_no,
  310. ];
  311. $res = self::curl_post(self::$path.$url,$body);
  312. if($res['error_code'] == 0){
  313. return $res['result'];
  314. }
  315. return false;
  316. }
  317. /**
  318. * 查询订单详情
  319. * @param string $out_order_no 订单编号
  320. * @return array|boolean
  321. * @author lin
  322. * @created 2022年5月19日11:46:54
  323. */
  324. public static function order_details(string $out_order_no){
  325. $url = '/api_v2/order/getOrderDetail';
  326. $body = [
  327. 'access_token' => self::access_token(),
  328. 'out_order_no' => $out_order_no,
  329. ];
  330. $res = self::curl_post(self::$path.$url,$body);
  331. if($res['error_code'] == 0){
  332. return $res['result'];
  333. }
  334. return false;
  335. }
  336. /**
  337. * 查询配送信息
  338. * @param string $out_order_no 订单编号
  339. * @return array|boolean
  340. * @author lin
  341. * @created 2022年5月19日11:46:54
  342. */
  343. public static function order_track(string $out_order_no){
  344. $url = '/api_v2/order/track';
  345. $body = [
  346. 'access_token' => self::access_token(),
  347. 'out_order_no' => $out_order_no,
  348. ];
  349. $res = self::curl_post(self::$path.$url,$body);
  350. if($res['error_code'] == 0){
  351. return $res['result'];
  352. }
  353. return false;
  354. }
  355. /**
  356. * 确认收货(未对接)
  357. * @param string $out_order_no 订单编号
  358. * @return array|boolean
  359. * @author lin
  360. * @created 2022年5月19日11:46:54
  361. */
  362. public static function confirm_received(string $out_order_no){
  363. $url = '/api/order/confirm_received';
  364. $body = [
  365. 'access_token' => self::access_token(),
  366. 'out_order_no' => $out_order_no,
  367. ];
  368. $res = self::curl_post(self::$path.$url,$body);
  369. if($res['error_code'] == 0){
  370. return $res['result'];
  371. }
  372. return false;
  373. }
  374. /**
  375. * 查询商品是否可售后属性(未对接)
  376. * @param string $out_order_no 订单编号
  377. * @return array|boolean
  378. * @author lin
  379. * @created 2022年5月19日11:46:54
  380. */
  381. public static function after_is_can(string $out_order_no){
  382. $url = '/api/after/is_can';
  383. $body = [
  384. 'access_token' => self::access_token(),
  385. 'out_order_no' => $out_order_no,
  386. ];
  387. $res = self::curl_post(self::$path.$url,$body);
  388. if($res['error_code'] == 0){
  389. return $res['result'];
  390. }
  391. return false;
  392. }
  393. /**
  394. * 申请售后(未对接)
  395. * @param string $out_order_no 订单编号
  396. * @param string $customer_expect 1-退货。2-换货
  397. * @param string $question_desc 申请理由
  398. * @param array $sku_list 多商品规格
  399. * @param string $img 退货图片json['',''],链接
  400. * @return array|boolean
  401. * @author lin
  402. * @created 2022年5月19日11:46:54
  403. */
  404. public static function after_create_apply(string $out_order_no, string $customer_expect, string $question_desc, array $sku_list, string $img){
  405. $url = '/api/after/create_apply';
  406. $body = [
  407. 'access_token' => self::access_token(),
  408. 'plat_order_no' => $out_order_no,
  409. 'after_type' => $customer_expect,
  410. 'explain' => $question_desc,
  411. 'sku_list' => $sku_list,
  412. 'img' => $img
  413. ];
  414. $res = self::curl_post(self::$path.$url,$body);
  415. return $res;
  416. }
  417. /**
  418. * 申请售后(未对接)
  419. * @param string $order_goods_id 订单商品id
  420. * @param string $out_order_no 系统订单号
  421. * @param string $after_type 1=未收货退款,2=退货退款,3=换货补发,4=不退货仅退款
  422. * @param string $explain 售后原因
  423. * @param string $after_num 售后数量
  424. * @return array|boolean
  425. * @author lin
  426. * @created 2022年5月19日11:46:54
  427. */
  428. public static function apply_after_sales(string $order_goods_id,string $out_order_no, string $after_type, string $explain, string $after_num){
  429. $url = '/api/After/applyAfterSales';
  430. $body = [
  431. 'access_token' => self::access_token(),
  432. 'order_goods_id' => $order_goods_id,
  433. 'after_type' => $after_type,
  434. 'after_num' => $after_num,
  435. 'explain' => $explain,
  436. 'plat_order_no' => $out_order_no,
  437. ];
  438. $res = self::curl_post(self::$path.$url,$body);
  439. return $res;
  440. }
  441. /**
  442. * 查询订单是否可售后(未对接)
  443. * @param string $out_order_no 系统订单号
  444. * @return mixed|string
  445. */
  446. public static function is_can_after(string $out_order_no){
  447. $url = '/api/After/isCanAfter';
  448. $body = [
  449. 'access_token' => self::access_token(),
  450. 'order_goods_id' => '',
  451. 'plat_order_no' => $out_order_no,
  452. ];
  453. $res = self::curl_post(self::$path.$url,$body);
  454. return $res;
  455. }
  456. /**
  457. * 售后订单状态(未对接)
  458. * @param string $after_sn 售后订单号
  459. * @return array|boolean
  460. */
  461. public static function after_status_new(string $after_sn){
  462. $url = '/api/after/afterStatus';
  463. $body = [
  464. 'access_token' => self::access_token(),
  465. 'plat_after_no' => $after_sn
  466. ];
  467. return self::curl_post(self::$path.$url,$body);
  468. }
  469. /**
  470. * 售后订单状态(未对接)
  471. * @param string $after_sn 售后订单号
  472. * @return array|boolean
  473. */
  474. public static function after_status(string $after_sn){
  475. $url = '/api/after/status';
  476. $body = [
  477. 'access_token' => self::access_token(),
  478. 'after_sn' => $after_sn
  479. ];
  480. return self::curl_post(self::$path.$url,$body);
  481. }
  482. /**
  483. * 退换货发货信息(未对接)
  484. * @param string $after_sn 售后订单编号。申请售后接口返回
  485. * @param string $logistics_company 1-退货。2-换货
  486. * @param string $logistics_number 申请理由
  487. * @return string|boolean
  488. * @author lin
  489. * @created 2022年5月19日13:40:52
  490. */
  491. public static function after_update_send_info(string $after_sn, string $logistics_number, string $logistics_company){
  492. $url = '/api/after/update_send_info';
  493. $body = [
  494. 'access_token' => self::access_token(),
  495. 'after_sn' => $after_sn,
  496. 'logistics_company' => $logistics_company,
  497. 'logistics_number' => $logistics_number
  498. ];
  499. $res = self::curl_post(self::$path.$url,$body);
  500. if($res['error_code'] == 0){
  501. return $res['result'];
  502. }
  503. return false;
  504. }
  505. /**
  506. * 取消申请售后(未对接)
  507. * @param string $after_sn 售后订单编号。申请售后接口返回
  508. * @param string $out_order_no 订单编号
  509. * @return string|boolean
  510. * @author lin
  511. * @created 2022年5月19日13:40:52
  512. */
  513. public static function after_cancel_apply(string $after_sn, string $out_order_no){
  514. $url = '/api/after/cancel_apply';
  515. $body = [
  516. 'access_token' => self::access_token(),
  517. 'after_sn' => $after_sn,
  518. 'out_order_no' => $out_order_no
  519. ];
  520. $res = self::curl_post(self::$path.$url,$body);
  521. if($res['error_code'] == 0){
  522. return $res['result'];
  523. }
  524. return false;
  525. }
  526. /**
  527. * 获取地区(未对接)
  528. * @param string $code 地区代码,为空查询省级信息,否则查询code的下级
  529. * @return string|boolean
  530. * @author lin
  531. * @created 2022年5月19日13:40:52
  532. */
  533. public static function get_region_code(string $code = null){
  534. $url = '/api/regions/get_region_code';
  535. $body = [
  536. 'access_token' => self::access_token(),
  537. ];
  538. if($code != null) $body['code'] = $code;
  539. $res = self::curl_post(self::$path.$url,$body);
  540. if($res['error_code'] == 0){
  541. return $res['result'];
  542. }
  543. return false;
  544. }
  545. /**
  546. * 获取消息池消息(未对接)
  547. * @param int $type 1-订单状态变动, 2-商品详情, 3-sku价格, 4-商品删除, 5-规格状态变更, 6-规格删除, 7-规格详情, 8-商品状态变更
  548. * @return array|boolean
  549. * @author lin
  550. * @created 2022年5月19日13:55:07
  551. */
  552. public static function MessagePool(int $type){
  553. $url = '/api/MessagePool/get';
  554. $body = [
  555. 'access_token' => self::access_token(),
  556. 'type' => $type,
  557. ];
  558. $res = self::curl_post(self::$path.$url,$body);
  559. if($res['error_code'] == 0){
  560. return $res['result'];
  561. }
  562. return false;
  563. }
  564. public static function getSign($time){
  565. //按照以下顺序将字符串拼接起来 app_id+mobile+timestamp+app_secret 再用md5加密
  566. return md5(self::$app_id.self::$mobile.$time.self::$app_secret);
  567. }
  568. //通过curl发送post请求带body传参
  569. public static function curl_post($url, $postFields = null, $header = array())
  570. {
  571. // Db::name('log')->insert(['data'=>'供应链发货-------3:']);
  572. if ($url != 'https://www.douhuomall.com/api/category/get_list') {
  573. Db::name("log")->insert(array('data' => '供应链【' . $url . '】body:' . json_encode($postFields)));//日志记录
  574. }
  575. $curl = curl_init();
  576. //设置抓取的url
  577. curl_setopt($curl, CURLOPT_URL, $url);
  578. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
  579. curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  580. curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postFields));
  581. // curl_setopt($curl, CURLOPT_TIMEOUT_MS, 500);
  582. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  583. // curl_setopt($curl, CURLOPT_HEADER, 1);
  584. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  585. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  586. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  587. //执行命令
  588. $data = curl_exec($curl);
  589. if(is_null(json_decode($data))){
  590. $data = strstr($data,'{');
  591. }
  592. if ($url != 'https://www.douhuomall.com/api/category/get_list'){
  593. // log::info('供应链【'.$url.'】body:'.json_encode($postFields,JSON_UNESCAPED_UNICODE));
  594. // log::info('供应链【'.$url.'】headerArray:'.json_encode($header,JSON_UNESCAPED_UNICODE));
  595. // log::info('供应链【'.$url.'】data:'.$data);
  596. // Db::name("log")->insert(array('data'=>'供应链【'.$url.'】body:'.json_encode($postFields)));//日志记录
  597. // Db::name("log")->insert(array('data'=>'供应链【'.$url.'】headerArray:'.json_encode($header)));//日志记录
  598. // Db::name("log")->insert(array('data'=>'供应链【'.$url.'】data:'.$data));//日志记录
  599. // dump($data);
  600. }
  601. // 显示错误信息
  602. if (curl_error($curl)) {
  603. return curl_error($curl);
  604. } else {
  605. curl_close($curl);
  606. return json_decode($data, true);
  607. }
  608. }
  609. }