mirror of
https://github.com/certd/certd.git
synced 2026-04-23 19:57:27 +08:00
chore: email template
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
|
||||
export type BuildContentReq = {
|
||||
data: any;
|
||||
}
|
||||
|
||||
export type BuildContentReply = Record<string, string>;
|
||||
|
||||
export interface ITemplateProvider {
|
||||
buildContent: (params: BuildContentReq) => Promise<BuildContentReply>;
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
import { AddonInput, BaseAddon } from "@certd/lib-server";
|
||||
import { BuildContentReply, BuildContentReq, ITemplateProvider } from "../api.js";
|
||||
import { get } from "lodash-es";
|
||||
|
||||
|
||||
export class BaseEmailTemplateProvider extends BaseAddon implements ITemplateProvider {
|
||||
@AddonInput({
|
||||
title: "配置说明",
|
||||
component:{
|
||||
name:"a-alert",
|
||||
props:{
|
||||
type:"info",
|
||||
message:"在标题和内容模版中,通过${param}引用参数,例如: 感谢注册${siteTitle},您的注册验证码为:${code}",
|
||||
}
|
||||
},
|
||||
order: 1,
|
||||
col:{span:24},
|
||||
})
|
||||
useIntro = "";
|
||||
|
||||
@AddonInput({
|
||||
title: "邮件标题模版",
|
||||
required: true,
|
||||
order: 10,
|
||||
})
|
||||
titleTemplate = "";
|
||||
|
||||
@AddonInput({
|
||||
title: "邮件内容模版",
|
||||
component: {
|
||||
placeholder: "邮件内容模版",
|
||||
},
|
||||
order: 20,
|
||||
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,
|
||||
}
|
||||
};
|
||||
|
||||
compile(templateString: string) {
|
||||
return function(data:any):string {
|
||||
return templateString.replace(/\${(.*?)}/g, (match, key) => {
|
||||
const value = get(data, key, '');
|
||||
return String(value);
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import { AddonInput, IsAddon } from "@certd/lib-server";
|
||||
import { BaseEmailTemplateProvider } from "./plugin-base.js";
|
||||
|
||||
@IsAddon({
|
||||
addonType: "emailTemplate",
|
||||
name: 'register',
|
||||
title: '注册邮件模版',
|
||||
desc: '注册邮件模版',
|
||||
icon:"simple-icons:gitee:red",
|
||||
showTest: false,
|
||||
})
|
||||
export class RegisterEmailTemplateProvider extends BaseEmailTemplateProvider {
|
||||
|
||||
@AddonInput({
|
||||
title: "可用参数",
|
||||
component:{
|
||||
name:"a-alert",
|
||||
props:{
|
||||
type:"info",
|
||||
message:"站点名称:${siteTitle};注册验证码:${code};有效期:${duration}分钟",
|
||||
}
|
||||
},
|
||||
order: 5,
|
||||
col:{span:24},
|
||||
})
|
||||
paramIntro = "";
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user