|
@@ -3,6 +3,7 @@
|
|
|
*/
|
|
*/
|
|
|
|
|
|
|
|
import Router from '@koa/router';
|
|
import Router from '@koa/router';
|
|
|
|
|
+import { optionalAuth } from '../../middleware/auth';
|
|
|
import {
|
|
import {
|
|
|
createVideoProject,
|
|
createVideoProject,
|
|
|
getVideoProjects,
|
|
getVideoProjects,
|
|
@@ -19,15 +20,22 @@ import {
|
|
|
|
|
|
|
|
const router = new Router();
|
|
const router = new Router();
|
|
|
|
|
|
|
|
|
|
+// 测试用户兜底(与其他模块一致)
|
|
|
|
|
+const TEST_USER_ID = 1;
|
|
|
|
|
+function currentUserId(ctx: any): number {
|
|
|
|
|
+ const uid = ctx.state.user?.userId;
|
|
|
|
|
+ return uid ? parseInt(String(uid)) : TEST_USER_ID;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
// ============ 视频项目管理 ============
|
|
// ============ 视频项目管理 ============
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* GET /api/video/projects
|
|
* GET /api/video/projects
|
|
|
- * 获取视频项目列表
|
|
|
|
|
|
|
+ * 获取视频项目列表(只返回当前用户的项目)
|
|
|
*/
|
|
*/
|
|
|
-router.get('/projects', async (ctx) => {
|
|
|
|
|
|
|
+router.get('/projects', optionalAuth, async (ctx) => {
|
|
|
const query = {
|
|
const query = {
|
|
|
- userId: ctx.query.userId ? Number(ctx.query.userId) : undefined,
|
|
|
|
|
|
|
+ userId: currentUserId(ctx), // 强制用当前登录用户,忽略客户端传入的 userId
|
|
|
status: ctx.query.status as any,
|
|
status: ctx.query.status as any,
|
|
|
page: ctx.query.page ? Number(ctx.query.page) : 1,
|
|
page: ctx.query.page ? Number(ctx.query.page) : 1,
|
|
|
pageSize: ctx.query.pageSize ? Number(ctx.query.pageSize) : 10,
|
|
pageSize: ctx.query.pageSize ? Number(ctx.query.pageSize) : 10,
|
|
@@ -41,9 +49,9 @@ router.get('/projects', async (ctx) => {
|
|
|
* POST /api/video/projects
|
|
* POST /api/video/projects
|
|
|
* 创建视频项目
|
|
* 创建视频项目
|
|
|
*/
|
|
*/
|
|
|
-router.post('/projects', async (ctx) => {
|
|
|
|
|
|
|
+router.post('/projects', optionalAuth, async (ctx) => {
|
|
|
const body = ctx.request.body as any;
|
|
const body = ctx.request.body as any;
|
|
|
- const userId = ctx.state.user?.id;
|
|
|
|
|
|
|
+ const userId = currentUserId(ctx);
|
|
|
|
|
|
|
|
const project = await createVideoProject(body, userId);
|
|
const project = await createVideoProject(body, userId);
|
|
|
ctx.body = { success: true, data: project };
|
|
ctx.body = { success: true, data: project };
|
|
@@ -53,9 +61,9 @@ router.post('/projects', async (ctx) => {
|
|
|
* GET /api/video/projects/:id
|
|
* GET /api/video/projects/:id
|
|
|
* 获取视频项目详情
|
|
* 获取视频项目详情
|
|
|
*/
|
|
*/
|
|
|
-router.get('/projects/:id', async (ctx) => {
|
|
|
|
|
|
|
+router.get('/projects/:id', optionalAuth, async (ctx) => {
|
|
|
const id = Number(ctx.params.id);
|
|
const id = Number(ctx.params.id);
|
|
|
- const project = await getVideoProject(id);
|
|
|
|
|
|
|
+ const project = await getVideoProject(id, currentUserId(ctx));
|
|
|
|
|
|
|
|
if (!project) {
|
|
if (!project) {
|
|
|
ctx.status = 404;
|
|
ctx.status = 404;
|
|
@@ -70,11 +78,11 @@ router.get('/projects/:id', async (ctx) => {
|
|
|
* PUT /api/video/projects/:id
|
|
* PUT /api/video/projects/:id
|
|
|
* 更新视频项目
|
|
* 更新视频项目
|
|
|
*/
|
|
*/
|
|
|
-router.put('/projects/:id', async (ctx) => {
|
|
|
|
|
|
|
+router.put('/projects/:id', optionalAuth, async (ctx) => {
|
|
|
const id = Number(ctx.params.id);
|
|
const id = Number(ctx.params.id);
|
|
|
const body = ctx.request.body as any;
|
|
const body = ctx.request.body as any;
|
|
|
|
|
|
|
|
- const project = await updateVideoProject(id, body);
|
|
|
|
|
|
|
+ const project = await updateVideoProject(id, body, currentUserId(ctx));
|
|
|
|
|
|
|
|
if (!project) {
|
|
if (!project) {
|
|
|
ctx.status = 404;
|
|
ctx.status = 404;
|
|
@@ -89,9 +97,9 @@ router.put('/projects/:id', async (ctx) => {
|
|
|
* DELETE /api/video/projects/:id
|
|
* DELETE /api/video/projects/:id
|
|
|
* 删除视频项目
|
|
* 删除视频项目
|
|
|
*/
|
|
*/
|
|
|
-router.delete('/projects/:id', async (ctx) => {
|
|
|
|
|
|
|
+router.delete('/projects/:id', optionalAuth, async (ctx) => {
|
|
|
const id = Number(ctx.params.id);
|
|
const id = Number(ctx.params.id);
|
|
|
- const success = await deleteVideoProject(id);
|
|
|
|
|
|
|
+ const success = await deleteVideoProject(id, currentUserId(ctx));
|
|
|
|
|
|
|
|
if (!success) {
|
|
if (!success) {
|
|
|
ctx.status = 404;
|
|
ctx.status = 404;
|
|
@@ -108,9 +116,9 @@ router.delete('/projects/:id', async (ctx) => {
|
|
|
* POST /api/video/projects/:id/generate
|
|
* POST /api/video/projects/:id/generate
|
|
|
* 开始生成视频
|
|
* 开始生成视频
|
|
|
*/
|
|
*/
|
|
|
-router.post('/projects/:id/generate', async (ctx) => {
|
|
|
|
|
|
|
+router.post('/projects/:id/generate', optionalAuth, async (ctx) => {
|
|
|
const id = Number(ctx.params.id);
|
|
const id = Number(ctx.params.id);
|
|
|
- const result = await generateVideoForProject(id);
|
|
|
|
|
|
|
+ const result = await generateVideoForProject(id, currentUserId(ctx));
|
|
|
|
|
|
|
|
if (!result.success) {
|
|
if (!result.success) {
|
|
|
ctx.status = 400;
|
|
ctx.status = 400;
|
|
@@ -132,9 +140,9 @@ router.post('/projects/:id/generate', async (ctx) => {
|
|
|
* GET /api/video/projects/:id/status
|
|
* GET /api/video/projects/:id/status
|
|
|
* 获取生成状态
|
|
* 获取生成状态
|
|
|
*/
|
|
*/
|
|
|
-router.get('/projects/:id/status', async (ctx) => {
|
|
|
|
|
|
|
+router.get('/projects/:id/status', optionalAuth, async (ctx) => {
|
|
|
const id = Number(ctx.params.id);
|
|
const id = Number(ctx.params.id);
|
|
|
- const status = await getGenerateProgress(id);
|
|
|
|
|
|
|
+ const status = await getGenerateProgress(id, currentUserId(ctx));
|
|
|
|
|
|
|
|
ctx.body = { success: true, data: status };
|
|
ctx.body = { success: true, data: status };
|
|
|
});
|
|
});
|
|
@@ -145,9 +153,9 @@ router.get('/projects/:id/status', async (ctx) => {
|
|
|
* GET /api/video/materials
|
|
* GET /api/video/materials
|
|
|
* 获取素材列表
|
|
* 获取素材列表
|
|
|
*/
|
|
*/
|
|
|
-router.get('/materials', async (ctx) => {
|
|
|
|
|
|
|
+router.get('/materials', optionalAuth, async (ctx) => {
|
|
|
const query = {
|
|
const query = {
|
|
|
- userId: ctx.query.userId ? Number(ctx.query.userId) : undefined,
|
|
|
|
|
|
|
+ userId: currentUserId(ctx),
|
|
|
type: ctx.query.type as any,
|
|
type: ctx.query.type as any,
|
|
|
category: ctx.query.category as string,
|
|
category: ctx.query.category as string,
|
|
|
page: ctx.query.page ? Number(ctx.query.page) : 1,
|
|
page: ctx.query.page ? Number(ctx.query.page) : 1,
|
|
@@ -162,8 +170,8 @@ router.get('/materials', async (ctx) => {
|
|
|
* POST /api/video/materials/upload
|
|
* POST /api/video/materials/upload
|
|
|
* 上传素材(处理 multipart/form-data 文件上传)
|
|
* 上传素材(处理 multipart/form-data 文件上传)
|
|
|
*/
|
|
*/
|
|
|
-router.post('/materials/upload', async (ctx) => {
|
|
|
|
|
- const userId = ctx.state.user?.id;
|
|
|
|
|
|
|
+router.post('/materials/upload', optionalAuth, async (ctx) => {
|
|
|
|
|
+ const userId = currentUserId(ctx);
|
|
|
|
|
|
|
|
// 处理 multipart form data
|
|
// 处理 multipart form data
|
|
|
const body = ctx.request.body as any;
|
|
const body = ctx.request.body as any;
|
|
@@ -206,7 +214,7 @@ router.post('/materials/upload', async (ctx) => {
|
|
|
* DELETE /api/video/materials/:id
|
|
* DELETE /api/video/materials/:id
|
|
|
* 删除素材
|
|
* 删除素材
|
|
|
*/
|
|
*/
|
|
|
-router.delete('/materials/:id', async (ctx) => {
|
|
|
|
|
|
|
+router.delete('/materials/:id', optionalAuth, async (ctx) => {
|
|
|
const id = Number(ctx.params.id);
|
|
const id = Number(ctx.params.id);
|
|
|
const success = await deleteMaterial(id);
|
|
const success = await deleteMaterial(id);
|
|
|
|
|
|
|
@@ -225,11 +233,11 @@ router.delete('/materials/:id', async (ctx) => {
|
|
|
* POST /api/video/books/:bookId/generate
|
|
* POST /api/video/books/:bookId/generate
|
|
|
* 从书籍生成视频项目
|
|
* 从书籍生成视频项目
|
|
|
*/
|
|
*/
|
|
|
-router.post('/books/:bookId/generate', async (ctx) => {
|
|
|
|
|
|
|
+router.post('/books/:bookId/generate', optionalAuth, async (ctx) => {
|
|
|
const bookId = Number(ctx.params.bookId);
|
|
const bookId = Number(ctx.params.bookId);
|
|
|
- const userId = ctx.state.user?.id;
|
|
|
|
|
|
|
+ const userId = currentUserId(ctx);
|
|
|
|
|
|
|
|
- const project = await createVideoProjectFromBook(bookId, userId);
|
|
|
|
|
|
|
+ const project = await createVideoProjectFromBook(bookId, undefined, userId);
|
|
|
|
|
|
|
|
if (!project) {
|
|
if (!project) {
|
|
|
ctx.status = 404;
|
|
ctx.status = 404;
|