SendCloudService.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. 
  2. using TEAMModelOS.SDK.Extension.MessagePush.Interfaces;
  3. using TEAMModelOS.SDK.Extension.MessagePush.Model;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Threading.Tasks;
  7. using TEAMModelOS.SDK.Extension.Language.Interfaces;
  8. using TEAMModelOS.SDK.Module.AzureTable.Interfaces;
  9. using Microsoft.Extensions.Options;
  10. using TEAMModelOS.SDK.Extension.Language.Model;
  11. using TEAMModelOS.SDK.Helper.Common.JsonHelper;
  12. using TEAMModelOS.SDK.Helper.Security.Md5Hash;
  13. using TEAMModelOS.SDK.Helper.Network.HttpHelper;
  14. using TEAMModelOS.SDK.Extension.HttpClient.Implements;
  15. namespace TEAMModelOS.SDK.Extension.MessagePush.Implements
  16. {
  17. public class SendCloudService : ISendCloudService
  18. {
  19. public SmsSendCloud smsSendCloud;
  20. public ILanguageService _languageService;
  21. public IAzureTableDBRepository _azureTableDBRepository;
  22. public HttpClientSendCloud _httpClientService;
  23. public SendCloudService(IOptions<SmsSendCloud> _option, ILanguageService languageService , IAzureTableDBRepository azureTableDBRepository , HttpClientSendCloud httpClientService )
  24. {
  25. _azureTableDBRepository = azureTableDBRepository;
  26. _languageService = languageService;
  27. smsSendCloud = _option.Value;
  28. _httpClientService = httpClientService;
  29. }
  30. public List<Dictionary<string, string>> Languages { get; set; } = new List<Dictionary<string, string>>
  31. {
  32. new Dictionary<string, string> { { "name", "简体中文" }, { "code","CHS" } },
  33. new Dictionary<string, string> { { "name", "繁体中文" }, { "code","CHT" } },
  34. new Dictionary<string, string> { { "name", "英语" }, { "code","EN" } } };
  35. public List<Dictionary<string, string>> BizCodes { get; set; } = new List<Dictionary<string, string>>
  36. {
  37. //new Dictionary<string, string> { { "name", "验证码业务" }, { "code", "Captcha" } },
  38. new Dictionary<string, string> { { "name", "报名通知业务" }, { "code", "SignUp" } }
  39. };
  40. public async Task<List<SmsConfig>> SaveOrUpdateSmsConfig(List<SmsConfig> configs) {
  41. return await _azureTableDBRepository.SaveOrUpdateAll<SmsConfig>(configs);
  42. }
  43. public async Task<List<SmsConfig>> InitSmsConfig(string BizNum)
  44. {
  45. List<SmsConfig> smsConfigs = new List<SmsConfig>() ;
  46. List<SmsConfig> SaveSmsConfigs = new List<SmsConfig>();
  47. foreach (Dictionary<string, string> BizCode in BizCodes) {
  48. foreach (Dictionary<string, string> Language in Languages) {
  49. Dictionary<string, object> dict = new Dictionary<string, object>
  50. {
  51. { "BizNum", BizNum },{ "BizCode", BizCode["code"] }, { "Language", Language["code"] } ,
  52. };
  53. SmsConfig smsConfig = await _azureTableDBRepository.FindOneByDict<SmsConfig>(dict);
  54. if (smsConfig != null && smsConfig.RowKey != null)
  55. {
  56. smsConfigs.Add(smsConfig);
  57. }
  58. else {
  59. DateTimeOffset now = DateTimeOffset.Now;
  60. SmsConfig SaveSmsConfig = new SmsConfig {
  61. RowKey = BizNum + "-" + BizCode["code"] + "-" + Language["code"],
  62. PartitionKey = BizNum,
  63. Language = Language["code"],
  64. LanguageName = Language["name"],
  65. BizCode = BizCode["code"],
  66. BizName = BizCode["name"],
  67. BizNum = BizNum,
  68. Template = null,
  69. Timestamp= now
  70. };
  71. SaveSmsConfigs.Add(SaveSmsConfig);
  72. smsConfigs.Add(SaveSmsConfig);
  73. }
  74. }
  75. }
  76. if (SaveSmsConfigs.Count > 0) {
  77. await _azureTableDBRepository.SaveOrUpdateAll<SmsConfig>(SaveSmsConfigs);
  78. }
  79. return smsConfigs;
  80. }
  81. /// <summary>
  82. /// https://sendcloud.kf5.com/posts/view/1074678/
  83. /// </summary>
  84. /// <param name="BizCode"></param>
  85. /// <param name="CountryCode"></param>
  86. /// <param name="phone"></param>
  87. /// <param name="vars"></param>
  88. /// <returns></returns>
  89. public async Task<SendCloudResponse> SendSmsByBizCode(string BizNum , string BizCode, int CountryCode, string phone, Dictionary<string, string> vars = null)
  90. {
  91. SmsCountryCode code = null;
  92. bool flag = _languageService.GetSmsLanguage().TryGetValue(CountryCode + "", out code);
  93. string SmsLang = "EN";
  94. int templateId = 0;
  95. if (flag)
  96. {
  97. SmsLang = code.SmsLang;
  98. }
  99. Dictionary<string, object> dict = new Dictionary<string, object>
  100. {
  101. { "BizNum", BizNum },{ "BizCode", BizCode }, { "Language", SmsLang } ,
  102. };
  103. SmsConfig smsConfig = await _azureTableDBRepository.FindOneByDict<SmsConfig>(dict);
  104. if (smsConfig != null && smsConfig.RowKey != null )
  105. {
  106. //默认调用英文,不管是否包含,发送时会去处理相关信息
  107. int.TryParse(smsConfig.Template ,out templateId);
  108. }
  109. //else {
  110. // throw new BizException("");
  111. // // templateId = smsSendCloud.SmsCode[code.SmsLang];
  112. //}
  113. int msgType = 0;
  114. if (CountryCode != 86)
  115. {
  116. msgType = 2;
  117. return await SendSms(templateId, "00" + CountryCode + phone, vars, msgType);
  118. }
  119. else
  120. {
  121. return await SendSms(templateId, phone, vars, msgType);
  122. }
  123. }
  124. public async Task<SendCloudResponse> SendSms(int templateId, string phone, Dictionary<string, string> vars = null, int msgType = 0)
  125. {
  126. List<KeyValuePair<string, string>> paramList = new List<KeyValuePair<string, string>>
  127. {
  128. new KeyValuePair<string, string>("msgType", msgType+""),//0表示短信, 1表示彩信,2表示国际短信, 默认值为0
  129. new KeyValuePair<string, string>("phone", phone),
  130. new KeyValuePair<string, string>("smsUser", smsSendCloud.SmsUser),
  131. new KeyValuePair<string, string>("templateId", templateId+""),
  132. };
  133. if (vars != null)
  134. {
  135. paramList.Add(new KeyValuePair<string, string>("vars", JsonNetHelper.ToJson(vars)));
  136. }
  137. var param_str = "";
  138. foreach (var param in paramList)
  139. {
  140. param_str += param.Key.ToString() + "=" + param.Value.ToString() + "&";
  141. }
  142. string sign_str = smsSendCloud.SmsKey + "&" + param_str + smsSendCloud.SmsKey;
  143. string sign = Md5Hash.Encrypt(sign_str);
  144. paramList.Add(new KeyValuePair<string, string>("signature", sign));
  145. string result = await _httpClientService.HttpPostAsync(smsSendCloud.SmsUrl, paramList);
  146. return JsonNetHelper.FromJson<SendCloudResponse>(result);
  147. }
  148. }
  149. }