|
@@ -0,0 +1,131 @@
|
|
|
+using System.Collections.Concurrent;
|
|
|
+using SmsTool.Models;
|
|
|
+using SmsTool.Tencent;
|
|
|
+using TencentCloud.Sms.V20210111.Models;
|
|
|
+
|
|
|
+TencentHelper.Init();
|
|
|
+var builder = WebApplication.CreateBuilder(args);
|
|
|
+var port = builder.Configuration.GetSection("Sms")["Port"];
|
|
|
+builder.WebHost.UseUrls($"http://*:{port}");
|
|
|
+var app = builder.Build();
|
|
|
+
|
|
|
+ConcurrentDictionary<string, VerifyCodeModel> _mobileVerifyCodes = new ConcurrentDictionary<string, VerifyCodeModel>();
|
|
|
+
|
|
|
+
|
|
|
+app.MapPost("/SendVerifyCode", (string mobile,string templateId) => {
|
|
|
+ //var verifyCodeLength = param.GetStringValue("codeLen").ToInt();
|
|
|
+ var validMinutes = 10;
|
|
|
+
|
|
|
+ var verifyCode = _getVerifyCodeWithTime(mobile, validMinutes);
|
|
|
+ var model = new SendSmsModel
|
|
|
+ {
|
|
|
+ Mobiles = new List<string> { mobile },
|
|
|
+ TemplateId = templateId,
|
|
|
+ Params = new List<string> { verifyCode }
|
|
|
+ };
|
|
|
+ //重置密码无需传入有效时间参数
|
|
|
+ if (templateId == "1009241")
|
|
|
+ {
|
|
|
+ model.Params.Add(validMinutes.ToString());
|
|
|
+ }
|
|
|
+ var result = TencentHelper.SendSmsSync(model);
|
|
|
+ return new
|
|
|
+ {
|
|
|
+ msg = "",
|
|
|
+ obj = verifyCode,
|
|
|
+ code = _getSendSmsCode(result)
|
|
|
+ };
|
|
|
+});
|
|
|
+
|
|
|
+
|
|
|
+app.MapPost("/CheckVerifyCode",(string mobile, string verifyCode)=>
|
|
|
+{
|
|
|
+ var mobileVerifyCode = _getVerifyCode(mobile);
|
|
|
+ Console.WriteLine($"[{DateTime.Now:MM-dd HH:mm:ss}]{mobile},{verifyCode},curr:{mobileVerifyCode.VerifyCode},{mobileVerifyCode.OverdueTime:MM-dd HH:mm:ss}");
|
|
|
+ if (mobileVerifyCode == null || string.IsNullOrWhiteSpace(mobileVerifyCode.VerifyCode))
|
|
|
+ {
|
|
|
+ return new
|
|
|
+ {
|
|
|
+ code = 404
|
|
|
+ };
|
|
|
+ }
|
|
|
+ if (mobileVerifyCode.IsOverdue)
|
|
|
+ {
|
|
|
+ return new
|
|
|
+ {
|
|
|
+ code = 413
|
|
|
+ };
|
|
|
+ }
|
|
|
+ if (mobileVerifyCode.VerifyCode != verifyCode)
|
|
|
+ {
|
|
|
+ return new
|
|
|
+ {
|
|
|
+ code = 414
|
|
|
+ };
|
|
|
+ }
|
|
|
+ return new
|
|
|
+ {
|
|
|
+ code = 200
|
|
|
+ };
|
|
|
+});
|
|
|
+
|
|
|
+app.MapPost("/SendMessage", (string mobiles, string templateId, string paras)=>
|
|
|
+{
|
|
|
+ var mobileList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(mobiles);
|
|
|
+ var messageDatas = Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(paras);
|
|
|
+
|
|
|
+ var result = TencentHelper.SendSmsSync(new SendSmsModel
|
|
|
+ {
|
|
|
+ Mobiles = mobileList,
|
|
|
+ TemplateId = templateId,
|
|
|
+ Params = messageDatas
|
|
|
+ });
|
|
|
+ return new
|
|
|
+ {
|
|
|
+ msg = "",
|
|
|
+ code = _getSendSmsCode(result)
|
|
|
+ };
|
|
|
+});
|
|
|
+
|
|
|
+string _getVerifyCodeWithTime(string mobile, int validMinutes)
|
|
|
+{
|
|
|
+ if (_mobileVerifyCodes.ContainsKey(mobile))
|
|
|
+ {
|
|
|
+ _mobileVerifyCodes.TryRemove(mobile, out _);
|
|
|
+ }
|
|
|
+ var mobileVerifyCode = _mobileVerifyCodes.GetOrAdd(mobile, new VerifyCodeModel
|
|
|
+ {
|
|
|
+ VerifyCode = new Random().Next(1000, 9999).ToString(),
|
|
|
+ ValidMinutes = validMinutes,
|
|
|
+ CreateTime = DateTime.Now,
|
|
|
+ });
|
|
|
+ return mobileVerifyCode.VerifyCode;
|
|
|
+}
|
|
|
+
|
|
|
+VerifyCodeModel _getVerifyCode(string mobile)
|
|
|
+{
|
|
|
+ if (_mobileVerifyCodes.ContainsKey(mobile))
|
|
|
+ {
|
|
|
+ return _mobileVerifyCodes[mobile];
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+}
|
|
|
+
|
|
|
+int _getSendSmsCode(SendSmsResponse result)
|
|
|
+{
|
|
|
+ if (result == null || result.SendStatusSet == null || !result.SendStatusSet.Any())
|
|
|
+ {
|
|
|
+ return 9999;
|
|
|
+ }
|
|
|
+ if (result.SendStatusSet.Any(x => x.Code == "Ok"))
|
|
|
+ {
|
|
|
+ return 200;
|
|
|
+ }
|
|
|
+ if (result.SendStatusSet.Any(x => x.Code == "LimitExceeded.PhoneNumberDailyLimit"))
|
|
|
+ {
|
|
|
+ return 416;
|
|
|
+ }
|
|
|
+ return 9999;
|
|
|
+}
|
|
|
+
|
|
|
+app.Run();
|