| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Common\Querry;
- use App\Models\GoodsModel;
- use App\Services\BaseService;
- use App\Services\RedisService;
- /**
- * szwego站点采集
- * @author laravel开发员
- * @since 2020/11/11
- * Class szwego
- * @package App\Services\Common
- */
- class Szwego extends BaseService
- {
- // 静态对象
- protected static $instance = null;
- /**
- * 静态入口
- * @return static|null
- */
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = (new static());
- }
- return self::$instance;
- }
- /**
- * 采集数据
- * @param $params
- * @return array|false|mixed
- */
- public function querryData($params)
- {
- $url = isset($params['url'])? $params['url'] : '';
- $tagId = isset($params['tagId'])? trim($params['tagId']) : '';
- $tagName = isset($params['tagName'])? trim($params['tagName']) : '';
- $picNum = isset($params['picNum'])? intval($params['picNum']) : 7;
- $cacheKey = "caches:querry:".md5($url);
- $datas = RedisService::get($cacheKey.':goods_'.$tagId);
- $goods = isset($datas['goods'])? $datas['goods'] : [];
- if($goods){
- return $goods;
- }
- $tokenData = $this->getQuerryToken($url);
- $apiUrl = isset($tokenData['apiUrl'])? $tokenData['apiUrl'] : '';
- $cookie = isset($tokenData['cookie'])? $tokenData['cookie'] : '';
- $shopId = isset($tokenData['shop_id'])? $tokenData['shop_id'] : '';
- $config = config('querry.szwego');
- $url = isset($config['getGoodsByTag'])? $config['getGoodsByTag'] : '';
- $url = sprintf($apiUrl.$url, $shopId);
- $params = $tagId? http_build_query(['tagList'=>urlencode("[$tagId]")]):'';
- $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"];
- $result = grabRequest($url, $header, $params, 'post', 'array', '',10);
- $code = isset($result['errcode'])? $result['errcode'] : '-1';
- $msg = isset($result['errmsg'])? $result['errmsg'] : '';
- $datas = isset($result['result'])? $result['result'] : [];
- $items = isset($datas['items'])? $datas['items'] : [];
- RedisService::set($cacheKey.':goods_result',['url'=> $url,'params'=> $params,'items'=> $items,'date'=>date('Y-m-d H:i:s')], 7200);
- if($code != 0){
- $this->error = $msg? $msg : '2010';
- return false;
- }
- if(empty($items)){
- $this->error = '2010';
- return false;
- }
- $goods = [];
- foreach ($items as $item){
- $imgsSrc = isset($item['imgsSrc'])? $item['imgsSrc'] : [];
- $goodsId = isset($item['goods_id'])? $item['goods_id'] : '';
- if(count($imgsSrc) >= $picNum){
- $data = [
- 'site_url'=> $apiUrl,
- 'goods_id'=> $goodsId,
- 'title'=> isset($item['title'])? $item['title'] : '',
- 'imgsSrc'=> $imgsSrc? $imgsSrc : [],
- 'thumb'=> $imgsSrc? $imgsSrc[0] : '',
- 'is_push'=> $this->checkHasByGoodsId($goodsId),
- 'link'=> isset($item['link'])? $item['link'] : '',
- 'price'=> isset($item['itemPrice'])? $item['itemPrice'] : 0.00,
- 'shop_id'=> isset($item['shop_id'])? $item['shop_id'] : $shopId,
- 'shop_name'=> isset($item['shop_name'])? $item['shop_name'] : '',
- 'sku'=> isset($item['sku']) && $item['sku']? json_encode($item['sku'], 256) : '',
- 'tagsArr'=> isset($item['tags']) ?$item['tags']: [],
- 'tags'=> $tagName?$tagName.'-'.$tagId: $tagId,
- 'create_time'=> isset($item['time_stamp']) && $item['time_stamp']? intval($item['time_stamp']/1000) : time(),
- 'update_time'=> time(),
- ];
- $data['create_time_text'] = $data['create_time']? date('Y-m-d H:i',$data['create_time']) : '';
- $goods[] = $data;
- $data['imgsSrc'] = $data['imgsSrc']? json_encode($data['imgsSrc'], 256) : '';
- unset($data['create_time_text']);
- unset($data['tagsArr']);
- }
- }
- if($goods){
- RedisService::set($cacheKey.':goods_'.$tagId,['url'=> $url,'goods'=> $goods,'date'=>date('Y-m-d H:i:s')], rand(20, 30));
- }else{
- $this->error = '2011';
- return false;
- }
- return $goods;
- }
- /**
- * 验证是否存在
- * @param $goodsId
- * @return bool
- */
- public function checkHasByGoodsId($goodsId)
- {
- if(empty($goodsId)){
- return false;
- }
- $cacheKey = "caches:querryGoods:has_{$goodsId}";
- if($id = RedisService::get($cacheKey)){
- return $id;
- }
- $this->model = new GoodsModel();
- if($id = $this->checkExists('goods_id', $goodsId)){
- RedisService::set($cacheKey, $id, 3 * 24 * 3600);
- return $id;
- }
- return false;
- }
- /**
- * 获取分类
- * @param $params
- * @return array|false|mixed
- */
- public function querryTags($params)
- {
- $url = isset($params['url'])? $params['url'] : '';
- $cacheKey = "caches:querry:".md5($url);
- $datas = RedisService::get($cacheKey.':tags');
- $tags = isset($datas['tags'])? $datas['tags'] : [];
- if($tags){
- return $tags;
- }
- $tokenData = $this->getQuerryToken($url);
- $apiUrl = isset($tokenData['apiUrl'])? $tokenData['apiUrl'] : '';
- $cookie = isset($tokenData['cookie'])? $tokenData['cookie'] : '';
- $shopId = isset($tokenData['shop_id'])? $tokenData['shop_id'] : '';
- $config = config('querry.szwego');
- $url = isset($config['getTags'])? $config['getTags'] : '';
- $url = sprintf($apiUrl.$url, $shopId);
- $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"];
- $result = grabRequest($url, $header, '', 'get', 'array', '',10);
- $code = isset($result['errcode'])? $result['errcode'] : '-1';
- $msg = isset($result['errmsg'])? $result['errmsg'] : '';
- $datas = isset($result['result'])? $result['result'] : [];
- $tags = isset($datas['allTags'])? $datas['allTags'] : [];
- RedisService::set($cacheKey.':tag_result',['url'=> $url,'tags'=> $tags,'date'=>date('Y-m-d H:i:s')], 7200);
- if($code != 0 || empty($tags)){
- $this->error = $msg? $msg : '2010';
- return false;
- }
- RedisService::set($cacheKey.':tags',['url'=> $url,'tags'=> $tags,'date'=>date('Y-m-d H:i:s')], rand(120, 600));
- return $tags;
- }
- /**
- * 采集页面登录信息
- * @param $url
- * @return false|string
- */
- public function getQuerryToken($url)
- {
- $cacheKey = "caches:querry:".md5($url).':token';
- $params = parse_url($url);
- $scheme = isset($params['scheme'])? $params['scheme']:'';
- $host = isset($params['host'])? $params['host']:'';
- $path = isset($params['path'])? $params['path']:'';
- $fragment = isset($params['fragment'])? $params['fragment']:'';
- $apiUrl = $scheme.'://'.$host;
- $datas = explode('/',$fragment);
- $shopId = end($datas);
- $shopId = preg_match("/^_/",$shopId)? $shopId : '';
- if(empty($apiUrl)){
- return false;
- }
- $cookie = getCookie($apiUrl.$path);
- if($cookie){
- RedisService::set($cacheKey, ['url'=> $url,'apiUrl'=>$apiUrl,'cookie'=> $cookie,'shop_id'=> $shopId,'date'=>date('Y-m-d H:i:s')], 7200);
- }
- return ['apiUrl'=>$apiUrl,'cookie'=> $cookie,'shop_id'=> $shopId];
- }
- }
|