- 新增Google OAuth 2.0认证模块 - 实现Google Strategy和AuthController - 添加Google平台适配器和用户统一管理 - 支持OAuth回调和token交换机制 - 完善Stripe支付集成 - 新增Stripe支付适配器和控制器 - 实现webhook事件处理和支付确认 - 支持多币种和国际化支付 - 扩展平台类型支持 - 在PlatformType中新增GOOGLE和STRIPE - 更新适配器工厂注册机制 - 完善跨平台用户身份管理 - 增强依赖和配置 - 添加@nestjs/passport和passport-google-oauth20 - 更新支付方法枚举和货币支持 - 完善订单号生成和回调URL构建
87 lines
2.8 KiB
TypeScript
87 lines
2.8 KiB
TypeScript
import { Body, Controller, Post, Get } from '@nestjs/common';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { Repository, DataSource } 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>,
|
|
private readonly dataSource: DataSource,
|
|
) {}
|
|
|
|
@Get('health')
|
|
async health(): Promise<ApiResponse<{ status: string; database: string; timestamp: string }>> {
|
|
try {
|
|
// 检查数据库连接
|
|
await this.dataSource.query('SELECT 1');
|
|
|
|
return ResponseUtil.success({
|
|
status: 'healthy',
|
|
database: 'connected',
|
|
timestamp: new Date().toISOString(),
|
|
}, '服务健康');
|
|
} catch (error) {
|
|
return ResponseUtil.error(`健康检查失败: ${error.message}`, 500);
|
|
}
|
|
}
|
|
|
|
@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, '回调处理成功');
|
|
}
|
|
}
|