| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- namespace app\index\service;
- use app\index\model\GoodsModel;
- use think\Config;
- class Grab
- {
- private static $grab;
- protected $config =[];
- public $urls = [];
- public function __construct($config=[])
- {
- $defConfig = config();
- $this->config = isset($defConfig['grab'])? $defConfig['grab'] : [];
- $this->config = $config? $config : $this->config;
- $this->urls = isset($this->config['urls'])? $this->config['urls'] : [];
- }
- /**
- * 初始化
- * @param array $config
- * @return Grab
- */
- public static function instance($config=[]){
- if(!self::$grab){
- self::$grab = new Grab($config);
- }
- return self::$grab;
- }
- /**
- * 抓取数据
- * @param string $resource
- */
- public function grabData($resource='', $refresh = false){
- set_time_limit(0);
- $urls = isset($this->urls[$resource])? $this->urls[$resource] : '';
- $url = isset($urls['url'])? trim($urls['url']) : '';
- if($url){
- $goodsList = $productIds = [];
- $result = grabRequest($url, '');
- $objects = isset($result['objects'])? $result['objects'] : [];
- PRedis::set('grabs:'.$resource.':results:'.date('YmdH'), $result, 3600);
- foreach($objects as $goods){
- // 预售参数
- $goodInfo = [];
- $productInfo = isset($goods['productInfo'])? $goods['productInfo'] : [];
- $goods = isset($productInfo[0])? $productInfo[0] : [];
- $merchProduct = isset($goods['merchProduct'])? $goods['merchProduct'] : [];
- $productId = isset($merchProduct['id'])? trim($merchProduct['id']) : '';
- // 验证商品是否已经更新
- if(!$productId || PRedis::get('grabs:'.$resource.':checkGoods:'.$productId)){
- continue;
- }
- $productIds[] = $productId;
- $startDate = isset($merchProduct['commerceStartDate'])? trim($merchProduct['commerceStartDate']) : '';
- $goodInfo['product_id'] = $productId;
- $goodInfo['start_date'] = $startDate? date('Y-m-d H:i:s', strtotime($startDate)) : '';
- $publishDate = isset($merchProduct['commercePublishDate'])? trim($merchProduct['commercePublishDate']) : '';
- $goodInfo['publish_date'] = $publishDate? date('Y-m-d H:i:s', strtotime($publishDate)) : '';
- $goodInfo['publish_type'] = isset($merchProduct['publishType'])? strtolower($merchProduct['publishType']) : '';
- $status = isset($merchProduct['status'])? trim($merchProduct['status']) : '';
- $goodInfo['status'] = strtolower($status) == 'active'? 1 : 3;
- // 价格参数
- $merchPrice = isset($goods['merchPrice'])? $goods['merchPrice'] : [];
- $goodInfo['msrp'] = isset($merchPrice['msrp'])? floatval($merchPrice['msrp']) : 0.00;
- $goodInfo['full_price'] = isset($merchPrice['fullPrice'])? floatval($merchPrice['fullPrice']) : 0.00;
- $goodInfo['current_price'] = isset($merchPrice['currentPrice'])? floatval($merchPrice['currentPrice']) : 0.00;
- $goodInfo['currency'] = isset($merchPrice['currency'])? trim($merchPrice['currency']) : 'CNY';
- $goodInfo['discounted'] = isset($merchPrice['discounted'])? intval($merchPrice['discounted']) : 0;
- $goodInfo['country'] = isset($merchPrice['country'])? $merchPrice['country'] : 'CN';
- // 可用状态
- $availability = isset($goods['availability'])? $goods['availability'] : [];
- $goodInfo['available'] = isset($availability['available'])? intval($availability['available']) : 0;
- // 商品信息
- $productContent = isset($goods['productContent'])? $goods['productContent'] : [];
- $goodInfo['colors_txt'] = isset($productContent['colorDescription'])? $productContent['colorDescription'] : '';
- $colors = isset($productContent['colors'])? $productContent['colors'] : [];
- $goodInfo['colors'] = is_array($colors) && $colors? serialize($colors) : '';
- $goodInfo['slug'] = isset($productContent['slug'])? $productContent['slug'] : '';
- $goodInfo['full_title'] = isset($productContent['fullTitle'])? $productContent['fullTitle'] : '';
- $goodInfo['title'] = isset($productContent['title'])? $productContent['title'] : '';
- $goodInfo['subtitle'] = isset($productContent['subtitle'])? $productContent['subtitle'] : '';
- $goodInfo['description'] = isset($productContent['description'])? $productContent['description'] : '';
- $goodInfo['pdpGeneral'] = isset($productContent['pdpGeneral'])? $productContent['pdpGeneral'] : '';
- // 相册数据
- $imgUrls = isset($goods['imageUrls'])? $goods['imageUrls'] : [];
- $goodInfo['thumb'] = isset($imgUrls['productImageUrl'])? $imgUrls['productImageUrl'] : '';
- $goodsList[] = $goodInfo;
- PRedis::rpush("queens:grabs_{$resource}:goods_".date('YmdH'), json_encode($goodInfo, 256));
- PRedis::expire("queens:grabs_{$resource}:goods_".date('YmdH'), 3 * 3600);
- PRedis::set('grabs:'.$resource.':checkGoods:'.$productId, $goodInfo, 3*24*3600);
- }
- PRedis::set('grabs:'.$resource.':goods:'.date('YmdH'), $goodsList, 7*24*3600);
- }
- return $productIds;
- }
- }
|