using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Dysmsapi.Model.V20170525;
using Newtonsoft.Json;
using Learun.Util;
namespace Quanjiang.DigitalScholl.SendSms
{
public class AliyunSms:ISms
{
private static readonly string RegionIdForPop = ConfigManager.AppSettings["regionIdForPop"].Value;
private static readonly string AccessId = ConfigManager.AppSettings["accessId"].Value;
private static readonly string AccessSecret = ConfigManager.AppSettings["accessSecret"].Value;
private static readonly string Product = ConfigManager.AppSettings["product"].Value;
private static readonly string Domain = ConfigManager.AppSettings["domain"].Value;
private static readonly string SignName = ConfigManager.AppSettings["SignName"].Value;
///
/// 发送短信
///
/// 手机号
/// 短信通知类型
/// 发送结果
public async Task<(string code,string randomNum, string message,string errorType)> SendSmsToSingle(string phoneNumber, SmsType st, List sendParams = null)
{
(string code, string randomNum, string message,string errorType) result;
IClientProfile profile = DefaultProfile.GetProfile(RegionIdForPop, AccessId, AccessSecret);
DefaultProfile.AddEndpoint(RegionIdForPop, RegionIdForPop, Product, Domain);
IAcsClient acsClient = new DefaultAcsClient(profile);
var request = new SendSmsRequest();
try
{
request.PhoneNumbers = phoneNumber;
request.SignName = SignName;
var (templatecode, templateparam, randomNum) = GetSmsTemplateBySmsType(st);
request.TemplateCode = templatecode;
request.TemplateParam = templateparam;
var sendSmsResponse = await Task.FromResult(acsClient.GetAcsResponse(request));
result=(sendSmsResponse.Code, randomNum, sendSmsResponse.Message,"");
}
catch (ServerException e)
{
result = (e.ErrorCode,"",e.ErrorMessage,Enum.GetName(typeof(ErrorType),e.ErrorType));
}
catch (ClientException e)
{
result = (e.ErrorCode,"", e.ErrorMessage, Enum.GetName(typeof(ErrorType), e.ErrorType));
}
return result;
}
public Task<(string code, string randomNum, string message, string errorType)> SendSmsToMulti(List phoneNumbers, SmsType st, List sendParams = null)
{
throw new NotImplementedException();
}
///
/// 根据短信通知类型获取短信模板
///
///
///
private (string templateCode, string templateParam, string randomNum) GetSmsTemplateBySmsType(SmsType st)
{
(string templateCode, string templateParam,string randomNum) result;
var randomNum = CommonHelper.RndNum(6);
switch (st)
{
case SmsType.Register:
result = ("SMS_217435176", JsonConvert.SerializeObject(new { code = randomNum }), randomNum);
break;
//case SmsType.LoginBind:
// result = ("SMS_137485060", JsonConvert.SerializeObject(new { code = randomNum }), randomNum);
// break;
case SmsType.ForgetPassWord:
result = ("SMS_217425193", JsonConvert.SerializeObject(new { code = randomNum }), randomNum);
break;
case SmsType.FindPassWord:
result = ("SMS_217425193", JsonConvert.SerializeObject(new { code = randomNum }), randomNum);
break;
//case SmsType.LeaveNotification:
// result = ("SMS_137485060", JsonConvert.SerializeObject(new { code = randomNum }), randomNum);
// break;
//case SmsType.MakeUpMissedLessonsNotification:
// result = ("SMS_137485060", JsonConvert.SerializeObject(new { code = randomNum }), randomNum);
// break;
default:
throw new ArgumentOutOfRangeException(nameof(st), st, null);
}
return result;
}
}
}