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.
 
 
 
 
 
 

101 lines
4.5 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Net.Http;
  7. using System.Net.Http.Headers;
  8. using System.Security.Cryptography;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using Learun.Util;
  12. using Newtonsoft.Json;
  13. using Newtonsoft.Json.Linq;
  14. using Quanjiang.DigitalScholl.SendSms.Banling.Sms;
  15. namespace Quanjiang.DigitalScholl.SendSms.Banling
  16. {
  17. /// <summary>
  18. /// 斑羚短信平台
  19. /// </summary>
  20. public class BanLingSms : ISms
  21. {
  22. private static readonly string SdkAppId = ConfigManager.AppSettings["sdkappid"].Value;
  23. private static readonly string AppKey = ConfigManager.AppSettings["appkey"].Value;
  24. public async Task<(string code, string randomNum, string message, string errorType)> SendSmsToSingle(string phoneNumber, SmsType st, List<string> sendParams = null)
  25. {
  26. (string code, string randomNum, string message, string errorType) result;
  27. var (templatecode, templateparam, randomNum) = GetSmsTemplate(st, sendParams);
  28. try
  29. {
  30. var singleSender = new SmsSingleSender(SdkAppId, AppKey);
  31. var singleResult = await Task.FromResult(singleSender.SendWithParam("86", phoneNumber, templatecode, templateparam, "深圳国际公益学院", "", ""));
  32. result = singleResult.result==0 ? ("OK", randomNum, "", "") : (singleResult.result.ToString(), "", singleResult.errmsg, "Client");
  33. }
  34. catch (Exception e)
  35. {
  36. result = ("", "", e.Message, e.GetType().ToString());
  37. }
  38. return result;
  39. }
  40. public async Task<(string code, string randomNum, string message, string errorType)> SendSmsToMulti(List<string> phoneNumbers, SmsType st, List<string> sendParams = null)
  41. {
  42. (string code, string randomNum, string message, string errorType) result;
  43. var (templatecode, templateparam, randomNum) = GetSmsTemplate(st, sendParams);
  44. try
  45. {
  46. var multiSender = new SmsMultiSender(SdkAppId, AppKey);
  47. var multiResult = await Task.FromResult(multiSender.SendWithParam("86", phoneNumbers, templatecode, templateparam, "深圳国际公益学院", "", ""));
  48. result = multiResult.result == 0 ? ("OK", randomNum, "", "") : (multiResult.result.ToString(), "", multiResult.errmsg, "Client");
  49. }
  50. catch (Exception e)
  51. {
  52. result = ("", "", e.Message, e.GetType().ToString());
  53. }
  54. return result;
  55. }
  56. /// <summary>
  57. /// 根据短信通知类型获取短信模板
  58. /// </summary>
  59. /// <param name="st"></param>
  60. /// <param name="sendParams"></param>
  61. /// <returns></returns>
  62. private (string templateCode, List<string> templateParam, string randomNum) GetSmsTemplate(SmsType st, List<string> sendParams)
  63. {
  64. (string templateCode, List<string> templateParam, string randomNum) result;
  65. var randomNum = CommonHelper.RandNum(6);
  66. switch (st)
  67. {
  68. case SmsType.Register:
  69. result = ("141577", new List<string>() { randomNum, "5" }, randomNum);
  70. break;
  71. case SmsType.LoginBind:
  72. result = ("141578", new List<string>() { randomNum, "5" }, randomNum);
  73. break;
  74. case SmsType.ForgetPassWord:
  75. result = ("141581", new List<string>() { randomNum, "5" }, randomNum);
  76. break;
  77. case SmsType.LessionNotification:
  78. result = ("141583", sendParams, randomNum);
  79. break;
  80. case SmsType.LeaveNotification:
  81. result = ("141585", sendParams, randomNum);
  82. break;
  83. case SmsType.MakeUpMissedLessonsNotification:
  84. result = ("141588", sendParams, randomNum);
  85. break;
  86. case SmsType.ClassManagerLeaveNotification:
  87. result = ("156728", sendParams, randomNum);
  88. break;
  89. case SmsType.ClassManagerMakeUpMissedLessonsNotification:
  90. result = ("156729", sendParams, randomNum);
  91. break;
  92. default:
  93. throw new ArgumentOutOfRangeException(nameof(st), st, null);
  94. }
  95. return result;
  96. }
  97. }
  98. }