123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- using TEAMModelOS.SDK.Extension.MessagePush.Interfaces;
- using TEAMModelOS.SDK.Extension.MessagePush.Model;
- using System;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using TEAMModelOS.SDK.Extension.Language.Interfaces;
- using Microsoft.Extensions.Options;
- using TEAMModelOS.SDK.Extension.Language.Model;
- using TEAMModelOS.SDK.Helper.Common.JsonHelper;
- using TEAMModelOS.SDK.Helper.Security.Md5Hash;
- using TEAMModelOS.SDK.Helper.Network.HttpHelper;
- using TEAMModelOS.SDK.Extension.HttpClient.Implements;
- using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
- using TEAMModelOS.SDK.DI;
- namespace TEAMModelOS.SDK.Extension.MessagePush.Implements
- {
- public class SendCloudService : ISendCloudService
- {
- public SmsSendCloud smsSendCloud;
- public ILanguageService _languageService;
- public AzureStorageFactory _azureTableDBRepository;
- public HttpClientSendCloud _httpClientService;
- public SendCloudService(IOptions<SmsSendCloud> _option, ILanguageService languageService , AzureStorageFactory azureTableDBRepository , HttpClientSendCloud httpClientService )
- {
- _azureTableDBRepository = azureTableDBRepository;
- _languageService = languageService;
- smsSendCloud = _option.Value;
- _httpClientService = httpClientService;
- }
- public List<Dictionary<string, string>> Languages { get; set; } = new List<Dictionary<string, string>>
- {
- new Dictionary<string, string> { { "name", "简体中文" }, { "code","CHS" } },
- new Dictionary<string, string> { { "name", "繁体中文" }, { "code","CHT" } },
- new Dictionary<string, string> { { "name", "英语" }, { "code","EN" } } };
- public List<Dictionary<string, string>> BizCodes { get; set; } = new List<Dictionary<string, string>>
- {
- //new Dictionary<string, string> { { "name", "验证码业务" }, { "code", "Captcha" } },
- new Dictionary<string, string> { { "name", "报名通知业务" }, { "code", "SignUp" } }
- };
- public async Task<List<SmsConfig>> SaveOrUpdateSmsConfig(List<SmsConfig> configs) {
- return await _azureTableDBRepository.SaveOrUpdateAll<SmsConfig>(configs);
- }
- public async Task<List<SmsConfig>> InitSmsConfig(string BizNum)
- {
- List<SmsConfig> smsConfigs = new List<SmsConfig>() ;
- List<SmsConfig> SaveSmsConfigs = new List<SmsConfig>();
- foreach (Dictionary<string, string> BizCode in BizCodes) {
- foreach (Dictionary<string, string> Language in Languages) {
- Dictionary<string, object> dict = new Dictionary<string, object>
- {
- { "BizNum", BizNum },{ "BizCode", BizCode["code"] }, { "Language", Language["code"] } ,
- };
- List<SmsConfig> smsConfig = await _azureTableDBRepository.FindListByDict<SmsConfig>(dict);
- if (smsConfig.IsNotEmpty() && smsConfig[0].RowKey != null)
- {
- smsConfigs.Add(smsConfig[0]);
- }
- else {
- DateTimeOffset now = DateTimeOffset.Now;
- SmsConfig SaveSmsConfig = new SmsConfig {
- RowKey = BizNum + "-" + BizCode["code"] + "-" + Language["code"],
- PartitionKey = BizNum,
- Language = Language["code"],
- LanguageName = Language["name"],
- BizCode = BizCode["code"],
- BizName = BizCode["name"],
- BizNum = BizNum,
- Template = null,
- Timestamp= now
- };
- SaveSmsConfigs.Add(SaveSmsConfig);
- smsConfigs.Add(SaveSmsConfig);
- }
- }
- }
- if (SaveSmsConfigs.Count > 0) {
- await _azureTableDBRepository.SaveOrUpdateAll<SmsConfig>(SaveSmsConfigs);
- }
- return smsConfigs;
- }
- /// <summary>
- /// https://sendcloud.kf5.com/posts/view/1074678/
- /// </summary>
- /// <param name="BizCode"></param>
- /// <param name="CountryCode"></param>
- /// <param name="phone"></param>
- /// <param name="vars"></param>
- /// <returns></returns>
- public async Task<SendCloudResponse> SendSmsByBizCode(string BizNum , string BizCode, int CountryCode, string phone, Dictionary<string, string> vars = null)
- {
- SmsCountryCode code = null;
- bool flag = _languageService.GetSmsLanguage().TryGetValue(CountryCode + "", out code);
- string SmsLang = "EN";
- int templateId = 0;
- if (flag)
- {
- SmsLang = code.SmsLang;
- }
- Dictionary<string, object> dict = new Dictionary<string, object>
- {
- { "BizNum", BizNum },{ "BizCode", BizCode }, { "Language", SmsLang } ,
- };
- List<SmsConfig> smsConfig = await _azureTableDBRepository.FindListByDict<SmsConfig>(dict);
- if (smsConfig.IsNotEmpty() && smsConfig[0].RowKey != null)
- {
-
- //默认调用英文,不管是否包含,发送时会去处理相关信息
- int.TryParse(smsConfig[0].Template ,out templateId);
- }
- //else {
- // throw new BizException("");
- // // templateId = smsSendCloud.SmsCode[code.SmsLang];
- //}
- int msgType = 0;
- if (CountryCode != 86)
- {
- msgType = 2;
- return await SendSms(templateId, "00" + CountryCode + phone, vars, msgType);
- }
- else
- {
- return await SendSms(templateId, phone, vars, msgType);
- }
-
- }
- public async Task<SendCloudResponse> SendSms(int templateId, string phone, Dictionary<string, string> vars = null, int msgType = 0)
- {
- List<KeyValuePair<string, string>> paramList = new List<KeyValuePair<string, string>>
- {
- new KeyValuePair<string, string>("msgType", msgType+""),//0表示短信, 1表示彩信,2表示国际短信, 默认值为0
- new KeyValuePair<string, string>("phone", phone),
- new KeyValuePair<string, string>("smsUser", smsSendCloud.SmsUser),
- new KeyValuePair<string, string>("templateId", templateId+""),
- };
- if (vars != null)
- {
- paramList.Add(new KeyValuePair<string, string>("vars", JsonNetHelper.ToJson(vars)));
- }
- var param_str = "";
- foreach (var param in paramList)
- {
- param_str += param.Key.ToString() + "=" + param.Value.ToString() + "&";
- }
- string sign_str = smsSendCloud.SmsKey + "&" + param_str + smsSendCloud.SmsKey;
- string sign = Md5Hash.Encrypt(sign_str);
- paramList.Add(new KeyValuePair<string, string>("signature", sign));
- string result = await _httpClientService.HttpPostAsync(smsSendCloud.SmsUrl, paramList);
- return JsonNetHelper.FromJson<SendCloudResponse>(result);
- }
- }
- }
|