refactor(service): 使用 Eta 替换手动模板渲染逻辑
移除手动实现的模板字符串替换逻辑,改用 Eta 模板引擎进行渲染 清理 .gitignore 中不再需要的文件路径
This commit is contained in:
parent
4bb0988034
commit
d91ec7bc60
|
|
@ -15,10 +15,5 @@ yarn.lock
|
||||||
**/config.prod.ts
|
**/config.prod.ts
|
||||||
**/config.local.ts
|
**/config.local.ts
|
||||||
container
|
container
|
||||||
ai/products-20251202 (1).csv
|
scripts
|
||||||
scripts/replace_punctuation.js
|
ai
|
||||||
scripts/test_db_count.d.ts
|
|
||||||
scripts/test_db_count.js
|
|
||||||
scripts/test_db_count.ts
|
|
||||||
ai/test.html
|
|
||||||
ai/wc-product-export-2-12-2025-1764686773307.csv
|
|
||||||
|
|
|
||||||
|
|
@ -3,12 +3,15 @@ import { InjectEntityModel } from '@midwayjs/typeorm';
|
||||||
import { Repository } from 'typeorm';
|
import { Repository } from 'typeorm';
|
||||||
import { Template } from '../entity/template.entity';
|
import { Template } from '../entity/template.entity';
|
||||||
import { CreateTemplateDTO, UpdateTemplateDTO } from '../dto/template.dto';
|
import { CreateTemplateDTO, UpdateTemplateDTO } from '../dto/template.dto';
|
||||||
|
import { Eta } from 'eta';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @service TemplateService 模板服务
|
* @service TemplateService 模板服务
|
||||||
*/
|
*/
|
||||||
@Provide()
|
@Provide()
|
||||||
export class TemplateService {
|
export class TemplateService {
|
||||||
|
private eta = new Eta();
|
||||||
|
|
||||||
// 注入 Template 实体模型
|
// 注入 Template 实体模型
|
||||||
@InjectEntityModel(Template)
|
@InjectEntityModel(Template)
|
||||||
templateModel: Repository<Template>;
|
templateModel: Repository<Template>;
|
||||||
|
|
@ -111,17 +114,7 @@ export class TemplateService {
|
||||||
throw new Error(`模板 '${name}' 不存在`);
|
throw new Error(`模板 '${name}' 不存在`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取模板的原始内容
|
// 使用 Eta 渲染
|
||||||
let rendered = template.value;
|
return this.eta.renderString(template.value, data);
|
||||||
// 遍历数据对象,替换模板中的占位符
|
|
||||||
for (const key in data) {
|
|
||||||
// 创建一个正则表达式来匹配 {{key}}
|
|
||||||
const regex = new RegExp(`{{${key}}}`, 'g');
|
|
||||||
// 执行替换操作
|
|
||||||
rendered = rendered.replace(regex, data[key]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 返回渲染后的字符串
|
|
||||||
return rendered;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue