184 lines
8.1 KiB
C#
184 lines
8.1 KiB
C#
using _2021_backend.Data;
|
|
using _2021_backend.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Linq;
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
using TencentCloud.Common;
|
|
using TencentCloud.Common.Profile;
|
|
using TencentCloud.Sms.V20210111;
|
|
using TencentCloud.Sms.V20210111.Models;
|
|
using System.Security.Cryptography;
|
|
|
|
namespace _2021_backend.Utils
|
|
{
|
|
public class TencentSMS
|
|
{
|
|
public static string Tencent_id { get; set; }
|
|
public static string Tencent_key { get; set; }
|
|
public static string SMS_appid { get; set; }
|
|
public static string SMSID_accept { get; set; }
|
|
public static string SMSID_reject { get; set; }
|
|
public static string SMSID_timeSet { get; set; }
|
|
public static string SMSID_signed { get; set; }
|
|
public static string SMSID_captcha { get; set; }
|
|
|
|
private static bool Initialized = false;
|
|
|
|
private static string SMSID_timeSelect { get; set; }
|
|
|
|
public static void Init(string tencentid, string tencentKey, string appid, string ID_accept, string ID_reject, string ID_timeSet, string ID_submitted, string ID_timeSelect,string ID_captcha)
|
|
{
|
|
Tencent_id = tencentid;
|
|
Tencent_key = tencentKey;
|
|
SMS_appid = appid;
|
|
SMSID_accept = ID_accept;
|
|
SMSID_reject = ID_reject;
|
|
SMSID_timeSet = ID_timeSet;
|
|
SMSID_signed = ID_submitted;
|
|
SMSID_timeSelect = ID_timeSelect;
|
|
SMSID_captcha = ID_captcha;
|
|
Initialized = true;
|
|
}
|
|
public static async Task<bool> Pull(BackendContext Context, Student stu, bool fullPull)
|
|
{
|
|
if (!Initialized) return false;
|
|
try
|
|
{
|
|
Credential cred = new Credential
|
|
{
|
|
SecretId = Tencent_id,
|
|
SecretKey = Tencent_key
|
|
};
|
|
|
|
ClientProfile clientProfile = new ClientProfile();
|
|
HttpProfile httpProfile = new HttpProfile();
|
|
httpProfile.Endpoint = ("sms.tencentcloudapi.com");
|
|
clientProfile.HttpProfile = httpProfile;
|
|
|
|
SmsClient client = new SmsClient(cred, "ap-nanjing", clientProfile);
|
|
PullSmsReplyStatusByPhoneNumberRequest req = new PullSmsReplyStatusByPhoneNumberRequest();
|
|
//var it = Context.SMS.Find(stu.Messages.LastOrDefault()).SendTime;
|
|
req.BeginTime = (ulong)DateTimeOffset.UtcNow.AddDays(-5).ToUnixTimeSeconds();
|
|
req.EndTime = (ulong)DateTimeOffset.UtcNow.AddSeconds(-1).ToUnixTimeSeconds();
|
|
req.Offset = 0;
|
|
req.Limit = 100;
|
|
req.PhoneNumber = "+86" + stu.Tel;
|
|
req.SmsSdkAppId = SMS_appid;
|
|
foreach (var msg in stu.Messages)
|
|
{
|
|
var sms = Context.SMS.Find(msg);
|
|
if (sms.SendTime.CompareTo(DateTime.Now.AddDays(-5)) > 0)
|
|
{
|
|
stu.Messages.Remove(msg);
|
|
Context.SMS.Remove(sms);
|
|
}
|
|
}
|
|
Context.SaveChanges();
|
|
PullSmsReplyStatusByPhoneNumberResponse resp = client.PullSmsReplyStatusByPhoneNumberSync(req);
|
|
var str = AbstractModel.ToJsonString(resp);
|
|
Console.WriteLine(resp);
|
|
SMSResponseDto obj = JsonSerializer.Deserialize<SMSResponseDto>(str, new JsonSerializerOptions { PropertyNameCaseInsensitive = false });
|
|
var lst = obj.PullSmsReplyStatusSet;
|
|
foreach (var item in lst)
|
|
{
|
|
var msg = new SMS(item, stu.Id);
|
|
Context.Add<SMS>(msg);
|
|
stu.Messages.Add(msg.Id);
|
|
}
|
|
Context.SaveChanges();
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e.ToString());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static async Task<bool> Send(BackendContext Context, SMSType type, Student stu, string sender)
|
|
{
|
|
if (!Initialized) return false;
|
|
try
|
|
{
|
|
Credential cred = new Credential
|
|
{
|
|
SecretId = Tencent_id,
|
|
SecretKey = Tencent_key
|
|
};
|
|
ClientProfile cp = new ClientProfile();
|
|
HttpProfile hp = new HttpProfile();
|
|
hp.Endpoint = ("sms.tencentcloudapi.com");
|
|
cp.HttpProfile = hp;
|
|
SmsClient client = new SmsClient(cred, "ap-nanjing", cp);
|
|
SendSmsRequest req = new SendSmsRequest();
|
|
req.PhoneNumberSet = new string[] { "" };
|
|
req.PhoneNumberSet[0] = "+86" + stu.Tel;
|
|
req.SmsSdkAppId = SMS_appid;
|
|
req.SignName = "ZJUEVA";
|
|
int index = stu.InterviewTime;
|
|
SMS sms = new SMS();
|
|
switch (type)
|
|
{
|
|
case SMSType.Accept:
|
|
sms.Type = SMSType.Accept;
|
|
req.TemplateId = SMSID_accept;
|
|
req.TemplateParamSet = new string[] { stu.Name };
|
|
break;
|
|
case SMSType.Reject:
|
|
sms.Type = SMSType.Reject;
|
|
req.TemplateId = SMSID_reject;
|
|
req.TemplateParamSet = new string[] { stu.Name };
|
|
break;
|
|
case SMSType.TimeSelect:
|
|
sms.Type = SMSType.TimeSelect;
|
|
req.TemplateId = SMSID_timeSelect;
|
|
req.TemplateParamSet = new string[] { };
|
|
break;
|
|
case SMSType.TimeSet:
|
|
sms.Type = SMSType.TimeSet;
|
|
var q = from e in Context.Sessions where e.Id == index select e;
|
|
var time = await q.FirstOrDefaultAsync();
|
|
req.TemplateParamSet = new string[] { stu.Name, time.Day.ToString("dd") ,time.BeginTime.ToString("HH"),time.BeginTime.ToString("mm"), time.BeginTime.AddHours(1).ToString("HH") ,time.BeginTime.AddMinutes(30).ToString("mm")};
|
|
req.TemplateId = SMSID_timeSet;
|
|
break;
|
|
case SMSType.Signed:
|
|
sms.Type = SMSType.Signed;
|
|
req.TemplateParamSet = new string[] { stu.Name };
|
|
req.TemplateId = SMSID_signed;
|
|
break;
|
|
case SMSType.Captcha:
|
|
sms.Type = SMSType.Captcha;
|
|
var captchaBytes = new byte[]{0,0,0,0,0,0};
|
|
RNGCryptoServiceProvider csp = new RNGCryptoServiceProvider();
|
|
csp.GetBytes(captchaBytes);
|
|
for (int i = 0; i < 6; i++) captchaBytes[i] %= 10;
|
|
string captcha = $"{captchaBytes[0]}{captchaBytes[1]}{captchaBytes[2]}{captchaBytes[3]}{captchaBytes[4]}{captchaBytes[5]}";
|
|
stu.LastCaptcha = captcha;
|
|
stu.LastCaptchaTime = DateTime.Now;
|
|
req.TemplateParamSet = new string[] { captcha };
|
|
req.TemplateId = SMSID_captcha;
|
|
break;
|
|
}
|
|
sms.Tel = stu.Tel;
|
|
sms.Host = stu.Id;
|
|
var query = from e in Context.Users where e.stuID == sender select e;
|
|
var usr = await query.FirstOrDefaultAsync();
|
|
sms.Sender = usr == null ? "null" : usr.Name;
|
|
sms.Data = req.TemplateParamSet.ToList<string>();
|
|
SendSmsResponse resp = client.SendSmsSync(req);
|
|
Console.WriteLine(AbstractModel.ToJsonString(resp));
|
|
Context.Add(sms);
|
|
Context.SaveChanges();
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e.ToString());
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|