You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

98 lines
4.5 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using Aliyun.Acs.Core;
  5. using Aliyun.Acs.Core.Exceptions;
  6. using Aliyun.Acs.Core.Profile;
  7. using Aliyun.Acs.Dysmsapi.Model.V20170525;
  8. using Newtonsoft.Json;
  9. using Learun.Util;
  10. namespace Quanjiang.DigitalScholl.SendSms
  11. {
  12. public class AliyunSms:ISms
  13. {
  14. private static readonly string RegionIdForPop = ConfigManager.AppSettings["regionIdForPop"].Value;
  15. private static readonly string AccessId = ConfigManager.AppSettings["accessId"].Value;
  16. private static readonly string AccessSecret = ConfigManager.AppSettings["accessSecret"].Value;
  17. private static readonly string Product = ConfigManager.AppSettings["product"].Value;
  18. private static readonly string Domain = ConfigManager.AppSettings["domain"].Value;
  19. private static readonly string SignName = ConfigManager.AppSettings["SignName"].Value;
  20. /// <summary>
  21. /// 发送短信
  22. /// </summary>
  23. /// <param name="phoneNumber">手机号</param>
  24. /// <param name="st">短信通知类型</param>
  25. /// <returns>发送结果</returns>
  26. public async Task<(string code,string randomNum, string message,string errorType)> SendSmsToSingle(string phoneNumber, SmsType st, List<string> sendParams = null)
  27. {
  28. (string code, string randomNum, string message,string errorType) result;
  29. IClientProfile profile = DefaultProfile.GetProfile(RegionIdForPop, AccessId, AccessSecret);
  30. DefaultProfile.AddEndpoint(RegionIdForPop, RegionIdForPop, Product, Domain);
  31. IAcsClient acsClient = new DefaultAcsClient(profile);
  32. var request = new SendSmsRequest();
  33. try
  34. {
  35. request.PhoneNumbers = phoneNumber;
  36. request.SignName = SignName;
  37. var (templatecode, templateparam, randomNum) = GetSmsTemplateBySmsType(st);
  38. request.TemplateCode = templatecode;
  39. request.TemplateParam = templateparam;
  40. var sendSmsResponse = await Task.FromResult(acsClient.GetAcsResponse(request));
  41. result=(sendSmsResponse.Code, randomNum, sendSmsResponse.Message,"");
  42. }
  43. catch (ServerException e)
  44. {
  45. result = (e.ErrorCode,"",e.ErrorMessage,Enum.GetName(typeof(ErrorType),e.ErrorType));
  46. }
  47. catch (ClientException e)
  48. {
  49. result = (e.ErrorCode,"", e.ErrorMessage, Enum.GetName(typeof(ErrorType), e.ErrorType));
  50. }
  51. return result;
  52. }
  53. public Task<(string code, string randomNum, string message, string errorType)> SendSmsToMulti(List<string> phoneNumbers, SmsType st, List<string> sendParams = null)
  54. {
  55. throw new NotImplementedException();
  56. }
  57. /// <summary>
  58. /// 根据短信通知类型获取短信模板
  59. /// </summary>
  60. /// <param name="st"></param>
  61. /// <returns></returns>
  62. private (string templateCode, string templateParam, string randomNum) GetSmsTemplateBySmsType(SmsType st)
  63. {
  64. (string templateCode, string templateParam,string randomNum) result;
  65. var randomNum = CommonHelper.RndNum(6);
  66. switch (st)
  67. {
  68. case SmsType.Register:
  69. result = ("SMS_217435176", JsonConvert.SerializeObject(new { code = randomNum }), randomNum);
  70. break;
  71. //case SmsType.LoginBind:
  72. // result = ("SMS_137485060", JsonConvert.SerializeObject(new { code = randomNum }), randomNum);
  73. // break;
  74. case SmsType.ForgetPassWord:
  75. result = ("SMS_217425193", JsonConvert.SerializeObject(new { code = randomNum }), randomNum);
  76. break;
  77. case SmsType.FindPassWord:
  78. result = ("SMS_217425193", JsonConvert.SerializeObject(new { code = randomNum }), randomNum);
  79. break;
  80. //case SmsType.LeaveNotification:
  81. // result = ("SMS_137485060", JsonConvert.SerializeObject(new { code = randomNum }), randomNum);
  82. // break;
  83. //case SmsType.MakeUpMissedLessonsNotification:
  84. // result = ("SMS_137485060", JsonConvert.SerializeObject(new { code = randomNum }), randomNum);
  85. // break;
  86. default:
  87. throw new ArgumentOutOfRangeException(nameof(st), st, null);
  88. }
  89. return result;
  90. }
  91. }
  92. }