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 TEAMModelOS.SDK.Module.AzureTable.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; namespace TEAMModelOS.SDK.Extension.MessagePush.Implements { public class SendCloudService : ISendCloudService { public SmsSendCloud smsSendCloud; public ILanguageService _languageService; public IAzureTableDBRepository _azureTableDBRepository; public HttpClientSendCloud _httpClientService; public SendCloudService(IOptions _option, ILanguageService languageService , IAzureTableDBRepository azureTableDBRepository , HttpClientSendCloud httpClientService ) { _azureTableDBRepository = azureTableDBRepository; _languageService = languageService; smsSendCloud = _option.Value; _httpClientService = httpClientService; } public List> Languages { get; set; } = new List> { new Dictionary { { "name", "简体中文" }, { "code","CHS" } }, new Dictionary { { "name", "繁体中文" }, { "code","CHT" } }, new Dictionary { { "name", "英语" }, { "code","EN" } } }; public List> BizCodes { get; set; } = new List> { //new Dictionary { { "name", "验证码业务" }, { "code", "Captcha" } }, new Dictionary { { "name", "报名通知业务" }, { "code", "SignUp" } } }; public async Task> SaveOrUpdateSmsConfig(List configs) { return await _azureTableDBRepository.SaveOrUpdateAll(configs); } public async Task> InitSmsConfig(string BizNum) { List smsConfigs = new List() ; List SaveSmsConfigs = new List(); foreach (Dictionary BizCode in BizCodes) { foreach (Dictionary Language in Languages) { Dictionary dict = new Dictionary { { "BizNum", BizNum },{ "BizCode", BizCode["code"] }, { "Language", Language["code"] } , }; SmsConfig smsConfig = await _azureTableDBRepository.FindOneByDict(dict); if (smsConfig != null && smsConfig.RowKey != null) { smsConfigs.Add(smsConfig); } 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(SaveSmsConfigs); } return smsConfigs; } /// /// https://sendcloud.kf5.com/posts/view/1074678/ /// /// /// /// /// /// public async Task SendSmsByBizCode(string BizNum , string BizCode, int CountryCode, string phone, Dictionary 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 dict = new Dictionary { { "BizNum", BizNum },{ "BizCode", BizCode }, { "Language", SmsLang } , }; SmsConfig smsConfig = await _azureTableDBRepository.FindOneByDict(dict); if (smsConfig != null && smsConfig.RowKey != null ) { //默认调用英文,不管是否包含,发送时会去处理相关信息 int.TryParse(smsConfig.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 SendSms(int templateId, string phone, Dictionary vars = null, int msgType = 0) { List> paramList = new List> { new KeyValuePair("msgType", msgType+""),//0表示短信, 1表示彩信,2表示国际短信, 默认值为0 new KeyValuePair("phone", phone), new KeyValuePair("smsUser", smsSendCloud.SmsUser), new KeyValuePair("templateId", templateId+""), }; if (vars != null) { paramList.Add(new KeyValuePair("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("signature", sign)); string result = await _httpClientService.HttpPostAsync(smsSendCloud.SmsUrl, paramList); return JsonNetHelper.FromJson(result); } } }