using Furion.DatabaseAccessor;
|
using Furion.DataEncryption;
|
using Furion.DependencyInjection;
|
using Furion.HttpRemote;
|
using Microsoft.Extensions.Options;
|
using Newtonsoft.Json.Linq;
|
using System;
|
using System.Collections.Generic;
|
using System.Diagnostics;
|
using System.Linq;
|
using System.Text;
|
using System.Threading;
|
using System.Threading.Tasks;
|
|
namespace ApiTools.Core
|
{
|
public class ChengLiYeSmsService(
|
IRepository<ThreeResourceLog, LogDbContextLocator> repThreeResourceLog,
|
IOptions<ChengLiYeSmsOptions> options,
|
IHttpRemoteService httpRemoteService
|
) : ISmsService, ITransient
|
{
|
private readonly IRepository<ThreeResourceLog, LogDbContextLocator> repThreeResourceLog = repThreeResourceLog;
|
private readonly IOptions<ChengLiYeSmsOptions> options = options;
|
private readonly IHttpRemoteService httpRemoteService = httpRemoteService;
|
|
public async Task<SmsResponse> SendAsync(string phoneNumber, EnumSmsTemplateCode templateCode, object templateParam, CancellationToken cancellationToken)
|
{
|
var content = options.Value.TemplateCodes[templateCode.ToString()];
|
if (templateParam != null)
|
{
|
var jObject = JObject.FromObject(templateParam);
|
var props = jObject.Properties();
|
foreach (var prop in props)
|
{
|
content = content.Replace($"${{{prop.Name}}}", prop.Value.ToString());
|
}
|
}
|
|
var body = new ChengLiYeSmsSubmitRequest
|
{
|
UserName = options.Value.UserName,
|
Sign = MD5Encryption.Encrypt($"{options.Value.UserName}{options.Value.Password}{phoneNumber}【{options.Value.SignName}】{content}"),
|
Mobile = phoneNumber,
|
Content = $"【{options.Value.SignName}】{content}",
|
};
|
var log = new ThreeResourceLog
|
{
|
Method = EnumResourceMethod.Get,
|
Domain = "https://www.sms-cly.cn",
|
Path = "/v7/msg/submit.json",
|
Request = body.ToJson(),
|
};
|
await repThreeResourceLog.InsertNowAsync(log);
|
var stopwatch = Stopwatch.StartNew();
|
var result = await httpRemoteService.PostAsAsync<ChengLiYeSmsSubmitResponse>($"{log.Domain}{log.Path}",
|
build => build.SetJsonContent(body));
|
log.UpdatedTime = DateTimeOffset.Now;
|
stopwatch.Stop();
|
log.ElapsedMilliseconds = stopwatch.ElapsedMilliseconds;
|
log.Response = result.ToJson();
|
log.IsSuccess = result.ResultCode == "1";
|
await repThreeResourceLog.UpdateNowAsync(log);
|
|
return new SmsResponse
|
{
|
Status = log.IsSuccess
|
? EnumSmsStatus.InProcess
|
: EnumSmsStatus.Fail,
|
Code = result.ResultCode,
|
Message = result.ResultMsg,
|
RequestId = result.Msgid
|
};
|
}
|
}
|
}
|