refactor: 统一使用 ResponseUtil 构造 API 响应格式
- 统一 unified-user.controller.ts 的响应格式 - 统一 template.controller.ts 的响应格式 - 统一 content-moderation.controller.ts 的响应格式 - 所有响应现在包含 code、message、data、timestamp、traceId 字段 - 提高响应格式一致性和可追踪性
This commit is contained in:
@@ -9,6 +9,7 @@ import {
|
||||
} from '../dto/audit-response.dto';
|
||||
import { PlatformAuthGuard } from '../../platform/guards/platform-auth.guard';
|
||||
import { PlatformType } from '../../entities/platform-user.entity';
|
||||
import { ResponseUtil, ApiResponse as ApiResponseType } from '../../utils/response.util';
|
||||
|
||||
// 模拟当前用户装饰器,实际应从现有项目中导入
|
||||
const CurrentUser = () => {
|
||||
@@ -41,11 +42,7 @@ export class ContentModerationController {
|
||||
|
||||
const result = await this.unifiedContentService.auditImage(platform, auditData);
|
||||
|
||||
return {
|
||||
code: 200,
|
||||
message: '审核提交成功',
|
||||
data: result,
|
||||
};
|
||||
return ResponseUtil.success(result, '审核提交成功');
|
||||
}
|
||||
|
||||
@Post(':platform/audit-batch')
|
||||
@@ -68,16 +65,12 @@ export class ContentModerationController {
|
||||
const successCount = results.filter(r => r.status === 'completed').length;
|
||||
const failureCount = results.length - successCount;
|
||||
|
||||
return {
|
||||
code: 200,
|
||||
message: '批量审核提交成功',
|
||||
data: {
|
||||
totalCount: results.length,
|
||||
successCount,
|
||||
failureCount,
|
||||
results,
|
||||
},
|
||||
};
|
||||
return ResponseUtil.success({
|
||||
totalCount: results.length,
|
||||
successCount,
|
||||
failureCount,
|
||||
results,
|
||||
}, '批量审核提交成功');
|
||||
}
|
||||
|
||||
@Get(':platform/result/:taskId')
|
||||
@@ -91,11 +84,7 @@ export class ContentModerationController {
|
||||
) {
|
||||
const result = await this.unifiedContentService.queryAuditResult(platform, taskId);
|
||||
|
||||
return {
|
||||
code: 200,
|
||||
message: '查询成功',
|
||||
data: result,
|
||||
};
|
||||
return ResponseUtil.success(result, '查询成功');
|
||||
}
|
||||
|
||||
@Post(':platform/callback')
|
||||
@@ -106,10 +95,7 @@ export class ContentModerationController {
|
||||
) {
|
||||
await this.unifiedContentService.handleAuditCallback(platform, callbackData);
|
||||
|
||||
return {
|
||||
code: 200,
|
||||
message: '回调处理成功',
|
||||
};
|
||||
return ResponseUtil.success(null, '回调处理成功');
|
||||
}
|
||||
|
||||
@Get('history')
|
||||
@@ -122,14 +108,10 @@ export class ContentModerationController {
|
||||
) {
|
||||
const history = await this.unifiedContentService.getUserAuditHistory(user.userId, limit);
|
||||
|
||||
return {
|
||||
code: 200,
|
||||
message: '获取成功',
|
||||
data: {
|
||||
total: history.length,
|
||||
list: history,
|
||||
},
|
||||
};
|
||||
return ResponseUtil.success({
|
||||
total: history.length,
|
||||
list: history,
|
||||
}, '获取成功');
|
||||
}
|
||||
|
||||
@Get('stats')
|
||||
@@ -157,23 +139,19 @@ export class ContentModerationController {
|
||||
const avgConfidence = stats.reduce((sum: number, item: any) =>
|
||||
sum + (parseFloat(item.avgConfidence) || 0), 0) / (stats.length || 1);
|
||||
|
||||
return {
|
||||
code: 200,
|
||||
message: '获取成功',
|
||||
data: {
|
||||
totalAudits,
|
||||
passRate: totalAudits > 0 ? (passCount / totalAudits * 100) : 0,
|
||||
rejectRate: totalAudits > 0 ? (rejectCount / totalAudits * 100) : 0,
|
||||
reviewRate: totalAudits > 0 ? (reviewCount / totalAudits * 100) : 0,
|
||||
averageConfidence: avgConfidence,
|
||||
platformStats: stats.map((item: any) => ({
|
||||
platform: item.platform,
|
||||
count: parseInt(item.count),
|
||||
conclusion: item.conclusion,
|
||||
avgConfidence: parseFloat(item.avgConfidence) || 0,
|
||||
})),
|
||||
},
|
||||
};
|
||||
return ResponseUtil.success({
|
||||
totalAudits,
|
||||
passRate: totalAudits > 0 ? (passCount / totalAudits * 100) : 0,
|
||||
rejectRate: totalAudits > 0 ? (rejectCount / totalAudits * 100) : 0,
|
||||
reviewRate: totalAudits > 0 ? (reviewCount / totalAudits * 100) : 0,
|
||||
averageConfidence: avgConfidence,
|
||||
platformStats: stats.map((item: any) => ({
|
||||
platform: item.platform,
|
||||
count: parseInt(item.count),
|
||||
conclusion: item.conclusion,
|
||||
avgConfidence: parseFloat(item.avgConfidence) || 0,
|
||||
})),
|
||||
}, '获取成功');
|
||||
}
|
||||
|
||||
@Get('user-stats')
|
||||
@@ -186,11 +164,7 @@ export class ContentModerationController {
|
||||
) {
|
||||
const stats = await this.unifiedContentService.getUserAuditStats(user.userId, days);
|
||||
|
||||
return {
|
||||
code: 200,
|
||||
message: '获取成功',
|
||||
data: stats,
|
||||
};
|
||||
return ResponseUtil.success(stats, '获取成功');
|
||||
}
|
||||
|
||||
@Get('platforms')
|
||||
@@ -198,11 +172,7 @@ export class ContentModerationController {
|
||||
async getSupportedPlatforms() {
|
||||
const platforms = this.unifiedContentService.getSupportedPlatforms();
|
||||
|
||||
return {
|
||||
code: 200,
|
||||
message: '获取成功',
|
||||
data: platforms,
|
||||
};
|
||||
return ResponseUtil.success(platforms, '获取成功');
|
||||
}
|
||||
|
||||
@Get('check/:taskId')
|
||||
@@ -210,13 +180,9 @@ export class ContentModerationController {
|
||||
async checkContentApproval(@Param('taskId') taskId: string) {
|
||||
const isApproved = await this.unifiedContentService.isContentApproved(taskId);
|
||||
|
||||
return {
|
||||
code: 200,
|
||||
message: '检查成功',
|
||||
data: {
|
||||
taskId,
|
||||
approved: isApproved,
|
||||
},
|
||||
};
|
||||
return ResponseUtil.success({
|
||||
taskId,
|
||||
approved: isApproved,
|
||||
}, '检查成功');
|
||||
}
|
||||
}
|
||||
@@ -98,17 +98,14 @@ export class TemplateController {
|
||||
const template = await this.templateFactory.createTemplate(templateId);
|
||||
const result = await template.execute(imageUrl);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
templateId,
|
||||
templateCode: template.code,
|
||||
templateName: template.name,
|
||||
inputUrl: imageUrl,
|
||||
outputUrl: result,
|
||||
creditCost: template.creditCost,
|
||||
},
|
||||
};
|
||||
return ResponseUtil.success({
|
||||
templateId,
|
||||
templateCode: template.code,
|
||||
templateName: template.name,
|
||||
inputUrl: imageUrl,
|
||||
outputUrl: result,
|
||||
creditCost: template.creditCost,
|
||||
}, '执行成功');
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'Template execution failed',
|
||||
@@ -328,17 +325,14 @@ export class TemplateController {
|
||||
imageUrls,
|
||||
);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
templateId,
|
||||
totalCount: imageUrls.length,
|
||||
results: imageUrls.map((inputUrl, index) => ({
|
||||
inputUrl,
|
||||
outputUrl: results[index],
|
||||
})),
|
||||
},
|
||||
};
|
||||
return ResponseUtil.success({
|
||||
templateId,
|
||||
totalCount: imageUrls.length,
|
||||
results: imageUrls.map((inputUrl, index) => ({
|
||||
inputUrl,
|
||||
outputUrl: results[index],
|
||||
})),
|
||||
}, '批量执行成功');
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'Batch execution failed',
|
||||
@@ -399,10 +393,7 @@ export class TemplateController {
|
||||
async getImageTemplates() {
|
||||
try {
|
||||
const templates = await this.templateFactory.getImageTemplates();
|
||||
return {
|
||||
success: true,
|
||||
data: templates,
|
||||
};
|
||||
return ResponseUtil.success(templates, '获取图片模板列表成功');
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'Failed to fetch image templates',
|
||||
@@ -424,10 +415,7 @@ export class TemplateController {
|
||||
async getVideoTemplates() {
|
||||
try {
|
||||
const templates = await this.templateFactory.getVideoTemplates();
|
||||
return {
|
||||
success: true,
|
||||
data: templates,
|
||||
};
|
||||
return ResponseUtil.success(templates, '获取视频模板列表成功');
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'Failed to fetch video templates',
|
||||
@@ -477,13 +465,10 @@ export class TemplateController {
|
||||
limit,
|
||||
);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
userTags,
|
||||
recommendedTemplates: templates,
|
||||
},
|
||||
};
|
||||
return ResponseUtil.success({
|
||||
userTags,
|
||||
recommendedTemplates: templates,
|
||||
}, '模板推荐成功');
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'Failed to recommend templates',
|
||||
@@ -511,20 +496,17 @@ export class TemplateController {
|
||||
try {
|
||||
const template = await this.templateFactory.createTemplate(templateId);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
id: templateId,
|
||||
code: template.code,
|
||||
name: template.name,
|
||||
description: template.description,
|
||||
creditCost: template.creditCost,
|
||||
version: template.version,
|
||||
tags: template.tags,
|
||||
inputExample: template.input,
|
||||
outputExample: template.output,
|
||||
},
|
||||
};
|
||||
return ResponseUtil.success({
|
||||
id: templateId,
|
||||
code: template.code,
|
||||
name: template.name,
|
||||
description: template.description,
|
||||
creditCost: template.creditCost,
|
||||
version: template.version,
|
||||
tags: template.tags,
|
||||
inputExample: template.input,
|
||||
outputExample: template.output,
|
||||
}, '获取模板详情成功');
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'Template not found',
|
||||
@@ -617,26 +599,23 @@ export class TemplateController {
|
||||
|
||||
const executions = await queryBuilder.getMany();
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: executions.map((execution) => ({
|
||||
taskId: execution.id,
|
||||
templateId: execution.templateId,
|
||||
templateName: execution.template?.name,
|
||||
templateCode: execution.template?.code,
|
||||
type: execution.type,
|
||||
status: execution.status,
|
||||
progress: execution.progress,
|
||||
inputImageUrl: execution.inputImageUrl,
|
||||
outputUrl: execution.outputUrl,
|
||||
thumbnailUrl: execution.thumbnailUrl,
|
||||
creditCost: execution.creditCost,
|
||||
startedAt: execution.startedAt,
|
||||
completedAt: execution.completedAt,
|
||||
executionDuration: execution.executionDuration,
|
||||
createdAt: execution.createdAt,
|
||||
})),
|
||||
};
|
||||
return ResponseUtil.success(executions.map((execution) => ({
|
||||
taskId: execution.id,
|
||||
templateId: execution.templateId,
|
||||
templateName: execution.template?.name,
|
||||
templateCode: execution.template?.code,
|
||||
type: execution.type,
|
||||
status: execution.status,
|
||||
progress: execution.progress,
|
||||
inputImageUrl: execution.inputImageUrl,
|
||||
outputUrl: execution.outputUrl,
|
||||
thumbnailUrl: execution.thumbnailUrl,
|
||||
creditCost: execution.creditCost,
|
||||
startedAt: execution.startedAt,
|
||||
completedAt: execution.completedAt,
|
||||
executionDuration: execution.executionDuration,
|
||||
createdAt: execution.createdAt,
|
||||
})), '获取用户执行任务列表成功');
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'Failed to fetch user executions',
|
||||
@@ -685,10 +664,7 @@ export class TemplateController {
|
||||
const template = this.templateRepository.create(createTemplateDto);
|
||||
const savedTemplate = await this.templateRepository.save(template);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: savedTemplate,
|
||||
};
|
||||
return ResponseUtil.success(savedTemplate, '模板创建成功');
|
||||
} catch (error) {
|
||||
if (error instanceof HttpException) {
|
||||
throw error;
|
||||
@@ -754,10 +730,7 @@ export class TemplateController {
|
||||
where: { id: templateId },
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: updatedTemplate,
|
||||
};
|
||||
return ResponseUtil.success(updatedTemplate, '模板更新成功');
|
||||
} catch (error) {
|
||||
if (error instanceof HttpException) {
|
||||
throw error;
|
||||
@@ -787,10 +760,7 @@ export class TemplateController {
|
||||
|
||||
await this.templateRepository.delete(templateId);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: 'Template deleted successfully',
|
||||
};
|
||||
return ResponseUtil.success(null, '模板删除成功');
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'Failed to delete template',
|
||||
@@ -817,10 +787,7 @@ export class TemplateController {
|
||||
throw new HttpException('Template not found', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: template,
|
||||
};
|
||||
return ResponseUtil.success(template, '获取模板详情成功');
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'Template not found',
|
||||
@@ -864,18 +831,7 @@ export class TemplateController {
|
||||
|
||||
const [templates, total] = await queryBuilder.getManyAndCount();
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
templates,
|
||||
pagination: {
|
||||
page,
|
||||
limit,
|
||||
total,
|
||||
totalPages: Math.ceil(total / limit),
|
||||
},
|
||||
},
|
||||
};
|
||||
return ResponseUtil.paginated(templates, total, page, limit, '获取模板列表成功');
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'Failed to fetch templates',
|
||||
|
||||
@@ -83,16 +83,12 @@ export class UnifiedUserController {
|
||||
},
|
||||
},
|
||||
})
|
||||
async register(@Body() registerDto: PlatformLoginDto) {
|
||||
async register(@Body() registerDto: PlatformLoginDto): Promise<ApiResponseType<any>> {
|
||||
const result = await this.unifiedUserService.register(
|
||||
registerDto.platform,
|
||||
registerDto,
|
||||
);
|
||||
return {
|
||||
code: 200,
|
||||
message: '注册成功',
|
||||
data: result,
|
||||
};
|
||||
return ResponseUtil.success(result, '注册成功');
|
||||
}
|
||||
|
||||
@Get('profile')
|
||||
@@ -122,13 +118,9 @@ export class UnifiedUserController {
|
||||
},
|
||||
})
|
||||
@ApiResponse({ status: 401, description: '未授权访问' })
|
||||
async getProfile(@Request() req) {
|
||||
async getProfile(@Request() req): Promise<ApiResponseType<any>> {
|
||||
const userInfo = await this.unifiedUserService.getUserInfo(req.user.userId);
|
||||
return {
|
||||
code: 200,
|
||||
message: '获取成功',
|
||||
data: userInfo,
|
||||
};
|
||||
return ResponseUtil.success(userInfo, '获取成功');
|
||||
}
|
||||
|
||||
@Put('profile')
|
||||
@@ -149,16 +141,13 @@ export class UnifiedUserController {
|
||||
},
|
||||
})
|
||||
@ApiResponse({ status: 401, description: '未授权访问' })
|
||||
async updateProfile(@Request() req, @Body() updateDto: UserInfoUpdateDto) {
|
||||
async updateProfile(@Request() req, @Body() updateDto: UserInfoUpdateDto): Promise<ApiResponseType<null>> {
|
||||
await this.unifiedUserService.updateUserInfo(
|
||||
req.user.platform,
|
||||
req.user.userId,
|
||||
updateDto,
|
||||
);
|
||||
return {
|
||||
code: 200,
|
||||
message: '更新成功',
|
||||
};
|
||||
return ResponseUtil.success(null, '更新成功');
|
||||
}
|
||||
|
||||
@Post('bind-platform')
|
||||
@@ -180,16 +169,13 @@ export class UnifiedUserController {
|
||||
})
|
||||
@ApiResponse({ status: 401, description: '未授权访问' })
|
||||
@ApiResponse({ status: 409, description: '平台账号已绑定其他用户' })
|
||||
async bindPlatform(@Request() req, @Body() bindDto: PlatformBindDto) {
|
||||
async bindPlatform(@Request() req, @Body() bindDto: PlatformBindDto): Promise<ApiResponseType<null>> {
|
||||
await this.unifiedUserService.bindPlatform(
|
||||
req.user.userId,
|
||||
bindDto.platform,
|
||||
bindDto,
|
||||
);
|
||||
return {
|
||||
code: 200,
|
||||
message: '绑定成功',
|
||||
};
|
||||
return ResponseUtil.success(null, '绑定成功');
|
||||
}
|
||||
|
||||
@Delete('unbind-platform/:platform')
|
||||
@@ -214,12 +200,9 @@ export class UnifiedUserController {
|
||||
async unbindPlatform(
|
||||
@Request() req,
|
||||
@Param('platform') platform: PlatformType,
|
||||
) {
|
||||
): Promise<ApiResponseType<null>> {
|
||||
await this.unifiedUserService.unbindPlatform(req.user.userId, platform);
|
||||
return {
|
||||
code: 200,
|
||||
message: '解绑成功',
|
||||
};
|
||||
return ResponseUtil.success(null, '解绑成功');
|
||||
}
|
||||
|
||||
@Get('platforms')
|
||||
@@ -249,12 +232,8 @@ export class UnifiedUserController {
|
||||
},
|
||||
},
|
||||
})
|
||||
async getSupportedPlatforms() {
|
||||
async getSupportedPlatforms(): Promise<ApiResponseType<any[]>> {
|
||||
const platforms = this.unifiedUserService.getSupportedPlatforms();
|
||||
return {
|
||||
code: 200,
|
||||
message: '获取成功',
|
||||
data: platforms,
|
||||
};
|
||||
return ResponseUtil.success(platforms, '获取成功');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user