using Aliyun.Acs.Core; using Aliyun.Acs.Core.Exceptions; using Aliyun.Acs.Core.Profile; using Aliyun.Acs.Dysmsapi.Model.V20170525; using BizCom; using SiteCore.Msn; using SiteCore.Redis; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SiteCore { public class MnsHelper { private const string product = "Dysmsapi";//短信API产品名称 private const string domain = "dysmsapi.aliyuncs.com";//短信API产品域名 private const string _endpoint = "http://1149691358616943.mns.cn-hangzhou.aliyuncs.com"; // eg. http://1234567890123456.mns.cn-shenzhen.aliyuncs.com private const string _accessKeyId = "LTAIqh6qdB6vpHxa"; private const string _secretAccessKey = "DkkMGAFSAB2yrtB4XtiFitru1e6Zin"; //private const string _topicName = "sms.topic-cn-hangzhou"; private static string regionIdForPop = "cn-hangzhou"; public static bool MoniSendBindSms(string mobile, string smsCode, out string msg) { msg = ""; MsnObj mo = redis.RedisHelper.StringGet(mobile); if (mo != null) { if (DateTime.Now.Subtract(mo.lastTime).TotalMinutes <= 1) { msg = "已获取过了,5分钟内有效"; return false; } if (mo.count >= 3) { if (DateTime.Now.Day > mo.lastTime.Day) { mo.count = 0; } else { msg = "每天最多获取3次"; return false; } } } if (mo == null) { mo = new MsnObj(); mo.mobile = mobile; mo.count = 1; } else { mo.count += 1; } mo.lastTime = DateTime.Now; mo.code = smsCode; redis.RedisHelper.StringSet(mobile, mo, TimeSpan.FromDays(2)); return true; } public static bool SendBindSms(string mobile,string smsCode,out string msg) { msg = ""; MsnObj mo = redis.RedisHelper.StringGet(mobile); if (mo != null) { if (DateTime.Now.Subtract(mo.lastTime).TotalMinutes <= 1) { msg = "已获取过了,5分钟内有效"; return false; } if (mo.count >= 3) { if (DateTime.Now.Day > mo.lastTime.Day) { mo.count = 0; } else { msg = "每天最多获取3次"; return false; } } } bool isSend= SendSms(mobile, smsCode, "", "SMS_152284257");//SMS_66435077 if (isSend) { if (mo == null) { mo = new MsnObj(); mo.mobile = mobile; mo.count = 1; } else { mo.count += 1; } mo.lastTime = DateTime.Now; mo.code = smsCode; redis.RedisHelper.StringSet(mobile, mo, TimeSpan.FromDays(2)); } else { msg = "站点维护中,暂时无法发送验证码"; } return isSend; } public static bool SendForgotSms(string mobile, string code) { //验证码${code},您正在尝试修改${product}登录密码,请妥善保管账户信息。 return SendSms(mobile, code,"", "SMS_69825108"); } private static bool SendSms(string mobile, string code,string parms2,string template) { IClientProfile profile = DefaultProfile.GetProfile(regionIdForPop, _accessKeyId, _secretAccessKey); DefaultProfile.AddEndpoint(regionIdForPop, regionIdForPop, product, domain); IAcsClient acsClient = new DefaultAcsClient(profile); SendSmsRequest request = new SendSmsRequest(); try { request.PhoneNumbers = mobile; request.SignName = "趣读吧"; request.TemplateCode = template;// "SMS_66535075"; request.TemplateParam = "{\"code\":\""+code+"\"}"; request.OutId = ""; //请求失败这里会抛ClientException异常 SendSmsResponse sendSmsResponse = acsClient.GetAcsResponse(request); XLog.SaveLog(0, sendSmsResponse.Message); //System.Console.WriteLine(sendSmsResponse.Message); //XLog.SaveLog(0, resp.MessageId + ",发送短信给" + mobile + ",验证码:" + code); return true; } catch (Exception ex) { XLog.SaveLog(0, "发送失败!" + mobile + ",验证码:" + code, ex); return false; } } } }