Szwego.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Common\Querry;
  12. use App\Models\GoodsModel;
  13. use App\Services\BaseService;
  14. use App\Services\RedisService;
  15. /**
  16. * szwego站点采集
  17. * @author laravel开发员
  18. * @since 2020/11/11
  19. * Class szwego
  20. * @package App\Services\Common
  21. */
  22. class Szwego extends BaseService
  23. {
  24. // 静态对象
  25. protected static $instance = null;
  26. /**
  27. * 静态入口
  28. * @return static|null
  29. */
  30. public static function make()
  31. {
  32. if (!self::$instance) {
  33. self::$instance = (new static());
  34. }
  35. return self::$instance;
  36. }
  37. /**
  38. * 采集数据
  39. * @param $params
  40. * @return array|false|mixed
  41. */
  42. public function querryData($params)
  43. {
  44. $url = isset($params['url'])? $params['url'] : '';
  45. $tagId = isset($params['tagId'])? trim($params['tagId']) : '';
  46. $tagName = isset($params['tagName'])? trim($params['tagName']) : '';
  47. $picNum = isset($params['picNum'])? intval($params['picNum']) : 7;
  48. $cacheKey = "caches:querry:".md5($url);
  49. $datas = RedisService::get($cacheKey.':goods_'.$tagId);
  50. $goods = isset($datas['goods'])? $datas['goods'] : [];
  51. if($goods){
  52. return $goods;
  53. }
  54. $tokenData = $this->getQuerryToken($url);
  55. $apiUrl = isset($tokenData['apiUrl'])? $tokenData['apiUrl'] : '';
  56. $cookie = isset($tokenData['cookie'])? $tokenData['cookie'] : '';
  57. $shopId = isset($tokenData['shop_id'])? $tokenData['shop_id'] : '';
  58. $config = config('querry.szwego');
  59. $url = isset($config['getGoodsByTag'])? $config['getGoodsByTag'] : '';
  60. $url = sprintf($apiUrl.$url, $shopId);
  61. $params = $tagId? http_build_query(['tagList'=>urlencode("[$tagId]")]):'';
  62. $header = ["Content-Type: application/x-www-form-urlencoded;","User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36","Cookie: {$cookie}","Origin: {$apiUrl}","Connection: keep-alive"];
  63. $result = grabRequest($url, $header, $params, 'post', 'array', '',10);
  64. $code = isset($result['errcode'])? $result['errcode'] : '-1';
  65. $msg = isset($result['errmsg'])? $result['errmsg'] : '';
  66. $datas = isset($result['result'])? $result['result'] : [];
  67. $items = isset($datas['items'])? $datas['items'] : [];
  68. RedisService::set($cacheKey.':goods_result',['url'=> $url,'params'=> $params,'items'=> $items,'date'=>date('Y-m-d H:i:s')], 7200);
  69. if($code != 0){
  70. $this->error = $msg? $msg : '2010';
  71. return false;
  72. }
  73. if(empty($items)){
  74. $this->error = '2010';
  75. return false;
  76. }
  77. $goods = [];
  78. foreach ($items as $item){
  79. $imgsSrc = isset($item['imgsSrc'])? $item['imgsSrc'] : [];
  80. $goodsId = isset($item['goods_id'])? $item['goods_id'] : '';
  81. if(count($imgsSrc) >= $picNum){
  82. $data = [
  83. 'site_url'=> $apiUrl,
  84. 'goods_id'=> $goodsId,
  85. 'title'=> isset($item['title'])? $item['title'] : '',
  86. 'imgsSrc'=> $imgsSrc? $imgsSrc : [],
  87. 'thumb'=> $imgsSrc? $imgsSrc[0] : '',
  88. 'is_push'=> $this->checkHasByGoodsId($goodsId),
  89. 'link'=> isset($item['link'])? $item['link'] : '',
  90. 'price'=> isset($item['itemPrice'])? $item['itemPrice'] : 0.00,
  91. 'shop_id'=> isset($item['shop_id'])? $item['shop_id'] : $shopId,
  92. 'shop_name'=> isset($item['shop_name'])? $item['shop_name'] : '',
  93. 'sku'=> isset($item['sku']) && $item['sku']? json_encode($item['sku'], 256) : '',
  94. 'tagsArr'=> isset($item['tags']) ?$item['tags']: [],
  95. 'tags'=> $tagName?$tagName.'-'.$tagId: $tagId,
  96. 'create_time'=> isset($item['time_stamp']) && $item['time_stamp']? intval($item['time_stamp']/1000) : time(),
  97. 'update_time'=> time(),
  98. ];
  99. $data['create_time_text'] = $data['create_time']? date('Y-m-d H:i',$data['create_time']) : '';
  100. $goods[] = $data;
  101. $data['imgsSrc'] = $data['imgsSrc']? json_encode($data['imgsSrc'], 256) : '';
  102. unset($data['create_time_text']);
  103. unset($data['tagsArr']);
  104. }
  105. }
  106. if($goods){
  107. RedisService::set($cacheKey.':goods_'.$tagId,['url'=> $url,'goods'=> $goods,'date'=>date('Y-m-d H:i:s')], rand(20, 30));
  108. }else{
  109. $this->error = '2011';
  110. return false;
  111. }
  112. return $goods;
  113. }
  114. /**
  115. * 验证是否存在
  116. * @param $goodsId
  117. * @return bool
  118. */
  119. public function checkHasByGoodsId($goodsId)
  120. {
  121. if(empty($goodsId)){
  122. return false;
  123. }
  124. $cacheKey = "caches:querryGoods:has_{$goodsId}";
  125. if($id = RedisService::get($cacheKey)){
  126. return $id;
  127. }
  128. $this->model = new GoodsModel();
  129. if($id = $this->checkExists('goods_id', $goodsId)){
  130. RedisService::set($cacheKey, $id, 3 * 24 * 3600);
  131. return $id;
  132. }
  133. return false;
  134. }
  135. /**
  136. * 获取分类
  137. * @param $params
  138. * @return array|false|mixed
  139. */
  140. public function querryTags($params)
  141. {
  142. $url = isset($params['url'])? $params['url'] : '';
  143. $cacheKey = "caches:querry:".md5($url);
  144. $datas = RedisService::get($cacheKey.':tags');
  145. $tags = isset($datas['tags'])? $datas['tags'] : [];
  146. if($tags){
  147. return $tags;
  148. }
  149. $tokenData = $this->getQuerryToken($url);
  150. $apiUrl = isset($tokenData['apiUrl'])? $tokenData['apiUrl'] : '';
  151. $cookie = isset($tokenData['cookie'])? $tokenData['cookie'] : '';
  152. $shopId = isset($tokenData['shop_id'])? $tokenData['shop_id'] : '';
  153. $config = config('querry.szwego');
  154. $url = isset($config['getTags'])? $config['getTags'] : '';
  155. $url = sprintf($apiUrl.$url, $shopId);
  156. $header = ["User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36","Cookie: {$cookie}","Origin: {$apiUrl}","Connection: keep-alive"];
  157. $result = grabRequest($url, $header, '', 'get', 'array', '',10);
  158. $code = isset($result['errcode'])? $result['errcode'] : '-1';
  159. $msg = isset($result['errmsg'])? $result['errmsg'] : '';
  160. $datas = isset($result['result'])? $result['result'] : [];
  161. $tags = isset($datas['allTags'])? $datas['allTags'] : [];
  162. RedisService::set($cacheKey.':tag_result',['url'=> $url,'tags'=> $tags,'date'=>date('Y-m-d H:i:s')], 7200);
  163. if($code != 0 || empty($tags)){
  164. $this->error = $msg? $msg : '2010';
  165. return false;
  166. }
  167. RedisService::set($cacheKey.':tags',['url'=> $url,'tags'=> $tags,'date'=>date('Y-m-d H:i:s')], rand(120, 600));
  168. return $tags;
  169. }
  170. /**
  171. * 采集页面登录信息
  172. * @param $url
  173. * @return false|string
  174. */
  175. public function getQuerryToken($url)
  176. {
  177. $cacheKey = "caches:querry:".md5($url).':token';
  178. $params = parse_url($url);
  179. $scheme = isset($params['scheme'])? $params['scheme']:'';
  180. $host = isset($params['host'])? $params['host']:'';
  181. $path = isset($params['path'])? $params['path']:'';
  182. $fragment = isset($params['fragment'])? $params['fragment']:'';
  183. $apiUrl = $scheme.'://'.$host;
  184. $datas = explode('/',$fragment);
  185. $shopId = end($datas);
  186. $shopId = preg_match("/^_/",$shopId)? $shopId : '';
  187. if(empty($apiUrl)){
  188. return false;
  189. }
  190. $cookie = getCookie($apiUrl.$path);
  191. if($cookie){
  192. RedisService::set($cacheKey, ['url'=> $url,'apiUrl'=>$apiUrl,'cookie'=> $cookie,'shop_id'=> $shopId,'date'=>date('Y-m-d H:i:s')], 7200);
  193. }
  194. return ['apiUrl'=>$apiUrl,'cookie'=> $cookie,'shop_id'=> $shopId];
  195. }
  196. }