作者 赵彬吉
... ... @@ -9,6 +9,7 @@
namespace App\Console\Commands\Test;
use App\Models\News\News;
use App\Services\CosService;
use Illuminate\Console\Command;
use App\Models\Project\Project;
... ... @@ -36,16 +37,28 @@ class HandleNewsText extends Command
*
* @return void
*/
protected $project_id = 0;
public function handle()
{
$projectModel = new Project();
$list = $projectModel->list(['delete_status'=>0,'type'=>['!=',0],'id'=>1618]);
$list = $projectModel->list(['delete_status'=>0,'type'=>['!=',0]]);
$data = [];
foreach ($list as $v){
$this->project_id = $v['id'];
echo date('Y-m-d H:i:s') . 'project_id:'.$v['id'] . PHP_EOL;
ProjectServer::useProject($v['id']);
DB::connection('custom_mysql')->statement('DROP TABLE IF EXISTS gl_news_copy');
DB::connection('custom_mysql')->statement('CREATE TABLE gl_news_copy LIKE gl_news');
DB::connection('custom_mysql')->statement('INSERT INTO gl_news_copy SELECT * FROM gl_news');
$newsModel = new News();
$news_list = $newsModel->list(['status'=>['!=',2]],'id',['id','text']);
if(!empty($news_list)){
foreach ($news_list as $key => $values){
echo date('Y-m-d H:i:s') . '处理的数据id:'.$values['id'] . PHP_EOL;
$text = $this->handleText($values['text']);
$newsModel->edit(['text'=>$text],['id'=>$values['id']]);
}
}
DB::disconnect('custom_mysql');
}
echo date('Y-m-d H:i:s') . 'end' . PHP_EOL;
... ... @@ -63,7 +76,7 @@ class HandleNewsText extends Command
$matches = [];
preg_match_all($pattern, $text, $matches);
$text = $this->saveBase64Images($matches[1],$text);
return $this->success($text);
return ($text);
}
/**
... ... @@ -77,12 +90,13 @@ class HandleNewsText extends Command
{
foreach ($imageSources as $src) {
if (preg_match('/^data:image\/(png|jpg|jpeg|gif);base64,/', $src, $match)) {
echo date('Y-m-d H:i:s') . '当前数据包含base64图片流。' . PHP_EOL;
$imageType = $match[1]; // Image type (png, jpg, etc.)
$imageUrl = $this->manager_uploads($src,$imageType);
$text = str_replace($src, $imageUrl, $text);
}
}
return $this->success($text);
return ($text);
}
/**
* @remark :自调用
... ... @@ -93,9 +107,9 @@ class HandleNewsText extends Command
*/
public function manager_uploads($files,$type = 'png'){
$this->uploads = config('upload.default_image');
$path = $this->uploads['path_b'].'/'.($this->user['project_id'] ?? 1618).'/image_news/'.date('Y-m');
$path = $this->uploads['path_b'].'/'.($this->project_id).'/image_news/'.date('Y-m');
$cosService = new CosService();
$fileName = md5(uniqid() . '_' . time() . '.' . ($this->user['project_id'] ?? 1618).rand(1,10000)) . '.' .$type;
$fileName = md5(uniqid() . '_' . time() . '.' . ($this->project_id).rand(1,10000)) . '.' .$type;
return getImageUrl($cosService->uploadFile($files,$path,$fileName));
}
}
... ...
... ... @@ -254,9 +254,9 @@ class NewsLogic extends BaseLogic
*/
public function manager_uploads($files,$type = 'png'){
$this->uploads = config('upload.default_image');
$path = $this->uploads['path_b'].'/'.($this->user['project_id'] ?? 1618).'/image_news/'.date('Y-m');
$path = $this->uploads['path_b'].'/'.($this->user['project_id']).'/image_news/'.date('Y-m');
$cosService = new CosService();
$fileName = md5(uniqid() . '_' . time() . '.' . ($this->user['project_id'] ?? 1618).rand(1,10000)) . '.' .$type;
$fileName = md5(uniqid() . '_' . time() . '.' . ($this->user['project_id']).rand(1,10000)) . '.' .$type;
return getImageUrl($cosService->uploadFile($files,$path,$fileName));
}
/**
... ...