Files
mini_tp/app/note/controller/v1/Note.php
T
nepiedg 36c506f4bf feat(note): add audio upload and sharing functionality
- Introduced `note_audio` table for storing audio attachments related to notes.
- Implemented audio upload endpoint in `Note` controller to handle audio file uploads.
- Added sharing functionality with `note_share` table to manage share tokens and view counts.
- Updated API routes to include endpoints for audio uploads and share creation.
- Enhanced documentation to reflect new audio and sharing features.
2026-04-17 10:33:33 +00:00

182 lines
5.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\NoteService;
use think\App;
use think\exception\ValidateException;
use think\Request;
/**
* 笔记控制器
*/
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);
}
}
/**
* 上传笔记录音
* POST /note/v1/item/audio/:id
*/
public function audio(Request $request, int $id)
{
try {
if ($id <= 0) {
return Response::error('笔记 ID 不正确', 400);
}
$file = $request->file('audio');
if (!$file) {
return Response::error('录音文件不能为空', 400);
}
$durationMs = (int) $request->post('audio_duration_ms', 0);
$result = $this->noteService->uploadAudio($this->getCurrentNoteUserId(), $id, $file, $durationMs);
return Response::success($result, '上传成功');
} catch (\Throwable $e) {
return Response::error($e->getMessage(), $e->getCode() ?: 500);
}
}
}