123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Configuration;
- using NPOI.DDF;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using XYY.Api.WarningNotice.WarningNoticeService;
- using XYY.Common.Standard;
- using XYY.Data.Standard;
- using XYY.Data.Standard.WX;
- using XYY.Service.Standard.DingDing;
- using XYY.Service.Strandard.EMailService;
- namespace XYY.Api.WarningNotice.Controllers
- {
- [Route("[controller]/[action]")]
- [ApiController]
- public class WarningNoticeController : ControllerBase
- {
- IEmailService emailService;
- IWXBindRepository wXBindRepository;
- IDingDingService _dingDingService;
- IWebHook wxWebHook;
- IUserInfoRepository userInfoRepository;
- public WarningNoticeController(IDingDingService dingDingService, IWebHook wxWebHook, IWXBindRepository wXBindRepository, IEmailService emailService, IUserInfoRepository userInfoRepository)
- {
- _dingDingService = dingDingService;
- this.wxWebHook = wxWebHook;
- this.wXBindRepository = wXBindRepository;
- this.emailService = emailService;
- this.userInfoRepository = userInfoRepository;
- }
- /// <summary>
- /// 以文本方式通知
- /// </summary>
- /// <param name="target">通知内容</param>
- /// <returns></returns>
- public async Task<IActionResult> NoticeText(TextMessageNoticeTarget target)
- {
- var result = await Task.Run<IActionResult>(async () =>
- {
- if (target.ServiceType == "钉钉")
- {
- DingTalkWarningNoticeService dingTalkWarningNoticeService = new DingTalkWarningNoticeService(_dingDingService);
- dingTalkWarningNoticeService.NoticeText(target);
- }
- else if (target.ServiceType == "微信")
- {
- string hook = target.Hook ?? "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=5eb4675a-773a-40a0-9bad-745501122580";
- QYWXWebHook webHook = new QYWXWebHook(hook);
- if (target.UserId != null && target.UserId.Length > 0)
- {
- //通知用户时,我们取一次关联数据
- var list = await userInfoRepository.QueryAsync<Tuple<string, string>>(@"SELECT a.WXUserId as Item1,b.NickName as Item2
- FROM [xyy_wms].[dbo].[QWUser_Info](nolock) a join User_Info(nolock) b on a.UserId=b.Id
- where NickName in @UserId", new { UserId = target.UserId });
- List<string> wxUsers = new List<string>();
- foreach (var item in target.UserId)
- {
- if (!list.Any(x => x.Item2 == item))
- throw new Exception($"在微信通讯录中,未找到用户{item},请检查系统内有无该用户信息,企微通讯录内的姓名是否错误");
- else
- {
- wxUsers.Add(list.First(x => x.Item2 == item).Item1);
- }
- }
- webHook.SendMarkdownAt(target.TargetName, target.Content, wxUsers.ToArray());
- if (!string.IsNullOrEmpty(target.FileStringBase64))
- {
- var file = await webHook.PostFileAsBase64(target.FileStringBase64, target.FileName);
- webHook.SendFile(file);
- }
- }
- else
- wxWebHook.SendMarkdown(target.TargetName, target.Content, target.TargetName, 2 * 60);
- }
- else
- {
- return new OkObjectResult(new { IsSuccess = false, ErrMsg = "未实现该服务" });
- }
- return new OkObjectResult(new { IsSuccess = true, ErrMsg = "" });
- });
- return result;
- }
- public async Task<IActionResult> NoticeTextToUser(TextMessageNoticeTarget target)
- {
- var result = await Task.Run<IActionResult>(() =>
- {
- if (target.ServiceType == "微信")
- {
- QYWXWebHook webHook = new QYWXWebHook("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=5eb4675a-773a-40a0-9bad-745501122580");
- webHook.SendText(target.TargetName, target.Content, target.UserId);
- }
- else
- {
- return new OkObjectResult(new { IsSuccess = false, ErrMsg = "未实现该服务" });
- }
- return new OkObjectResult(new { IsSuccess = true, ErrMsg = "" });
- });
- return result;
- }
- public async Task<IActionResult> NoticeTextToXYYCustomer(TextMessageNoticeTarget target)
- {
- if (target.CustomerId == null || target.CustomerId.Count() == 0)
- throw new Exception("传入客户ID必须不为空");
- var user = await wXBindRepository.GetWXUserId(target.UserId.Select(x => int.Parse(x)).ToArray());
- string wxid = user.First().WXUserId;
- int[] customerIds = null;
- try
- {
- customerIds = target.CustomerId.Select(x => int.Parse(x)).ToArray();
- }
- catch (Exception ex)
- {
- throw new Exception("传入客户ID必须为不为空的纯数字数组");
- }
- var userInfo = await wXBindRepository.GetCustomerWXUserId(customerIds);
- var result = await Task.Run<IActionResult>(() =>
- {
- if (target.ServiceType == "微信")
- {
- QYWXWebHook webHook = new QYWXWebHook("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=5eb4675a-773a-40a0-9bad-745501122580");
- webHook.SendMarkdownToCustomer(target.TargetName, target.Content, userInfo.Select(x => x.WXUserId).ToArray(), wxid);
- }
- else
- {
- return new OkObjectResult(new { IsSuccess = false, ErrMsg = "未实现该服务" });
- }
- return new OkObjectResult(new { IsSuccess = true, ErrMsg = "" });
- });
- return result;
- }
- public async Task<IActionResult> NoticeTextToXYYUser(TextMessageNoticeTarget target)
- {
- if (target.UserId == null || target.UserId.Count() == 0)
- throw new Exception("传入用户ID必须不为空");
- int[] userIds = null;
- try
- {
- userIds = target.UserId.Select(x => int.Parse(x)).ToArray();
- }
- catch (Exception ex)
- {
- throw new Exception("传入用户ID必须为不为空的纯数字数组");
- }
- var userInfo = await wXBindRepository.GetWXUserId(userIds);
- var result = await Task.Run<IActionResult>(() =>
- {
- if (target.ServiceType == "微信")
- {
- QYWXWebHook webHook = new QYWXWebHook("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=5eb4675a-773a-40a0-9bad-745501122580");
- webHook.SendMarkdown(target.TargetName, target.Content, userInfo.Select(x => x.WXUserId).ToArray());
- }
- else
- {
- return new OkObjectResult(new { IsSuccess = false, ErrMsg = "未实现该服务" });
- }
- return new OkObjectResult(new { IsSuccess = true, ErrMsg = "" });
- });
- return result;
- }
- public async Task<IActionResult> NoticeEmail(EMailContent config)
- {
- try
- {
- emailService.Send(new List<string> { config.To }, config.Title, config.HTMLBody, true, !string.IsNullOrEmpty(config.CC) ? config.CC.Split(',').ToList() : null);
- return new OkObjectResult(new { IsSuccess = true, Message = "" });
- }
- catch (Exception ex)
- {
- return new OkObjectResult(new { IsSuccess = false, Message = ex.Message });
- }
- }
- /// <summary>
- /// 以文本方式通知
- /// </summary>
- /// <param name="target">通知内容</param>
- /// <returns></returns>
- public async Task<IActionResult> NoticeCard(ActionCardNoticeTarget target)
- {
- if (target.ServiceType == "钉钉")
- {
- DingTalkWarningNoticeService dingTalkWarningNoticeService = new DingTalkWarningNoticeService(_dingDingService);
- await dingTalkWarningNoticeService.NoticeActionCard(target);
- }
- else
- {
- return new OkObjectResult(new { IsSuccess = false, ErrMsg = "未实现该服务" });
- }
- return new OkObjectResult(new { IsSuccess = true, ErrMsg = "" });
- }
- }
- }
|