Compare commits
4 Commits
e30d7cc4da
...
457e69f46b
| Author | SHA1 | Date |
|---|---|---|
|
|
457e69f46b | |
|
|
52c22ab9bd | |
|
|
b2cf85f51d | |
|
|
d2a4db2863 |
|
|
@ -1,21 +1,162 @@
|
||||||
import { Body, Controller, Get, Param, Post } from '@nestjs/common';
|
import { Body, Controller, Get, Param, Post } from '@nestjs/common';
|
||||||
import { TemplateService } from './templates/index'
|
import { TemplateService } from './templates/index'
|
||||||
@Controller()
|
import { Template } from './templates/types';
|
||||||
|
interface ApiResponse<T = any> {
|
||||||
|
status: boolean | string;
|
||||||
|
data: T;
|
||||||
|
msg: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Controller('api/v1/templates')
|
||||||
export class AppController {
|
export class AppController {
|
||||||
constructor(private readonly tempalte: TemplateService) { }
|
constructor(private readonly tempalte: TemplateService) { }
|
||||||
|
|
||||||
@Get()
|
@Get()
|
||||||
async getTemplates() {
|
async getTemplates(): Promise<ApiResponse<Template[]>> {
|
||||||
return this.tempalte.getAllTemplates();
|
console.log(`📋 [获取模板列表] 请求获取所有模板`);
|
||||||
|
console.log(`⏰ 请求时间: ${new Date().toISOString()}`);
|
||||||
|
|
||||||
|
const templates = this.tempalte.getAllTemplates();
|
||||||
|
|
||||||
|
console.log(`✅ [获取模板列表] 成功获取 ${templates.length} 个模板`);
|
||||||
|
|
||||||
|
// 安全地输出模板列表
|
||||||
|
try {
|
||||||
|
const templateNames = templates.map(t => t?.code || t?.name || '[未命名模板]').filter(Boolean);
|
||||||
|
console.log(`📊 模板列表: ${templateNames.join(', ')}`);
|
||||||
|
} catch (error) {
|
||||||
|
console.log(`📊 模板列表: [无法获取模板名称]`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
status: true,
|
||||||
|
data: templates,
|
||||||
|
msg: 'success'
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get(':templateCode')
|
@Get(':templateCode')
|
||||||
async getTemplate(@Param('templateCode') templateCode: string) {
|
async getTemplate(@Param('templateCode') templateCode: string): Promise<ApiResponse<Template | null>> {
|
||||||
return this.tempalte.getTemplate(templateCode);
|
console.log(`🔍 [获取模板] 请求获取模板: ${templateCode}`);
|
||||||
|
console.log(`⏰ 请求时间: ${new Date().toISOString()}`);
|
||||||
|
|
||||||
|
const template = this.tempalte.getTemplate(templateCode);
|
||||||
|
|
||||||
|
if (template) {
|
||||||
|
console.log(`✅ [获取模板] 模板找到: ${template.name || templateCode}`);
|
||||||
|
|
||||||
|
// 安全地输出模板信息,避免循环引用
|
||||||
|
try {
|
||||||
|
const templateInfo = {
|
||||||
|
code: template.code,
|
||||||
|
name: template.name,
|
||||||
|
description: template.description,
|
||||||
|
// 只输出基本信息,避免复杂对象
|
||||||
|
};
|
||||||
|
console.log(`📋 模板信息: ${JSON.stringify(templateInfo, null, 2)}`);
|
||||||
|
} catch (jsonError) {
|
||||||
|
console.log(`📋 模板信息: [无法序列化的复杂对象]`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
status: true,
|
||||||
|
data: template,
|
||||||
|
msg: 'success'
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`❌ [获取模板] 模板不存在: ${templateCode}`);
|
||||||
|
|
||||||
|
return {
|
||||||
|
status: false,
|
||||||
|
data: null,
|
||||||
|
msg: '模板不存在'
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post()
|
@Post()
|
||||||
async executeTemplate(@Body('imageUrl') imageUrl: string, @Body('templateCode') templateCode: string) {
|
async executeTemplate(@Body('imageUrl') imageUrl: string, @Body('templateCode') templateCode: string): Promise<ApiResponse<string | null>> {
|
||||||
return this.tempalte.executeTemplate(templateCode, imageUrl);
|
const startTime = Date.now();
|
||||||
|
console.log(`🚀 [模板执行] 开始执行模板`);
|
||||||
|
console.log(`📋 模板代码: ${templateCode}`);
|
||||||
|
console.log(`🖼️ 图片URL: ${imageUrl}`);
|
||||||
|
console.log(`⏰ 开始时间: ${new Date().toISOString()}`);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 验证输入参数
|
||||||
|
if (!templateCode) {
|
||||||
|
console.log(`❌ [模板执行] 参数验证失败: 模板代码为空`);
|
||||||
|
return {
|
||||||
|
status: false,
|
||||||
|
data: null,
|
||||||
|
msg: '模板代码不能为空'
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!imageUrl) {
|
||||||
|
console.log(`❌ [模板执行] 参数验证失败: 图片URL为空`);
|
||||||
|
return {
|
||||||
|
status: false,
|
||||||
|
data: null,
|
||||||
|
msg: '图片URL不能为空'
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`✅ [模板执行] 参数验证通过,开始执行模板处理`);
|
||||||
|
|
||||||
|
const result = await this.tempalte.executeTemplate(templateCode, imageUrl);
|
||||||
|
const executionTime = Date.now() - startTime;
|
||||||
|
|
||||||
|
if (result) {
|
||||||
|
console.log(`🎉 [模板执行] 执行成功`);
|
||||||
|
console.log(`📊 执行结果类型: ${typeof result}`);
|
||||||
|
|
||||||
|
// 安全地获取结果长度
|
||||||
|
if (typeof result === 'string') {
|
||||||
|
console.log(`📊 执行结果长度: ${result.length} 字符`);
|
||||||
|
} else if (result && typeof result === 'object') {
|
||||||
|
console.log(`📊 执行结果: [对象类型]`);
|
||||||
|
} else {
|
||||||
|
console.log(`📊 执行结果: ${result}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`⏱️ 执行耗时: ${executionTime}ms`);
|
||||||
|
console.log(`✨ 完成时间: ${new Date().toISOString()}`);
|
||||||
|
|
||||||
|
return {
|
||||||
|
status: true,
|
||||||
|
data: result,
|
||||||
|
msg: 'success'
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`⚠️ [模板执行] 执行完成但结果为空或undefined`);
|
||||||
|
console.log(`📊 结果值: ${result}`);
|
||||||
|
console.log(`⏱️ 执行耗时: ${executionTime}ms`);
|
||||||
|
|
||||||
|
return {
|
||||||
|
status: false,
|
||||||
|
data: null,
|
||||||
|
msg: '执行失败'
|
||||||
|
};
|
||||||
|
} catch (e) {
|
||||||
|
const executionTime = Date.now() - startTime;
|
||||||
|
console.log(`💥 [模板执行] 执行异常`);
|
||||||
|
console.log(`❌ 错误信息: ${e?.message || '未知错误'}`);
|
||||||
|
|
||||||
|
// 安全地输出错误堆栈
|
||||||
|
if (e?.stack) {
|
||||||
|
console.log(`📍 错误堆栈: ${e.stack}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`⏱️ 执行耗时: ${executionTime}ms`);
|
||||||
|
console.log(`🔚 异常时间: ${new Date().toISOString()}`);
|
||||||
|
|
||||||
|
return {
|
||||||
|
status: false,
|
||||||
|
data: null,
|
||||||
|
msg: e?.message || '模板执行异常'
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,11 @@
|
||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { AppController } from './app.controller';
|
import { AppController } from './app.controller';
|
||||||
import { TemplateService } from './templates/index';
|
import { TemplateService } from './templates/index';
|
||||||
|
import { TemplateManager } from './templates/types';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [],
|
imports: [],
|
||||||
controllers: [AppController],
|
controllers: [AppController],
|
||||||
providers: [TemplateService],
|
providers: [TemplateService, TemplateManager],
|
||||||
})
|
})
|
||||||
export class AppModule {}
|
export class AppModule {}
|
||||||
|
|
|
||||||
19
src/main.ts
19
src/main.ts
|
|
@ -3,6 +3,23 @@ import { AppModule } from './app.module';
|
||||||
|
|
||||||
async function bootstrap() {
|
async function bootstrap() {
|
||||||
const app = await NestFactory.create(AppModule);
|
const app = await NestFactory.create(AppModule);
|
||||||
await app.listen(process.env.PORT ?? 3000);
|
|
||||||
|
// 启用 CORS 支持多平台访问
|
||||||
|
app.enableCors({
|
||||||
|
origin: true,
|
||||||
|
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
|
||||||
|
allowedHeaders: ['Content-Type', 'Authorization', 'X-Requested-With'],
|
||||||
|
credentials: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
// 设置全局前缀
|
||||||
|
app.setGlobalPrefix('');
|
||||||
|
|
||||||
|
const port = process.env.PORT ?? 3000;
|
||||||
|
await app.listen(port);
|
||||||
|
|
||||||
|
console.log(`🚀 多平台小程序后台服务启动成功!`);
|
||||||
|
console.log(`📡 服务地址: http://localhost:${port}`);
|
||||||
|
console.log(`📋 模板管理 API: http://localhost:${port}/api/v1/templates`);
|
||||||
}
|
}
|
||||||
bootstrap();
|
bootstrap();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue