6 changed files with 378 additions and 3 deletions
-
93src/Shentun.ColumnReferencePlugIns/Sms/CreateSmsTaskDto.cs
-
194src/Shentun.ColumnReferencePlugIns/Sms/SmsPlugIns.cs
-
84src/Shentun.Peis.Application/TransToWebPeis/TransToWebPeisAppService.cs
-
3src/Shentun.Peis.Domain.Shared/Enums/ThirdInterfaceTypeFlag.cs
-
2src/Shentun.Peis.Domain/Shentun.Peis.Domain.csproj
-
5src/Shentun.Peis.HttpApi.Host/appsettings.json
@ -0,0 +1,93 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Shentun.Peis.PlugIns.Sms |
|||
{ |
|||
public class CreateSmsTaskDto |
|||
{ |
|||
/// <summary>
|
|||
/// 短信类别ID
|
|||
/// </summary>
|
|||
public Guid SmsTypeId { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 人员ID
|
|||
/// </summary>
|
|||
public string PersonId { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 姓名
|
|||
/// </summary>
|
|||
public string PersonName { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 手机号国家代码
|
|||
/// </summary>
|
|||
public string CountryCode { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 手机号
|
|||
/// </summary>
|
|||
public string MobileTelephone { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 短信内容
|
|||
/// </summary>
|
|||
public string Content { get; set; } |
|||
|
|||
|
|||
/// <summary>
|
|||
/// 应用ID
|
|||
/// </summary>
|
|||
public Guid SmsAppId { get; set; } |
|||
|
|||
|
|||
/// <summary>
|
|||
/// 第三方系统唯一ID
|
|||
/// </summary>
|
|||
public string? ThirdId { get; set; } |
|||
|
|||
|
|||
|
|||
/// <summary>
|
|||
/// 任务周期类别
|
|||
/// </summary>
|
|||
public char TaskCycleType { get; set; } |
|||
|
|||
|
|||
/// <summary>
|
|||
/// 任务表达式
|
|||
/// </summary>
|
|||
public string? TaskCorn { get; set; } |
|||
|
|||
|
|||
/// <summary>
|
|||
/// 停止执行时间
|
|||
/// </summary>
|
|||
public string? StopTime { get; set; } |
|||
|
|||
|
|||
///// <summary>
|
|||
///// 是否启用
|
|||
///// </summary>
|
|||
//public char IsActive { get; set; } = 'Y';
|
|||
|
|||
/// <summary>
|
|||
/// 发送者用户ID
|
|||
/// </summary>
|
|||
public string? SenderId { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 发送者用户名
|
|||
/// </summary>
|
|||
public string? SenderName { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 短信模板ID
|
|||
/// </summary>
|
|||
public string TemplateId { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,194 @@ |
|||
using Newtonsoft.Json.Converters; |
|||
using Newtonsoft.Json; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.IdentityModel.Tokens.Jwt; |
|||
using System.Linq; |
|||
using System.Net.Http.Headers; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.Extensions.Configuration; |
|||
|
|||
namespace Shentun.Peis.PlugIns.Sms |
|||
{ |
|||
public class SmsPlugIns : ThirdPlugInsBase |
|||
{ |
|||
private string _smsUser; |
|||
private string _smsPassword; |
|||
private string _smsBaseAddress; |
|||
private static string _smsToken; |
|||
|
|||
public SmsPlugIns(Guid thirdInterfaceId) : base(thirdInterfaceId) |
|||
{ |
|||
var configurationBuilder = new ConfigurationBuilder() |
|||
.AddJsonStream(new MemoryStream(Encoding.UTF8.GetBytes(_thirdInterfaceDto.ParmValue))); |
|||
InterfaceConfig = configurationBuilder.Build(); |
|||
|
|||
_smsUser = InterfaceConfig.GetSection("Interface").GetSection("User").Value; |
|||
_smsPassword = InterfaceConfig.GetSection("Interface").GetSection("Password").Value; |
|||
_smsBaseAddress = InterfaceConfig.GetSection("Interface").GetSection("BaseAddress").Value; |
|||
} |
|||
|
|||
|
|||
public async Task<TOut> CallSmsAppServiceAsync<TInput, TOut>(string url, TInput data, string method = "post") |
|||
{ |
|||
//if (string.IsNullOrWhiteSpace(_webPeisBaseAddress))
|
|||
//{
|
|||
// throw new Exception("_webPeisBaseAddress不能为空");
|
|||
//}
|
|||
string baseAddress = _smsBaseAddress; |
|||
await CheckSmsLoginAsync(); |
|||
using (var httpClientHandler = new HttpClientHandler()) |
|||
{ |
|||
using (var httpClient = new HttpClient(httpClientHandler)) |
|||
{ |
|||
httpClient.BaseAddress = new Uri(baseAddress); |
|||
|
|||
httpClient.DefaultRequestHeaders.Accept.Add( |
|||
new MediaTypeWithQualityHeaderValue("application/json"));//设置accept标头,告诉JSON是可接受的响应类型
|
|||
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _smsToken); |
|||
IsoDateTimeConverter timeFormat = new IsoDateTimeConverter(); |
|||
timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss"; |
|||
var sendData = JsonConvert.SerializeObject(data, Formatting.Indented, timeFormat); |
|||
using (HttpContent httpContent = new StringContent(sendData)) |
|||
{ |
|||
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); |
|||
HttpResponseMessage response = null; |
|||
if (method == "post") |
|||
{ |
|||
response = await httpClient.PostAsync(url, httpContent); |
|||
} |
|||
else |
|||
{ |
|||
response = await httpClient.GetAsync(url); |
|||
} |
|||
|
|||
string result; |
|||
if (!response.IsSuccessStatusCode) |
|||
{ |
|||
result = response.Content.ReadAsStringAsync().Result; |
|||
throw new Exception("http通信错误:" + response.StatusCode + ",结果:" + result); |
|||
} |
|||
result = await response.Content.ReadAsStringAsync(); |
|||
|
|||
var resultDto = JsonConvert.DeserializeObject<WebApiOutDto<TOut>>(result); |
|||
if (resultDto != null) |
|||
{ |
|||
if (resultDto.Code == -1) |
|||
{ |
|||
throw new Exception($"调用WebApi失败,返回-1,消息:" + resultDto.Message); |
|||
} |
|||
|
|||
return resultDto.Data; |
|||
} |
|||
return resultDto.Data; |
|||
} |
|||
|
|||
} |
|||
} |
|||
} |
|||
|
|||
private async Task CheckSmsLoginAsync() |
|||
{ |
|||
if (string.IsNullOrWhiteSpace(_smsToken)) |
|||
{ |
|||
await LoginSmsAsync(); |
|||
} |
|||
else |
|||
{ |
|||
var handler = new JwtSecurityTokenHandler(); |
|||
var payload = handler.ReadJwtToken(_smsToken).Payload; |
|||
var claims = payload.Claims; |
|||
var exp = claims.First(claim => claim.Type == "exp").Value; |
|||
if (exp == null) |
|||
{ |
|||
await LoginSmsAsync(); |
|||
} |
|||
else |
|||
{ |
|||
if (long.TryParse(exp, out var expires)) |
|||
{ |
|||
var expireTime = DateTimeOffset.FromUnixTimeSeconds(expires).LocalDateTime; |
|||
if (expireTime <= DateTime.Now) |
|||
{ |
|||
await LoginSmsAsync(); |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
await LoginSmsAsync(); |
|||
} |
|||
|
|||
} |
|||
|
|||
} |
|||
} |
|||
|
|||
private async Task<WebApiOutDto<LoginOutDataDto>> LoginSmsAsync() |
|||
{ |
|||
if (string.IsNullOrWhiteSpace(_smsUser)) |
|||
{ |
|||
throw new Exception("WebPeisUser不能为空"); |
|||
} |
|||
if (string.IsNullOrWhiteSpace(_smsPassword)) |
|||
{ |
|||
throw new Exception("WebPeisPassword不能为空"); |
|||
} |
|||
var relult = await LoginSmsAsync(_smsUser, _smsPassword); |
|||
return relult; |
|||
} |
|||
|
|||
private async Task<WebApiOutDto<LoginOutDataDto>> LoginSmsAsync(string userId, string password) |
|||
{ |
|||
if (string.IsNullOrWhiteSpace(userId)) |
|||
{ |
|||
throw new Exception("用户ID不能为空"); |
|||
} |
|||
if (string.IsNullOrWhiteSpace(password)) |
|||
{ |
|||
throw new Exception("密码不能为空"); |
|||
} |
|||
using (var httpClientHandler = new HttpClientHandler()) |
|||
{ |
|||
using (var httpClient = new HttpClient(httpClientHandler)) |
|||
{ |
|||
httpClient.BaseAddress = new Uri(_smsBaseAddress); |
|||
|
|||
httpClient.DefaultRequestHeaders.Accept.Add( |
|||
new MediaTypeWithQualityHeaderValue("application/json"));//设置accept标头,告诉JSON是可接受的响应类型
|
|||
var url = "api/app/CustomerUser/UserLogin"; |
|||
var loginUser = new LoginInputDto() |
|||
{ |
|||
UserName = userId, |
|||
Password = password |
|||
}; |
|||
var sendData = JsonConvert.SerializeObject(loginUser); |
|||
using (HttpContent httpContent = new StringContent(sendData)) |
|||
{ |
|||
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); |
|||
HttpResponseMessage response = await httpClient.PostAsync(url, httpContent); |
|||
string result; |
|||
if (!response.IsSuccessStatusCode) |
|||
{ |
|||
result = response.Content.ReadAsStringAsync().Result; |
|||
throw new Exception("http通信错误:" + response.StatusCode + ",结果:" + result); |
|||
} |
|||
result = await response.Content.ReadAsStringAsync(); |
|||
var restultDto = JsonConvert.DeserializeObject<WebApiOutDto<LoginOutDataDto>>(result); |
|||
if (restultDto == null) |
|||
{ |
|||
throw new Exception("返回结果是空"); |
|||
} |
|||
if (restultDto.Code != 1) |
|||
{ |
|||
throw new Exception($"登录失败{restultDto.Message}"); |
|||
} |
|||
_smsToken = restultDto.Data.access_token; |
|||
return restultDto; |
|||
} |
|||
|
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue