新增删除修改通知后,会向当前登录的用户组发送通知,修改通知后,如果该通知之前被用户已读过,则将其重新置于未读状态
This commit is contained in:
parent
99d2ebcbe8
commit
67992af570
@ -14,6 +14,7 @@
|
||||
<PackageReference Include="AspectCore.Abstractions" Version="2.4.0" />
|
||||
<PackageReference Include="BloomFilter.CSRedis.NetCore" Version="2.1.1" />
|
||||
<PackageReference Include="Caching.CSRedis" Version="3.8.670" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="7.0.12" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="7.0.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
<PackageReference Include="RulesEngine" Version="5.0.2" />
|
||||
|
||||
@ -1,12 +1,14 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Microsoft.AspNetCore.SignalR.Client;
|
||||
using SqlSugar;
|
||||
using ZR.Admin.WebApi.Filters;
|
||||
using ZR.Model;
|
||||
using ZR.Model.System;
|
||||
using ZR.Service.System.IService;
|
||||
using ZR.ServiceCore.Model;
|
||||
using ZR.ServiceCore.Model.Dto;
|
||||
using ZR.ServiceCore.Services.IService;
|
||||
using ZR.ServiceCore.Signalr;
|
||||
|
||||
namespace ZR.Admin.WebApi.Controllers.System
|
||||
@ -24,11 +26,14 @@ namespace ZR.Admin.WebApi.Controllers.System
|
||||
/// </summary>
|
||||
private readonly ISysNoticeService _sysNoticeService;
|
||||
private readonly IHubContext<MessageHub> _hubContext;
|
||||
|
||||
public SysNoticeController(ISysNoticeService sysNoticeService, IHubContext<MessageHub> hubContext)
|
||||
private readonly ISysNoticeLogService _sysNoticeLogService;
|
||||
private readonly IWebHostEnvironment _webHostEnvironment;
|
||||
public SysNoticeController(ISysNoticeService sysNoticeService, IHubContext<MessageHub> hubContext, ISysNoticeLogService sysNoticeLogService, IWebHostEnvironment webHostEnvironment)
|
||||
{
|
||||
_sysNoticeService = sysNoticeService;
|
||||
_hubContext = hubContext;
|
||||
_sysNoticeLogService = sysNoticeLogService;
|
||||
_webHostEnvironment = webHostEnvironment;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -77,7 +82,7 @@ namespace ZR.Admin.WebApi.Controllers.System
|
||||
[HttpPost]
|
||||
[ActionPermissionFilter(Permission = "system:notice:add")]
|
||||
[Log(Title = "发布通告", BusinessType = BusinessType.INSERT)]
|
||||
public IActionResult AddSysNotice([FromBody] SysNoticeDto parm)
|
||||
public async Task<IActionResult> AddSysNotice([FromBody] SysNoticeDto parm)
|
||||
{
|
||||
var modal = parm.Adapt<SysNotice>().ToCreate(HttpContext);
|
||||
modal.Create_by = HttpContext.GetUId();
|
||||
@ -95,8 +100,23 @@ namespace ZR.Admin.WebApi.Controllers.System
|
||||
// it.Create_time
|
||||
// });
|
||||
|
||||
var result = _sysNoticeService.Insertable(modal).ExecuteReturnSnowflakeId();
|
||||
|
||||
var result = await _sysNoticeService
|
||||
.Insertable(modal)
|
||||
.ExecuteReturnSnowflakeIdAsync();
|
||||
var scheme = HttpContext.Request.Scheme + "://";
|
||||
var serverIP = HttpContext.Request.Host.Value;
|
||||
if (_webHostEnvironment.IsProduction())
|
||||
{
|
||||
var host = await Dns.GetHostEntryAsync(Dns.GetHostName());
|
||||
var ip = host.AddressList
|
||||
.FirstOrDefault(it => it.AddressFamily == AddressFamily.InterNetwork);
|
||||
serverIP = ip + ":" + Request.HttpContext.Connection.LocalPort;//获取服务器IP
|
||||
}
|
||||
var url = scheme + serverIP;
|
||||
var hubConnection = new HubConnectionBuilder().WithUrl(url + "/msghub").Build();
|
||||
await hubConnection.StartAsync();
|
||||
await hubConnection.InvokeAsync("SendNoticeToOnlineUsers", result, true);
|
||||
await hubConnection.StopAsync();
|
||||
return SUCCESS(result);
|
||||
}
|
||||
|
||||
@ -107,13 +127,13 @@ namespace ZR.Admin.WebApi.Controllers.System
|
||||
[HttpPut]
|
||||
[ActionPermissionFilter(Permission = "system:notice:update")]
|
||||
[Log(Title = "修改公告", BusinessType = BusinessType.UPDATE)]
|
||||
public IActionResult UpdateSysNotice([FromBody] SysNoticeDto parm)
|
||||
public async Task<IActionResult> UpdateSysNotice([FromBody] SysNoticeDto parm)
|
||||
{
|
||||
var config = new TypeAdapterConfig();
|
||||
config.ForType<SysNoticeDto, SysNotice>()
|
||||
.Map(dest => dest.NoticeId, src => src.NoticeId.ParseToLong());
|
||||
var model = parm.Adapt<SysNotice>(config).ToUpdate(HttpContext);
|
||||
|
||||
var nowDate = DateTime.Now;
|
||||
var response = _sysNoticeService.Update(w => w.NoticeId == model.NoticeId, it => new SysNotice()
|
||||
{
|
||||
NoticeTitle = model.NoticeTitle,
|
||||
@ -123,9 +143,24 @@ namespace ZR.Admin.WebApi.Controllers.System
|
||||
Remark = model.Remark,
|
||||
Update_by = HttpContext.GetUId(),
|
||||
Update_name = HttpContext.GetNickName(),
|
||||
Update_time = DateTime.Now
|
||||
Update_time = nowDate,
|
||||
Create_time = nowDate
|
||||
});
|
||||
|
||||
|
||||
var scheme = HttpContext.Request.Scheme + "://";
|
||||
var serverIP = HttpContext.Request.Host.Value;
|
||||
if (_webHostEnvironment.IsProduction())
|
||||
{
|
||||
var host = await Dns.GetHostEntryAsync(Dns.GetHostName());
|
||||
var ip = host.AddressList
|
||||
.FirstOrDefault(it => it.AddressFamily == AddressFamily.InterNetwork);
|
||||
serverIP = ip + ":" + Request.HttpContext.Connection.LocalPort;//获取服务器IP
|
||||
}
|
||||
var url = scheme + serverIP;
|
||||
var hubConnection = new HubConnectionBuilder().WithUrl(url + "/msghub").Build();
|
||||
await hubConnection.StartAsync();
|
||||
await hubConnection.InvokeAsync("SendNoticeToOnlineUsers", model.NoticeId, true);
|
||||
await hubConnection.StopAsync();
|
||||
return SUCCESS(response);
|
||||
}
|
||||
/// <summary>
|
||||
@ -156,13 +191,30 @@ namespace ZR.Admin.WebApi.Controllers.System
|
||||
[HttpDelete("{ids}")]
|
||||
[ActionPermissionFilter(Permission = "system:notice:delete")]
|
||||
[Log(Title = "通知公告", BusinessType = BusinessType.DELETE)]
|
||||
public IActionResult DeleteSysNotice(string ids)
|
||||
public async Task<IActionResult> DeleteSysNotice(string ids)
|
||||
{
|
||||
var idsArr = Tools.SpitLongArrary(ids);
|
||||
if (idsArr.Length <= 0) { return ToResponse(ApiResult.Error($"删除失败Id 不能为空")); }
|
||||
|
||||
var response = _sysNoticeService.Delete(idsArr);
|
||||
|
||||
await _sysNoticeLogService.Deleteable()
|
||||
.Where(it => idsArr.Contains(it.NoticeId))
|
||||
.ExecuteCommandAsync();
|
||||
|
||||
var scheme = HttpContext.Request.Scheme + "://";
|
||||
var serverIP = HttpContext.Request.Host.Value;
|
||||
if (_webHostEnvironment.IsProduction())
|
||||
{
|
||||
var host = await Dns.GetHostEntryAsync(Dns.GetHostName());
|
||||
var ip = host.AddressList
|
||||
.FirstOrDefault(it => it.AddressFamily == AddressFamily.InterNetwork);
|
||||
serverIP = ip + ":" + Request.HttpContext.Connection.LocalPort;//获取服务器IP
|
||||
}
|
||||
var url = scheme + serverIP;
|
||||
var hubConnection = new HubConnectionBuilder().WithUrl(url + "/msghub").Build();
|
||||
await hubConnection.StartAsync();
|
||||
await hubConnection.InvokeAsync("SendNoticeToOnlineUsers", null, false);
|
||||
await hubConnection.StopAsync();
|
||||
return SUCCESS(response);
|
||||
}
|
||||
}
|
||||
|
||||
@ -30,11 +30,11 @@ namespace ZR.Admin.WebApi.Controllers.monitor
|
||||
[HttpGet("list")]
|
||||
public IActionResult Index([FromQuery] PagerInfo parm)
|
||||
{
|
||||
var result = MessageHub.onlineClients
|
||||
var result = MessageHub.OnlineClients
|
||||
.OrderByDescending(f => f.LoginTime)
|
||||
.Skip(parm.PageNum - 1).Take(parm.PageSize);
|
||||
|
||||
return SUCCESS(new { result, totalNum = MessageHub.onlineClients.Count });
|
||||
return SUCCESS(new { result, totalNum = MessageHub.OnlineClients.Count });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@ -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");
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user