GongApiRequest.php 20 KB

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