SmsService.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using System.Text;
  6. using GxPress.Common.Encrypt;
  7. using GxPress.Common.Sms;
  8. using GxPress.Service.Interface.Sms;
  9. using Newtonsoft.Json;
  10. using GxPress.Repository.Interface.SmsLog;
  11. using System.Threading.Tasks;
  12. namespace GxPress.Service.Implement.Sms
  13. {
  14. public class SmsService : ISmsService
  15. {
  16. private readonly ISmsLogRepository smsLogRepository;
  17. public SmsService(ISmsLogRepository smsLogRepository)
  18. {
  19. this.smsLogRepository = smsLogRepository;
  20. }
  21. /// <summary>
  22. /// 广西短信模板
  23. /// </summary>
  24. /// <param name="phone"></param>
  25. /// <param name="code"></param>
  26. /// <returns></returns>
  27. public async Task<bool> SendSmsTemplate(List<string> phones, string templateId, int sourceId=0, int sourceType=0)
  28. {
  29. var smsLogs = new List<Entity.SmsLog>();
  30. foreach (var item in phones)
  31. {
  32. smsLogs.Add(new Entity.SmsLog
  33. {
  34. Phone = item,
  35. IsSend = false,
  36. SourceId = sourceId,
  37. SourceType = sourceType,
  38. Message = "发送中"
  39. });
  40. }
  41. await smsLogRepository.InsertAsync(smsLogs);
  42. var url = "http://112.35.10.201:5992/sms/tmpsubmit";
  43. try
  44. {
  45. var ecName = "广西出版传媒集团有限公司";
  46. var appId = "gxcbcm";
  47. var sign = "FOdugjqlG";
  48. var secretKey = "gxcbcm";
  49. var phone = Common.Tools.StringUtils.ObjectCollectionToString(phones);
  50. var paramsValue = "[\\\"\\\"]";
  51. var paramsValueMd5 = "[\"\"]";
  52. var addSerial = "";
  53. var md5 = EncryptProvider.Md5($"{ecName}{appId}{secretKey}{templateId}{phone}{paramsValueMd5}{sign}{addSerial}").ToLower();
  54. var json = "{\"ecName\":\"" + ecName + "\", \"apId\":\"" + appId + "\", \"templateId\":\"" + templateId + "\", \"mobiles\":\"" + phone + "\",\"params\":\"" + paramsValue + "\", \"sign\":\"" + sign + "\", \"addSerial\":\"\", \"mac\":\"" + md5 + "\"}";
  55. var base64Encode = EncryptProvider.Base64Encode(json);
  56. byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(base64Encode); //转化
  57. HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(url));
  58. webReq.Method = "POST";
  59. webReq.ContentType = "application/x-www-form-urlencoded";
  60. //webReq.ContentType = "application/json";
  61. webReq.ContentLength = byteArray.Length;
  62. Stream newStream = webReq.GetRequestStream();
  63. newStream.Write(byteArray, 0, byteArray.Length);//写入参数
  64. newStream.Close();
  65. HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
  66. StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
  67. var ret = sr.ReadToEnd();
  68. sr.Close();
  69. response.Close();
  70. newStream.Close();
  71. var success = JsonConvert.DeserializeObject<SmsSuccess>(ret);
  72. smsLogs = new List<Entity.SmsLog>();
  73. foreach (var item in phones)
  74. {
  75. smsLogs.Add(new Entity.SmsLog
  76. {
  77. Phone = item,
  78. IsSend = success.Success,
  79. SourceId = sourceId,
  80. SourceType = sourceType,
  81. Message = "发送成功"
  82. });
  83. }
  84. await smsLogRepository.InsertAsync(smsLogs);
  85. return success.Success;
  86. }
  87. catch (Exception ex)
  88. {
  89. smsLogs = new List<Entity.SmsLog>();
  90. foreach (var item in phones)
  91. {
  92. smsLogs.Add(new Entity.SmsLog
  93. {
  94. Phone = item,
  95. IsSend = false,
  96. SourceId = sourceId,
  97. SourceType = sourceType,
  98. Message = "发送失败"
  99. });
  100. }
  101. await smsLogRepository.InsertAsync(smsLogs);
  102. throw new Common.Exceptions.BusinessException(ex.Message);
  103. }
  104. }
  105. }
  106. }