齐越消息中心
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

83 lines
2.8 KiB

using BBWYB.Server.Business;
using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json;
using QYMessageCenter.Common.Extensions;
using QYMessageCenter.Common.Http;
using QYMessageCenter.Common.Log;
using QYMessageCenter.Common.Models;
using QYMessageCenter.Model.DB;
using QYMessageCenter.Model.DTO;
using System.Text.RegularExpressions;
using Yitter.IdGenerator;
namespace QYMessageCenter.Business
{
public class MessageBusiness : BaseBusiness, IDenpendency
{
private readonly string Key = "BC-8cf10d7731c8417bae1c437818b7f8b3";
private IMemoryCache memoryCache;
private TimeSpan msgCacheTimeSpan;
public MessageBusiness(IFreeSql fsql, NLogManager nLogManager, IIdGenerator idGenerator, RestApiService restApiService, IMemoryCache memoryCache) : base(fsql, nLogManager, idGenerator, restApiService)
{
this.memoryCache = memoryCache;
this.msgCacheTimeSpan = new TimeSpan(1, 0, 0);
}
/// <summary>
/// 发送消息
/// </summary>
/// <param name="request"></param>
public void Send(SendMessageRequest request)
{
#region 过滤消息中的随机字符
#region PJZS
{
if (request.AppCode == "PJZS")
{
var matchResult = Regex.Match(request.Content, @"(#\w+)\(Solution");
if (matchResult.Success)
{
var replaceStr = matchResult.Groups[1].Value;
request.Content = request.Content.Replace(replaceStr, string.Empty);
}
}
}
#endregion
#endregion
#region 检查消息md5
var md5 = $"{request.AppCode}_{request.TeamId}_{request.ShopId}_{request.CustomTypeCode}_{request.Content}".Md5Encrypt();
if (memoryCache.TryGetValue(md5, out _))
return;
#endregion
var msg = request.Map<QYNotification>();
msg.Id = idGenerator.NewLong();
msg.CreateTime = DateTime.Now;
#region 调用goeasy
var goeasy_httpResult = restApiService.SendRequest("https://rest-hz.goeasy.io/", "v2/pubsub/publish", new
{
appkey = Key,
channel = request.Channel,
content = JsonConvert.SerializeObject(msg)
}, null, HttpMethod.Post);
if (goeasy_httpResult.StatusCode != System.Net.HttpStatusCode.OK)
throw new BusinessException($"消息发送失败 httpcode {goeasy_httpResult.StatusCode} conent {goeasy_httpResult.Content}");
#endregion
#region 保存入库
#endregion
#region 缓存消息
memoryCache.Set(md5, msg, msgCacheTimeSpan);
#endregion
}
}
}