HandleNewsText.php 3.6 KB
<?php
/**
 * @remark :
 * @name   :HandleNewsText.php
 * @author :lyh
 * @method :post
 * @time   :2024/11/8 9:14
 */

namespace App\Console\Commands\Test;

use App\Models\News\News;
use App\Services\CosService;
use Illuminate\Console\Command;
use App\Models\Project\Project;
use App\Models\Template\BTemplateCom;
use App\Services\ProjectServer;
use Illuminate\Support\Facades\DB;
class HandleNewsText extends Command
{

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'news_text';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'news_text';
    /**
     * Execute the job.
     *
     * @return void
     */
    protected $project_id = 0;
    public function handle()
    {
        $projectModel = new Project();
        $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;
    }

    /**
     * @remark :处理text
     * @name   :handleText
     * @author :lyh
     * @method :post
     * @time   :2024/11/7 17:20
     */
    public function handleText($text){
        $pattern = '/<img\s+[^>]*src=["\']([^"\']+)["\']/i';
        $matches = [];
        preg_match_all($pattern, $text, $matches);
        $text = $this->saveBase64Images($matches[1],$text);
        return ($text);
    }

    /**
     * @remark :解码图片
     * @name   :saveBase64Images
     * @author :lyh
     * @method :post
     * @time   :2024/11/7 16:52
     */
    public function saveBase64Images($imageSources,&$text)
    {
        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 ($text);
    }
    /**
     * @remark :自调用
     * @name   :manager_uploads
     * @author :lyh
     * @method :post
     * @time   :2024/11/7 11:00
     */
    public function manager_uploads($files,$type = 'png'){
        $this->uploads = config('upload.default_image');
        $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;
        return getImageUrl($cosService->uploadFile($files,$path,$fileName));
    }
}