Program.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System.Collections.Concurrent;
  2. using SmsTool.Models;
  3. using SmsTool.Tencent;
  4. using TencentCloud.Sms.V20210111.Models;
  5. TencentHelper.Init();
  6. var builder = WebApplication.CreateBuilder(args);
  7. var port = builder.Configuration.GetSection("Sms")["Port"];
  8. builder.WebHost.UseUrls($"http://*:{port}");
  9. var app = builder.Build();
  10. ConcurrentDictionary<string, VerifyCodeModel> _mobileVerifyCodes = new ConcurrentDictionary<string, VerifyCodeModel>();
  11. app.MapPost("/SendVerifyCode", (string mobile,string templateId) => {
  12. //var verifyCodeLength = param.GetStringValue("codeLen").ToInt();
  13. var validMinutes = 10;
  14. var verifyCode = _getVerifyCodeWithTime(mobile, validMinutes);
  15. var model = new SendSmsModel
  16. {
  17. Mobiles = new List<string> { mobile },
  18. TemplateId = templateId,
  19. Params = new List<string> { verifyCode }
  20. };
  21. //重置密码无需传入有效时间参数
  22. if (templateId == "1009241")
  23. {
  24. model.Params.Add(validMinutes.ToString());
  25. }
  26. var result = TencentHelper.SendSmsSync(model);
  27. return new
  28. {
  29. msg = "",
  30. obj = verifyCode,
  31. code = _getSendSmsCode(result)
  32. };
  33. });
  34. app.MapPost("/CheckVerifyCode",(string mobile, string verifyCode)=>
  35. {
  36. var mobileVerifyCode = _getVerifyCode(mobile);
  37. Console.WriteLine($"[{DateTime.Now:MM-dd HH:mm:ss}]{mobile},{verifyCode},curr:{mobileVerifyCode.VerifyCode},{mobileVerifyCode.OverdueTime:MM-dd HH:mm:ss}");
  38. if (mobileVerifyCode == null || string.IsNullOrWhiteSpace(mobileVerifyCode.VerifyCode))
  39. {
  40. return new
  41. {
  42. code = 404
  43. };
  44. }
  45. if (mobileVerifyCode.IsOverdue)
  46. {
  47. return new
  48. {
  49. code = 413
  50. };
  51. }
  52. if (mobileVerifyCode.VerifyCode != verifyCode)
  53. {
  54. return new
  55. {
  56. code = 414
  57. };
  58. }
  59. return new
  60. {
  61. code = 200
  62. };
  63. });
  64. app.MapPost("/SendMessage", (string mobiles, string templateId, string paras)=>
  65. {
  66. var mobileList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(mobiles);
  67. var messageDatas = Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(paras);
  68. var result = TencentHelper.SendSmsSync(new SendSmsModel
  69. {
  70. Mobiles = mobileList,
  71. TemplateId = templateId,
  72. Params = messageDatas
  73. });
  74. return new
  75. {
  76. msg = "",
  77. code = _getSendSmsCode(result)
  78. };
  79. });
  80. string _getVerifyCodeWithTime(string mobile, int validMinutes)
  81. {
  82. if (_mobileVerifyCodes.ContainsKey(mobile))
  83. {
  84. _mobileVerifyCodes.TryRemove(mobile, out _);
  85. }
  86. var mobileVerifyCode = _mobileVerifyCodes.GetOrAdd(mobile, new VerifyCodeModel
  87. {
  88. VerifyCode = new Random().Next(1000, 9999).ToString(),
  89. ValidMinutes = validMinutes,
  90. CreateTime = DateTime.Now,
  91. });
  92. return mobileVerifyCode.VerifyCode;
  93. }
  94. VerifyCodeModel _getVerifyCode(string mobile)
  95. {
  96. if (_mobileVerifyCodes.ContainsKey(mobile))
  97. {
  98. return _mobileVerifyCodes[mobile];
  99. }
  100. return null;
  101. }
  102. int _getSendSmsCode(SendSmsResponse result)
  103. {
  104. if (result == null || result.SendStatusSet == null || !result.SendStatusSet.Any())
  105. {
  106. return 9999;
  107. }
  108. if (result.SendStatusSet.Any(x => x.Code == "Ok"))
  109. {
  110. return 200;
  111. }
  112. if (result.SendStatusSet.Any(x => x.Code == "LimitExceeded.PhoneNumberDailyLimit"))
  113. {
  114. return 416;
  115. }
  116. return 9999;
  117. }
  118. app.Run();