GongApiRequestBefen.php 20 KB

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