其他分享
首页 > 其他分享> > 简单的消息发送方法

简单的消息发送方法

作者:互联网

using System.Collections.Generic;
using System;
using Newtonsoft.Json;
using MessgaeApi;

namespace MessageApi
{
    public class MessageHelper
    {
        static log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo");
        /// <summary>
        /// 发送方法传参数
        /// </summary>
        /// <param name="type"></param>
        /// <param name="templateId"></param>
        /// <param name="Account"></param>
        /// <param name="keyPairs"></param>
        /// <returns></returns>
        public static string SendMessage(int type, string templateId, string Account, Dictionary<string, string> keyPairs)
        {
            var plan = new MessagePlan()
            {
                SerialId = "M" + DateTime.Now.ToString("yyyyMMddHHmmssfff"),
                Type = type,
                TemplateId = templateId,
                Account = Account,
                KeyPairs = keyPairs
            };
            return SendMessage(plan);
        }
        /// <summary>
        /// 发送方法传实体
        /// </summary>
        /// <param name="plan"></param>
        /// <returns></returns>
        private static string SendMessage(MessagePlan plan)
        {
            var resMsg = "";
            var finalContent = "";
            try
            {
                if (plan == null) return "对象不能为null,请检查!";
                if (string.IsNullOrEmpty(plan.TemplateId)) return "模板id不能为空!";
                if (string.IsNullOrEmpty(plan.Account)) return "要发送的账户不能为空!";
                //替换信息
                finalContent = ReplaceContent(plan.KeyPairs, plan.TemplateId);
                switch (plan.Type)
                {
                    case 0:
                        resMsg = SendEms(plan.Account, finalContent);
                        break;
                    case 1:
                        resMsg = SendEmail(plan.Account, finalContent);
                        break;
                    case 2:
                        resMsg = SendQyWx(plan.Account, finalContent);
                        break;
                }
            }
            catch (Exception ex)
            {
                resMsg = ex.Message;
            }
            loginfo.Info($"请求对象:{ JsonConvert.SerializeObject(plan)},发送内容:{finalContent},发送结果:{resMsg}");
            return resMsg;
        }
        /// <summary>
        /// 替换内容
        /// </summary>
        /// <param name="keyPair"></param>
        /// <param name="temppateId"></param>
        /// <returns></returns>
        private static string ReplaceContent(Dictionary<string, string> keyPair, string templateId)
        {
            //根据id拿到待替换的内容
            string sourceContent = GetSourceCountById(templateId);
            if (string.IsNullOrEmpty(sourceContent)) return "";
            if (keyPair.Keys.Count == 0) return sourceContent;
            foreach (var item in keyPair)
            {
                sourceContent = sourceContent.Replace("{" + item.Key.Trim() + "}", item.Value.Trim());
            }
            return sourceContent;
        }
        /// <summary>
        /// 根据模板id拿到原始内容
        /// </summary>
        /// <param name="templateId"></param>
        /// <returns></returns>
        private static string GetSourceCountById(string templateId)
        {
            return "【短信】尊敬的:{Name},您的验证码是:{Code}";
        }

        private static string SendEms(string phone, string message)
        {
            return "success";
        }
        private static string SendEmail(string email, string message)
        {
            return "success";
        }
        private static string SendQyWx(string employeeNo, string message)
        {
           return  new QYWeChatHelper().SendMessage(employeeNo.Split(','), message);
        }
    }
    /// <summary>
    /// 消息实体
    /// </summary>
    public class MessagePlan
    {
        /// <summary>
        /// 序列号,建议 
        /// 1: DateTime.Now.ToString("yyyyMMddHHmmssfff");
        /// 2: Guid.NewGuid().ToString();  
        /// </summary>
        public string SerialId { get; set; }
        /// <summary>
        /// 消息类型 0 短信,1邮件,2企业微信
        /// </summary>
        public int Type { get; set; }
        /// <summary>
        /// 手机号或者邮件,或者企业微信员工账号,
        /// 多个用','隔开
        /// </summary>
        public string Account { get; set; }
        /// <summary>
        /// 模板id
        /// </summary>
        public string TemplateId { get; set; }
        /// <summary>
        /// key-value ,待替换的值
        /// </summary>
        public Dictionary<string, string> KeyPairs { get; set; }
        /// <summary>
        /// 附件地址
        /// </summary>
        public string AttachedUrl { get; set; }
    }
}

 

标签:Account,return,string,发送,static,plan,简单,public,消息
来源: https://www.cnblogs.com/request/p/16482188.html