private static async Task SendEmailLog(LogInfo log) { var canSend = await CheckSendRate(log); if (!canSend) { // 当前靠此做熔断机制 return; } var receiverRes = await GetLogReceivers(log.source_name, log.msg_key); if (!receiverRes.IsSuccess()) { return; } var notifyMsg = new NotifyReq { targets = receiverRes.data, body_paras = new Dictionary <string, string> { { "module_name", log.source_name }, { "level", log.level.ToString() }, { "msg_key", log.msg_key }, { "log_id", log.log_id }, { "msg_body", log.msg_body.ToString() } }, msg_title = $"系统日志({log.source_name} 模块)", t_code = DirConfigKeys.plugs_notify_email_log_tcode }; await InsContainer <INotifyServiceProxy> .Instance.Send(notifyMsg); }
public async Task <NotifyResp> NotifyMsg(NotifyTemplateConfig template, NotifyReq msg) { var config = await GetEmailConfig(); if (config == null) { return(new NotifyResp().WithResp(RespTypes.ObjectNull, "未发现邮件配置信息!")); } var body = msg.body_paras.Aggregate(template.content, (current, p) => current.Replace(string.Concat("{", p.Key, "}"), p.Value)); var emailMsg = new EmailMsgMo { body = body, is_html = template.is_html > 0, subject = string.IsNullOrEmpty(msg.msg_title) ? template.title : msg.msg_title, to_emails = msg.targets }; var eRes = await SendAsync(config, emailMsg); var notifyResp = new NotifyResp().WithResp(eRes); notifyResp.msg_biz_id = msg.msg_Id; return(notifyResp); }
private async Task <NotifyResp> NotifyEmailMsg(NotifyTemplateMo template, NotifyReq msg, bool isFromLogModule) { var emailConfigRes = await GetEmailConfig(isFromLogModule); if (!emailConfigRes.IsSuccess()) { return(new NotifyResp().WithResp(emailConfigRes)); } var body = msg.body_paras.Aggregate(template.content, (current, p) => current.Replace(string.Concat("{", p.Key, "}"), p.Value)); var emailMsg = new EmailMsgMo { body = body, is_html = template.is_html, subject = string.IsNullOrEmpty(msg.msg_title) ? template.title : msg.msg_title, to_emails = msg.targets }; var eRes = await EmailHelper.SendAsync(emailConfigRes.data, emailMsg); var notifyResp = new NotifyResp().WithResp(eRes); notifyResp.msg_biz_id = msg.msg_Id; return(notifyResp); }
/// <summary> /// 发送通知消息 /// </summary> /// <param name="msg">消息实体</param> /// <param name="isFromLogModule">【重要】是否来自日志模块,直接决定当前方法内部异常写日志的操作</param> /// <returns></returns> public async Task <NotifyResp> Send(NotifyReq msg, bool isFromLogModule = false) { string errCode = null; try { var tRes = GetMsgTemplate(msg.t_code); if (!tRes.IsSuccess()) { return(new NotifyResp().WithResp(tRes)); } // todo 添加发送记录 return(await NotifyMsg(tRes.data, msg, isFromLogModule)); } catch (Exception ex) { if (!isFromLogModule) { errCode = LogHelper.Error($"发送信息出错,错误信息:{ex.Message}", this.GetType().Name); } } return(new NotifyResp().WithResp(SysRespTypes.ApplicationError, $"发送消息失败,错误码:{errCode}!")); }
/// <summary> /// 发送通知消息 /// </summary> /// <param name="msg">消息实体</param> /// <returns></returns> public async Task <NotifyResp> Send(NotifyReq msg) { string errCode; try { var tRes = await GetTemplateConfig(msg.t_code); if (!tRes.IsSuccess()) { return(new NotifyResp().WithResp(tRes)); } var template = tRes.data; // todo 添加发送记录 var handler = NotifyAdapterHub.GetNotifyHandler(template.notify_type, template.notify_channel); if (handler == null) { return(new NotifyResp().WithResp(RespTypes.OperateFailed, "未知的发送平台!")); } return(await handler.NotifyMsg(template, msg)); } catch (Exception ex) { errCode = LogHelper.Error($"发送信息出错,错误信息:{ex.Message}", this.GetType().Name); } return(new NotifyResp().WithResp(SysRespTypes.ApplicationError, $"发送消息失败,错误码:{errCode}!")); }
/// <summary> /// 发送动态码 /// </summary> /// <param name="loginName"></param> /// <param name="type"></param> /// <returns></returns> public async Task <Resp> SendCode(string loginName, RegLoginType type) { var code = NumHelper.RandomNum(); var notifyMsg = new NotifyReq { targets = new List <string>() { loginName }, body_paras = new Dictionary <string, string> { { "code", code } }, t_code = type == RegLoginType.Mobile ? DirConfigKeys.plugs_notify_sms_portal_tcode : DirConfigKeys.plugs_notify_email_portal_tcode }; var res = await InsContainer <INotifyServiceProxy> .Instance.Send(notifyMsg); if (!res.IsSuccess()) { return(res ?? new Resp().WithResp(RespTypes.UnKnowSource, "未知类型!")); } var key = string.Concat(CacheKeys.Portal_Passcode_ByLoginName, loginName); await CacheHelper.SetAbsoluteAsync(key, code, TimeSpan.FromMinutes(2)); return(res); }
private async Task <NotifyResp> NotifySmsMsg(NotifyTemplateMo template, NotifyReq msg, bool isFromLogModule) { //todo 添加不同通道处理 switch (template.notify_channel) { case NotifyChannel.AliYun: return(await NotifySmsMsg_AliYun(template, msg, isFromLogModule)); } return(new NotifyResp().WithResp(RespTypes.UnKnowOperate, "未知短信发送通道")); }
private static SendAliSmsReq GetSendAliSmsReq(NotifyTemplateMo template, NotifyReq msg) { var aliMsg = new SendAliSmsReq(); aliMsg.PhoneNums = msg.targets; aliMsg.body_paras = msg.body_paras; aliMsg.sign_name = template.sign_name; aliMsg.template_code = template.t_plat_code; return(aliMsg); }
private Task <NotifyResp> NotifyMsg(NotifyTemplateMo template, NotifyReq msg, bool isFromLogModule) { switch (template.notify_type) { case NotifyType.Email: return(NotifyEmailMsg(template, msg, isFromLogModule)); case NotifyType.SMS: return(NotifySmsMsg(template, msg, isFromLogModule)); } return(Task.FromResult(new NotifyResp().WithResp(RespTypes.UnKnowOperate, "未知通知消息类型!"))); }
public async Task WriteLog(LogInfo log) { try { if (log.source_name != ModuleNames.Log) { await _defaultFileLog.WriteLogAsync(log); var receiverRes = await GetLogReceivers(log.source_name, log.msg_key); if (!receiverRes.IsSuccess()) { return; } var notifyMsg = new NotifyReq { targets = receiverRes.data, body_paras = new Dictionary <string, string> { { "module_name", log.source_name }, { "level", log.level.ToString() }, { "msg_key", log.msg_key }, { "log_id", log.log_id }, { "msg_body", log.msg_body.ToString() } }, msg_title = $"系统日志({log.source_name} 模块)", t_code = "Email_Log_NotifyDetail" }; await InsContainer <INotifyServiceProxy> .Instance.Send(notifyMsg, true); } } catch { // 防止日志模块本身出错,再写日志,死循环 } }
private async Task <NotifyResp> NotifySmsMsg_AliYun(NotifyTemplateMo template, NotifyReq msg, bool isFromLogModule) { var aliSmsConfigRes = await GetAliSmsConfig(isFromLogModule); if (!aliSmsConfigRes.IsSuccess()) { return(new NotifyResp().WithResp(aliSmsConfigRes)); } var smsClient = SingleInstance <AliSmsClient> .Instance; smsClient.SetContextConfig(aliSmsConfigRes.data); var sendMsg = GetSendAliSmsReq(template, msg); return((await smsClient.Send(sendMsg)).ToNotifyResp()); }