JuheSendSmsUtils.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using Aliyun.OSS.Model;
  2. using CSRedis;
  3. using JiaZhiQuan.Common.AliMessage;
  4. using Newtonsoft.Json;
  5. using System;
  6. using System.Net.Http;
  7. using System.Threading.Tasks;
  8. using System.Web;
  9. using Wicture.DbRESTFul;
  10. using Wicture.DbRESTFul.Cache;
  11. namespace JiaZhiQuan.Common.JuheAPI.SendSms
  12. {
  13. /// <summary>
  14. /// 发送短信服务
  15. /// 1分钟两次,每天最多10次
  16. /// </summary>
  17. public static class JuheSendSmsUtils
  18. {
  19. const string JUHE_SMS_KEY = "d753412f9bf62957f872b4e0a12d3122";
  20. const int JUHE_SMS_TPL_ID = 266760;
  21. /// <summary>
  22. /// 发送验证码短信
  23. /// </summary>
  24. /// <param name="mobile">手机号</param>
  25. /// <param name="client"></param>
  26. /// <returns></returns>
  27. public static async Task SendCodeSms(string mobile, string code, RedisCacheProvider redis, HttpClient client)
  28. {
  29. string dayLimitKey = CacheKeys.MobileDayLimit(mobile),
  30. continuousLimitKey = CacheKeys.ContinuousLimit(mobile);
  31. int dayLimitCount = await redis.Get<int>(dayLimitKey);
  32. dynamic continuousLimit = await redis.Get<dynamic>(continuousLimitKey);
  33. if (dayLimitCount >= 10)
  34. {
  35. throw new LogicalException("您当日发送短信太频繁,请明天再试", ErrorCodes.SMSendDayLimitError);
  36. }
  37. if (continuousLimit != null && continuousLimit.count >= 2)
  38. {
  39. throw new LogicalException("您发送短信太频繁,请稍后再试", ErrorCodes.SMSendBusinessLimitError);
  40. }
  41. // 发送短信
  42. string sendParams = HttpUtility.UrlEncode($"#code#={code}");
  43. //使用阿里云短信服务
  44. var res = AliMessageHelper.SendMessage(mobile, AliMessageTemplateEnum.SMS_480085126, JsonConvert.SerializeObject(new { code }));
  45. // await client.GetStringAsync($"http://v.juhe.cn/sms/send?mobile={mobile}&tpl_id={JUHE_SMS_TPL_ID}&tpl_value={sendParams}&key={JUHE_SMS_KEY}");
  46. if (!res.Equals("OK"))
  47. {
  48. LoggerManager.Logger.Error($"调用阿里云发送验证码短信API失败:" + JsonConvert.SerializeObject(res));
  49. throw new LogicalException("发送短信失败,请稍后再试[" + res + "]", ErrorCodes.Exception);
  50. }
  51. // 设置今天的发送次数
  52. int newDayLimitCount = dayLimitCount + 1;
  53. await redis.Set(dayLimitKey, newDayLimitCount, DateTime.Now.Date.AddDays(1));
  54. // 设置连续发送的次数
  55. DateTime expiration = continuousLimit is null ? DateTime.Now.AddMinutes(1) : continuousLimit.endTime;
  56. int newcontinuousLimitCount = continuousLimit is null ? 1 : continuousLimit.count + 1;
  57. dynamic newContinuousLimit = new
  58. {
  59. count = newcontinuousLimitCount,
  60. endTime = expiration
  61. };
  62. await redis.Set(continuousLimitKey, newContinuousLimit, expiration);
  63. // 查看发送结果
  64. //var obj = JsonConvert.DeserializeObject<JuheResponseModel1<JuheMobileAddressResultDetails>>(res);
  65. //if (obj.error_code != 0)
  66. //{
  67. // LoggerManager.Logger.Error($"调用聚合发送验证码短信API失败:" + JsonConvert.SerializeObject(obj));
  68. // throw new LogicalException("发送短信失败,请稍后再试[" + res + "]", ErrorCodes.Exception);
  69. //}
  70. }
  71. }
  72. public class JuheMobileAddressResultDetails
  73. {
  74. public string sid { get; set; }
  75. public int count { get; set; }
  76. public int fee { get; set; }
  77. }
  78. }