|
- 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 Learun.Util;
- using Newtonsoft.Json;
-
- 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;
-
- /// <summary>
- /// 发送短信
- /// </summary>
- /// <param name="phoneNumber">手机号</param>
- /// <param name="st">短信通知类型</param>
- /// <returns>发送结果</returns>
- public async Task<(string code, string randomNum, string message, string errorType)> SendSmsToSingle(string phoneNumber, SmsType st, List<string> 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<string> phoneNumbers, SmsType st, List<string> sendParams = null)
- {
- throw new NotImplementedException();
- }
-
-
- /// <summary>
- /// 根据短信通知类型获取短信模板
- /// </summary>
- /// <param name="st"></param>
- /// <returns></returns>
- private (string templateCode, string templateParam, string randomNum) GetSmsTemplateBySmsType(SmsType st)
- {
- (string templateCode, string templateParam, string randomNum) result;
- var randomNum = CommonHelper.RandNum(6);
- switch (st)
- {
- case SmsType.LoginBind:
- result = ("SMS_471435023", JsonConvert.SerializeObject(new { code = randomNum }), randomNum);
- break;
- case SmsType.ForgetPassWord:
- result = ("SMS_471435023", JsonConvert.SerializeObject(new { code = randomNum }), randomNum);
- break;
- default:
- throw new ArgumentOutOfRangeException(nameof(st), st, null);
- }
- return result;
- }
- }
- }
|