Forráskód Böngészése

编辑评论将图片链接改为占位符存储

kely 11 hónapja
szülő
commit
710553b0b1

+ 35 - 1
app/Http/Controllers/API/ActionController.php

@@ -10,6 +10,7 @@ use App\Models\Enums\FileObjectType;
 use App\Models\Enums\ObjectAction;
 use App\Repositories\ActionRepository;
 use App\Services\File\FileAssociationService;
+use DOMDocument;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Auth;
 
@@ -38,6 +39,39 @@ class ActionController extends Controller
     {
         $actionObjectType =  ActionObjectType::from($objectType);
 
+        $fileIds=$request->get('file_ids');
+        $comment=$request->get('comment');
+        $newComment = null;
+
+        $dom = new DOMDocument();
+        libxml_use_internal_errors(true); // 禁用错误报告
+        $dom->loadHTML($comment, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
+        libxml_clear_errors();
+        // 获取所有的 img 标签
+        $imgTags = $dom->getElementsByTagName('img');
+        if(!empty($imgTags)){
+            // 遍历所有的 img 标签
+            foreach ($imgTags as $imgTag) {
+                $src = $imgTag->getAttribute('src');
+                // 查找 &fild_ 的位置
+                $fildPos = strpos($src, 'fild_');
+                // 如果找到了 &fild_,则截取该位置之前的数据
+                if ($fildPos !== false) {
+                    $src = substr($src, $fildPos); // 保留 &fild_ 及之后的部分
+                } else {
+                    // 如果没有找到 &fild_,则清空整个 src 属性(或者根据需求处理)
+                    $src = '';
+                }
+                // 设置修改后的 src 属性
+                $imgTag->setAttribute('src', $src);
+            }
+            $newComment = $dom->saveHTML();
+            $newComment = preg_replace('/^.*<body>(.*)<\/body>.*$/s', '$1', $newComment);
+            $newComment = trim($newComment);
+        }
+        dd($newComment);
+
+
         $object = $actionObjectType?->modelBuilderAllowed($objectId)
             ->where("company_id", Auth::user()->company_id)
             ->findOrFail($objectId);
@@ -53,7 +87,7 @@ class ActionController extends Controller
             $actionObjectType,
             ObjectAction::COMMENTED,
             projectId: $projectId,
-            comment: $request->comment,
+            comment: $newComment,
         );
 
         $service->association(

+ 5 - 1
app/Http/Requests/API/Action/CommentRequest.php

@@ -22,7 +22,11 @@ class CommentRequest extends FormRequest
     public function rules(): array
     {
         return [
-            "comment" => "required"
+            "comment" => "required",
+            'file_ids' => [
+                'nullable',
+                'array',
+            ]
         ];
     }
 }

+ 18 - 1
app/Repositories/ActionRepository.php

@@ -5,13 +5,17 @@ namespace App\Repositories;
 use App\Models\Action;
 use App\Models\Enums\ActionObjectType;
 use App\Models\Enums\ObjectAction;
-use App\Models\History;
+use App\Models\File;
 use App\Models\Project;
 use App\Models\Requirement;
 use App\Models\Task;
 use Carbon\Carbon;
 use Illuminate\Support\Collection;
 use Illuminate\Support\Facades\Auth;
+use Illuminate\Support\Facades\Storage;
+use Illuminate\Support\Str;
+use Overtrue\CosClient\Client;
+use Overtrue\Flysystem\Cos\CosAdapter;
 
 class ActionRepository
 {
@@ -124,6 +128,19 @@ class ActionRepository
         $objectLabelKey = sprintf("action-labels.object_type.%s", $action['object_type']);
 
         $cratedAt = Carbon::parse($action['created_at']);
+        if(isset($action['extra_fields'])){
+            $comment=$action['comment'];
+            $files=File::query()->whereIn('id',$action['extra_fields'])->get();
+            $search = [];
+            $replace = [];
+            foreach ($files as $file){
+                $url=Storage::url($file->pathname);
+                $placeholder = "{" . $file->id . "}"; // 生成占位符
+                $search[] = $placeholder; // 将占位符添加到 $search 数组
+                $replace[] = $url; // 将替换 URL 添加到 $replace 数组
+            }
+            $action['comment'] = Str::replace($search, $replace, $comment);
+        }
         return [
             'id' => $action['id'],
             'time' => $cratedAt->toTimeString(),

+ 35 - 0
app/helpers.php

@@ -49,3 +49,38 @@ if (!function_exists('text_diff')) {
 }
 
 
+if (!function_exists('comment_image')) {
+    function comment_image(string $text1): string
+    {
+        $newComment = null;
+        $dom = new DOMDocument();
+        libxml_use_internal_errors(true); // 禁用错误报告
+        $dom->loadHTML('<?xml encoding="UTF-8">' . $text1, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
+        libxml_clear_errors();
+        // 获取所有的 img 标签
+        $imgTags = $dom->getElementsByTagName('img');
+        if(isset($imgTags)){
+            // 遍历所有的 img 标签
+            foreach ($imgTags as $imgTag) {
+                $src = $imgTag->getAttribute('src');
+                // 查找 &fild_ 的位置
+                $fildPos = strpos($src, 'fild_');
+                // 如果找到了 &fild_,则截取该位置之前的数据
+                if ($fildPos !== false) {
+                    $src = substr($src, $fildPos); // 保留 &fild_ 及之后的部分
+                } else {
+                    // 如果没有找到 &fild_,则清空整个 src 属性(或者根据需求处理)
+                    $src = '';
+                }
+                // 设置修改后的 src 属性
+                $imgTag->setAttribute('src', $src);
+            }
+            $newComment = $dom->saveHTML();
+            $newComment = preg_replace('/^.*<body>(.*)<\/body>.*$/s', '$1', $newComment);
+            $newComment = trim($newComment);
+        }
+        return $newComment;
+    }
+}
+
+