新增删除修改通知后,会向当前登录的用户组发送通知,修改通知后,如果该通知之前被用户已读过,则将其重新置于未读状态

This commit is contained in:
2023-10-20 17:12:58 +08:00
parent 99d2ebcbe8
commit 67992af570
6 changed files with 265 additions and 70 deletions
@@ -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>