作者 lyh

gx

  1 +<?php
  2 +/**
  3 + * @remark :
  4 + * @name :HandleNewsText.php
  5 + * @author :lyh
  6 + * @method :post
  7 + * @time :2024/11/8 9:14
  8 + */
  9 +
  10 +namespace App\Console\Commands\Test;
  11 +
  12 +use App\Services\CosService;
  13 +use Illuminate\Console\Command;
  14 +use App\Models\Project\Project;
  15 +use App\Models\Template\BTemplateCom;
  16 +use App\Services\ProjectServer;
  17 +use Illuminate\Support\Facades\DB;
  18 +class HandleNewsText extends Command
  19 +{
  20 +
  21 + /**
  22 + * The name and signature of the console command.
  23 + *
  24 + * @var string
  25 + */
  26 + protected $signature = 'news_text';
  27 +
  28 + /**
  29 + * The console command description.
  30 + *
  31 + * @var string
  32 + */
  33 + protected $description = 'news_text';
  34 + /**
  35 + * Execute the job.
  36 + *
  37 + * @return void
  38 + */
  39 + public function handle()
  40 + {
  41 + $projectModel = new Project();
  42 + $list = $projectModel->list(['delete_status'=>0,'type'=>['!=',0]]);
  43 + $data = [];
  44 + foreach ($list as $v){
  45 + echo date('Y-m-d H:i:s') . 'project_id:'.$v['id'] . PHP_EOL;
  46 + ProjectServer::useProject($v['id']);
  47 + DB::statement('CREATE TABLE gl_news_copy AS SELECT * FROM gl_news');
  48 + DB::disconnect('custom_mysql');
  49 + }
  50 + echo date('Y-m-d H:i:s') . 'end' . PHP_EOL;
  51 + }
  52 +
  53 + /**
  54 + * @remark :处理text
  55 + * @name :handleText
  56 + * @author :lyh
  57 + * @method :post
  58 + * @time :2024/11/7 17:20
  59 + */
  60 + public function handleText($text){
  61 + $pattern = '/<img\s+[^>]*src=["\']([^"\']+)["\']/i';
  62 + $matches = [];
  63 + preg_match_all($pattern, $text, $matches);
  64 + $text = $this->saveBase64Images($matches[1],$text);
  65 + return $this->success($text);
  66 + }
  67 +
  68 + /**
  69 + * @remark :解码图片
  70 + * @name :saveBase64Images
  71 + * @author :lyh
  72 + * @method :post
  73 + * @time :2024/11/7 16:52
  74 + */
  75 + public function saveBase64Images($imageSources,&$text)
  76 + {
  77 + foreach ($imageSources as $src) {
  78 + if (preg_match('/^data:image\/(png|jpg|jpeg|gif);base64,/', $src, $match)) {
  79 + $imageType = $match[1]; // Image type (png, jpg, etc.)
  80 + $imageUrl = $this->manager_uploads($src,$imageType);
  81 + $text = str_replace($src, $imageUrl, $text);
  82 + }
  83 + }
  84 + return $this->success($text);
  85 + }
  86 + /**
  87 + * @remark :自调用
  88 + * @name :manager_uploads
  89 + * @author :lyh
  90 + * @method :post
  91 + * @time :2024/11/7 11:00
  92 + */
  93 + public function manager_uploads($files,$type = 'png'){
  94 + $this->uploads = config('upload.default_image');
  95 + $path = $this->uploads['path_b'].'/'.($this->user['project_id'] ?? 1618).'/image_news/'.date('Y-m');
  96 + $cosService = new CosService();
  97 + $fileName = md5(uniqid() . '_' . time() . '.' . ($this->user['project_id'] ?? 1618).rand(1,10000)) . '.' .$type;
  98 + return getImageUrl($cosService->uploadFile($files,$path,$fileName));
  99 + }
  100 +}