using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MyCode.Project.Domain.Businesses.Sms { /// /// 短信模板基类 /// public abstract class SmsTemplateBase { /// /// 参数字典 /// protected Dictionary ParamDict = new Dictionary(); /// /// 短信模板 /// protected string Template { get; set; } /// /// 接收手机号码 /// public string Phone { get; set; } /// /// 初始化一个类型的实例 /// /// 短信模板 protected SmsTemplateBase(string template) { Template = template; } /// /// 设置 短信模板 /// /// 短信模板 public void SetTemplate(string template) { Template = template; } /// /// 添加参数 /// /// 键 /// 值 protected void Add(string key, object value) { if (string.IsNullOrEmpty(key)) { return; } if (value ==null || string.IsNullOrEmpty(value.ToString())) { return; } ParamDict.Add(key, value); } /// /// 初始化参数值 /// protected abstract void InitParamValue(); /// /// 格式化模板 /// /// public string FormatTemplate() { InitParamValue(); var tpl = Template; foreach (var param in ParamDict) { var value = param.Value.ToString(); tpl = tpl.Replace(param.Key, value); } return tpl; } } }