ProductCopyJob.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\jobs\product;
  12. use app\services\product\product\CopyTaobaoServices;
  13. use app\services\product\product\StoreDescriptionServices;
  14. use app\services\product\product\StoreProductServices;
  15. use app\services\product\sku\StoreProductAttrValueServices;
  16. use crmeb\basic\BaseJobs;
  17. use crmeb\services\CacheService;
  18. use crmeb\traits\QueueTrait;
  19. use think\facade\Log;
  20. /**
  21. * 复制商品
  22. * Class ProductCopyJob
  23. * @package app\jobs
  24. */
  25. class ProductCopyJob extends BaseJobs
  26. {
  27. use QueueTrait;
  28. /**
  29. * 下载商品详情图片
  30. * @param $id
  31. * @param $description
  32. * @param $image
  33. * @param $count
  34. * @return bool
  35. * @throws \Psr\SimpleCache\InvalidArgumentException
  36. */
  37. public function copyDescriptionImage($id, $description, $image, $count)
  38. {
  39. try {
  40. /** @var CopyTaobaoServices $copyTaobao */
  41. $copyTaobao = app()->make(CopyTaobaoServices::class);
  42. /** @var StoreDescriptionServices $storeDescriptionServices */
  43. $storeDescriptionServices = app()->make(StoreDescriptionServices::class);
  44. if (is_int(strpos($image, 'http'))) {
  45. $d_image = $image;
  46. } else {
  47. $d_image = 'http://' . ltrim($image, '\//');
  48. }
  49. $description_cache = CacheService::getTokenBucket('desc_images_' . $id);
  50. if ($description_cache === null) {
  51. $description_cache = $description;
  52. CacheService::setTokenBucket('desc_images_count' . $id, 0);
  53. }
  54. $res = $copyTaobao->downloadCopyImage($d_image);
  55. $description_cache = str_replace($image, $res, $description_cache);
  56. $desc_count = CacheService::getTokenBucket('desc_images_count' . $id) + 1;
  57. if ($desc_count == $count) {
  58. CacheService::clearToken('desc_images_' . $id);
  59. CacheService::clearToken('desc_images_count' . $id);
  60. $storeDescriptionServices->saveDescription((int)$id, $description_cache);
  61. } else {
  62. CacheService::setTokenBucket('desc_images_' . $id, $description_cache);
  63. CacheService::setTokenBucket('desc_images_count' . $id, $desc_count);
  64. }
  65. } catch (\Throwable $e) {
  66. Log::error('下载商品详情图片失败,失败原因:' . $e->getMessage());
  67. }
  68. return true;
  69. }
  70. /**
  71. * 下载商品轮播图片
  72. * @param $id
  73. * @param $image
  74. * @param $count
  75. * @return bool
  76. * @throws \Psr\SimpleCache\InvalidArgumentException
  77. */
  78. public function copySliderImage($id, $image, $count)
  79. {
  80. try {
  81. /** @var CopyTaobaoServices $copyTaobao */
  82. $copyTaobao = app()->make(CopyTaobaoServices::class);
  83. /** @var StoreProductServices $StoreProductServices */
  84. $StoreProductServices = app()->make(StoreProductServices::class);
  85. /** @var StoreProductAttrValueServices $StoreProductAttrValueServices */
  86. $StoreProductAttrValueServices = app()->make(StoreProductAttrValueServices::class);
  87. //下载图片
  88. $res = $copyTaobao->downloadCopyImage($image);
  89. //获取缓存中的轮播图
  90. $slider_images = CacheService::getTokenBucket('slider_images_' . $id);
  91. //缓存为null则赋值[]
  92. if ($slider_images === null) $slider_images = [];
  93. //将下载的图片插入数组
  94. array_push($slider_images, $res);
  95. //如果$slider_images中图片数量和传入的$count相等,说明已经下载完成,写入商品表,如果不等则继续插入缓存
  96. if (count($slider_images) == $count) {
  97. CacheService::clearToken('slider_images_' . $id);
  98. $image = $slider_images[0];
  99. $slider_images = $slider_images ? json_encode($slider_images) : '';
  100. $StoreProductServices->update($id, ['slider_image' => $slider_images, 'image' => $image]);
  101. $StoreProductAttrValueServices->update(['product_id' => $id], ['image' => $image]);
  102. } else {
  103. CacheService::setTokenBucket('slider_images_' . $id, $slider_images);
  104. }
  105. } catch (\Throwable $e) {
  106. Log::error('下载商品轮播图片失败,失败原因:' . $e->getMessage());
  107. }
  108. return true;
  109. }
  110. /**
  111. * 采集商品规格,分批队列
  112. * @param $id
  113. * @param $type
  114. * @return bool
  115. */
  116. public function copyAttrImage($id, $type = 0)
  117. {
  118. if (!$id) {
  119. return true;
  120. }
  121. try {
  122. $productAttrValue = app()->make(StoreProductAttrValueServices::class);
  123. $attrValueList = $productAttrValue->getColumn(['product_id' => $id, 'type' => $type], 'image', 'id');
  124. foreach ($attrValueList as $value_id => $value_image) {
  125. ProductCopyJob::dispatch('copyAttrImageDo', [$value_id, $value_image]);
  126. }
  127. } catch (\Throwable $e) {
  128. Log::error('下载商品轮播图片失败,失败原因:' . $e->getMessage());
  129. }
  130. return true;
  131. }
  132. /**
  133. * 采集规格图片
  134. * @param $attrId
  135. * @param $value_image
  136. * @return bool
  137. */
  138. public function copyAttrImageDo($attrId, $value_image)
  139. {
  140. if (!$attrId) {
  141. return true;
  142. }
  143. try {
  144. /** @var CopyTaobaoServices $copyTaobao */
  145. $copyTaobao = app()->make(CopyTaobaoServices::class);
  146. /** @var StoreProductAttrValueServices $StoreProductAttrValueServices */
  147. $StoreProductAttrValueServices = app()->make(StoreProductAttrValueServices::class);
  148. //下载图片
  149. $res = $copyTaobao->downloadCopyImage($value_image);
  150. $StoreProductAttrValueServices->update($attrId, ['image' => $res]);
  151. } catch (\Throwable $e) {
  152. Log::error('下载商品规格图片失败,失败原因:' . $e->getMessage() . '_' . $e->getFile() . '_' . $e->getLine());
  153. }
  154. return true;
  155. }
  156. }