123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- 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();
|