ProductCopyJob.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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;
  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. * @return bool
  32. */
  33. public function copyDescriptionImage($id, $description, $image, $count)
  34. {
  35. try {
  36. /** @var CopyTaobaoServices $copyTaobao */
  37. $copyTaobao = app()->make(CopyTaobaoServices::class);
  38. /** @var StoreDescriptionServices $storeDescriptionServices */
  39. $storeDescriptionServices = app()->make(StoreDescriptionServices::class);
  40. if (is_int(strpos($image, 'http'))) {
  41. $d_image = $image;
  42. } else {
  43. $d_image = 'http://' . ltrim($image, '\//');
  44. }
  45. $description_cache = CacheService::get('desc_images_' . $id);
  46. if ($description_cache === null || $description_cache === '') {
  47. $description_cache = $description;
  48. CacheService::set('desc_images_count' . $id, 0);
  49. }
  50. $res = $copyTaobao->downloadCopyImage($d_image);
  51. $description_cache = str_replace($image, $res, $description_cache);
  52. $desc_count = CacheService::get('desc_images_count' . $id) + 1;
  53. if ($desc_count == $count) {
  54. CacheService::delete('desc_images_' . $id);
  55. CacheService::delete('desc_images_count' . $id);
  56. $storeDescriptionServices->saveDescription((int)$id, $description_cache);
  57. } else {
  58. CacheService::set('desc_images_' . $id, $description_cache);
  59. CacheService::set('desc_images_count' . $id, $desc_count);
  60. }
  61. } catch (\Throwable $e) {
  62. Log::error('下载商品详情图片失败,失败原因:' . $e->getMessage() . '_' . $e->getFile() . '_' . $e->getLine());
  63. }
  64. return true;
  65. }
  66. /**
  67. * 下载商品轮播图片
  68. * @param $id
  69. * @return bool
  70. */
  71. public function copySliderImage($id, $image, $count)
  72. {
  73. try {
  74. /** @var CopyTaobaoServices $copyTaobao */
  75. $copyTaobao = app()->make(CopyTaobaoServices::class);
  76. /** @var StoreProductServices $StoreProductServices */
  77. $StoreProductServices = app()->make(StoreProductServices::class);
  78. //下载图片
  79. $res = $copyTaobao->downloadCopyImage($image);
  80. //获取缓存中的轮播图
  81. $slider_images = CacheService::get('slider_images_' . $id) ?? [];
  82. //缓存为null则赋值[]
  83. if ($slider_images === null || $slider_images === '') $slider_images = [];
  84. //将下载的图片插入数组
  85. array_push($slider_images, $res);
  86. //如果$slider_images中图片数量和传入的$count相等,说明已经下载完成,写入商品表,如果不等则继续插入缓存
  87. if (count($slider_images) == $count) {
  88. CacheService::delete('slider_images_' . $id);
  89. $image = $slider_images[0];
  90. $slider_images = $slider_images ? json_encode($slider_images) : '';
  91. $StoreProductServices->update($id, ['slider_image' => $slider_images, 'image' => $image]);
  92. } else {
  93. CacheService::set('slider_images_' . $id, $slider_images);
  94. }
  95. } catch (\Throwable $e) {
  96. Log::error('下载商品轮播图片失败,失败原因:' . $e->getMessage() . '_' . $e->getFile() . '_' . $e->getLine());
  97. }
  98. return true;
  99. }
  100. /**
  101. * 下载商品规格图片
  102. * @param $value_id
  103. * @param $value_image
  104. * @return bool
  105. */
  106. public function copyAttrImage($value_id, $value_image)
  107. {
  108. try {
  109. /** @var CopyTaobaoServices $copyTaobao */
  110. $copyTaobao = app()->make(CopyTaobaoServices::class);
  111. /** @var StoreProductAttrValueServices $StoreProductAttrValueServices */
  112. $StoreProductAttrValueServices = app()->make(StoreProductAttrValueServices::class);
  113. //下载图片
  114. $res = $copyTaobao->downloadCopyImage($value_image);
  115. $StoreProductAttrValueServices->update($value_id, ['image' => $res]);
  116. } catch (\Throwable $e) {
  117. Log::error('下载商品规格图片失败,失败原因:' . $e->getMessage() . '_' . $e->getFile() . '_' . $e->getLine());
  118. }
  119. return true;
  120. }
  121. }