This commit is contained in:
tongfei
2024-03-28 14:12:45 +08:00
parent 18ed2a7d82
commit 739726cf37
6 changed files with 113 additions and 1 deletions

View File

@@ -0,0 +1,56 @@
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Text;
using WMS.Web.Domain.IService;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Dysmsapi.Model.V20170525;
using Aliyun.Acs.Core.Exceptions;
namespace WMS.Web.Domain.Services
{
/// <summary>
/// 阿里云短息服务
/// </summary>
public class AliyunSmsService: IAliyunSmsService
{
private readonly ILogger<AliyunSmsService> _logger;
private readonly IAcsClient profile;
public AliyunSmsService(ILogger<AliyunSmsService> logger, string accessKeyId, string accessKeySecret)
{
_logger = logger;
profile = new DefaultAcsClient(DefaultProfile.GetProfile("cn-hangzhou", accessKeyId, accessKeySecret));
}
public bool SendSms(string phoneNumbers, string templateCode, string signName, string templateParam = null)
{
SendSmsRequest request = new SendSmsRequest
{
PhoneNumbers = phoneNumbers,
SignName = signName,
TemplateCode = templateCode,
TemplateParam = templateParam
};
try
{
SendSmsResponse response = profile.GetAcsResponse(request);
return response.Code == "OK";
}
catch (ServerException ex)
{
// 处理服务端异常
Console.WriteLine(ex.Message);
}
catch (ClientException ex)
{
// 处理客户端异常
Console.WriteLine(ex.Message);
}
return false;
}
}
}