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.
This commit is contained in:
parent
3955dd6049
commit
6f9bb5f43b
@ -30,10 +30,10 @@ class WeekReportController extends Controller
|
|||||||
try {
|
try {
|
||||||
$file = $request->file('image');
|
$file = $request->file('image');
|
||||||
$filename = time() . '_' . uniqid() . '.' . $file->getClientOriginalExtension();
|
$filename = time() . '_' . uniqid() . '.' . $file->getClientOriginalExtension();
|
||||||
|
|
||||||
// 存储到public目录
|
// 存储到public目录
|
||||||
$path = $file->storeAs('uploads', $filename, 'public');
|
$path = $file->storeAs('uploads', $filename, 'public');
|
||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'success' => true,
|
'success' => true,
|
||||||
'path' => $path,
|
'path' => $path,
|
||||||
@ -54,7 +54,7 @@ class WeekReportController extends Controller
|
|||||||
{
|
{
|
||||||
// 设置较长的执行时间,因为AI生成可能需要较长时间
|
// 设置较长的执行时间,因为AI生成可能需要较长时间
|
||||||
set_time_limit(300); // 5分钟
|
set_time_limit(300); // 5分钟
|
||||||
|
|
||||||
$request->validate([
|
$request->validate([
|
||||||
'tasks' => 'required|array|min:1',
|
'tasks' => 'required|array|min:1',
|
||||||
'tasks.*.description' => 'required|string|max:2000',
|
'tasks.*.description' => 'required|string|max:2000',
|
||||||
@ -80,7 +80,7 @@ class WeekReportController extends Controller
|
|||||||
try {
|
try {
|
||||||
// 调用千问API
|
// 调用千问API
|
||||||
$reportContent = $this->callQwenApi($prompt);
|
$reportContent = $this->callQwenApi($prompt);
|
||||||
|
|
||||||
// 插入图片到报告中
|
// 插入图片到报告中
|
||||||
$reportWithImages = $this->insertImagesToReport($reportContent, $tasks);
|
$reportWithImages = $this->insertImagesToReport($reportContent, $tasks);
|
||||||
|
|
||||||
@ -120,7 +120,7 @@ class WeekReportController extends Controller
|
|||||||
if (empty($tasks)) {
|
if (empty($tasks)) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
$list = [];
|
$list = [];
|
||||||
foreach ($tasks as $index => $desc) {
|
foreach ($tasks as $index => $desc) {
|
||||||
if (!empty(trim($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
|
private function buildPrompt(string $taskList, string $weekStart, string $weekEnd, string $author, string $nextWeekList = ''): string
|
||||||
{
|
{
|
||||||
$authorInfo = $author ? "作者:{$author}" : "";
|
$authorInfo = $author ? "作者:{$author}" : "";
|
||||||
|
|
||||||
// 根据是否有下周计划来调整要求
|
// 根据是否有下周计划来调整要求
|
||||||
$nextWeekSection = '';
|
$nextWeekSection = '';
|
||||||
$nextWeekRequirement = '';
|
$nextWeekRequirement = '';
|
||||||
|
|
||||||
if (!empty(trim($nextWeekList))) {
|
if (!empty(trim($nextWeekList))) {
|
||||||
$nextWeekSection = "\n\n下周计划任务:\n{$nextWeekList}";
|
$nextWeekSection = "\n\n下周计划任务:\n{$nextWeekList}";
|
||||||
$nextWeekRequirement = "\n8. 添加\"下周工作计划\"部分,简要列出计划要做的事项";
|
$nextWeekRequirement = "\n8. 添加\"下周工作计划\"部分,简要列出计划要做的事项";
|
||||||
} else {
|
} else {
|
||||||
$nextWeekRequirement = "\n8. 不要添加下周工作计划部分";
|
$nextWeekRequirement = "\n8. 不要添加下周工作计划部分";
|
||||||
}
|
}
|
||||||
|
|
||||||
return <<<PROMPT
|
return <<<PROMPT
|
||||||
你是一位经验丰富的职场人士,擅长撰写简洁实用的工作周报。请根据以下任务信息,用自然、朴实的语言写一份周报。
|
你是一位经验丰富的职场人士,擅长撰写简洁实用的工作周报。请根据以下任务信息,用自然、朴实的语言写一份周报。
|
||||||
|
|
||||||
@ -215,7 +215,7 @@ PROMPT;
|
|||||||
}
|
}
|
||||||
|
|
||||||
$data = $response->json();
|
$data = $response->json();
|
||||||
|
|
||||||
if (!isset($data['choices'][0]['message']['content'])) {
|
if (!isset($data['choices'][0]['message']['content'])) {
|
||||||
throw new \Exception('API返回数据格式错误');
|
throw new \Exception('API返回数据格式错误');
|
||||||
}
|
}
|
||||||
@ -228,9 +228,10 @@ PROMPT;
|
|||||||
*/
|
*/
|
||||||
private function insertImagesToReport(string $report, array $tasks): string
|
private function insertImagesToReport(string $report, array $tasks): string
|
||||||
{
|
{
|
||||||
|
// 首先尝试替换AI生成的占位符
|
||||||
foreach ($tasks as $index => $task) {
|
foreach ($tasks as $index => $task) {
|
||||||
$num = $index + 1;
|
$num = $index + 1;
|
||||||
|
|
||||||
// 支持多种占位符格式
|
// 支持多种占位符格式
|
||||||
$placeholders = [
|
$placeholders = [
|
||||||
"[图片占位符-任务{$num}]",
|
"[图片占位符-任务{$num}]",
|
||||||
@ -240,7 +241,7 @@ PROMPT;
|
|||||||
"【图片占位符-{$num}】",
|
"【图片占位符-{$num}】",
|
||||||
"【图片占位符{$num}】",
|
"【图片占位符{$num}】",
|
||||||
];
|
];
|
||||||
|
|
||||||
if (!empty($task['image'])) {
|
if (!empty($task['image'])) {
|
||||||
$imageUrl = asset('storage/' . $task['image']);
|
$imageUrl = asset('storage/' . $task['image']);
|
||||||
$imageMarkdown = "\n\n\n";
|
$imageMarkdown = "\n\n\n";
|
||||||
@ -254,18 +255,62 @@ PROMPT;
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 清理所有未使用的占位符(各种格式)
|
// 清理所有未使用的占位符(各种格式)
|
||||||
$report = preg_replace('/\[图片占位符[-]?任务?\d*\]/', '', $report);
|
$report = preg_replace('/\[图片占位符[-]?任务?\d*\]/', '', $report);
|
||||||
$report = preg_replace('/【图片占位符[-]?任务?\d*】/', '', $report);
|
$report = preg_replace('/【图片占位符[-]?任务?\d*】/', '', $report);
|
||||||
$report = preg_replace('/\[?图片占位符[-—]?\d+\]?/', '', $report);
|
$report = preg_replace('/\[?图片占位符[-—]?\d+\]?/', '', $report);
|
||||||
|
|
||||||
// 清理可能产生的多余空行
|
// 清理可能产生的多余空行
|
||||||
$report = preg_replace('/\n{3,}/', "\n\n", $report);
|
$report = preg_replace('/\n{3,}/', "\n\n", $report);
|
||||||
|
|
||||||
|
// 收集所有有图片的任务,并在报告末尾添加截图附录
|
||||||
|
$imagesSection = $this->buildImagesSection($tasks);
|
||||||
|
if (!empty($imagesSection)) {
|
||||||
|
$report .= $imagesSection;
|
||||||
|
}
|
||||||
|
|
||||||
return $report;
|
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格式
|
* 下载Markdown格式
|
||||||
*/
|
*/
|
||||||
@ -309,7 +354,7 @@ PROMPT;
|
|||||||
$pdf->setPaper('A4', 'portrait');
|
$pdf->setPaper('A4', 'portrait');
|
||||||
$pdf->setOption('isRemoteEnabled', true);
|
$pdf->setOption('isRemoteEnabled', true);
|
||||||
$pdf->setOption('defaultFont', 'sans-serif');
|
$pdf->setOption('defaultFont', 'sans-serif');
|
||||||
|
|
||||||
return $pdf->download($filename);
|
return $pdf->download($filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user