using CRM.Core.CoreService.PkgSms.InterfaceModel; using Newtonsoft.Json; using qcloudsms_csharp; using System; using WX.CRM.Common; namespace CRM.Core.CoreService.PkgSms.InterfaceExec { public class TencentSms : IMsgSend { private readonly int _appId; private readonly string _appKey; public TencentSms(string appId = "", string appKey = "") { _appId = int.Parse(appId); _appKey = appKey; } public ReturnResult Execute(string[] mobiles, string msg, string typeCode) { var mobilesStr = string.Join(",", mobiles); var result = new ReturnResult { SendStr = mobilesStr, ReturnStr = mobilesStr }; try { // 短信应用SDK AppID //int appid = 1400157076; // 短信应用SDK AppKey //string appkey = "7defb5fad78e9d28179d29d6a36896be"; LogHelper.Info(mobiles[0]); if (mobiles[0].Contains(",")) { var msender = new SmsMultiSender(_appId, _appKey); var arr = mobiles[0].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries); var msgData = JsonConvert.DeserializeAnonymousType(msg, new { TemplateId = 0, SmsSign = string.Empty }); //LogHelper.Info("mobile:" + mobiles[0] + "--TemplateId:" + msgData.TemplateId.ToString()); var mrsp = msender.sendWithParam("86", arr, msgData.TemplateId, new string[] { }, msgData.SmsSign, "", ""); if (mrsp.result == 0) { result.submitStatus = SubmitStatus.success; } else { result.submitStatus = SubmitStatus.fail; } } else { var ssender = new SmsSingleSender(_appId, _appKey); SmsSingleSenderResult res = null; if (typeCode == "ResetPwd") { var message = Utility.JSONToObject(msg); res = ssender.sendWithParam("86", mobiles[0], message.TemplateId, new[] { message.SoftUserName, message.PassWord }, message.SmsSign, "", ""); } else if (typeCode == "OpenOrder") { var message = Utility.JSONToObject(msg); if (string.IsNullOrEmpty(message.DonateDays)) { //res = ssender.sendWithParam("86", mobiles[0], message.TemplateId, new[] { message.SoftUserName, message.ProductName, GetQiXian(int.Parse(message.OpenDays)) }, message.SmsSign, "", ""); res = ssender.sendWithParam("86", mobiles[0], message.TemplateId, new[] { message.ProductName, GetQiXian(int.Parse(message.OpenDays)) }, message.SmsSign, "", ""); } else { //res = ssender.sendWithParam("86", mobiles[0], message.TemplateId, new[] { message.SoftUserName, message.ProductName, GetQiXian(int.Parse(message.OpenDays)), GetQiXian(int.Parse(message.DonateDays)) }, message.SmsSign, "", ""); res = ssender.sendWithParam("86", mobiles[0], message.TemplateId, new[] { message.ProductName, GetQiXian(int.Parse(message.OpenDays)), GetQiXian(int.Parse(message.DonateDays)) }, message.SmsSign, "", ""); } } else if (typeCode == "HgMsg") { var message = Utility.JSONToObject(msg); res = ssender.sendWithParam("86", mobiles[0], message.TemplateId, new string[] { }, message.SmsSign, "", ""); } else if (typeCode == "OnlinePay") { var message = Utility.JSONToObject(msg); res = ssender.sendWithParam("86", mobiles[0], message.TemplateId, new string[] { message.Price.ToString(), message.PayName }, message.SmsSign, "", ""); } else if (typeCode == "SoftRegisterMsg") { var message = Utility.JSONToObject(msg); res = ssender.sendWithParam("86", mobiles[0], message.TemplateId, new string[] { }, message.SmsSign, "", ""); } else if (typeCode == "WebOrderHg") { //611622 var msgData = JsonConvert.DeserializeAnonymousType(msg, new { TemplateId = 0, SmsSign = string.Empty }); res = ssender.sendWithParam("86", mobiles[0], msgData.TemplateId, new string[] { }, msgData.SmsSign, "", ""); } else if (typeCode == "SH_TIP01") { var msgData = JsonConvert.DeserializeAnonymousType(msg, new { TemplateId = 0, SmsSign = string.Empty, Para = string.Empty }); if (!string.IsNullOrEmpty(msgData.Para)) { var paraArr = msgData.Para.Split(','); var userName = paraArr[0]; var pwd = paraArr[1]; var product = paraArr[2]; //LogHelper.Info("mobile:" + mobiles[0] + "--TemplateId:" + msgData.TemplateId.ToString()); res = ssender.sendWithParam("86", mobiles[0], msgData.TemplateId, new string[] { userName, pwd, product }, msgData.SmsSign, "", ""); } else { res = ssender.sendWithParam("86", mobiles[0], msgData.TemplateId, new string[] { }, msgData.SmsSign, "", ""); } } else { //LogHelper.Error("短信没有找到合适的发送类型,请确认!"); //默认发送类型 var msgData = JsonConvert.DeserializeAnonymousType(msg, new { TemplateId = 0, SmsSign = string.Empty }); //LogHelper.Info("mobile:" + mobiles[0] + "--TemplateId:" + msgData.TemplateId.ToString()); res = ssender.sendWithParam("86", mobiles[0], msgData.TemplateId, new string[] { }, msgData.SmsSign, "", ""); } LogHelper.Info(res.ToJson()); if (res.result == 0) result.submitStatus = SubmitStatus.success; else result.submitStatus = SubmitStatus.fail; } } catch (Exception ex) { result.submitStatus = SubmitStatus.fail; LogHelper.Error("TencentSms.Send:" + ex.Message + ex.StackTrace); } return result; } public decimal queryAmt() { throw new NotImplementedException(); } private string GetQiXian(decimal day) { //var cc = Math.Round((day / 373), MidpointRounding.AwayFromZero); //if (cc >= 1) //{ // if (cc >= 50) // return "永久"; // else // return cc.ToString() + "年"; //} //cc = Math.Round((day / 187), MidpointRounding.AwayFromZero); //if (cc >= 1) // return "半年"; //cc = Math.Round((day / 31), MidpointRounding.AwayFromZero); //if (cc >= 1) // return cc.ToString() + "个月"; return day.ToString() + "天"; } private class ResetPwdMessageTemplate { public int TemplateId { get; set; } public string SmsSign { get; set; } public string SoftUserName { get; set; } public string PassWord { get; set; } } private class OpenOrderMessageTemplate { public int TemplateId { get; set; } public string SmsSign { get; set; } public string SoftUserName { get; set; } public string ProductName { get; set; } public string OpenDays { get; set; } public string DonateDays { get; set; } } private class HgMessageTemplate { public int TemplateId { get; set; } public string SmsSign { get; set; } public string Status { get; set; } public string ResId { get; set; } } private class PayMessageTemplate { public int TemplateId { get; set; } public string SmsSign { get; set; } public decimal Price { get; set; } public string PayName { get; set; } } public class SoftReisterTemplate { /// /// 模板ID /// public int TemplateId { get; set; } /// /// 短信签名 /// public string SmsSign { get; set; } /// /// 用户名 /// public string SoftUserName { get; set; } } } }