Grab.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace app\index\service;
  3. use app\index\model\GoodsModel;
  4. use think\Config;
  5. class Grab
  6. {
  7. private static $grab;
  8. protected $config =[];
  9. public $urls = [];
  10. public function __construct($config=[])
  11. {
  12. $defConfig = config();
  13. $this->config = isset($defConfig['grab'])? $defConfig['grab'] : [];
  14. $this->config = $config? $config : $this->config;
  15. $this->urls = isset($this->config['urls'])? $this->config['urls'] : [];
  16. }
  17. /**
  18. * 初始化
  19. * @param array $config
  20. * @return Grab
  21. */
  22. public static function instance($config=[]){
  23. if(!self::$grab){
  24. self::$grab = new Grab($config);
  25. }
  26. return self::$grab;
  27. }
  28. /**
  29. * 抓取数据
  30. * @param string $resource
  31. */
  32. public function grabData($resource='', $refresh = false){
  33. set_time_limit(0);
  34. $urls = isset($this->urls[$resource])? $this->urls[$resource] : '';
  35. $url = isset($urls['url'])? trim($urls['url']) : '';
  36. if($url){
  37. $goodsList = $productIds = [];
  38. $result = grabRequest($url, '');
  39. $objects = isset($result['objects'])? $result['objects'] : [];
  40. PRedis::set('grabs:'.$resource.':results:'.date('YmdH'), $result, 3600);
  41. foreach($objects as $goods){
  42. // 预售参数
  43. $goodInfo = [];
  44. $productInfo = isset($goods['productInfo'])? $goods['productInfo'] : [];
  45. $goods = isset($productInfo[0])? $productInfo[0] : [];
  46. $merchProduct = isset($goods['merchProduct'])? $goods['merchProduct'] : [];
  47. $productId = isset($merchProduct['id'])? trim($merchProduct['id']) : '';
  48. // 验证商品是否已经更新
  49. if(!$productId || PRedis::get('grabs:'.$resource.':checkGoods:'.$productId)){
  50. continue;
  51. }
  52. $productIds[] = $productId;
  53. $startDate = isset($merchProduct['commerceStartDate'])? trim($merchProduct['commerceStartDate']) : '';
  54. $goodInfo['product_id'] = $productId;
  55. $goodInfo['start_date'] = $startDate? date('Y-m-d H:i:s', strtotime($startDate)) : '';
  56. $publishDate = isset($merchProduct['commercePublishDate'])? trim($merchProduct['commercePublishDate']) : '';
  57. $goodInfo['publish_date'] = $publishDate? date('Y-m-d H:i:s', strtotime($publishDate)) : '';
  58. $goodInfo['publish_type'] = isset($merchProduct['publishType'])? strtolower($merchProduct['publishType']) : '';
  59. $status = isset($merchProduct['status'])? trim($merchProduct['status']) : '';
  60. $goodInfo['status'] = strtolower($status) == 'active'? 1 : 3;
  61. // 价格参数
  62. $merchPrice = isset($goods['merchPrice'])? $goods['merchPrice'] : [];
  63. $goodInfo['msrp'] = isset($merchPrice['msrp'])? floatval($merchPrice['msrp']) : 0.00;
  64. $goodInfo['full_price'] = isset($merchPrice['fullPrice'])? floatval($merchPrice['fullPrice']) : 0.00;
  65. $goodInfo['current_price'] = isset($merchPrice['currentPrice'])? floatval($merchPrice['currentPrice']) : 0.00;
  66. $goodInfo['currency'] = isset($merchPrice['currency'])? trim($merchPrice['currency']) : 'CNY';
  67. $goodInfo['discounted'] = isset($merchPrice['discounted'])? intval($merchPrice['discounted']) : 0;
  68. $goodInfo['country'] = isset($merchPrice['country'])? $merchPrice['country'] : 'CN';
  69. // 可用状态
  70. $availability = isset($goods['availability'])? $goods['availability'] : [];
  71. $goodInfo['available'] = isset($availability['available'])? intval($availability['available']) : 0;
  72. // 商品信息
  73. $productContent = isset($goods['productContent'])? $goods['productContent'] : [];
  74. $goodInfo['colors_txt'] = isset($productContent['colorDescription'])? $productContent['colorDescription'] : '';
  75. $colors = isset($productContent['colors'])? $productContent['colors'] : [];
  76. $goodInfo['colors'] = is_array($colors) && $colors? serialize($colors) : '';
  77. $goodInfo['slug'] = isset($productContent['slug'])? $productContent['slug'] : '';
  78. $goodInfo['full_title'] = isset($productContent['fullTitle'])? $productContent['fullTitle'] : '';
  79. $goodInfo['title'] = isset($productContent['title'])? $productContent['title'] : '';
  80. $goodInfo['subtitle'] = isset($productContent['subtitle'])? $productContent['subtitle'] : '';
  81. $goodInfo['description'] = isset($productContent['description'])? $productContent['description'] : '';
  82. $goodInfo['pdpGeneral'] = isset($productContent['pdpGeneral'])? $productContent['pdpGeneral'] : '';
  83. // 相册数据
  84. $imgUrls = isset($goods['imageUrls'])? $goods['imageUrls'] : [];
  85. $goodInfo['thumb'] = isset($imgUrls['productImageUrl'])? $imgUrls['productImageUrl'] : '';
  86. $goodsList[] = $goodInfo;
  87. PRedis::rpush("queens:grabs_{$resource}:goods_".date('YmdH'), json_encode($goodInfo, 256));
  88. PRedis::expire("queens:grabs_{$resource}:goods_".date('YmdH'), 3 * 3600);
  89. PRedis::set('grabs:'.$resource.':checkGoods:'.$productId, $goodInfo, 3*24*3600);
  90. }
  91. PRedis::set('grabs:'.$resource.':goods:'.date('YmdH'), $goodsList, 7*24*3600);
  92. }
  93. return $productIds;
  94. }
  95. }