From 6f9bb5f43b39cb4807b64f092064c8d743f96d1b Mon Sep 17 00:00:00 2001 From: Ethanfly Date: Fri, 9 Jan 2026 12:35:20 +0800 Subject: [PATCH] Enhance week report generation by adding image appendix feature. Implemented a new method to build an images section at the end of the report, which includes task images and descriptions. Cleaned up unused placeholders and improved formatting for better readability. --- app/Http/Controllers/WeekReportController.php | 75 +++++++++++++++---- 1 file changed, 60 insertions(+), 15 deletions(-) diff --git a/app/Http/Controllers/WeekReportController.php b/app/Http/Controllers/WeekReportController.php index 2fb7ca2..a6e1c30 100644 --- a/app/Http/Controllers/WeekReportController.php +++ b/app/Http/Controllers/WeekReportController.php @@ -30,10 +30,10 @@ class WeekReportController extends Controller try { $file = $request->file('image'); $filename = time() . '_' . uniqid() . '.' . $file->getClientOriginalExtension(); - + // 存储到public目录 $path = $file->storeAs('uploads', $filename, 'public'); - + return response()->json([ 'success' => true, 'path' => $path, @@ -54,7 +54,7 @@ class WeekReportController extends Controller { // 设置较长的执行时间,因为AI生成可能需要较长时间 set_time_limit(300); // 5分钟 - + $request->validate([ 'tasks' => 'required|array|min:1', 'tasks.*.description' => 'required|string|max:2000', @@ -80,7 +80,7 @@ class WeekReportController extends Controller try { // 调用千问API $reportContent = $this->callQwenApi($prompt); - + // 插入图片到报告中 $reportWithImages = $this->insertImagesToReport($reportContent, $tasks); @@ -120,7 +120,7 @@ class WeekReportController extends Controller if (empty($tasks)) { return ''; } - + $list = []; foreach ($tasks as $index => $desc) { if (!empty(trim($desc))) { @@ -137,18 +137,18 @@ class WeekReportController extends Controller private function buildPrompt(string $taskList, string $weekStart, string $weekEnd, string $author, string $nextWeekList = ''): string { $authorInfo = $author ? "作者:{$author}" : ""; - + // 根据是否有下周计划来调整要求 $nextWeekSection = ''; $nextWeekRequirement = ''; - + if (!empty(trim($nextWeekList))) { $nextWeekSection = "\n\n下周计划任务:\n{$nextWeekList}"; $nextWeekRequirement = "\n8. 添加\"下周工作计划\"部分,简要列出计划要做的事项"; } else { $nextWeekRequirement = "\n8. 不要添加下周工作计划部分"; } - + return <<json(); - + if (!isset($data['choices'][0]['message']['content'])) { throw new \Exception('API返回数据格式错误'); } @@ -228,9 +228,10 @@ PROMPT; */ private function insertImagesToReport(string $report, array $tasks): string { + // 首先尝试替换AI生成的占位符 foreach ($tasks as $index => $task) { $num = $index + 1; - + // 支持多种占位符格式 $placeholders = [ "[图片占位符-任务{$num}]", @@ -240,7 +241,7 @@ PROMPT; "【图片占位符-{$num}】", "【图片占位符{$num}】", ]; - + if (!empty($task['image'])) { $imageUrl = asset('storage/' . $task['image']); $imageMarkdown = "\n\n![任务{$num}截图]({$imageUrl})\n"; @@ -254,18 +255,62 @@ PROMPT; } } } - + // 清理所有未使用的占位符(各种格式) $report = preg_replace('/\[图片占位符[-]?任务?\d*\]/', '', $report); $report = preg_replace('/【图片占位符[-]?任务?\d*】/', '', $report); $report = preg_replace('/\[?图片占位符[-—]?\d+\]?/', '', $report); - + // 清理可能产生的多余空行 $report = preg_replace('/\n{3,}/', "\n\n", $report); - + + // 收集所有有图片的任务,并在报告末尾添加截图附录 + $imagesSection = $this->buildImagesSection($tasks); + if (!empty($imagesSection)) { + $report .= $imagesSection; + } + return $report; } + /** + * 构建图片附录部分 + */ + private function buildImagesSection(array $tasks): string + { + $images = []; + + foreach ($tasks as $index => $task) { + if (!empty($task['image'])) { + $num = $index + 1; + $imageUrl = asset('storage/' . $task['image']); + // 提取任务描述的前30个字符作为图片说明 + $desc = mb_substr(trim($task['description']), 0, 50); + if (mb_strlen(trim($task['description'])) > 50) { + $desc .= '...'; + } + $images[] = [ + 'num' => $num, + 'url' => $imageUrl, + 'desc' => $desc, + ]; + } + } + + if (empty($images)) { + return ''; + } + + $section = "\n\n---\n\n## 附:任务截图\n\n"; + + foreach ($images as $img) { + $section .= "**任务{$img['num']}**: {$img['desc']}\n\n"; + $section .= "![任务{$img['num']}截图]({$img['url']})\n\n"; + } + + return $section; + } + /** * 下载Markdown格式 */ @@ -309,7 +354,7 @@ PROMPT; $pdf->setPaper('A4', 'portrait'); $pdf->setOption('isRemoteEnabled', true); $pdf->setOption('defaultFont', 'sans-serif'); - + return $pdf->download($filename); }