using Sanhe.Abp.RealTime.Localization; using System; using Volo.Abp.Data; using Volo.Abp.EventBus; namespace Sanhe.Abp.Notifications; /// /// 通知数据 /// /// /// 把通知的标题和内容设计为 让客户端自行本地化 /// [Serializable] [EventName("notifications")] public class NotificationData : IHasExtraProperties { /// /// 用来标识是否需要本地化的信息 /// public const string LocalizerKey = "L"; /// /// 类型完全名称 /// public virtual string Type => GetType().FullName; /// /// 获取或设置扩展属性 /// /// /// public object this[string key] { get { return this.GetProperty(key); } set { this.SetProperty(key, value); } } /// /// 扩展属性 /// public ExtraPropertyDictionary ExtraProperties { get; set; } public NotificationData() { ExtraProperties = new ExtraPropertyDictionary(); this.SetDefaultsForExtraProperties(); TrySetData(LocalizerKey, false); } /// /// 写入本地化的消息数据 /// /// /// /// /// /// /// public NotificationData WriteLocalizedData( LocalizableStringInfo title, LocalizableStringInfo message, DateTime createTime, string formUser, LocalizableStringInfo description = null) { TrySetData("title", title); TrySetData("message", message); TrySetData("formUser", formUser); TrySetData("createTime", createTime); TrySetData(LocalizerKey, true); if (description != null) { TrySetData("description", description); } return this; } /// /// 写入标准数据 /// /// 标题 /// 内容 /// 创建时间 /// 来源用户 /// 附加说明 /// public NotificationData WriteStandardData(string title, string message, DateTime createTime, string formUser, string description = "") { TrySetData("title", title); TrySetData("message", message); TrySetData("description", description); TrySetData("formUser", formUser); TrySetData("createTime", createTime); TrySetData(LocalizerKey, false); return this; } /// /// 写入标准数据 /// /// 数据前缀 /// 标识 /// 数据内容 /// public NotificationData WriteStandardData(string prefix, string key, object value) { TrySetData(string.Concat(prefix, key), value); TrySetData(LocalizerKey, false); return this; } /// /// 转换为标准数据 /// /// 原始数据 /// public static NotificationData ToStandardData(NotificationData sourceData) { var data = new NotificationData(); data.TrySetData("title", sourceData.TryGetData("title")); data.TrySetData("message", sourceData.TryGetData("message")); data.TrySetData("description", sourceData.TryGetData("description")); data.TrySetData("formUser", sourceData.TryGetData("formUser")); data.TrySetData("createTime", sourceData.TryGetData("createTime")); data.TrySetData(LocalizerKey, sourceData.TryGetData(LocalizerKey)); return data; } /// /// 转换为标准数据 /// /// 数据前缀 /// 原始数据 /// public static NotificationData ToStandardData(string prefix, NotificationData sourceData) { var data = ToStandardData(sourceData); foreach (var property in sourceData.ExtraProperties) { if (property.Key.StartsWith(prefix)) { var key = property.Key.Replace(prefix, ""); data.TrySetData(key, property.Value); } } return data; } public object TryGetData(string key) { return this.GetProperty(key); } public void TrySetData(string key, object value) { this.SetProperty(key, value); } /// /// 需要本地化 /// /// public bool NeedLocalizer() { var localizer = TryGetData(LocalizerKey); if (localizer != null && localizer is bool needLocalizer) { return needLocalizer; } return false; } }