新增删除修改通知后,会向当前登录的用户组发送通知,修改通知后,如果该通知之前被用户已读过,则将其重新置于未读状态
This commit is contained in:
@@ -10,7 +10,7 @@ namespace ZR.ServiceCore.Model
|
||||
/// </summary>
|
||||
[SugarTable("sys_notice", "通知公告表")]
|
||||
[Tenant(0)]
|
||||
public class SysNotice : SysBase
|
||||
public class SysNotice
|
||||
{
|
||||
/// <summary>
|
||||
/// 公告ID
|
||||
@@ -38,5 +38,36 @@ namespace ZR.ServiceCore.Model
|
||||
/// </summary>
|
||||
[SugarColumn(DefaultValue = "0", ExtendedAttribute = ProteryConstant.NOTNULL)]
|
||||
public int Status { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 备注
|
||||
/// </summary>
|
||||
public string Remark { get; set; }
|
||||
|
||||
[SugarColumn(IsOnlyIgnoreUpdate = true)]
|
||||
[JsonProperty(propertyName: "CreateBy")]
|
||||
public long Create_by { get; set; }
|
||||
|
||||
[SugarColumn(IsOnlyIgnoreUpdate = true)]
|
||||
[JsonProperty(propertyName: "CreateName")]
|
||||
public string Create_name { get; set; }
|
||||
|
||||
[JsonProperty(propertyName: "CreateTime")]
|
||||
public DateTime Create_time { get; set; } = DateTime.Now;
|
||||
|
||||
[JsonIgnore]
|
||||
[JsonProperty(propertyName: "UpdateBy")]
|
||||
[SugarColumn(IsOnlyIgnoreInsert = true)]
|
||||
public long Update_by { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
[JsonProperty(propertyName: "UpdateName")]
|
||||
[SugarColumn(IsOnlyIgnoreInsert = true)]
|
||||
public string Update_name { get; set; }
|
||||
|
||||
//[JsonIgnore]
|
||||
[SugarColumn(IsOnlyIgnoreInsert = true)]
|
||||
[JsonProperty(propertyName: "UpdateTime")]
|
||||
public DateTime? Update_time { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -10,4 +10,15 @@ public class SysNoticeLog
|
||||
public long UserId { get; set; }
|
||||
|
||||
public string Status { get; set; }
|
||||
|
||||
public SysNoticeLog()
|
||||
{
|
||||
}
|
||||
|
||||
public SysNoticeLog(long noticeId, long userId, string status)
|
||||
{
|
||||
NoticeId = noticeId;
|
||||
UserId = userId;
|
||||
Status = status;
|
||||
}
|
||||
}
|
||||
@@ -20,24 +20,29 @@ namespace ZR.ServiceCore.Signalr
|
||||
public class MessageHub : Hub
|
||||
{
|
||||
//创建用户集合,用于存储所有链接的用户数据
|
||||
public static readonly List<OnlineUsers> onlineClients = new();
|
||||
public static List<OnlineUsers> users = new();
|
||||
public static readonly List<OnlineUsers> OnlineClients = new();
|
||||
public static List<OnlineUsers> Users = new();
|
||||
//private readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
private readonly ISysNoticeService SysNoticeService;
|
||||
private readonly ISysNoticeService _sysNoticeService;
|
||||
private readonly ISysNoticeLogService _sysNoticeLogService;
|
||||
|
||||
public MessageHub(ISysNoticeService noticeService, ISysNoticeLogService sysNoticeLogService)
|
||||
{
|
||||
SysNoticeService = noticeService;
|
||||
_sysNoticeService = noticeService;
|
||||
_sysNoticeLogService = sysNoticeLogService;
|
||||
}
|
||||
|
||||
private ApiResult SendNotice()
|
||||
{
|
||||
var result = SysNoticeService.GetSysNotices();
|
||||
var result = _sysNoticeService.GetSysNotices();
|
||||
|
||||
return new ApiResult(200, "success", result);
|
||||
}
|
||||
|
||||
private ApiResult SendNotice(object notice)
|
||||
{
|
||||
return new ApiResult(200, "success", notice);
|
||||
}
|
||||
|
||||
#region 客户端连接
|
||||
|
||||
@@ -45,7 +50,7 @@ namespace ZR.ServiceCore.Signalr
|
||||
/// 客户端连接的时候调用
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public override Task OnConnectedAsync()
|
||||
public override async Task OnConnectedAsync()
|
||||
{
|
||||
var context = App.HttpContext;
|
||||
var name = HttpContextExtension.GetName(context);
|
||||
@@ -60,8 +65,8 @@ namespace ZR.ServiceCore.Signalr
|
||||
|
||||
long userid = HttpContextExtension.GetUId(context);
|
||||
string uuid = device + userid + ip;
|
||||
var user = onlineClients.Any(u => u.ConnnectionId == Context.ConnectionId);
|
||||
var user2 = onlineClients.Any(u => u.Uuid == uuid);
|
||||
var user = OnlineClients.Any(u => u.ConnnectionId == Context.ConnectionId);
|
||||
var user2 = OnlineClients.Any(u => u.Uuid == uuid);
|
||||
|
||||
//判断用户是否存在,否则添加集合!user2 && !user &&
|
||||
if (!user2 && !user && Context.User.Identity.IsAuthenticated)
|
||||
@@ -73,47 +78,43 @@ namespace ZR.ServiceCore.Signalr
|
||||
Platform = from,
|
||||
ClientId = clientId ?? Context.ConnectionId
|
||||
};
|
||||
onlineClients.Add(onlineUser);
|
||||
Log.WriteLine(msg: $"{name},{Context.ConnectionId}连接服务端success,当前已连接{onlineClients.Count}个");
|
||||
OnlineClients.Add(onlineUser);
|
||||
Log.WriteLine(msg: $"{name},{Context.ConnectionId}连接服务端success,当前已连接{OnlineClients.Count}个");
|
||||
//Clients.All.SendAsync("welcome", $"欢迎您:{name},当前时间:{DateTime.Now}");
|
||||
|
||||
var noticeRes = (List<SysNotice>) SendNotice()[ApiResult.DATA_TAG];
|
||||
var notifications = _sysNoticeLogService.Queryable()
|
||||
// 获取当前在线用户的通知日志记录id
|
||||
var unreadAndReadNoticeIds = await _sysNoticeLogService.Queryable()
|
||||
.Where(it => it.UserId == userid)
|
||||
.ToList();
|
||||
var unreadNotificationIds = notifications
|
||||
.Where(it => it.Status == SysNoticeLogStatus.Unread)
|
||||
.Select(it => it.NoticeId)
|
||||
.ToList();
|
||||
var readNotificationIds = notifications
|
||||
.Where(it => it.Status == SysNoticeLogStatus.Read)
|
||||
.Select(it => it.NoticeId)
|
||||
.ToList();
|
||||
var unreadAndReadNoticeIds = unreadNotificationIds
|
||||
.Union(readNotificationIds).ToList();
|
||||
.ToListAsync();
|
||||
|
||||
// 当前在线用户的日志通知记录里如果有不存在的通知记录则添加未读记录
|
||||
foreach (var notice in noticeRes.Select(it => it.NoticeId).ToList().Except(unreadAndReadNoticeIds))
|
||||
{
|
||||
_sysNoticeLogService.Insertable(new SysNoticeLog
|
||||
await _sysNoticeLogService.Insertable(new SysNoticeLog
|
||||
{
|
||||
NoticeId = notice,
|
||||
UserId = userid,
|
||||
Status = SysNoticeLogStatus.Unread
|
||||
}).ExecuteCommand();
|
||||
}).ExecuteReturnSnowflakeIdAsync();
|
||||
}
|
||||
|
||||
foreach (var notice in unreadAndReadNoticeIds.Except(noticeRes.Select(it => it.NoticeId).ToList()))
|
||||
{
|
||||
_sysNoticeLogService.Deleteable()
|
||||
.Where(it => it.UserId == userid && it.NoticeId == notice)
|
||||
.ExecuteCommand();
|
||||
}
|
||||
var newUnReadNotificationIds = _sysNoticeLogService
|
||||
.Queryable().Where(it => it.Status == SysNoticeLogStatus.Unread && it.UserId == userid)
|
||||
// 当前在线用户的日志通知记录里如果有冗余的通知记录则删除已读记录
|
||||
// foreach (var notice in unreadAndReadNoticeIds.Except(noticeRes.Select(it => it.NoticeId).ToList()))
|
||||
// {
|
||||
// _sysNoticeLogService.Deleteable()
|
||||
// .Where(it => it.UserId == userid && it.NoticeId == notice)
|
||||
// .ExecuteCommand();
|
||||
// }
|
||||
var newUnReadNotificationIdsAndReadNotifications = await _sysNoticeLogService.Queryable()
|
||||
.Where(it => it.UserId == userid)
|
||||
.ToListAsync();
|
||||
var newUnReadNotificationIds = newUnReadNotificationIdsAndReadNotifications
|
||||
.Where(it => it.Status == SysNoticeLogStatus.Unread)
|
||||
.Select(it => it.NoticeId)
|
||||
.ToList();
|
||||
var newReadNotificationIds = _sysNoticeLogService
|
||||
.Queryable().Where(it => it.Status == SysNoticeLogStatus.Read && it.UserId == userid)
|
||||
var newReadNotificationIds = newUnReadNotificationIdsAndReadNotifications
|
||||
.Where(it => it.Status == SysNoticeLogStatus.Read)
|
||||
.Select(it => it.NoticeId)
|
||||
.ToList();
|
||||
var config = new TypeAdapterConfig();
|
||||
@@ -123,7 +124,11 @@ namespace ZR.ServiceCore.Signalr
|
||||
newUnReadNotificationIds.Contains(it.NoticeId)).ToList().Adapt<List<SysNoticeDto>>(config);
|
||||
var newReadNotifications = noticeRes.Where(it =>
|
||||
newReadNotificationIds.Contains(it.NoticeId)).ToList().Adapt<List<SysNoticeDto>>(config);
|
||||
Clients.Caller.SendAsync(HubsConstant.MoreNotice, newUnReadNotifications, newReadNotifications);
|
||||
await Clients.Caller.SendAsync(HubsConstant.MoreNotice, SendNotice(new
|
||||
{
|
||||
unReadNotifications = newUnReadNotifications,
|
||||
readNotifications = newReadNotifications
|
||||
}));
|
||||
// Clients.Caller.SendAsync(HubsConstant.MoreNotice, SendNotice());
|
||||
// Clients.Caller.SendAsync(HubsConstant.ConnId, onlineUser.ConnnectionId);
|
||||
}
|
||||
@@ -131,7 +136,7 @@ namespace ZR.ServiceCore.Signalr
|
||||
if (userInfo == null)
|
||||
{
|
||||
userInfo = new OnlineUsers() { Userid = userid, Name = name, LoginTime = DateTime.Now };
|
||||
users.Add(userInfo);
|
||||
Users.Add(userInfo);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -140,23 +145,22 @@ namespace ZR.ServiceCore.Signalr
|
||||
userInfo.LoginTime = DateTime.Now;
|
||||
userInfo.TodayOnlineTime = 0;
|
||||
}
|
||||
var clientUser = onlineClients.Find(x => x.Userid == userid);
|
||||
var clientUser = OnlineClients.Find(x => x.Userid == userid);
|
||||
userInfo.TodayOnlineTime += Math.Round(clientUser?.OnlineTime ?? 0, 2);
|
||||
}
|
||||
//给当前所有登录当前账号的用户下发登录时长
|
||||
var connIds = onlineClients.Where(f => f.Userid == userid).ToList();
|
||||
var connIds = OnlineClients.Where(f => f.Userid == userid).ToList();
|
||||
userInfo.ClientNum = connIds.Count;
|
||||
|
||||
// Clients.Clients(connIds.Select(f => f.ConnnectionId)).SendAsync("onlineInfo", userInfo);
|
||||
|
||||
Log.WriteLine(ConsoleColor.Blue, msg: $"用户{name}已连接,今日已在线{userInfo?.TodayOnlineTime}分钟,当前已连接{onlineClients.Count}个");
|
||||
Log.WriteLine(ConsoleColor.Blue, msg: $"用户{name}已连接,今日已在线{userInfo?.TodayOnlineTime}分钟,当前已连接{OnlineClients.Count}个");
|
||||
//给所有用户更新在线人数
|
||||
Clients.All.SendAsync(HubsConstant.OnlineNum, new
|
||||
await Clients.All.SendAsync(HubsConstant.OnlineNum, new
|
||||
{
|
||||
num = onlineClients.Count,
|
||||
onlineClients
|
||||
num = OnlineClients.Count, onlineClients = OnlineClients
|
||||
});
|
||||
return base.OnConnectedAsync();
|
||||
await base.OnConnectedAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -165,15 +169,15 @@ namespace ZR.ServiceCore.Signalr
|
||||
/// <returns></returns>
|
||||
public override Task OnDisconnectedAsync(Exception exception)
|
||||
{
|
||||
var user = onlineClients.Where(p => p.ConnnectionId == Context.ConnectionId).FirstOrDefault();
|
||||
var user = OnlineClients.Where(p => p.ConnnectionId == Context.ConnectionId).FirstOrDefault();
|
||||
if (user != null)
|
||||
{
|
||||
onlineClients.Remove(user);
|
||||
OnlineClients.Remove(user);
|
||||
//给所有用户更新在线人数
|
||||
Clients.All.SendAsync(HubsConstant.OnlineNum, new
|
||||
{
|
||||
num = onlineClients.Count,
|
||||
onlineClients,
|
||||
num = OnlineClients.Count,
|
||||
onlineClients = OnlineClients,
|
||||
leaveUser = user
|
||||
});
|
||||
|
||||
@@ -183,7 +187,7 @@ namespace ZR.ServiceCore.Signalr
|
||||
{
|
||||
userInfo.TodayOnlineTime += user?.OnlineTime ?? 0;
|
||||
}
|
||||
Log.WriteLine(ConsoleColor.Red, msg: $"用户{user?.Name}离开了,已在线{userInfo?.TodayOnlineTime}分,当前已连接{onlineClients.Count}个");
|
||||
Log.WriteLine(ConsoleColor.Red, msg: $"用户{user?.Name}离开了,已在线{userInfo?.TodayOnlineTime}分,当前已连接{OnlineClients.Count}个");
|
||||
}
|
||||
return base.OnDisconnectedAsync(exception);
|
||||
}
|
||||
@@ -202,7 +206,7 @@ namespace ZR.ServiceCore.Signalr
|
||||
{
|
||||
var userName = HttpContextExtension.GetName(App.HttpContext);
|
||||
long userid = HttpContextExtension.GetUId(App.HttpContext);
|
||||
var toUserList = onlineClients.Where(p => p.Userid == toUserId);
|
||||
var toUserList = OnlineClients.Where(p => p.Userid == toUserId);
|
||||
var toUserInfo = toUserList.FirstOrDefault();
|
||||
IList<string> sendToUser = toUserList.Select(x => x.ConnnectionId).ToList();
|
||||
sendToUser.Add(GetConnectId());
|
||||
@@ -230,6 +234,94 @@ namespace ZR.ServiceCore.Signalr
|
||||
Console.WriteLine($"用户{userName}对{toConnectId}-{toUserId}说:{message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 对当前在线用户累加新通知公告
|
||||
/// </summary>
|
||||
/// <param name="noticeId"></param>
|
||||
/// <param name="enableDelete"></param>
|
||||
public async Task SendNoticeToOnlineUsers(long? noticeId, bool enableDelete = true)
|
||||
{
|
||||
if (enableDelete)
|
||||
{
|
||||
// 防止日志记录有冗余数据
|
||||
await _sysNoticeLogService.Deleteable()
|
||||
.Where(it => it.NoticeId == noticeId)
|
||||
.ExecuteCommandAsync();
|
||||
// 获取所有通知
|
||||
var noticeRes = (List<SysNotice>) SendNotice()[ApiResult.DATA_TAG];
|
||||
|
||||
foreach (var onlineUser in OnlineClients)
|
||||
{
|
||||
var userid = onlineUser.Userid;
|
||||
|
||||
var sysNoticeLogStore = await _sysNoticeLogService
|
||||
.Storageable(new SysNoticeLog(noticeId!.Value, onlineUser.Userid, SysNoticeLogStatus.Unread))
|
||||
.WhereColumns(it => new { it.NoticeId, it.UserId })
|
||||
.ToStorageAsync();
|
||||
await sysNoticeLogStore
|
||||
.AsInsertable
|
||||
.ExecuteReturnSnowflakeIdAsync();
|
||||
await sysNoticeLogStore
|
||||
.AsUpdateable
|
||||
.ExecuteCommandAsync();
|
||||
// 获取当前在线用户的通知日志记录id
|
||||
var unreadAndReadNotices = await _sysNoticeLogService.Queryable()
|
||||
.Where(it => it.UserId == userid)
|
||||
.ToListAsync();
|
||||
var config = new TypeAdapterConfig();
|
||||
config.ForType<SysNotice, SysNoticeDto>()
|
||||
.Map(dest => dest.NoticeId, src => src.NoticeId.ToString());
|
||||
var unReadNotifications = noticeRes.Where(it =>
|
||||
unreadAndReadNotices.Where(o => o.Status == SysNoticeLogStatus.Unread)
|
||||
.Select(o => o.NoticeId).Contains(it.NoticeId)).ToList()
|
||||
.Adapt<List<SysNoticeDto>>(config);
|
||||
var readNotifications = noticeRes.Where(it =>
|
||||
unreadAndReadNotices.Where(o => o.Status == SysNoticeLogStatus.Read)
|
||||
.Select(o => o.NoticeId).Contains(it.NoticeId)).ToList()
|
||||
.Adapt<List<SysNoticeDto>>(config);
|
||||
|
||||
await Clients.Client(onlineUser.ConnnectionId).SendAsync(HubsConstant.MoreNotice,
|
||||
SendNotice(new
|
||||
{
|
||||
unReadNotifications,
|
||||
readNotifications
|
||||
}));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// 获取所有通知
|
||||
var noticeRes = (List<SysNotice>) SendNotice()[ApiResult.DATA_TAG];
|
||||
foreach (var onlineUser in OnlineClients)
|
||||
{
|
||||
var userid = onlineUser.Userid;
|
||||
// 获取当前在线用户的通知日志记录id
|
||||
var unreadAndReadNotices = await _sysNoticeLogService.Queryable()
|
||||
.Where(it => it.UserId == userid)
|
||||
.ToListAsync();
|
||||
var config = new TypeAdapterConfig();
|
||||
config.ForType<SysNotice, SysNoticeDto>()
|
||||
.Map(dest => dest.NoticeId, src => src.NoticeId.ToString());
|
||||
var unReadNotifications = noticeRes.Where(it =>
|
||||
unreadAndReadNotices.Where(o => o.Status == SysNoticeLogStatus.Unread)
|
||||
.Select(o => o.NoticeId).Contains(it.NoticeId)).ToList()
|
||||
.Adapt<List<SysNoticeDto>>(config);
|
||||
var readNotifications = noticeRes.Where(it =>
|
||||
unreadAndReadNotices.Where(o => o.Status == SysNoticeLogStatus.Read)
|
||||
.Select(o => o.NoticeId).Contains(it.NoticeId)).ToList()
|
||||
.Adapt<List<SysNoticeDto>>(config);
|
||||
await Clients.Client(onlineUser.ConnnectionId).SendAsync(HubsConstant.MoreNotice,
|
||||
SendNotice(new
|
||||
{
|
||||
unReadNotifications,
|
||||
readNotifications
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
// var onlineUsers = ClientUsers.Where(it => it.ConnnectionId != Context.ConnectionId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 全部标为已读
|
||||
/// </summary>
|
||||
@@ -268,7 +360,11 @@ namespace ZR.ServiceCore.Signalr
|
||||
newUnReadNotificationIds.Contains(it.NoticeId)).ToList();
|
||||
var newReadNotifications = noticeRes.Where(it =>
|
||||
newReadNotificationIds.Contains(it.NoticeId)).ToList();
|
||||
await Clients.Caller.SendAsync(HubsConstant.MoreNotice, newUnReadNotifications, newReadNotifications);
|
||||
await Clients.Caller.SendAsync(HubsConstant.MoreNotice, SendNotice(new
|
||||
{
|
||||
unReadNotifications = newUnReadNotifications,
|
||||
readNotifications = newReadNotifications
|
||||
}));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -303,16 +399,20 @@ namespace ZR.ServiceCore.Signalr
|
||||
newUnReadNotificationIds.Contains(it.NoticeId)).ToList();
|
||||
var newReadNotifications = noticeRes.Where(it =>
|
||||
newReadNotificationIds.Contains(it.NoticeId)).ToList();
|
||||
await Clients.Caller.SendAsync(HubsConstant.MoreNotice, newUnReadNotifications, newReadNotifications);
|
||||
await Clients.Caller.SendAsync(HubsConstant.MoreNotice, SendNotice(new
|
||||
{
|
||||
unReadNotifications = newUnReadNotifications,
|
||||
readNotifications = newReadNotifications
|
||||
}));
|
||||
}
|
||||
|
||||
private OnlineUsers GetUserByConnId(string connId)
|
||||
{
|
||||
return onlineClients.Where(p => p.ConnnectionId == connId).FirstOrDefault();
|
||||
return OnlineClients.Where(p => p.ConnnectionId == connId).FirstOrDefault();
|
||||
}
|
||||
private static OnlineUsers GetUserById(long userid)
|
||||
{
|
||||
return users.Where(f => f.Userid == userid).FirstOrDefault();
|
||||
return Users.Where(f => f.Userid == userid).FirstOrDefault();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -336,7 +436,7 @@ namespace ZR.ServiceCore.Signalr
|
||||
long userid = HttpContextExtension.GetUId(App.HttpContext);
|
||||
if (singleLogin)
|
||||
{
|
||||
var onlineUsers = onlineClients.Where(p => p.ConnnectionId != Context.ConnectionId && p.Userid == userid);
|
||||
var onlineUsers = OnlineClients.Where(p => p.ConnnectionId != Context.ConnectionId && p.Userid == userid);
|
||||
await Clients.Clients(onlineUsers.Select(x => x.ConnnectionId))
|
||||
.SendAsync("logOut");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user