新增系统通知实时通知给连接的客户端
This commit is contained in:
@@ -1,8 +1,5 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Infrastructure;
|
||||
using Infrastructure.Attribute;
|
||||
using Infrastructure.Enums;
|
||||
@@ -15,6 +12,9 @@ using ZR.Common;
|
||||
using ZR.Model.Dto;
|
||||
using ZR.Model.Models;
|
||||
using ZR.Service.System.IService;
|
||||
using ZR.Admin.WebApi.Hubs;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Infrastructure.Constant;
|
||||
|
||||
namespace ZR.Admin.WebApi.Controllers.System
|
||||
{
|
||||
@@ -26,10 +26,12 @@ namespace ZR.Admin.WebApi.Controllers.System
|
||||
/// 通知公告表接口
|
||||
/// </summary>
|
||||
private readonly ISysNoticeService _SysNoticeService;
|
||||
private readonly IHubContext<MessageHub> _hubContext;
|
||||
|
||||
public SysNoticeController(ISysNoticeService SysNoticeService)
|
||||
public SysNoticeController(ISysNoticeService SysNoticeService, IHubContext<MessageHub> hubContext)
|
||||
{
|
||||
_SysNoticeService = SysNoticeService;
|
||||
_hubContext = hubContext;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -96,11 +98,11 @@ namespace ZR.Admin.WebApi.Controllers.System
|
||||
throw new CustomException("请求参数错误");
|
||||
}
|
||||
//从 Dto 映射到 实体
|
||||
var model = parm.Adapt<SysNotice>().ToCreate(HttpContext);
|
||||
model.Create_by = User.Identity.Name;
|
||||
model.Create_time = DateTime.Now;
|
||||
var modal = parm.Adapt<SysNotice>().ToCreate(HttpContext);
|
||||
modal.Create_by = User.Identity.Name;
|
||||
modal.Create_time = DateTime.Now;
|
||||
|
||||
return SUCCESS(_SysNoticeService.Insert(model, it => new
|
||||
int result = _SysNoticeService.Insert(modal, it => new
|
||||
{
|
||||
it.NoticeTitle,
|
||||
it.NoticeType,
|
||||
@@ -109,7 +111,9 @@ namespace ZR.Admin.WebApi.Controllers.System
|
||||
it.Remark,
|
||||
it.Create_by,
|
||||
it.Create_time
|
||||
}));
|
||||
});
|
||||
|
||||
return SUCCESS(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -142,6 +146,26 @@ namespace ZR.Admin.WebApi.Controllers.System
|
||||
|
||||
return SUCCESS(response);
|
||||
}
|
||||
/// <summary>
|
||||
/// 发送通知公告表
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpPut("send/{NoticeId}")]
|
||||
[ActionPermissionFilter(Permission = "system:notice:update")]
|
||||
[Log(Title = "通知公告表", BusinessType = BusinessType.OTHER)]
|
||||
public IActionResult SendNotice(int NoticeId = 0)
|
||||
{
|
||||
if (NoticeId <= 0)
|
||||
{
|
||||
throw new CustomException("请求实体不能为空");
|
||||
}
|
||||
var response = _SysNoticeService.GetFirst(x => x.NoticeId == NoticeId);
|
||||
if (response != null && response.Status == "0")
|
||||
{
|
||||
_hubContext.Clients.All.SendAsync(HubsConstant.ReceiveNotice, response.NoticeTitle, response.NoticeContent);
|
||||
}
|
||||
return SUCCESS(response);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除通知公告表
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Infrastructure.Constant;
|
||||
using Infrastructure.Model;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using ZR.Admin.WebApi.Filters;
|
||||
using ZR.Model;
|
||||
using ZR.Service.System.IService;
|
||||
|
||||
namespace ZR.Admin.WebApi.Hubs
|
||||
{
|
||||
@@ -15,6 +14,20 @@ namespace ZR.Admin.WebApi.Hubs
|
||||
{
|
||||
//创建用户集合,用于存储所有链接的用户数据
|
||||
private static readonly List<OnlineUsers> clientUsers = new();
|
||||
private readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
private ISysNoticeService SysNoticeService;
|
||||
|
||||
public MessageHub(ISysNoticeService noticeService)
|
||||
{
|
||||
SysNoticeService = noticeService;
|
||||
}
|
||||
|
||||
private ApiResult SendNotice()
|
||||
{
|
||||
var result = SysNoticeService.GetSysNotices();
|
||||
|
||||
return new ApiResult(200, "success", result);
|
||||
}
|
||||
|
||||
#region 客户端连接
|
||||
|
||||
@@ -25,16 +38,18 @@ namespace ZR.Admin.WebApi.Hubs
|
||||
public override Task OnConnectedAsync()
|
||||
{
|
||||
var name = Context.User.Identity.Name;
|
||||
|
||||
|
||||
var user = clientUsers.Any(u => u.ConnnectionId == Context.ConnectionId);
|
||||
//判断用户是否存在,否则添加集合
|
||||
if (!user && Context.User.Identity.IsAuthenticated)
|
||||
{
|
||||
clientUsers.Add(new OnlineUsers(Context.ConnectionId, name));
|
||||
Console.WriteLine($"{DateTime.Now}:{name},{Context.ConnectionId}连接服务端success,当前已连接{clientUsers.Count}个");
|
||||
Clients.All.SendAsync("welcome", $"欢迎您:{name},当前时间:{DateTime.Now}");
|
||||
Clients.All.SendAsync(HubsConstant.MoreNotice, SendNotice());
|
||||
}
|
||||
|
||||
Clients.All.SendAsync("onlineNum", clientUsers.Count);
|
||||
|
||||
Clients.All.SendAsync(HubsConstant.OnlineNum, clientUsers.Count);
|
||||
return base.OnConnectedAsync();
|
||||
}
|
||||
|
||||
@@ -50,7 +65,7 @@ namespace ZR.Admin.WebApi.Hubs
|
||||
{
|
||||
Console.WriteLine($"用户{user?.Name}离开了,当前已连接{clientUsers.Count}个");
|
||||
clientUsers.Remove(user);
|
||||
Clients.All.SendAsync("onlineNum", clientUsers.Count);
|
||||
Clients.All.SendAsync(HubsConstant.OnlineNum, clientUsers.Count);
|
||||
}
|
||||
return base.OnDisconnectedAsync(exception);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user