|
@@ -1,9 +1,16 @@
|
|
|
using DotNetCore.CAP;
|
|
|
using Hotline.Share.Dtos.Push;
|
|
|
+using Hotline.Share.Dtos.SendSms;
|
|
|
using Hotline.Share.Enums.Push;
|
|
|
using MapsterMapper;
|
|
|
using MediatR;
|
|
|
+using Microsoft.AspNetCore.Http;
|
|
|
+using Microsoft.Extensions.Logging;
|
|
|
+using Microsoft.Extensions.Options;
|
|
|
+using System.Net;
|
|
|
+using System.Net.Http.Json;
|
|
|
using System.Text.RegularExpressions;
|
|
|
+using System.Xml;
|
|
|
using XF.Domain.Dependency;
|
|
|
using XF.Domain.Exceptions;
|
|
|
using XF.Domain.Repository;
|
|
@@ -21,6 +28,9 @@ public class PushDomainService : IPushDomainService, IScopeDependency
|
|
|
private readonly IMediator _mediator;
|
|
|
private readonly IRepository<MessageTemplate> _messageTemplateRepository;
|
|
|
private readonly ICapPublisher _capPublisher;
|
|
|
+ private readonly IHttpClientFactory _httpClientFactory;
|
|
|
+ private readonly ILogger<PushDomainService> _logger;
|
|
|
+ private readonly IOptionsSnapshot<SendSmsConfiguration> _sendSmsConfiguration;
|
|
|
|
|
|
/// <summary>
|
|
|
///
|
|
@@ -30,17 +40,27 @@ public class PushDomainService : IPushDomainService, IScopeDependency
|
|
|
/// <param name="mediator"></param>
|
|
|
/// <param name="messageTemplateRepository"></param>
|
|
|
/// <param name="capPublisher"></param>
|
|
|
+ /// <param name="httpClientFactory"></param>
|
|
|
+ /// <param name="logger"></param>
|
|
|
+ /// <param name="sendSmsConfiguration"></param>
|
|
|
public PushDomainService(IRepository<Message> messageRepository,
|
|
|
IMapper mapper,
|
|
|
IMediator mediator,
|
|
|
IRepository<MessageTemplate> messageTemplateRepository,
|
|
|
- ICapPublisher capPublisher)
|
|
|
+ ICapPublisher capPublisher,
|
|
|
+ IHttpClientFactory httpClientFactory,
|
|
|
+ ILogger<PushDomainService> logger,
|
|
|
+ IOptionsSnapshot<SendSmsConfiguration> sendSmsConfiguration
|
|
|
+ )
|
|
|
{
|
|
|
_messageRepository = messageRepository;
|
|
|
_mapper = mapper;
|
|
|
_mediator = mediator;
|
|
|
_messageTemplateRepository = messageTemplateRepository;
|
|
|
_capPublisher = capPublisher;
|
|
|
+ _httpClientFactory = httpClientFactory;
|
|
|
+ _logger = logger;
|
|
|
+ _sendSmsConfiguration = sendSmsConfiguration;
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
@@ -100,7 +120,7 @@ public class PushDomainService : IPushDomainService, IScopeDependency
|
|
|
TelNumber = message.TelNumber
|
|
|
};
|
|
|
|
|
|
- await _capPublisher.PublishAsync(Hotline.Share.Mq.EventNames.SendSms, pushMessage, cancellationToken: cancellation);
|
|
|
+ await ExecuteAsync<PushMessageDto, SendSmsReponse>(_sendSmsConfiguration.Value.Url, pushMessage, cancellationToken: cancellation);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -143,6 +163,87 @@ public class PushDomainService : IPushDomainService, IScopeDependency
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 修改短信推送状态
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="dto"></param>
|
|
|
+ /// <param name="cancellation"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public async Task PushMsgUpdateStateNewAsync(PushReceiveMessageDto dto, CancellationToken cancellation)
|
|
|
+ {
|
|
|
+ var data = await _messageRepository.GetAsync(p => p.Id == dto.ExternalId, cancellation);
|
|
|
+ if (data != null)
|
|
|
+ {
|
|
|
+ data.SmsWaitSendingId = dto.SmsWaitSendingId;
|
|
|
+ data.Status = EPushStatus.Success;
|
|
|
+ data.SendTime = Convert.ToDateTime(dto.SendTime);
|
|
|
+ data.SendState = dto.SendState;
|
|
|
+ data.MsgCount = dto.MsgCount;
|
|
|
+ data.SmsSendingCompletedId = dto.SmsSendingCompletedId;
|
|
|
+ data.IsSmsReply = dto.IsSmsReply;
|
|
|
+ data.SmsReplyTime = Convert.ToDateTime(dto.SmsReplyTime);
|
|
|
+ data.SmsReplyContent = dto.SmsReplyContent;
|
|
|
+ await _messageRepository.UpdateAsync(data, cancellation);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ ///
|
|
|
+ /// </summary>
|
|
|
+ /// <typeparam name="TRequest"></typeparam>
|
|
|
+ /// <typeparam name="TResponse"></typeparam>
|
|
|
+ /// <param name="path"></param>
|
|
|
+ /// <param name="request"></param>
|
|
|
+ /// <param name="clientInitialize"></param>
|
|
|
+ /// <param name="cancellationToken"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ private async Task<TResponse?> ExecuteAsync<TRequest, TResponse>(string path, TRequest request,
|
|
|
+ Action<HttpClient>? clientInitialize = null, CancellationToken cancellationToken = default)
|
|
|
+ {
|
|
|
+ var client = _httpClientFactory.CreateClient("sendwaitsms");
|
|
|
+ clientInitialize?.Invoke(client);
|
|
|
|
|
|
+ using var responseMessage = await client.PostAsJsonAsync(path, request, cancellationToken);
|
|
|
+ responseMessage.EnsureSuccessStatusCode();
|
|
|
+ var result = await responseMessage.Content.ReadFromJsonAsync<TResponse>(cancellationToken: cancellationToken);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ #region http请求
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 封装使用HttpClient调用WebService
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="url">URL地址</param>
|
|
|
+ /// <param name="content">参数</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ private async Task<string> PostHelper(string url, HttpContent content)
|
|
|
+ {
|
|
|
+ _logger.LogInformation($"准备推送短信, {nameof(PostHelper)}");
|
|
|
+ var result = string.Empty;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ using var client = _httpClientFactory.CreateClient();
|
|
|
+ using (var response = await client.PostAsync(url, content))
|
|
|
+ {
|
|
|
+ if (response.StatusCode == HttpStatusCode.OK)
|
|
|
+ {
|
|
|
+ result = await response.Content.ReadAsStringAsync();
|
|
|
+ XmlDocument doc = new XmlDocument();
|
|
|
+ doc.LoadXml(result);
|
|
|
+ result = doc.InnerText;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ _logger.LogError(ex.InnerException?.Message);
|
|
|
+ result = ex.Message;
|
|
|
+ }
|
|
|
+ _logger.LogInformation($"推送响应:{result}");
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ #endregion
|
|
|
}
|