123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Net;
- using System.Text;
- using GxPress.Common.Encrypt;
- using GxPress.Common.Sms;
- using GxPress.Service.Interface.Sms;
- using Newtonsoft.Json;
- using GxPress.Repository.Interface.SmsLog;
- using System.Threading.Tasks;
- namespace GxPress.Service.Implement.Sms
- {
- public class SmsService : ISmsService
- {
- private readonly ISmsLogRepository smsLogRepository;
- public SmsService(ISmsLogRepository smsLogRepository)
- {
- this.smsLogRepository = smsLogRepository;
- }
- /// <summary>
- /// 广西短信模板
- /// </summary>
- /// <param name="phone"></param>
- /// <param name="code"></param>
- /// <returns></returns>
- public async Task<bool> SendSmsTemplate(List<string> phones, string templateId, int sourceId=0, int sourceType=0)
- {
- var smsLogs = new List<Entity.SmsLog>();
- foreach (var item in phones)
- {
- smsLogs.Add(new Entity.SmsLog
- {
- Phone = item,
- IsSend = false,
- SourceId = sourceId,
- SourceType = sourceType,
- Message = "发送中"
- });
- }
- await smsLogRepository.InsertAsync(smsLogs);
- var url = "http://112.35.10.201:5992/sms/tmpsubmit";
- try
- {
- var ecName = "广西出版传媒集团有限公司";
- var appId = "gxcbcm";
- var sign = "FOdugjqlG";
- var secretKey = "gxcbcm";
- var phone = Common.Tools.StringUtils.ObjectCollectionToString(phones);
- var paramsValue = "[\\\"\\\"]";
- var paramsValueMd5 = "[\"\"]";
- var addSerial = "";
- var md5 = EncryptProvider.Md5($"{ecName}{appId}{secretKey}{templateId}{phone}{paramsValueMd5}{sign}{addSerial}").ToLower();
- var json = "{\"ecName\":\"" + ecName + "\", \"apId\":\"" + appId + "\", \"templateId\":\"" + templateId + "\", \"mobiles\":\"" + phone + "\",\"params\":\"" + paramsValue + "\", \"sign\":\"" + sign + "\", \"addSerial\":\"\", \"mac\":\"" + md5 + "\"}";
- var base64Encode = EncryptProvider.Base64Encode(json);
- byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(base64Encode); //转化
- HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(url));
- webReq.Method = "POST";
- webReq.ContentType = "application/x-www-form-urlencoded";
- //webReq.ContentType = "application/json";
- webReq.ContentLength = byteArray.Length;
- Stream newStream = webReq.GetRequestStream();
- newStream.Write(byteArray, 0, byteArray.Length);//写入参数
- newStream.Close();
- HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
- StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
- var ret = sr.ReadToEnd();
- sr.Close();
- response.Close();
- newStream.Close();
- var success = JsonConvert.DeserializeObject<SmsSuccess>(ret);
- smsLogs = new List<Entity.SmsLog>();
- foreach (var item in phones)
- {
- smsLogs.Add(new Entity.SmsLog
- {
- Phone = item,
- IsSend = success.Success,
- SourceId = sourceId,
- SourceType = sourceType,
- Message = "发送成功"
- });
- }
- await smsLogRepository.InsertAsync(smsLogs);
- return success.Success;
- }
- catch (Exception ex)
- {
- smsLogs = new List<Entity.SmsLog>();
- foreach (var item in phones)
- {
- smsLogs.Add(new Entity.SmsLog
- {
- Phone = item,
- IsSend = false,
- SourceId = sourceId,
- SourceType = sourceType,
- Message = "发送失败"
- });
- }
- await smsLogRepository.InsertAsync(smsLogs);
- throw new Common.Exceptions.BusinessException(ex.Message);
- }
- }
- }
- }
|