66 lines
2.2 KiB
TypeScript
66 lines
2.2 KiB
TypeScript
import { Body, Controller, Post } from '@nestjs/common';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { Repository } from 'typeorm';
|
|
import { TemplateExecutionEntity, ExecutionStatus } from './entities/template-execution.entity';
|
|
import { ResponseUtil, ApiResponse } from './utils/response.util';
|
|
|
|
@Controller()
|
|
export class AppController {
|
|
constructor(
|
|
@InjectRepository(TemplateExecutionEntity)
|
|
private readonly executionRepository: Repository<TemplateExecutionEntity>,
|
|
) { }
|
|
@Post('callback')
|
|
async callback(@Body() body: any): Promise<ApiResponse<string | null>> {
|
|
console.log(`🚀 [回调] 开始执行回调`);
|
|
console.log(`📋 回调参数: ${JSON.stringify(body, null, 2)}`);
|
|
const task_id = body.task_id;
|
|
if (!task_id) return ResponseUtil.error('缺少task_id', 400)
|
|
const res = body.data;
|
|
// {status: true, data: string[], task_id: string}
|
|
let resultUrl = ``
|
|
if (body.status) {
|
|
const data = body.data;
|
|
if (!data) throw new Error(`结果有误`);
|
|
if (Array.isArray(data) && data.length > 0) {
|
|
resultUrl = data[0];
|
|
}
|
|
resultUrl = data;
|
|
}
|
|
// 通过 taskId 查找对应的执行记录并更新状态
|
|
const execution = await this.executionRepository.findOne({
|
|
where: { taskId: task_id }
|
|
});
|
|
|
|
if (!execution) {
|
|
console.error(`未找到 taskId 为 ${task_id} 的执行记录`);
|
|
return ResponseUtil.error('未找到对应的执行记录', 404);
|
|
}
|
|
|
|
// 更新执行状态
|
|
const updateData: Partial<TemplateExecutionEntity> = {
|
|
completedAt: new Date(),
|
|
executionDuration: execution.startedAt
|
|
? Date.now() - execution.startedAt.getTime()
|
|
: undefined,
|
|
};
|
|
|
|
if (body.status) {
|
|
// 成功状态
|
|
updateData.status = ExecutionStatus.COMPLETED;
|
|
updateData.progress = 100;
|
|
updateData.outputUrl = resultUrl;
|
|
updateData.executionResult = res;
|
|
} else {
|
|
// 失败状态
|
|
updateData.status = ExecutionStatus.FAILED;
|
|
updateData.errorMessage = body.message || '任务执行失败';
|
|
updateData.executionResult = body;
|
|
}
|
|
|
|
await this.executionRepository.update(execution.id, updateData);
|
|
|
|
return ResponseUtil.success(resultUrl, '回调处理成功');
|
|
}
|
|
}
|