Browse Source

0407

master
wxd 2 years ago
parent
commit
ff81198496
  1. 55
      src/Shentun.Sms.Application/Jobs/SmsSendJob.cs
  2. 130
      src/Shentun.Sms.Application/Jobs/SmsTaskJob.cs
  3. 10
      src/Shentun.Sms.EntityFrameworkCore/EntityFrameworkCore/SmsDbContext.cs
  4. 52
      src/Shentun.Sms.HttpApi.Host/Logger/CustomLogger.cs
  5. 43
      src/Shentun.Sms.HttpApi.Host/Program.cs
  6. 30
      src/Shentun.Sms.HttpApi.Host/SmsHttpApiHostModule.cs
  7. 6
      src/Shentun.Sms.HttpApi.Host/appsettings.json
  8. 1
      src/Shentun.Sms.Service/Shentun.Sms.Service.csproj
  9. 14
      src/Shentun.Sms.Service/Sms/SmsFactory.cs
  10. 11
      src/Shentun.Sms.Service/Sms/TencentSms.cs

55
src/Shentun.Sms.Application/Jobs/SmsSendJob.cs

@ -12,6 +12,8 @@ using Volo.Abp.Domain.Repositories;
using Volo.Abp;
using Microsoft.Extensions.Configuration;
using Shentun.Sms.Service.Sms;
using Volo.Abp.Caching;
using Microsoft.Extensions.Logging;
namespace Shentun.Sms.Jobs
{
@ -21,24 +23,32 @@ namespace Shentun.Sms.Jobs
[RemoteService(false)]
public class SmsSendJob : ApplicationService, ISmsSendJob
{
//private readonly IServiceProvider _service;
private readonly IRepository<SmsSend, Guid> _smsSendRepository;
private readonly IRepository<SmsTask, Guid> _smsTaskRepository;
private readonly SmsSendManager _smsSendManager;
private readonly IConfiguration _tencentSmsConfig;
private readonly IConfiguration _configuration;
private readonly SmsFactory _smsFactory;
private readonly ILogger<SmsSendJob> _logger;
public SmsSendJob(
SmsSendManager smsSendManager,
IRepository<SmsSend, Guid> smsSendRepository,
IConfiguration tencentSmsConfig,
IRepository<SmsTask, Guid> smsTaskRepository
)
IConfiguration configuration,
IRepository<SmsTask, Guid> smsTaskRepository,
ILogger<SmsSendJob> logger,
SmsFactory smsFactory)
{
_smsSendManager = smsSendManager;
_smsSendRepository = smsSendRepository;
_tencentSmsConfig = tencentSmsConfig;
_configuration = configuration;
_smsTaskRepository = smsTaskRepository;
_logger = logger;
_smsFactory = smsFactory;
}
public async Task DoWork()
{
try
@ -49,7 +59,8 @@ namespace Shentun.Sms.Jobs
select new
{
a,
CountryCode = b.CountryCode
CountryCode = b.CountryCode,
Content = b.Content
};
@ -58,15 +69,18 @@ namespace Shentun.Sms.Jobs
foreach (var item in smsSendQuery.ToList())
{
string CountryCode = item.CountryCode;
SmsSendAsync(item.a, CountryCode);
string Content = item.Content;
SmsSendAsync(item.a, CountryCode, Content);
smsSendUpdateList.Add(item.a);
}
await _smsSendRepository.UpdateManyAsync(smsSendUpdateList);
_logger.LogInformation($"------扫描处理了【{smsSendUpdateList.Count}】条SmsSend记录------当前时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
}
catch (Exception ex)
{
_logger.LogInformation(ex.ToString());
}
}
@ -76,25 +90,18 @@ namespace Shentun.Sms.Jobs
/// </summary>
/// <param name="smsSend"></param>
/// <param name="CountryCode"></param>
public void SmsSendAsync(SmsSend smsSend, string CountryCode)
public void SmsSendAsync(SmsSend smsSend, string CountryCode, string Content)
{
SmsFactory smsFactory = new SmsFactory(_tencentSmsConfig);
SmsBase smsBase = smsFactory.CreateSms();
Random rd = new Random();
string[] phoneNumber = { $"+{CountryCode}{smsSend.MobileTelephone}" };
string[] templateParam = { rd.Next(1000, 9999).ToString(), "10" };
if (!string.IsNullOrEmpty(CountryCode) && !string.IsNullOrEmpty(smsSend.MobileTelephone))
{
SmsBase smsBase = _smsFactory.CreateSms();
//phoneNumber.Append($"+{CountryCode}{smsSend.MobileTelephone}");
//templateParam.Append(rd.Next(1000, 9999).ToString());
//templateParam.Append("10");
string[] phoneNumber = { $"+{CountryCode}{smsSend.MobileTelephone}" };
smsBase.Send(phoneNumber, templateParam);
string[] templateParam = Content.Trim('|').Split("|", StringSplitOptions.RemoveEmptyEntries);
smsBase.Send(phoneNumber, templateParam);
}
smsSend.IsActive = 'N';
smsSend.IsComplete = 'Y';
}

130
src/Shentun.Sms.Application/Jobs/SmsTaskJob.cs

@ -1,6 +1,8 @@
using Cronos;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Shentun.Sms.Enums;
using Shentun.Sms.Service.Sms;
using Shentun.Sms.SmsApps;
using Shentun.Sms.SmsSends;
using Shentun.Sms.SmsTasks;
@ -9,8 +11,10 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TencentCloud.Ic.V20190307.Models;
using Volo.Abp;
using Volo.Abp.Application.Services;
using Volo.Abp.Caching;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Identity;
@ -21,53 +25,72 @@ namespace Shentun.Sms.Jobs
/// </summary>
public class SmsTaskJob : ApplicationService, ISmsTaskJob
{
private readonly IRepository<SmsTask, Guid> _smsTaskRepository;
private readonly IRepository<SmsSend, Guid> _smsSendRepository;
private readonly SmsSendManager _smsSendManager;
private readonly IConfiguration _configuration;
private readonly ILogger<SmsTaskJob> _logger;
private readonly SmsFactory _smsFactory;
public SmsTaskJob(
IRepository<SmsTask, Guid> smsTaskRepository,
SmsSendManager smsSendManager,
IConfiguration configuration,
IRepository<SmsSend, Guid> smsSendRepository
)
IRepository<SmsSend, Guid> smsSendRepository,
SmsFactory smsFactory,
ILogger<SmsTaskJob> logger)
{
_smsTaskRepository = smsTaskRepository;
_smsSendManager = smsSendManager;
_configuration = configuration;
_smsSendRepository = smsSendRepository;
_smsFactory = smsFactory;
_logger = logger;
}
[RemoteService(false)]
//[RemoteService(false)]
public async Task DoWork()
{
var smsTaskList = (from a in await _smsTaskRepository.GetQueryableAsync()
join b in await _smsSendRepository.GetQueryableAsync() on a.Id equals b.SmsTaskId into bb
from ab in bb.DefaultIfEmpty()
where a.IsActive == 'Y' && ab == null
select a).ToList();
try
{
_logger.LogInformation("测试");
var smsTaskList = (from a in await _smsTaskRepository.GetQueryableAsync()
join b in await _smsSendRepository.GetQueryableAsync() on a.Id equals b.SmsTaskId into bb
from ab in bb.DefaultIfEmpty()
where a.IsActive == 'Y' && ab == null
select a).ToList();
List<SmsSend> smsSendList = new List<SmsSend>();
List<SmsSend> smsSendList = new List<SmsSend>();
foreach (var smsTask in smsTaskList)
{
CreateSmsSendAsync(smsTask, smsSendList);
}
foreach (var smsTask in smsTaskList)
{
CreateSmsSendAsync(smsTask, smsSendList);
}
if (smsSendList.Any())
{
await _smsSendRepository.InsertManyAsync(smsSendList);
}
if (smsSendList.Any())
{
await _smsSendRepository.InsertManyAsync(smsSendList);
}
if (smsTaskList.Any())
if (smsTaskList.Any())
{
await _smsTaskRepository.UpdateManyAsync(smsTaskList);
}
_logger.LogInformation($"------扫描处理了【{smsTaskList.Count}】条SmsTask记录,生成了【{smsSendList.Count}】条短信记录------当前时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
}
catch (Exception ex)
{
await _smsTaskRepository.UpdateManyAsync(smsTaskList);
_logger.LogInformation(ex.ToString());
}
}
/// <summary>
@ -80,17 +103,13 @@ namespace Shentun.Sms.Jobs
public void CreateSmsSendAsync(SmsTask smsTask, List<SmsSend> smsSendList)
{
string SmsInterfaceId = _configuration.GetValue<string>("Sms:SmsInterfaceId");
if (string.IsNullOrEmpty(SmsInterfaceId))
{
SmsInterfaceId = "001";
}
if (smsTask.TaskCycleType == TaskCycleTypeFlag.TimelySend)
{
var smsSendEntity = new SmsSend(GuidGenerator.Create())
{
Content = smsTask.Content,
IsActive = 'Y',
IsComplete = 'N',
IsActive = 'N',
IsComplete = 'Y',
SmsInterfaceId = SmsInterfaceId,
SmsTaskId = smsTask.Id,
PlanSendTime = DateTime.Now
@ -98,6 +117,17 @@ namespace Shentun.Sms.Jobs
_smsSendManager.CreateAsync(smsSendEntity);
//发送短信
// SmsFactory smsFactory = new SmsFactory(_configuration);
SmsBase smsBase = _smsFactory.CreateSms();
string[] phoneNumber = { $"+{smsTask.CountryCode}{smsTask.MobileTelephone}" };
string[] templateParam = smsTask.Content.Trim('|').Split("|", StringSplitOptions.RemoveEmptyEntries);
smsBase.Send(phoneNumber, templateParam);
smsSendList.Add(smsSendEntity);
}
@ -109,24 +139,40 @@ namespace Shentun.Sms.Jobs
#region 解析Cron表达式
var schedule = CronExpression.Parse(taskCorn, CronFormat.IncludeSeconds);
var occurrences = schedule.GetOccurrences(DateTime.UtcNow, smsTask.StopTime.Value.ToUniversalTime()); //获取截止时间前所有的计划时间
foreach (var occurrence in occurrences)
try
{
var smsSendEntity = new SmsSend(GuidGenerator.Create())
var schedule = CronExpression.Parse(taskCorn, CronFormat.IncludeSeconds);
var occurrences = schedule.GetOccurrences(DateTime.UtcNow, smsTask.StopTime.Value.ToUniversalTime()); //获取截止时间前所有的计划时间
if (occurrences.Count() < 10)
{
Content = smsTask.Content,
IsActive = 'Y',
IsComplete = 'N',
SmsInterfaceId = SmsInterfaceId,
SmsTaskId = smsTask.Id,
MobileTelephone = smsTask.MobileTelephone,
PlanSendTime = occurrence
};
_smsSendManager.CreateAsync(smsSendEntity);
smsSendList.Add(smsSendEntity);
foreach (var occurrence in occurrences)
{
var smsSendEntity = new SmsSend(GuidGenerator.Create())
{
Content = smsTask.Content,
IsActive = 'Y',
IsComplete = 'N',
SmsInterfaceId = SmsInterfaceId,
SmsTaskId = smsTask.Id,
MobileTelephone = smsTask.MobileTelephone,
PlanSendTime = occurrence
};
_smsSendManager.CreateAsync(smsSendEntity);
smsSendList.Add(smsSendEntity);
}
}
else
{
_logger.LogInformation($"------Id为【{smsTask.Id}】的SmsTask记录,Corn表达式记录超出10条,已跳过------当前时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
}
}
catch (Exception ex)
{
_logger.LogInformation($"------处理Corn表达式异常------,{ex.ToString()}");
}

10
src/Shentun.Sms.EntityFrameworkCore/EntityFrameworkCore/SmsDbContext.cs

@ -1,11 +1,13 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.Extensions.Logging;
using Shentun.Sms.DbMapping;
using Shentun.Sms.SmsApps;
using Shentun.Sms.SmsInterfaces;
using Shentun.Sms.SmsSends;
using Shentun.Sms.SmsTasks;
using Shentun.Sms.SmsTypes;
using System;
using Volo.Abp.AuditLogging;
using Volo.Abp.AuditLogging.EntityFrameworkCore;
using Volo.Abp.BackgroundJobs;
@ -165,4 +167,12 @@ public class SmsDbContext :
base.ApplyAbpConceptsForAddedEntity(entry);
SetModificationAuditProperties(entry);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
////日志输出SQL语句
//optionsBuilder.LogTo(Console.WriteLine, LogLevel.Information)
// .EnableSensitiveDataLogging();
base.OnConfiguring(optionsBuilder);
}
}

52
src/Shentun.Sms.HttpApi.Host/Logger/CustomLogger.cs

@ -0,0 +1,52 @@
using Microsoft.Extensions.Logging;
using System;
using System.IO;
namespace Shentun.Sms.Logger
{
public class CustomLogger : ILogger
{
private string _categoryName;
public CustomLogger(string categoryName)
{
_categoryName = categoryName;
}
public IDisposable BeginScope<TState>(TState state)
{
return null;
}
public bool IsEnabled(LogLevel logLevel)
{
return true;
}
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
string message = $"{DateTime.Now} - {logLevel} - {_categoryName} - {formatter(state, exception)}";
File.AppendAllText("job_log.txt", message);
}
//public static void LogInformation(this ILogger logger, string? message, params object?[] args)
//{
// logger.Log(LogLevel.Information, message, args);
//}
}
public class CustomLoggerProvider : ILoggerProvider
{
public ILogger CreateLogger(string categoryName)
{
return new CustomLogger(categoryName);
}
public void Dispose()
{
}
}
}

43
src/Shentun.Sms.HttpApi.Host/Program.cs

@ -2,6 +2,7 @@
using System.Threading.Tasks;
using Hangfire;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;
@ -14,23 +15,38 @@ public class Program
{
public async static Task<int> Main(string[] args)
{
// Log.Logger = new LoggerConfiguration()
//#if DEBUG
// .MinimumLevel.Debug()
//#else
// .MinimumLevel.Information()
//#endif
// .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
// .MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Warning)
// .Enrich.FromLogContext()
// .WriteTo.Async(c => c.File("Logs/logs.txt"))
// .WriteTo.Async(c => c.Console())
// .CreateLogger();
Log.Logger = new LoggerConfiguration()
#if DEBUG
.MinimumLevel.Debug()
.MinimumLevel.Debug()
#else
.MinimumLevel.Information()
#endif
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Warning)
.Enrich.FromLogContext()
.WriteTo.Async(c => c.File("Logs/logs.txt"))
.WriteTo.Async(c => c.File("Logs/logs.txt", rollingInterval: RollingInterval.Day))
#if DEBUG
.WriteTo.Async(c => c.Console())
#endif
.CreateLogger();
try
{
Log.Information("Starting Shentun.Sms.HttpApi.Host.");
var builder = WebApplication.CreateBuilder(args);
builder.Host.AddAppSettingsSecretsJson()
@ -40,17 +56,18 @@ public class Program
var app = builder.Build();
await app.InitializeApplicationAsync();
var TaskCron = builder.Configuration["JobCron:TaskCron"];
if (!string.IsNullOrWhiteSpace(TaskCron))
{
RecurringJob.AddOrUpdate<ISmsTaskJob>("扫描短信计划", o => o.DoWork(), TaskCron);
}
//var TaskCron = builder.Configuration["JobCron:TaskCron"];
//if (!string.IsNullOrWhiteSpace(TaskCron))
//{
// RecurringJob.AddOrUpdate<ISmsTaskJob>("扫描短信计划", o => o.DoWork(), TaskCron);
//}
//var SendCron = builder.Configuration["JobCron:SendCron"];
//if (!string.IsNullOrWhiteSpace(SendCron))
//{
// RecurringJob.AddOrUpdate<ISmsSendJob>("执行发送短信扫描", o => o.DoWork(), SendCron);
//}
var SendCron = builder.Configuration["JobCron:SendCron"];
if (!string.IsNullOrWhiteSpace(SendCron))
{
RecurringJob.AddOrUpdate<ISmsSendJob>("执行发送短信扫描", o => o.DoWork(), SendCron);
}
await app.RunAsync();

30
src/Shentun.Sms.HttpApi.Host/SmsHttpApiHostModule.cs

@ -12,10 +12,13 @@ using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using OpenIddict.Validation.AspNetCore;
using Shentun.Sms.EntityFrameworkCore;
using Shentun.Sms.Logger;
using Shentun.Sms.MultiTenancy;
using Shentun.Sms.Service.Sms;
using System;
using System.Collections.Generic;
using System.Globalization;
@ -135,12 +138,29 @@ public class SmsHttpApiHostModule : AbpModule
#endregion
///解除https限制
#region 解除https限制
context.Services.AddOpenIddict()
.AddServer(option =>
{
option.UseAspNetCore().DisableTransportSecurityRequirement();
});
#endregion
#region 本地日志
context.Services.AddLogging(builder =>
{
builder.AddProvider(new CustomLoggerProvider());
});
#endregion
#region 注册服务
context.Services.AddSingleton<SmsFactory>();
context.Services.AddSingleton<TencentSms>();
#endregion
}
/// <summary>
@ -150,14 +170,16 @@ public class SmsHttpApiHostModule : AbpModule
/// <param name="configuration"></param>
private void ConfigureHangfire(ServiceConfigurationContext context, IConfiguration configuration)
{
context.Services.AddHangfire(config =>
{
//TimeZoneInfo.Local
config.UsePostgreSqlStorage(configuration.GetConnectionString("Default"), new PostgreSqlStorageOptions
{
QueuePollInterval = TimeSpan.FromSeconds(30),
QueuePollInterval = TimeSpan.FromSeconds(30)
});
//config.UseDashboardMetric(DashboardMetrics.ServerCount)
// .UseDashboardMetric(DashboardMetrics.RecurringJobCount)
// .UseDashboardMetric(DashboardMetrics.RetriesCount)
@ -356,7 +378,7 @@ public class SmsHttpApiHostModule : AbpModule
}
// app.UseHangfireDashboard();
app.UseHangfireDashboard();
// app.UseHangfireDashboard("/hangfire", new DashboardOptions
// {

6
src/Shentun.Sms.HttpApi.Host/appsettings.json

@ -26,11 +26,10 @@
"Sms": {
"SmsInterfaceId": "001",
"Platform": "Tencent"
},
"JobCron": {
"TaskCron": "0 0 */2 * * ?", /*Task 10*/
"SendCron": "0 */2 * * * ?" /*Send 1*/
"TaskCron": "0 0,30 */1 * * ?", /*Task 1030*/
"SendCron": "0 10 */1 * * ?" /*Send 110*/
},
"AspNetCore": {
"ConventionalControllers": {
@ -51,4 +50,5 @@
"Timeout": 60 //
}
}

1
src/Shentun.Sms.Service/Shentun.Sms.Service.csproj

@ -9,6 +9,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.2" />
<PackageReference Include="TencentCloudSDK" Version="3.0.976" />
</ItemGroup>

14
src/Shentun.Sms.Service/Sms/SmsFactory.cs

@ -1,4 +1,5 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
@ -10,13 +11,17 @@ namespace Shentun.Sms.Service.Sms
public class SmsFactory
{
private readonly IConfiguration _tencentSmsConfig;
private readonly TencentSms _tencentSms;
public SmsFactory(
IConfiguration tencentSmsConfig
IConfiguration tencentSmsConfig,
TencentSms tencentSms
)
{
_tencentSmsConfig = tencentSmsConfig;
_tencentSms = tencentSms;
}
public SmsBase CreateSms()
{
@ -25,9 +30,10 @@ namespace Shentun.Sms.Service.Sms
switch (platform)
{
case "Tencent":
sms = new TencentSms(_tencentSmsConfig);
// sms = new TencentSms(_tencentSmsConfig);
sms = _tencentSms;
break;
default: throw new ArgumentException("不支持的短信驱动");
}
return sms;

11
src/Shentun.Sms.Service/Sms/TencentSms.cs

@ -8,6 +8,8 @@ using TencentCloud.Common;
using Microsoft.Extensions.Configuration;
using TencentCloud.Sms.V20210111;
using TencentCloud.Sms.V20210111.Models;
using Microsoft.Extensions.Logging;
using TencentCloud.Tsf.V20180326.Models;
namespace Shentun.Sms.Service.Sms
{
@ -15,12 +17,14 @@ namespace Shentun.Sms.Service.Sms
{
private readonly IConfiguration _tencentSmsConfig;
private readonly ILogger<TencentSms> _logger;
public TencentSms(
IConfiguration tencentSmsConfig
)
IConfiguration tencentSmsConfig,
ILogger<TencentSms> logger)
{
_tencentSmsConfig = tencentSmsConfig;
_logger = logger;
}
/// <summary>
@ -127,6 +131,9 @@ namespace Shentun.Sms.Service.Sms
SendSmsResponse resp = client.SendSmsSync(req);
_logger.LogInformation($"------短信发送记录=>{AbstractModel.ToJsonString(resp)}------当前时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
// 输出json格式的字符串回包
Console.WriteLine(AbstractModel.ToJsonString(resp));

Loading…
Cancel
Save