MnsHelper.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using Aliyun.Acs.Core;
  2. using Aliyun.Acs.Core.Exceptions;
  3. using Aliyun.Acs.Core.Profile;
  4. using Aliyun.Acs.Dysmsapi.Model.V20170525;
  5. using BizCom;
  6. using SiteCore.Msn;
  7. using SiteCore.Redis;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. namespace SiteCore
  14. {
  15. public class MnsHelper
  16. {
  17. private const string product = "Dysmsapi";//短信API产品名称
  18. private const string domain = "dysmsapi.aliyuncs.com";//短信API产品域名
  19. private const string _endpoint = "http://1149691358616943.mns.cn-hangzhou.aliyuncs.com"; // eg. http://1234567890123456.mns.cn-shenzhen.aliyuncs.com
  20. private const string _accessKeyId = "LTAIqh6qdB6vpHxa";
  21. private const string _secretAccessKey = "DkkMGAFSAB2yrtB4XtiFitru1e6Zin";
  22. //private const string _topicName = "sms.topic-cn-hangzhou";
  23. private static string regionIdForPop = "cn-hangzhou";
  24. public static bool MoniSendBindSms(string mobile, string smsCode, out string msg)
  25. {
  26. msg = "";
  27. MsnObj mo = redis.RedisHelper.StringGet<MsnObj>(mobile);
  28. if (mo != null)
  29. {
  30. if (DateTime.Now.Subtract(mo.lastTime).TotalMinutes <= 1)
  31. {
  32. msg = "已获取过了,5分钟内有效";
  33. return false;
  34. }
  35. if (mo.count >= 3)
  36. {
  37. if (DateTime.Now.Day > mo.lastTime.Day)
  38. {
  39. mo.count = 0;
  40. }
  41. else {
  42. msg = "每天最多获取3次";
  43. return false;
  44. }
  45. }
  46. }
  47. if (mo == null)
  48. {
  49. mo = new MsnObj();
  50. mo.mobile = mobile;
  51. mo.count = 1;
  52. }
  53. else
  54. {
  55. mo.count += 1;
  56. }
  57. mo.lastTime = DateTime.Now;
  58. mo.code = smsCode;
  59. redis.RedisHelper.StringSet(mobile, mo, TimeSpan.FromDays(2));
  60. return true;
  61. }
  62. public static bool SendBindSms(string mobile,string smsCode,out string msg)
  63. {
  64. msg = "";
  65. MsnObj mo = redis.RedisHelper.StringGet<MsnObj>(mobile);
  66. if (mo != null)
  67. {
  68. if (DateTime.Now.Subtract(mo.lastTime).TotalMinutes <= 1)
  69. {
  70. msg = "已获取过了,5分钟内有效";
  71. return false;
  72. }
  73. if (mo.count >= 3)
  74. {
  75. if (DateTime.Now.Day > mo.lastTime.Day)
  76. {
  77. mo.count = 0;
  78. }
  79. else {
  80. msg = "每天最多获取3次";
  81. return false;
  82. }
  83. }
  84. }
  85. bool isSend= SendSms(mobile, smsCode, "", "SMS_152284257");//SMS_66435077
  86. if (isSend)
  87. {
  88. if (mo == null)
  89. {
  90. mo = new MsnObj();
  91. mo.mobile = mobile;
  92. mo.count = 1;
  93. }
  94. else
  95. {
  96. mo.count += 1;
  97. }
  98. mo.lastTime = DateTime.Now;
  99. mo.code = smsCode;
  100. redis.RedisHelper.StringSet(mobile, mo, TimeSpan.FromDays(2));
  101. }
  102. else
  103. {
  104. msg = "站点维护中,暂时无法发送验证码";
  105. }
  106. return isSend;
  107. }
  108. public static bool SendForgotSms(string mobile, string code)
  109. {
  110. //验证码${code},您正在尝试修改${product}登录密码,请妥善保管账户信息。
  111. return SendSms(mobile, code,"", "SMS_69825108");
  112. }
  113. private static bool SendSms(string mobile, string code,string parms2,string template)
  114. {
  115. IClientProfile profile = DefaultProfile.GetProfile(regionIdForPop, _accessKeyId, _secretAccessKey);
  116. DefaultProfile.AddEndpoint(regionIdForPop, regionIdForPop, product, domain);
  117. IAcsClient acsClient = new DefaultAcsClient(profile);
  118. SendSmsRequest request = new SendSmsRequest();
  119. try
  120. {
  121. request.PhoneNumbers = mobile;
  122. request.SignName = "趣读吧";
  123. request.TemplateCode = template;// "SMS_66535075";
  124. request.TemplateParam = "{\"code\":\""+code+"\"}";
  125. request.OutId = "";
  126. //请求失败这里会抛ClientException异常
  127. SendSmsResponse sendSmsResponse = acsClient.GetAcsResponse(request);
  128. XLog.SaveLog(0, sendSmsResponse.Message);
  129. //System.Console.WriteLine(sendSmsResponse.Message);
  130. //XLog.SaveLog(0, resp.MessageId + ",发送短信给" + mobile + ",验证码:" + code);
  131. return true;
  132. }
  133. catch (Exception ex)
  134. {
  135. XLog.SaveLog(0, "发送失败!" + mobile + ",验证码:" + code, ex);
  136. return false;
  137. }
  138. }
  139. }
  140. }