240 lines
9.9 KiB
C#
240 lines
9.9 KiB
C#
using Hg.Core.Entity;
|
||
using Microsoft.AspNetCore.Razor.TagHelpers;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Security.Cryptography;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using Zxd.Core.Shared.Helpers;
|
||
|
||
namespace Hg.Core.Domain
|
||
{
|
||
internal class CsvrMessageDomain : ICsvrMessageDomain
|
||
{
|
||
private readonly IBaseRepository<ZxdDbContext> _zxdRepository;
|
||
private readonly IRedisManager _redisManager;
|
||
private readonly IConfiguration _configuration;
|
||
private readonly IHttpClient _httpClient;
|
||
private readonly IMapper _mapper;
|
||
private readonly SystemConfig _systemConfig;
|
||
private readonly IInneruserDomain _inneruserDomain;
|
||
private readonly ICacheDomain _cacheDomain;
|
||
private readonly NotificationHub _notificationHub;
|
||
|
||
public CsvrMessageDomain(IBaseRepository<ZxdDbContext> zxdRepository,
|
||
IRedisManager redisManager,
|
||
IConfiguration configuration,
|
||
IMapper mapper,
|
||
IHttpClient httpClient,
|
||
IInneruserDomain inneruserDomain,
|
||
ICacheDomain cacheDomain,
|
||
NotificationHub notificationHub)
|
||
{
|
||
_zxdRepository = zxdRepository;
|
||
_redisManager = redisManager;
|
||
_mapper = mapper;
|
||
_httpClient = httpClient;
|
||
_configuration = configuration;
|
||
_inneruserDomain = inneruserDomain;
|
||
_cacheDomain = cacheDomain;
|
||
_systemConfig = _configuration.GetSection("SystemConfig").Get<SystemConfig>();
|
||
_notificationHub = notificationHub;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 推送 文案审核消息类型推送(比较特殊,需要从配置的角色中找到匹配部门才行)
|
||
/// </summary>
|
||
/// <param name="dto">消息编码</param>
|
||
/// <returns></returns>
|
||
public async Task<bool> PushNewsMsg(PushNewsMsgDto dto)
|
||
{
|
||
try
|
||
{
|
||
if (dto.SendDingDing)
|
||
{
|
||
var configStr = await _cacheDomain.GetValueParameter("DingtalkConfig");
|
||
var configs = JsonHelper.FromJson<List<DingtalkConfig>>(configStr);
|
||
var config = configs.Find(x => x.RobotName == "未接电话提醒");
|
||
if (config != null)
|
||
{
|
||
await Dingtalk(config.SecretKey, config.RobotUrl, dto.DingDingContent);
|
||
}
|
||
return true;
|
||
}
|
||
var typemodel = await _zxdRepository.GetRepository<CsvrMessageType>().Query()
|
||
.Where(m => m.Msgcode == dto.Msgcode)
|
||
.FirstOrDefaultAsync();
|
||
if (typemodel == null)
|
||
{
|
||
Log.Error($"编码:{dto.Msgcode}错误!");
|
||
return false;
|
||
}
|
||
Log.Information("获取的数据:" + typemodel.ToJson());
|
||
List<int> sendeidlist = new List<int>();//需要发送的eid
|
||
if (typemodel.Sendtype == "all")
|
||
{
|
||
sendeidlist = await _zxdRepository.GetRepository<BAS_INNERUSER>().Query()
|
||
.Where(m => m.ISDISMISS == 0 && m.ISHIDE == 0).Select(m => m.EID)
|
||
.ToListAsync();
|
||
}
|
||
else if (typemodel.Sendtype == "eid")
|
||
{
|
||
List<int> eids = new List<int>();//需要发送的eid
|
||
string[] models = dto.Eid.Split(',');
|
||
foreach (var item in models)
|
||
{
|
||
if (string.IsNullOrEmpty(item))
|
||
continue;
|
||
int meid = 0;
|
||
if (int.TryParse(item, out meid))
|
||
{
|
||
eids.Add(meid);
|
||
}
|
||
}
|
||
sendeidlist = await _zxdRepository.GetRepository<BAS_INNERUSER>().Query()
|
||
.Where(m => m.ISDISMISS == 0 && m.ISHIDE == 0 && eids.Contains(m.EID)).Select(m => m.EID)
|
||
.ToListAsync();
|
||
}
|
||
else if (typemodel.Sendtype == "role")
|
||
{
|
||
string[] models = typemodel.SendConfig.Split(',');
|
||
|
||
sendeidlist = (from a in _zxdRepository.GetRepository<BAS_INNERUSER>().Query()
|
||
join b in _zxdRepository.GetRepository<BAS_INNERUSERROLE>().Query() on a.PKID equals b.INNERUSERID
|
||
join c in _zxdRepository.GetRepository<BAS_ROLE>().Query() on b.ROLEID equals c.ROLEID
|
||
join d in _zxdRepository.GetRepository<Bas_Role_Com>().Query() on c.ROLEID equals d.roleid
|
||
where models.Contains(c.CODE)
|
||
where a.ISDISMISS == 0
|
||
where a.ISHIDE == 0
|
||
where d.companycode.Contains(dto.Companycode)
|
||
select a.EID
|
||
).ToList();
|
||
}
|
||
Log.Information("获取的数据:" + sendeidlist.ToJson());
|
||
if (sendeidlist != null && sendeidlist.Count > 0)
|
||
{
|
||
var list = new List<CsvrMessage>();
|
||
foreach (var item in sendeidlist)
|
||
{
|
||
list.Add(new CsvrMessage()
|
||
{
|
||
Ctime = DateTime.Now,
|
||
Eid = item,
|
||
Fromer = dto.Fromer,
|
||
Islook = 0,
|
||
Message = dto.Title,
|
||
Msgcode = dto.Msgcode,
|
||
Param = dto.Param
|
||
});
|
||
}
|
||
list = await _zxdRepository.GetRepository<CsvrMessage>().BatchInsertAsync(list);
|
||
|
||
// 消息推送
|
||
foreach (var item in list)
|
||
{
|
||
await _notificationHub.Send(item, typemodel.Title);
|
||
}
|
||
}
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
Log.Error(e.ToString());
|
||
}
|
||
return true;
|
||
}
|
||
|
||
private async Task Dingtalk(string? secretkey, string? postUrl, string? content)
|
||
{
|
||
try
|
||
{
|
||
var timeStamp = GetTimeStamp();
|
||
var sign = GetSign(timeStamp, secretkey);
|
||
var dingtalk = $"{postUrl}×tamp={timeStamp}&sign={sign}";
|
||
var postBody = new
|
||
{
|
||
at = new
|
||
{
|
||
atMobiles = new List<string>(),
|
||
atUserIds = new List<string>(),
|
||
isAtAll = false,
|
||
},
|
||
text = new
|
||
{
|
||
content
|
||
},
|
||
msgtype = "text"
|
||
};
|
||
var res = await _httpClient.PostAsync(dingtalk, postBody);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Log.Error(ex, string.Concat("【钉钉通知】dingtalk(),:content", content, ex.Message, ex.StackTrace));
|
||
}
|
||
}
|
||
|
||
private static string GetSign(string timeStamp, string secret)
|
||
{
|
||
string stringToSign = timeStamp + "\n" + secret;
|
||
var encoding = new System.Text.ASCIIEncoding();
|
||
byte[] keyByte = encoding.GetBytes(secret);
|
||
byte[] messageBytes = encoding.GetBytes(stringToSign);
|
||
using (var hmacsha256 = new HMACSHA256(keyByte))
|
||
{
|
||
byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
|
||
return System.Web.HttpUtility.UrlEncode(Convert.ToBase64String(hashmessage), Encoding.UTF8);
|
||
}
|
||
}
|
||
|
||
private static string GetTimeStamp()
|
||
{
|
||
TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
|
||
long shijianchuo = ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000);
|
||
return shijianchuo.ToString();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取用户消息数量
|
||
/// </summary>
|
||
/// <param name="eid"></param>
|
||
/// <returns></returns>
|
||
public async Task<int> GetMessageCount(int eid)
|
||
{
|
||
var nowtime = DateTime.Now;
|
||
var time = new DateTime(nowtime.Year, nowtime.Month, nowtime.Day).AddDays(-5);//未读5天数据
|
||
var count = await _zxdRepository.GetRepository<CsvrMessage>().Query()
|
||
.Where(m => m.Eid == eid && m.Islook == 0 && m.Ctime >= time)
|
||
.OrderBy(M => M.Id).CountAsync();
|
||
return count;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取用户消息
|
||
/// </summary>
|
||
/// <param name="eid"></param>
|
||
/// <param name="msgcode"></param>
|
||
/// <returns></returns>
|
||
public async Task<CsvrMessage> GetNotificationByEid(int eid, string msgcode)
|
||
{
|
||
var nowtime = DateTime.Now;
|
||
var time = new DateTime(nowtime.Year, nowtime.Month, nowtime.Day).AddDays(-5);//未读5天数据
|
||
var csvrMessage = await _zxdRepository.GetRepository<CsvrMessage>().Query()
|
||
.Where(m => m.Eid == eid && m.Msgcode == msgcode && m.Islook == 0 && m.Ctime >= time)
|
||
.OrderByDescending(M => M.Id).FirstOrDefaultAsync();
|
||
return csvrMessage;
|
||
}
|
||
|
||
public class DingtalkConfig
|
||
{
|
||
[JsonPropertyName("robotName")]
|
||
public string? RobotName { get; set; }
|
||
|
||
[JsonPropertyName("robotUrl")]
|
||
public string? RobotUrl { get; set; }
|
||
|
||
[JsonPropertyName("secretKey")]
|
||
public string? SecretKey { get; set; }
|
||
}
|
||
}
|
||
}
|