82 lines
2.0 KiB
PHP
82 lines
2.0 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace app\note\controller\v1;
|
|
|
|
use app\api\common\Response;
|
|
use app\note\controller\BaseController;
|
|
use app\note\service\AiService;
|
|
use app\note\service\NoteService;
|
|
use think\App;
|
|
|
|
/**
|
|
* 笔记 AI 能力控制器
|
|
*/
|
|
class Ai extends BaseController
|
|
{
|
|
/**
|
|
* @var PlanningService
|
|
*/
|
|
protected $aiService;
|
|
|
|
/**
|
|
* @var NoteService
|
|
*/
|
|
protected $noteService;
|
|
|
|
public function __construct(App $app)
|
|
{
|
|
parent::__construct($app);
|
|
$this->aiService = new AiService();
|
|
$this->noteService = new NoteService();
|
|
}
|
|
|
|
/**
|
|
* 发起 AI 总结
|
|
* POST /note/v1/ai/summary/:id
|
|
*/
|
|
public function summary(int $id)
|
|
{
|
|
try {
|
|
$noteUserId = $this->getCurrentNoteUserId();
|
|
|
|
if ($id <= 0) {
|
|
return Response::error('笔记 ID 不正确', 400);
|
|
}
|
|
|
|
$note = $this->noteService->getOwnedNote($noteUserId, $id);
|
|
$data = $this->request->post();
|
|
$result = $this->aiService->createSummary(
|
|
$note,
|
|
(string) ($data['summary_type'] ?? 'brief'),
|
|
!empty($data['force_refresh'])
|
|
);
|
|
|
|
return Response::success($result, '总结生成成功');
|
|
} catch (\Throwable $e) {
|
|
return Response::error($e->getMessage(), $e->getCode() ?: 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 查看 AI 总结结果
|
|
* GET /note/v1/ai/summary/:id
|
|
*/
|
|
public function readSummary(int $id)
|
|
{
|
|
try {
|
|
$noteUserId = $this->getCurrentNoteUserId();
|
|
|
|
if ($id <= 0) {
|
|
return Response::error('笔记 ID 不正确', 400);
|
|
}
|
|
|
|
$this->noteService->getOwnedNote($noteUserId, $id);
|
|
$result = $this->aiService->getSummary($id);
|
|
return Response::success($result);
|
|
} catch (\Throwable $e) {
|
|
return Response::error($e->getMessage(), $e->getCode() ?: 500);
|
|
}
|
|
}
|
|
}
|