Files
certd/packages/ui/certd-server/src/plugins/plugin-template/email/plugin-base.ts
T

93 lines
2.1 KiB
TypeScript
Raw Normal View History

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