perf: 支持邮件模版设置

This commit is contained in:
xiaojunnuo
2025-12-14 01:36:20 +08:00
parent 437d956cad
commit a6c0d2c6f1
31 changed files with 703 additions and 214 deletions
@@ -1,52 +1,87 @@
import { AddonInput, BaseAddon } from "@certd/lib-server";
import { BuildContentReply, BuildContentReq, ITemplateProvider } from "../api.js";
import { get } from "lodash-es";
import { BuildContentReq, EmailContent, ITemplateProvider } from "../api.js";
export class BaseEmailTemplateProvider extends BaseAddon implements ITemplateProvider {
export class BaseEmailTemplateProvider extends BaseAddon implements ITemplateProvider<EmailContent> {
@AddonInput({
title: "配置说明",
component:{
name:"a-alert",
props:{
type:"info",
message:"在标题和内容模版中,通过${param}引用参数,例如: 感谢注册${siteTitle},您的注册验证码为:${code}",
component: {
name: "a-alert",
props: {
type: "info",
message: "在标题和内容模版中,通过${param}引用参数,例如: 感谢注册${siteTitle},您的注册验证码为:${code}",
}
},
order: 1,
col:{span:24},
col: { span: 24 },
})
useIntro = "";
@AddonInput({
title: "邮件格式",
component: {
name: "a-select",
props: {
options: [
{ label: "HTML", value: "html" },
{ label: "TEXT", value: "text" },
]
}
},
order: 1,
col: { span: 24 },
})
formatType = "";
@AddonInput({
title: "邮件标题模版",
required: true,
order: 10,
component: {
name: "a-input",
props: {
placeholder: "邮件标题模版",
}
},
col: { span: 24 },
})
titleTemplate = "";
@AddonInput({
title: "邮件内容模版",
component: {
placeholder: "邮件内容模版",
name: "a-textarea",
rows: 6,
},
order: 20,
col: { span: 24 },
required: true,
})
contentTemplate = "";
async buildContent(params: BuildContentReq) : Promise<BuildContentReply>{
const title = this.compile(this.titleTemplate)(params.data)
const content = this.compile(this.contentTemplate)(params.data)
return {
title,
content,
}
async buildContent(params: BuildContentReq): Promise<EmailContent> {
const title = this.compile(this.titleTemplate)(params.data)
const content = this.compile(this.contentTemplate)(params.data)
const body: any = {
subject: title,
}
if (this.formatType === "html") {
body.html = content
} else {
body.content = content
}
return body
};
async buildDefaultContent(params: BuildContentReq): Promise<EmailContent> {
throw new Error("请实现 buildDefaultContent 方法")
}
compile(templateString: string) {
return function(data:any):string {
return function (data: any): string {
return templateString.replace(/\${(.*?)}/g, (match, key) => {
const value = get(data, key, '');
return String(value);