|
@@ -2,13 +2,17 @@ using Azure.Storage.Blobs.Models;
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
+using Microsoft.Extensions.Configuration;
|
|
|
using Microsoft.Extensions.Options;
|
|
|
+using StackExchange.Redis;
|
|
|
using PuppeteerSharp;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Drawing.Imaging;
|
|
|
+using System.IdentityModel.Tokens.Jwt;
|
|
|
using System.IO;
|
|
|
using System.Linq;
|
|
|
+using System.Net;
|
|
|
using System.Net.Http;
|
|
|
using System.Reflection;
|
|
|
using System.Runtime.InteropServices;
|
|
@@ -37,7 +41,9 @@ namespace TEAMModelOS.Controllers
|
|
|
private readonly Option _option;
|
|
|
private readonly HttpClient _httpClient;
|
|
|
private readonly IPSearcher _searcher;
|
|
|
- public CoreController(IPSearcher searcher, AzureRedisFactory azureRedis, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option, HttpClient httpClient)
|
|
|
+ private readonly CoreAPIHttpService _coreAPIHttpService;
|
|
|
+ private readonly IConfiguration _configuration;
|
|
|
+ public CoreController(CoreAPIHttpService coreAPIHttpService,IConfiguration configuration,IPSearcher searcher, AzureRedisFactory azureRedis, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option, HttpClient httpClient)
|
|
|
{
|
|
|
_searcher = searcher;
|
|
|
_azureStorage = azureStorage;
|
|
@@ -45,10 +51,129 @@ namespace TEAMModelOS.Controllers
|
|
|
_option = option?.Value;
|
|
|
_httpClient = httpClient;
|
|
|
_azureRedis = azureRedis;
|
|
|
+ _configuration = configuration;
|
|
|
+ _coreAPIHttpService = coreAPIHttpService;
|
|
|
}
|
|
|
+ [HttpPost("sendsms/pin")]
|
|
|
+ public async Task<IActionResult> SendSmsPinCode(JsonElement request)
|
|
|
+ {
|
|
|
+ (string ip, string region) = await LoginService.LoginIp(HttpContext, _searcher);
|
|
|
+
|
|
|
+ //获取投票活动的选项及投票数
|
|
|
+ string ipkey = $"Ip:Pin:Count:{ip}";
|
|
|
+
|
|
|
+ bool ipkeyexist = await _azureRedis.GetRedisClient(8).KeyExistsAsync(ipkey);
|
|
|
+ if (ipkeyexist)
|
|
|
+ {
|
|
|
+ await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync(ipkey, ip, 1);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync(ipkey, ip, 1);
|
|
|
+ var Expire = DateTime.UtcNow.AddSeconds(600);
|
|
|
+ _azureRedis.GetRedisClient(8).KeyExpire(ipkey, Expire);
|
|
|
+ }
|
|
|
+ var counts = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores(ipkey);
|
|
|
+ long sum = 0;
|
|
|
+ if (counts != null && counts.Length > 0)
|
|
|
+ {
|
|
|
+ foreach (var count in counts)
|
|
|
+ {
|
|
|
+ sum += (int)count.Score;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ int limit = 1000;
|
|
|
+ if (sum > limit) {
|
|
|
+ await _dingDing.SendBotMsg($"{_option.Location}\nIp:{ip}\n位置:{region}\n 短信验证码10分钟内访问次数超过:{limit}次!",GroupNames.成都开发測試群組);
|
|
|
+ return Ok(new { send = 2 });
|
|
|
+ }
|
|
|
+ if (!request.TryGetProperty("area", out JsonElement _area)) return BadRequest();
|
|
|
+ if (!request.TryGetProperty("to", out JsonElement _to)) return BadRequest();
|
|
|
+ if (!request.TryGetProperty("lang", out JsonElement _lang)) return BadRequest();
|
|
|
+ request.TryGetProperty("HasUser", out JsonElement _HasUser);
|
|
|
+ string code=$"{_area}{_to}";
|
|
|
+ int exp = 120;
|
|
|
+ string key = $"Random:Code:PinCode-{_area}{_to}";
|
|
|
+ bool exist = await _azureRedis.GetRedisClient(8).KeyExistsAsync(key);
|
|
|
+ if (!exist)
|
|
|
+ {
|
|
|
+ //不存在则发送请求。
|
|
|
+
|
|
|
+ Dictionary<string, object> dict = null;
|
|
|
+ if (_HasUser.ValueKind.Equals(JsonValueKind.True))
|
|
|
+ {
|
|
|
+ dict = new Dictionary<string, object> { { "country", $"{_area}" }, { "to", $"{_to}" }, { "lang", $"{_lang}" }, { "HasUser", true } };
|
|
|
+ }
|
|
|
+ else if (_HasUser.ValueKind.Equals(JsonValueKind.False)) {
|
|
|
+ dict = new Dictionary<string, object> { { "country", $"{_area}" }, { "to", $"{_to}" }, { "lang", $"{_lang}" }, { "HasUser", false } };
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ dict = new Dictionary<string, object> { { "country", $"{_area}" }, { "to", $"{_to}" }, { "lang", $"{_lang}" } };
|
|
|
+ }
|
|
|
+ var httpresp = await _coreAPIHttpService.SendSmsPin(dict, _option.Location, _configuration, _dingDing);
|
|
|
+ if (httpresp.code.Equals(HttpStatusCode.OK))
|
|
|
+ {
|
|
|
+ var Expire = DateTime.UtcNow.AddSeconds(exp);
|
|
|
+ //send=1 表示已发送
|
|
|
+ await _azureRedis.GetRedisClient(8).StringSetAsync(key, new { code = code, send = 1, Expire = Expire.Ticks }.ToJsonString());
|
|
|
+ _azureRedis.GetRedisClient(8).KeyExpire(key, Expire);
|
|
|
+ if (!string.IsNullOrWhiteSpace(httpresp.content))
|
|
|
+ {
|
|
|
+ return Ok(httpresp.content.ToObject<JsonElement>());
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ return Ok(new { send = 1 });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return Ok(new { send = 0 });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ //检查当前key是否已经发送了.
|
|
|
+ RedisValue value = await _azureRedis.GetRedisClient(8).StringGetAsync(key);
|
|
|
+ JsonElement element = value.ToString().ToObject<JsonElement>();
|
|
|
+ int send = 0;
|
|
|
+ if (element.TryGetProperty("send", out JsonElement _send))
|
|
|
+ {
|
|
|
+ if (_send.ValueKind.Equals(JsonValueKind.Number))
|
|
|
+ {
|
|
|
+ if (int.Parse($"{_send}") == 1)
|
|
|
+ {
|
|
|
+ send = 1;
|
|
|
|
|
|
- [HttpPost("apply-school")]
|
|
|
+ }
|
|
|
+ else if (int.Parse($"{_send}") == 0)
|
|
|
+ {
|
|
|
+ send = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (send == 0)
|
|
|
+ {
|
|
|
+ await _azureRedis.GetRedisClient(8).KeyDeleteAsync(key);
|
|
|
+ return Ok(new { send = 0 });
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ TimeSpan? timeSpan = await _azureRedis.GetRedisClient(8).KeyTimeToLiveAsync(key);
|
|
|
+ if (timeSpan != null && timeSpan.HasValue)
|
|
|
+ {
|
|
|
+ int seconds = timeSpan.Value.Seconds;
|
|
|
+ return Ok(new { send = 1 });
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return Ok(new { send = 0 });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
+ }
|
|
|
+
|
|
|
+ [HttpPost("apply-school")]
|
|
|
public async Task<IActionResult> ApplySchool(ApplySchool request)
|
|
|
{
|
|
|
if (_option.Location.Equals("China"))
|