WarningNoticeController.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using Microsoft.AspNetCore.Mvc;
  2. using Microsoft.Extensions.Configuration;
  3. using NPOI.DDF;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using XYY.Api.WarningNotice.WarningNoticeService;
  9. using XYY.Common.Standard;
  10. using XYY.Data.Standard;
  11. using XYY.Data.Standard.WX;
  12. using XYY.Service.Standard.DingDing;
  13. using XYY.Service.Strandard.EMailService;
  14. namespace XYY.Api.WarningNotice.Controllers
  15. {
  16. [Route("[controller]/[action]")]
  17. [ApiController]
  18. public class WarningNoticeController : ControllerBase
  19. {
  20. IEmailService emailService;
  21. IWXBindRepository wXBindRepository;
  22. IDingDingService _dingDingService;
  23. IWebHook wxWebHook;
  24. IUserInfoRepository userInfoRepository;
  25. public WarningNoticeController(IDingDingService dingDingService, IWebHook wxWebHook, IWXBindRepository wXBindRepository, IEmailService emailService, IUserInfoRepository userInfoRepository)
  26. {
  27. _dingDingService = dingDingService;
  28. this.wxWebHook = wxWebHook;
  29. this.wXBindRepository = wXBindRepository;
  30. this.emailService = emailService;
  31. this.userInfoRepository = userInfoRepository;
  32. }
  33. /// <summary>
  34. /// 以文本方式通知
  35. /// </summary>
  36. /// <param name="target">通知内容</param>
  37. /// <returns></returns>
  38. public async Task<IActionResult> NoticeText(TextMessageNoticeTarget target)
  39. {
  40. var result = await Task.Run<IActionResult>(async () =>
  41. {
  42. if (target.ServiceType == "钉钉")
  43. {
  44. DingTalkWarningNoticeService dingTalkWarningNoticeService = new DingTalkWarningNoticeService(_dingDingService);
  45. dingTalkWarningNoticeService.NoticeText(target);
  46. }
  47. else if (target.ServiceType == "微信")
  48. {
  49. string hook = target.Hook ?? "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=5eb4675a-773a-40a0-9bad-745501122580";
  50. QYWXWebHook webHook = new QYWXWebHook(hook);
  51. if (target.UserId != null && target.UserId.Length > 0)
  52. {
  53. //通知用户时,我们取一次关联数据
  54. var list = await userInfoRepository.QueryAsync<Tuple<string, string>>(@"SELECT a.WXUserId as Item1,b.NickName as Item2
  55. FROM [xyy_wms].[dbo].[QWUser_Info](nolock) a join User_Info(nolock) b on a.UserId=b.Id
  56. where NickName in @UserId", new { UserId = target.UserId });
  57. List<string> wxUsers = new List<string>();
  58. foreach (var item in target.UserId)
  59. {
  60. if (!list.Any(x => x.Item2 == item))
  61. throw new Exception($"在微信通讯录中,未找到用户{item},请检查系统内有无该用户信息,企微通讯录内的姓名是否错误");
  62. else
  63. {
  64. wxUsers.Add(list.First(x => x.Item2 == item).Item1);
  65. }
  66. }
  67. webHook.SendMarkdownAt(target.TargetName, target.Content, wxUsers.ToArray());
  68. if (!string.IsNullOrEmpty(target.FileStringBase64))
  69. {
  70. var file = await webHook.PostFileAsBase64(target.FileStringBase64, target.FileName);
  71. webHook.SendFile(file);
  72. }
  73. }
  74. else
  75. wxWebHook.SendMarkdown(target.TargetName, target.Content, target.TargetName, 2 * 60);
  76. }
  77. else
  78. {
  79. return new OkObjectResult(new { IsSuccess = false, ErrMsg = "未实现该服务" });
  80. }
  81. return new OkObjectResult(new { IsSuccess = true, ErrMsg = "" });
  82. });
  83. return result;
  84. }
  85. public async Task<IActionResult> NoticeTextToUser(TextMessageNoticeTarget target)
  86. {
  87. var result = await Task.Run<IActionResult>(() =>
  88. {
  89. if (target.ServiceType == "微信")
  90. {
  91. QYWXWebHook webHook = new QYWXWebHook("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=5eb4675a-773a-40a0-9bad-745501122580");
  92. webHook.SendText(target.TargetName, target.Content, target.UserId);
  93. }
  94. else
  95. {
  96. return new OkObjectResult(new { IsSuccess = false, ErrMsg = "未实现该服务" });
  97. }
  98. return new OkObjectResult(new { IsSuccess = true, ErrMsg = "" });
  99. });
  100. return result;
  101. }
  102. public async Task<IActionResult> NoticeTextToXYYCustomer(TextMessageNoticeTarget target)
  103. {
  104. if (target.CustomerId == null || target.CustomerId.Count() == 0)
  105. throw new Exception("传入客户ID必须不为空");
  106. var user = await wXBindRepository.GetWXUserId(target.UserId.Select(x => int.Parse(x)).ToArray());
  107. string wxid = user.First().WXUserId;
  108. int[] customerIds = null;
  109. try
  110. {
  111. customerIds = target.CustomerId.Select(x => int.Parse(x)).ToArray();
  112. }
  113. catch (Exception ex)
  114. {
  115. throw new Exception("传入客户ID必须为不为空的纯数字数组");
  116. }
  117. var userInfo = await wXBindRepository.GetCustomerWXUserId(customerIds);
  118. var result = await Task.Run<IActionResult>(() =>
  119. {
  120. if (target.ServiceType == "微信")
  121. {
  122. QYWXWebHook webHook = new QYWXWebHook("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=5eb4675a-773a-40a0-9bad-745501122580");
  123. webHook.SendMarkdownToCustomer(target.TargetName, target.Content, userInfo.Select(x => x.WXUserId).ToArray(), wxid);
  124. }
  125. else
  126. {
  127. return new OkObjectResult(new { IsSuccess = false, ErrMsg = "未实现该服务" });
  128. }
  129. return new OkObjectResult(new { IsSuccess = true, ErrMsg = "" });
  130. });
  131. return result;
  132. }
  133. public async Task<IActionResult> NoticeTextToXYYUser(TextMessageNoticeTarget target)
  134. {
  135. if (target.UserId == null || target.UserId.Count() == 0)
  136. throw new Exception("传入用户ID必须不为空");
  137. int[] userIds = null;
  138. try
  139. {
  140. userIds = target.UserId.Select(x => int.Parse(x)).ToArray();
  141. }
  142. catch (Exception ex)
  143. {
  144. throw new Exception("传入用户ID必须为不为空的纯数字数组");
  145. }
  146. var userInfo = await wXBindRepository.GetWXUserId(userIds);
  147. var result = await Task.Run<IActionResult>(() =>
  148. {
  149. if (target.ServiceType == "微信")
  150. {
  151. QYWXWebHook webHook = new QYWXWebHook("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=5eb4675a-773a-40a0-9bad-745501122580");
  152. webHook.SendMarkdown(target.TargetName, target.Content, userInfo.Select(x => x.WXUserId).ToArray());
  153. }
  154. else
  155. {
  156. return new OkObjectResult(new { IsSuccess = false, ErrMsg = "未实现该服务" });
  157. }
  158. return new OkObjectResult(new { IsSuccess = true, ErrMsg = "" });
  159. });
  160. return result;
  161. }
  162. public async Task<IActionResult> NoticeEmail(EMailContent config)
  163. {
  164. try
  165. {
  166. emailService.Send(new List<string> { config.To }, config.Title, config.HTMLBody, true, !string.IsNullOrEmpty(config.CC) ? config.CC.Split(',').ToList() : null);
  167. return new OkObjectResult(new { IsSuccess = true, Message = "" });
  168. }
  169. catch (Exception ex)
  170. {
  171. return new OkObjectResult(new { IsSuccess = false, Message = ex.Message });
  172. }
  173. }
  174. /// <summary>
  175. /// 以文本方式通知
  176. /// </summary>
  177. /// <param name="target">通知内容</param>
  178. /// <returns></returns>
  179. public async Task<IActionResult> NoticeCard(ActionCardNoticeTarget target)
  180. {
  181. if (target.ServiceType == "钉钉")
  182. {
  183. DingTalkWarningNoticeService dingTalkWarningNoticeService = new DingTalkWarningNoticeService(_dingDingService);
  184. await dingTalkWarningNoticeService.NoticeActionCard(target);
  185. }
  186. else
  187. {
  188. return new OkObjectResult(new { IsSuccess = false, ErrMsg = "未实现该服务" });
  189. }
  190. return new OkObjectResult(new { IsSuccess = true, ErrMsg = "" });
  191. }
  192. }
  193. }