作者 lyh

hx

@@ -10,6 +10,7 @@ use App\Models\News\NewsCategory; @@ -10,6 +10,7 @@ use App\Models\News\NewsCategory;
10 use App\Models\News\NewsCategory as NewsCategoryModel; 10 use App\Models\News\NewsCategory as NewsCategoryModel;
11 use App\Models\RouteMap\RouteMap; 11 use App\Models\RouteMap\RouteMap;
12 use App\Models\Template\BTemplate; 12 use App\Models\Template\BTemplate;
  13 +use App\Services\CosService;
13 use Illuminate\Support\Facades\DB; 14 use Illuminate\Support\Facades\DB;
14 use mysql_xdevapi\Exception; 15 use mysql_xdevapi\Exception;
15 16
@@ -186,7 +187,9 @@ class NewsLogic extends BaseLogic @@ -186,7 +187,9 @@ class NewsLogic extends BaseLogic
186 if(isset($param['image'])){ 187 if(isset($param['image'])){
187 $param['image'] = str_replace_url($param['image'] ?? ''); 188 $param['image'] = str_replace_url($param['image'] ?? '');
188 } 189 }
  190 + if(isset($this->param['text'])){
189 191
  192 + }
190 if(isset($this->param['id'])){ 193 if(isset($this->param['id'])){
191 $param['operator_id'] = $this->user['id']; 194 $param['operator_id'] = $this->user['id'];
192 }else{ 195 }else{
@@ -209,6 +212,49 @@ class NewsLogic extends BaseLogic @@ -209,6 +212,49 @@ class NewsLogic extends BaseLogic
209 return $this->success($param); 212 return $this->success($param);
210 } 213 }
211 214
  215 + public function processBase64Image($inputString) {
  216 + // 正则匹配Base64图片流文件
  217 + $pattern = '/data:image\/(png|jpg|jpeg|gif);base64,([a-zA-Z0-9\/+\=]+)/';
  218 + $outputString = $inputString;
  219 + // 找到所有符合条件的Base64图片流
  220 + if (preg_match_all($pattern, $inputString, $matches)) {
  221 + // 遍历匹配的Base64流文件
  222 + foreach ($matches[0] as $index => $base64String) {
  223 + $imageType = $matches[1][$index]; // 图片类型
  224 + $base64Data = $matches[2][$index]; // Base64数据
  225 + // 解码Base64图片
  226 + $decodedData = base64_decode($base64Data);
  227 + if ($decodedData === false) {
  228 + continue; // 如果解码失败,则跳过
  229 + }
  230 + // 设置文件名
  231 + $outputFile = 'images/' . uniqid() . '.' . $imageType;
  232 + $filePath = __DIR__ . '/' . $outputFile;
  233 + // 保存图片到服务器
  234 + if (file_put_contents($filePath, $decodedData)) {
  235 + // 生成图片URL
  236 + $imageUrl = 'http://your-domain.com/' . $outputFile;
  237 + // 替换原字符串中的Base64流为图片链接
  238 + $outputString = str_replace($base64String, $imageUrl, $outputString);
  239 + }
  240 + }
  241 + }
  242 + return $outputString;
  243 + }
  244 + /**
  245 + * @remark :自调用
  246 + * @name :manager_uploads
  247 + * @author :lyh
  248 + * @method :post
  249 + * @time :2024/11/7 11:00
  250 + */
  251 + public function manager_uploads($files){
  252 + $this->uploads = config('upload.default_image');
  253 + $path = $this->uploads['path_b'].'/'.$this->user['project_id'].'/image_news/'.date('Y-m');
  254 + $cosService = new CosService();
  255 + $fileName = md5(uniqid() . '_' . time() . '.' . $this->user['project_id'].rand(1,10000));
  256 + return $cosService->uploadFile($files,$path,$fileName);
  257 + }
212 /** 258 /**
213 * @remark :获取分类(字符串) 259 * @remark :获取分类(字符串)
214 * @name :getLastCategory 260 * @name :getLastCategory
@@ -5,6 +5,7 @@ namespace App\Services; @@ -5,6 +5,7 @@ namespace App\Services;
5 use App\Exceptions\InquiryFilterException; 5 use App\Exceptions\InquiryFilterException;
6 use App\Models\File\ImageSetting; 6 use App\Models\File\ImageSetting;
7 use App\Utils\LogUtils; 7 use App\Utils\LogUtils;
  8 +use Illuminate\Support\Str;
8 use Qcloud\Cos\Client; 9 use Qcloud\Cos\Client;
9 /** 10 /**
10 * @remark :对象存储cos 11 * @remark :对象存储cos
@@ -34,7 +35,19 @@ class CosService @@ -34,7 +35,19 @@ class CosService
34 ], 35 ],
35 ]); 36 ]);
36 $key = $path.'/'.$filename; 37 $key = $path.'/'.$filename;
37 - $Body = $binary ? $files : fopen($files->getRealPath(), 'r'); 38 + // 判断是否为 Base64 编码的图片流文件
  39 + if (Str::startsWith($files, 'data:image')) {
  40 + // 分离 Base64 头部和数据部分
  41 + [$meta, $base64Data] = explode(',', $files);
  42 + // 解码 Base64 数据
  43 + $Body = base64_decode($base64Data);
  44 + if ($Body === false) {
  45 + throw new \Exception("Base64 数据解码失败");
  46 + }
  47 + } else {
  48 + // 如果不是 Base64 流文件,处理为普通文件上传
  49 + $Body = $binary ? $files : fopen($files->getRealPath(), 'r');
  50 + }
38 $options = [ 51 $options = [
39 'Bucket' => $cos['bucket'], 52 'Bucket' => $cos['bucket'],
40 'Key' => $key, 53 'Key' => $key,
@@ -53,6 +66,7 @@ class CosService @@ -53,6 +66,7 @@ class CosService
53 ], true); 66 ], true);
54 } 67 }
55 $res = $cosClient->putObject($options); 68 $res = $cosClient->putObject($options);
  69 + @file_put_contents(storage_path('logs/lyh_error.log'), var_export($res, true) . PHP_EOL, FILE_APPEND);
56 return $key; 70 return $key;
57 } 71 }
58 72