作者 lyh

Merge branch 'master' of http://47.244.231.31:8099/zhl/globalso-v6 into lyh-server

@@ -4,10 +4,10 @@ namespace App\Console\Commands\Project; @@ -4,10 +4,10 @@ namespace App\Console\Commands\Project;
4 4
5 use App\Helper\Arr; 5 use App\Helper\Arr;
6 use App\Models\Product\Product; 6 use App\Models\Product\Product;
7 -use App\Models\Project\Project;  
8 use App\Services\ProjectServer; 7 use App\Services\ProjectServer;
9 use Illuminate\Console\Command; 8 use Illuminate\Console\Command;
10 use Illuminate\Support\Facades\DB; 9 use Illuminate\Support\Facades\DB;
  10 +use Illuminate\Support\Facades\Redis;
11 11
12 class ThumbProjectImage extends Command 12 class ThumbProjectImage extends Command
13 { 13 {
@@ -16,7 +16,7 @@ class ThumbProjectImage extends Command @@ -16,7 +16,7 @@ class ThumbProjectImage extends Command
16 * 16 *
17 * @var string 17 * @var string
18 */ 18 */
19 - protected $signature = 'thumb_project_image {project_id}'; 19 + protected $signature = 'thumb_project_image';
20 20
21 /** 21 /**
22 * The console command description. 22 * The console command description.
@@ -27,79 +27,49 @@ class ThumbProjectImage extends Command @@ -27,79 +27,49 @@ class ThumbProjectImage extends Command
27 27
28 public function handle() 28 public function handle()
29 { 29 {
30 - $project_id = $this->argument('project_id');  
31 -  
32 - if ($project_id > 0) {  
33 - //指定项目  
34 - $this->output('project_id:' . $project_id . ' | start');  
35 -  
36 - $project_info = ProjectServer::useProject($project_id);  
37 -  
38 - if ($project_info) {  
39 - $thumb_w = $project_info->deploy_build->thumb_w ?? 0;  
40 - Product::select(['id', 'project_id', 'thumb'])->chunk(100, function ($products) use ($thumb_w) {  
41 - foreach ($products as $product) {  
42 - $thumb = $product->thumb;  
43 - if (isset($thumb['url']) && $thumb['url']) {  
44 - $new_thumb = thumbImageByUrl($thumb['url'], $thumb_w);  
45 - if ($new_thumb != $thumb['url']) {  
46 - $thumb['url'] = $new_thumb;  
47 - $json_thumb = Arr::a2s($thumb);  
48 - if (strlen($json_thumb) <= 500) {  
49 - $product->timestamps = false;  
50 - $product->thumb = $json_thumb;  
51 - $product->save();  
52 -  
53 - $this->output('project_id:' . $product->project_id . ',product_id:' . $product->id . ' | success');  
54 - }  
55 - }  
56 - }  
57 - }  
58 - });  
59 -  
60 - DB::disconnect('custom_mysql');  
61 - } 30 + while (true) {
  31 + $this->start_thumb();
  32 + }
  33 + }
62 34
63 - $this->output('project_id:' . $project_id . ' | end');  
64 - } else {  
65 - //所有项目  
66 - $projectModel = new Project();  
67 - $list = $projectModel->list(['delete_status' => 0, 'is_upgrade' => 0, 'type' => ['in', [1, 2, 3, 4, 6]]], 'id', ['id'], 'asc');  
68 - foreach ($list as $k => $v) {  
69 - $project_id = $v['id']; 35 + public function start_thumb()
  36 + {
  37 + $key = 'thumb_project_image';
  38 + $project_id = Redis::rpop($key);
  39 + if (empty($project_id)) {
  40 + sleep(60);
  41 + return true;
  42 + }
70 43
71 - $this->output('project_id:' . $project_id . ' | start'); 44 + $this->output('project_id:' . $project_id . ' | start');
72 45
73 - $project_info = ProjectServer::useProject($project_id); 46 + $project_info = ProjectServer::useProject($project_id);
74 47
75 - if ($project_info) {  
76 - $thumb_w = $project_info->deploy_build->thumb_w ?? 0;  
77 - Product::select(['id', 'project_id', 'thumb'])->chunk(100, function ($products) use ($thumb_w) {  
78 - foreach ($products as $product) {  
79 - $thumb = $product->thumb;  
80 - if (isset($thumb['url']) && $thumb['url']) {  
81 - $new_thumb = thumbImageByUrl($thumb['url'], $thumb_w);  
82 - if ($new_thumb != $thumb['url']) {  
83 - $thumb['url'] = $new_thumb;  
84 - $json_thumb = Arr::a2s($thumb);  
85 - if (strlen($json_thumb) <= 500) {  
86 - $product->timestamps = false;  
87 - $product->thumb = $json_thumb;  
88 - $product->save(); 48 + if ($project_info) {
  49 + $thumb_w = $project_info->deploy_build->thumb_w ?? 0;
  50 + Product::select(['id', 'project_id', 'gallery'])->chunk(100, function ($products) use ($thumb_w) {
  51 + foreach ($products as $product) {
  52 + $thumb = $product['gallery'][0] ?? [];
  53 + if (isset($thumb['url']) && $thumb['url']) {
  54 + $thumb['url'] = thumbImageByUrl($thumb['url'], $thumb_w);
  55 + $json_thumb = Arr::a2s($thumb);
  56 + if (strlen($json_thumb) <= 500) {
  57 + $product->timestamps = false;
  58 + $product->thumb = $json_thumb;
  59 + $product->save();
89 60
90 - $this->output('project_id:' . $product->project_id . ',product_id:' . $product->id . ' | success');  
91 - }  
92 - }  
93 - } 61 + $this->output('project_id:' . $product->project_id . ',product_id:' . $product->id . ',thumb:' . $thumb_w . ' | success');
94 } 62 }
95 - });  
96 -  
97 - DB::disconnect('custom_mysql'); 63 + }
98 } 64 }
  65 + });
99 66
100 - $this->output('project_id:' . $project_id . ' | end');  
101 - } 67 + DB::disconnect('custom_mysql');
102 } 68 }
  69 +
  70 + $this->output('project_id:' . $project_id . ' | end');
  71 +
  72 + return true;
103 } 73 }
104 74
105 75
@@ -1468,68 +1468,66 @@ function diffInHours($startTime, $endTime) @@ -1468,68 +1468,66 @@ function diffInHours($startTime, $endTime)
1468 return round($hours, 1); 1468 return round($hours, 1);
1469 } 1469 }
1470 1470
1471 -/**  
1472 - * 通过图片地址压缩图片  
1473 - * @param $url  
1474 - * @param int $width  
1475 - * @return string  
1476 - * @author Akun  
1477 - * @date 2025/09/01 15:18  
1478 - */  
1479 -function thumbImageByUrl($url, $width = 360)  
1480 -{  
1481 - if (empty($url)) {  
1482 - return $url;  
1483 - } 1471 +if (!function_exists('thumbImageByUrl')) {
  1472 + /**
  1473 + * 通过图片地址压缩图片
  1474 + * @param $url
  1475 + * @param int $width
  1476 + * @return string
  1477 + * @author Akun
  1478 + * @date 2025/09/01 15:18
  1479 + */
  1480 + function thumbImageByUrl($url, $width = 360)
  1481 + {
  1482 + if (empty($url)) {
  1483 + return $url;
  1484 + }
1484 1485
1485 - if ($width == 0) {  
1486 - return $url;  
1487 - } 1486 + if ($width == 0) {
  1487 + return $url;
  1488 + }
1488 1489
1489 - if (strpos($url, '_thumb') !== false) {  
1490 - return $url;  
1491 - } 1490 + //获取图片完整访问地址
  1491 + $url_complete = getImageUrl($url, 0, 0, 0);//先用v6-file地址
  1492 + if (strpos($url_complete, 'v6-file') !== false) {
  1493 + $is_exists = checkRemoteFileExists($url_complete);
  1494 + if (!$is_exists) {
  1495 + //不存在,再用cdn地址
  1496 + $url_complete = getImageUrl($url);
  1497 + }
  1498 + }
1492 1499
1493 - //获取图片完整访问地址  
1494 - $url_complete = getImageUrl($url, 0, 0, 0);//先用v6-file地址  
1495 - if (strpos($url_complete, 'v6-file') !== false) {  
1496 - $is_exists = checkRemoteFileExists($url_complete);  
1497 - if (!$is_exists) {  
1498 - //不存在,再用cdn地址  
1499 - $url_complete = getImageUrl($url); 1500 + //获取与原图存储路径相同的压缩路径
  1501 + $path = parse_url($url_complete, PHP_URL_PATH);
  1502 + $path_arr = explode('.', $path);
  1503 + if (count($path_arr) != 2) {
  1504 + return $url;
1500 } 1505 }
1501 - } 1506 + $path_arr[0] = $path_arr[0] . '_thumbW' . $width;
  1507 + $key = implode('.', $path_arr);
1502 1508
1503 - //获取与原图存储路径相同的压缩路径  
1504 - $path = parse_url($url_complete, PHP_URL_PATH);  
1505 - $path_arr = explode('.', $path);  
1506 - if (count($path_arr) != 2) {  
1507 - return $url;  
1508 - }  
1509 - $path_arr[0] = $path_arr[0] . '_thumbW' . $width;  
1510 - $key = implode('.', $path_arr); 1509 + try {
  1510 + $img = \Intervention\Image\Facades\Image::make($url_complete);
1511 1511
1512 - try {  
1513 - $img = \Intervention\Image\Facades\Image::make($url_complete); 1512 + //宽度按设定,高度自动调整
  1513 + $img->resize($width, null, function ($constraint) {
  1514 + $constraint->aspectRatio();
  1515 + $constraint->upsize();
  1516 + });
1514 1517
1515 - //宽度按设定,高度自动调整  
1516 - $img->resize($width, null, function ($constraint) {  
1517 - $constraint->aspectRatio();  
1518 - $constraint->upsize();  
1519 - }); 1518 + //获取处理后的图片二进制资源
  1519 + $resource = $img->stream()->__toString();
1520 1520
1521 - //获取处理后的图片二进制资源  
1522 - $resource = $img->stream()->__toString(); 1521 + //上传存储桶
  1522 + $thumb_url = CosService::uploadRemote('', '', '', $key, $resource);
1523 1523
1524 - //上传存储桶  
1525 - $thumb_url = CosService::uploadRemote('', '', '', $key, $resource); 1524 + $url = $thumb_url ? $thumb_url : $url;
  1525 + } catch (\Exception $e) {
  1526 + Log::channel('thumb_img')->error($e->getMessage(), [$url_complete, $width]);
  1527 + }
1526 1528
1527 - $url = $thumb_url ? $thumb_url : $url;  
1528 - } catch (\Exception $e) {  
1529 - Log::channel('thumb_img')->error($e->getMessage(), [$url, $width]); 1529 + return $url;
1530 } 1530 }
1531 -  
1532 - return $url;  
1533 } 1531 }
1534 1532
1535 if (!function_exists('checkRemoteFileExists')) { 1533 if (!function_exists('checkRemoteFileExists')) {
@@ -59,6 +59,7 @@ use Illuminate\Support\Facades\Cache; @@ -59,6 +59,7 @@ use Illuminate\Support\Facades\Cache;
59 use Illuminate\Support\Facades\DB; 59 use Illuminate\Support\Facades\DB;
60 use Illuminate\Support\Facades\Http; 60 use Illuminate\Support\Facades\Http;
61 use Illuminate\Support\Facades\Log as LogInfo; 61 use Illuminate\Support\Facades\Log as LogInfo;
  62 +use Illuminate\Support\Facades\Redis;
62 63
63 /** 64 /**
64 * Class ProjectLogic 65 * Class ProjectLogic
@@ -528,6 +529,13 @@ class ProjectLogic extends BaseLogic @@ -528,6 +529,13 @@ class ProjectLogic extends BaseLogic
528 protected function saveProjectDeployBuild($deploy_build){ 529 protected function saveProjectDeployBuild($deploy_build){
529 $deployBuildModel = new DeployBuild(); 530 $deployBuildModel = new DeployBuild();
530 $deploy_build['configuration'] = Arr::a2s(!empty($deploy_build['configuration']) ? $deploy_build['configuration'] : []); 531 $deploy_build['configuration'] = Arr::a2s(!empty($deploy_build['configuration']) ? $deploy_build['configuration'] : []);
  532 +
  533 + //如果更改了缩略图压缩大小,同步写入处理缩略图任务队列
  534 + $old_thumb_info = $deployBuildModel->read(['id'=>$deploy_build['id']],['thumb_w']);
  535 + if(isset($old_thumb_info['thumb_w']) && $old_thumb_info['thumb_w'] != $deploy_build['thumb_w']){
  536 + Redis::lpush('thumb_project_image',$deploy_build['project_id']);
  537 + }
  538 +
531 $deployBuildModel->edit($deploy_build,['id'=>$deploy_build['id']]); 539 $deployBuildModel->edit($deploy_build,['id'=>$deploy_build['id']]);
532 return $this->success(); 540 return $this->success();
533 } 541 }