SendCloudService.cs 7.0 KB

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