feat: add note module and route fixes

This commit is contained in:
nepiedg
2026-04-17 07:48:44 +00:00
parent 866ddb046b
commit 84e1c0daac
25 changed files with 2196 additions and 38 deletions
+30
View File
@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace app\note\controller;
/**
* note 模块基础控制器
*/
abstract class BaseController extends \app\api\controller\BaseController
{
/**
* 获取当前 note 模块登录用户 ID。
*
* 说明:
* - note 模块复用全局 JWT 中间件
* - 但要求 token 载荷中必须带 `guard=note`
*
* @return int
*/
protected function getCurrentNoteUserId(): int
{
$payload = $this->getLoginPayload();
if (($payload['guard'] ?? '') !== 'note') {
throw new \RuntimeException('note 模块登录态无效', 401);
}
return (int) ($payload['userid'] ?? 0);
}
}
+81
View File
@@ -0,0 +1,81 @@
<?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);
}
}
}
+70
View File
@@ -0,0 +1,70 @@
<?php
declare(strict_types=1);
namespace app\note\controller\v1;
use app\api\common\Response;
use app\note\controller\BaseController;
use app\note\service\AuthService;
use think\App;
use think\exception\ValidateException;
/**
* 笔记小程序认证控制器
*/
class Auth extends BaseController
{
/**
* @var PlanningService
*/
protected $authService;
public function __construct(App $app)
{
parent::__construct($app);
$this->authService = new AuthService();
}
/**
* 微信小程序登录
* POST /note/v1/auth/wechat-login
*/
public function wechatLogin()
{
try {
$data = $this->request->post();
validate([
'code' => 'require',
], [
'code.require' => '微信登录 code 不能为空',
])->check($data);
$result = $this->authService->wechatLogin(
(string) $data['code'],
isset($data['nickname']) ? (string) $data['nickname'] : null,
isset($data['avatar_url']) ? (string) $data['avatar_url'] : null
);
return Response::success($result, '登录成功');
} catch (ValidateException $e) {
return Response::error($e->getMessage(), 400);
} catch (\Throwable $e) {
return Response::error($e->getMessage(), $e->getCode() ?: 500);
}
}
/**
* 获取当前小程序用户信息
* GET /note/v1/auth/me
*/
public function me()
{
try {
$result = $this->authService->getUserInfo($this->getCurrentNoteUserId());
return Response::success($result);
} catch (\Throwable $e) {
return Response::error($e->getMessage(), $e->getCode() ?: 500);
}
}
}
+35
View File
@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace app\note\controller\v1;
use app\api\common\Response;
use app\note\controller\BaseController;
use app\note\service\PlanningService;
use think\App;
/**
* 笔记模块元信息控制器
*/
class Meta extends BaseController
{
/**
* @var PlanningService
*/
protected $planningService;
public function __construct(App $app)
{
parent::__construct($app);
$this->planningService = new PlanningService();
}
/**
* 获取 note 模块接口规划概览
* GET /note/v1/meta/interfaces
*/
public function interfaces()
{
return Response::success($this->planningService->getModuleOverview());
}
}
+156
View File
@@ -0,0 +1,156 @@
<?php
declare(strict_types=1);
namespace app\note\controller\v1;
use app\api\common\Response;
use app\note\controller\BaseController;
use app\note\service\NoteService;
use think\App;
use think\exception\ValidateException;
/**
* 笔记控制器
*/
class Note extends BaseController
{
/**
* @var PlanningService
*/
protected $noteService;
public function __construct(App $app)
{
parent::__construct($app);
$this->noteService = new NoteService();
}
/**
* 创建笔记
* POST /note/v1/item/create
*/
public function create()
{
try {
$noteUserId = $this->getCurrentNoteUserId();
$data = $this->request->post();
validate([
'source_type' => 'require|in:text,audio,mix',
], [
'source_type.require' => '笔记来源类型不能为空',
'source_type.in' => '笔记来源类型不正确',
])->check($data);
$result = $this->noteService->create($noteUserId, $data);
return Response::success($result, '创建成功');
} catch (ValidateException $e) {
return Response::error($e->getMessage(), 400);
} catch (\Throwable $e) {
return Response::error($e->getMessage(), $e->getCode() ?: 500);
}
}
/**
* 笔记列表
* GET /note/v1/item/list
*/
public function index()
{
try {
$result = $this->noteService->getList($this->getCurrentNoteUserId(), $this->request->get());
return Response::success($result);
} catch (\Throwable $e) {
return Response::error($e->getMessage(), $e->getCode() ?: 500);
}
}
/**
* 笔记详情
* GET /note/v1/item/:id
*/
public function read(int $id)
{
try {
$noteUserId = $this->getCurrentNoteUserId();
if ($id <= 0) {
return Response::error('笔记 ID 不正确', 400);
}
$result = $this->noteService->getDetail($noteUserId, $id);
return Response::success($result);
} catch (\Throwable $e) {
return Response::error($e->getMessage(), $e->getCode() ?: 500);
}
}
/**
* 更新笔记
* POST /note/v1/item/update/:id
*/
public function update(int $id)
{
try {
$noteUserId = $this->getCurrentNoteUserId();
if ($id <= 0) {
return Response::error('笔记 ID 不正确', 400);
}
$result = $this->noteService->update($noteUserId, $id, $this->request->post());
return Response::success($result, '更新成功');
} catch (\Throwable $e) {
return Response::error($e->getMessage(), $e->getCode() ?: 500);
}
}
/**
* 删除笔记
* POST /note/v1/item/delete/:id
*/
public function delete(int $id)
{
try {
$noteUserId = $this->getCurrentNoteUserId();
if ($id <= 0) {
return Response::error('笔记 ID 不正确', 400);
}
$result = $this->noteService->delete($noteUserId, $id);
return Response::success($result, '删除成功');
} catch (\Throwable $e) {
return Response::error($e->getMessage(), $e->getCode() ?: 500);
}
}
/**
* 保存实时转写内容
* POST /note/v1/item/transcript/:id
*/
public function transcript(int $id)
{
try {
$noteUserId = $this->getCurrentNoteUserId();
$data = $this->request->post();
if ($id <= 0) {
return Response::error('笔记 ID 不正确', 400);
}
validate([
'full_text' => 'require',
], [
'full_text.require' => '转写文本不能为空',
])->check($data);
$result = $this->noteService->saveTranscript($noteUserId, $id, $data);
return Response::success($result, '转写保存成功');
} catch (ValidateException $e) {
return Response::error($e->getMessage(), 400);
} catch (\Throwable $e) {
return Response::error($e->getMessage(), $e->getCode() ?: 500);
}
}
}