优化代码
This commit is contained in:
parent
cba0f56a33
commit
7bb725deb1
@ -1,5 +1,8 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using ZR.Admin.WebApi.Filters;
|
||||
using ZR.Admin.WebApi.Hubs;
|
||||
using ZR.Model;
|
||||
|
||||
namespace ZR.Admin.WebApi.Controllers.monitor
|
||||
{
|
||||
@ -7,10 +10,26 @@ namespace ZR.Admin.WebApi.Controllers.monitor
|
||||
[Route("monitor/online")]
|
||||
public class SysUserOnlineController : BaseController
|
||||
{
|
||||
[HttpGet("list")]
|
||||
public IActionResult Index()
|
||||
private IHubContext<Hub> HubContext;
|
||||
|
||||
public SysUserOnlineController(IHubContext<Hub> hubContext)
|
||||
{
|
||||
return SUCCESS(1);
|
||||
HubContext = hubContext;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取在线用户列表
|
||||
/// </summary>
|
||||
/// <param name="parm"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("list")]
|
||||
public IActionResult Index([FromQuery] PagerInfo parm)
|
||||
{
|
||||
var result = MessageHub.clientUsers
|
||||
.OrderByDescending(f => f.LoginTime)
|
||||
.Skip(parm.PageNum - 1).Take(parm.PageSize);
|
||||
|
||||
return SUCCESS(new { result, totalNum = MessageHub.clientUsers.Count });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,7 +1,4 @@
|
||||
using AspNetCoreRateLimit;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
|
||||
namespace ZR.Admin.WebApi.Extensions
|
||||
{
|
||||
|
||||
@ -9,7 +9,7 @@ namespace ZR.Admin.WebApi.Framework
|
||||
public class DateTimeNullConverter : JsonConverter<DateTime?>
|
||||
{
|
||||
public override DateTime? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
=> string.IsNullOrEmpty(reader.GetString()) ? default(DateTime?) : ParseDateTime(reader.GetString());
|
||||
=> string.IsNullOrEmpty(reader.GetString()) ? default : ParseDateTime(reader.GetString());
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, DateTime? value, JsonSerializerOptions options)
|
||||
=> writer.WriteStringValue(value?.ToString("yyyy-MM-dd HH:mm:ss"));
|
||||
|
||||
@ -125,15 +125,20 @@ namespace ZR.Admin.WebApi.Framework
|
||||
{
|
||||
try
|
||||
{
|
||||
var userData = jwtToken.FirstOrDefault(x => x.Type == ClaimTypes.UserData).Value;
|
||||
var loginUser = JsonConvert.DeserializeObject<LoginUser>(userData);
|
||||
var permissions = CacheService.GetUserPerms(GlobalConstant.UserPermKEY + loginUser?.UserId);
|
||||
if (loginUser?.UserName == GlobalConstant.AdminRole)
|
||||
LoginUser loginUser = null;
|
||||
|
||||
var userData = jwtToken.FirstOrDefault(x => x.Type == ClaimTypes.UserData)?.Value;
|
||||
if (userData != null)
|
||||
{
|
||||
permissions = new List<string>() { GlobalConstant.AdminPerm };
|
||||
loginUser = JsonConvert.DeserializeObject<LoginUser>(userData);
|
||||
var permissions = CacheService.GetUserPerms(GlobalConstant.UserPermKEY + loginUser?.UserId);
|
||||
if (loginUser?.UserName == GlobalConstant.AdminRole)
|
||||
{
|
||||
permissions = new List<string>() { GlobalConstant.AdminPerm };
|
||||
}
|
||||
if (permissions == null) return null;
|
||||
loginUser.Permissions = permissions;
|
||||
}
|
||||
if (permissions == null) return null;
|
||||
loginUser.Permissions = permissions;
|
||||
return loginUser;
|
||||
}
|
||||
catch (Exception ex)
|
||||
@ -150,10 +155,6 @@ namespace ZR.Admin.WebApi.Framework
|
||||
/// <returns></returns>
|
||||
public static List<Claim> AddClaims(LoginUser user)
|
||||
{
|
||||
if (user?.Permissions.Count > 50)
|
||||
{
|
||||
user.Permissions = new List<string>();
|
||||
}
|
||||
var claims = new List<Claim>()
|
||||
{
|
||||
new Claim(ClaimTypes.PrimarySid, user.UserId.ToString()),
|
||||
|
||||
@ -3,13 +3,8 @@ using Infrastructure.Constant;
|
||||
using Infrastructure.Model;
|
||||
using IPTools.Core;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using UAParser;
|
||||
using ZR.Admin.WebApi.Extensions;
|
||||
using ZR.Model;
|
||||
using ZR.Service.System.IService;
|
||||
|
||||
namespace ZR.Admin.WebApi.Hubs
|
||||
@ -20,7 +15,7 @@ namespace ZR.Admin.WebApi.Hubs
|
||||
public class MessageHub : Hub
|
||||
{
|
||||
//创建用户集合,用于存储所有链接的用户数据
|
||||
private static readonly List<OnlineUsers> clientUsers = new();
|
||||
public static readonly List<OnlineUsers> clientUsers = new();
|
||||
private readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
private readonly ISysNoticeService SysNoticeService;
|
||||
|
||||
@ -44,7 +39,7 @@ namespace ZR.Admin.WebApi.Hubs
|
||||
/// <returns></returns>
|
||||
public override Task OnConnectedAsync()
|
||||
{
|
||||
var name = HttpContextExtension.GetName(App.HttpContext);// Context.User.Identity.Name;
|
||||
var name = HttpContextExtension.GetName(App.HttpContext);
|
||||
var ip = HttpContextExtension.GetClientUserIp(App.HttpContext);
|
||||
var ip_info = IpTool.Search(ip);
|
||||
|
||||
@ -56,10 +51,10 @@ namespace ZR.Admin.WebApi.Hubs
|
||||
var user = clientUsers.Any(u => u.ConnnectionId == Context.ConnectionId);
|
||||
var user2 = clientUsers.Any(u => u.Uuid == uuid);
|
||||
|
||||
//判断用户是否存在,否则添加集合
|
||||
if (!user2 && !user && Context.User.Identity.IsAuthenticated)
|
||||
//判断用户是否存在,否则添加集合!user2 && !user &&
|
||||
if (Context.User.Identity.IsAuthenticated)
|
||||
{
|
||||
OnlineUsers users = new(Context.ConnectionId, name, userid, ip)
|
||||
OnlineUsers users = new(Context.ConnectionId, name, userid, ip, device)
|
||||
{
|
||||
Location = ip_info.City,
|
||||
Uuid = uuid
|
||||
@ -71,7 +66,6 @@ namespace ZR.Admin.WebApi.Hubs
|
||||
}
|
||||
|
||||
Clients.All.SendAsync(HubsConstant.OnlineNum, clientUsers.Count);
|
||||
Clients.All.SendAsync(HubsConstant.OnlineUser, clientUsers);
|
||||
return base.OnConnectedAsync();
|
||||
}
|
||||
|
||||
@ -87,7 +81,7 @@ namespace ZR.Admin.WebApi.Hubs
|
||||
{
|
||||
clientUsers.Remove(user);
|
||||
Clients.All.SendAsync(HubsConstant.OnlineNum, clientUsers.Count);
|
||||
Clients.All.SendAsync(HubsConstant.OnlineUser, clientUsers);
|
||||
|
||||
Console.WriteLine($"用户{user?.Name}离开了,当前已连接{clientUsers.Count}个");
|
||||
}
|
||||
return base.OnDisconnectedAsync(exception);
|
||||
|
||||
@ -13,16 +13,28 @@
|
||||
public string Name { get; set; }
|
||||
public DateTime LoginTime { get; set; }
|
||||
public string UserIP { get; set; }
|
||||
/// <summary>
|
||||
/// 登录地点
|
||||
/// </summary>
|
||||
public string? Location { get; set; }
|
||||
public string? Uuid{ get; set; }
|
||||
|
||||
public OnlineUsers(string clientid, string name, long? userid, string userip)
|
||||
/// <summary>
|
||||
/// 判断用户唯一
|
||||
/// </summary>
|
||||
public string? Uuid{ get; set; }
|
||||
/// <summary>
|
||||
/// 浏览器
|
||||
/// </summary>
|
||||
public string Browser { get; set; }
|
||||
|
||||
public OnlineUsers(string clientid, string name, long? userid, string userip, string browser)
|
||||
{
|
||||
ConnnectionId = clientid;
|
||||
Name = name;
|
||||
LoginTime = DateTime.Now;
|
||||
Userid = userid;
|
||||
UserIP = userip;
|
||||
Browser = browser;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,6 +8,7 @@ using ZR.Admin.WebApi.Filters;
|
||||
using ZR.Admin.WebApi.Middleware;
|
||||
using ZR.Admin.WebApi.Hubs;
|
||||
using ZR.Common.Cache;
|
||||
using AspNetCoreRateLimit;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
@ -41,6 +42,8 @@ builder.Services.AddDataProtection()
|
||||
.PersistKeysToFileSystem(new DirectoryInfo(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "DataProtection"));
|
||||
//普通验证码
|
||||
builder.Services.AddCaptcha(builder.Configuration);
|
||||
//IPRatelimit
|
||||
builder.Services.AddIPRate(builder.Configuration);
|
||||
//builder.Services.AddSession();
|
||||
builder.Services.AddHttpContextAccessor();
|
||||
//绑定整个对象到Model上
|
||||
@ -119,7 +122,9 @@ app.UseResponseCaching();
|
||||
app.UseAddTaskSchedulers();
|
||||
//使用全局异常中间件
|
||||
app.UseMiddleware<GlobalExceptionMiddleware>();
|
||||
|
||||
//启用客户端IP限制速率
|
||||
app.UseIpRateLimiting();
|
||||
app.UseRateLimiter();
|
||||
//设置socket连接
|
||||
app.MapHub<MessageHub>("/msgHub");
|
||||
|
||||
|
||||
@ -1,8 +1,6 @@
|
||||
using Infrastructure;
|
||||
using Infrastructure.Attribute;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using ZR.Model.System;
|
||||
using ZR.Service.System.IService;
|
||||
|
||||
@ -32,7 +30,7 @@ namespace ZR.Service.System
|
||||
/// <returns>角色权限信息</returns>
|
||||
public List<string> GetRolePermission(SysUser user)
|
||||
{
|
||||
List<string> roles = new List<string>();
|
||||
List<string> roles = new();
|
||||
// 管理员拥有所有权限
|
||||
if (user.IsAdmin())
|
||||
{
|
||||
|
||||
@ -273,6 +273,7 @@ INSERT into sys_menu VALUES (107, '参数设置', 1, 8, 'config','system/config/
|
||||
INSERT INTO sys_menu VALUES (108, '日志管理', 1, 10, 'log', '' , 0, 0, 'M', '0', '0', '', 'log', '', SYSDATE(), '', NULL, '日志管理菜单', 'menu.systemLog');
|
||||
INSERT INTO sys_menu VALUES (109, '通知公告', 1, 9, 'notice', 'system/notice/index', 0, 0, 'C', '0', '0', 'system:notice:list', 'message', '', SYSDATE(), '', NULL, '通知公告菜单', 'menu.systemNotice');
|
||||
INSERT INTO sys_menu VALUES (110, '定时任务', 2, 10, 'job', 'monitor/job/index', 0, 0, 'C', '0', '0', '', 'job', '', SYSDATE(), '', NULL, '定时任务菜单', 'menu.timedTask');
|
||||
INSERT INTO sys_menu VALUES (111, '在线用户', 2, 10, 'onlineusers', 'monitor/onlineuser/index', 0, 0, 'C', '0', '0', '', 'online', '', SYSDATE(), '', NULL, '在线用户', 'layout.onlineUsers');
|
||||
INSERT INTO sys_menu VALUES (112, '服务监控', 2, 11, 'server', 'monitor/server/index', 0, 0, 'C', '0', '0', 'monitor:server:list', 'server', '', SYSDATE(), '', NULL, '服务监控菜单', 'menu.serviceMonitor');
|
||||
INSERT INTO sys_menu VALUES (113, '缓存监控', 2, 12, 'cache', 'monitor/cache/index', 0, 0, 'C', '1', '1', 'monitor:cache:list', 'redis', '', SYSDATE(), '', NULL, '缓存监控菜单', 'menu.cacheMonitor');
|
||||
|
||||
|
||||
@ -246,7 +246,8 @@ INSERT INTO sys_menu(menuId, menuName, parentId, orderNum, path, component, isFr
|
||||
INSERT into sys_menu(menuId, menuName, parentId, orderNum, path, component, isFrame, isCache, menuType, visible, status, perms, icon, create_by,create_time, menuName_key) VALUES (107, '参数设置', 1, 8, 'config', 'system/config/index', 0, 0, 'C', '0', '0', 'system:config:list','edit','', GETDATE(), 'menu.systemParam');
|
||||
INSERT INTO sys_menu(menuId, menuName, parentId, orderNum, path, component, isFrame, isCache, menuType, visible, status, perms, icon, create_by,create_time, menuName_key) VALUES (108, '日志管理', 1, 10, 'log', '', 0, 0, 'M', '0', '0', '', 'log', '', GETDATE(), 'menu.systemLog');
|
||||
INSERT INTO sys_menu(menuId, menuName, parentId, orderNum, path, component, isFrame, isCache, menuType, visible, status, perms, icon, create_by,create_time, menuName_key) VALUES (109, '通知公告', 1, 9, 'notice', 'system/notice/index', 0, 0, 'C', '0', '0', 'system:notice:list', 'message', '', GETDATE(), 'menu.systemNotice');
|
||||
INSERT INTO sys_menu(menuId, menuName, parentId, orderNum, path, component, isFrame, isCache, menuType, visible, status, perms, icon, create_by,create_time, menuName_key) VALUES (110, '定时任务', 2, 1, 'job', 'monitor/job/index', 0, 0, 'C', '0', '0', '', 'job', '', GETDATE(), 'menu.timedTask');
|
||||
INSERT INTO sys_menu(menuId, menuName, parentId, orderNum, path, component, isFrame, isCache, menuType, visible, status, perms, icon, create_by,create_time, menuName_key) VALUES (110, '定时任务', 2, 1, 'job', 'monitor/job/index', 0, 0, 'C', '0', '0', '', '', '', GETDATE(), 'menu.timedTask');
|
||||
INSERT INTO sys_menu(menuId, menuName, parentId, orderNum, path, component, isFrame, isCache, menuType, visible, status, perms, icon, create_by,create_time, menuName_key) VALUES (111, '在线用户', 2, 2, 'onlineusers', 'monitor/onlineuser/index', 0, 0, 'C', '0', '0', '', 'online', '', GETDATE(), 'layout.onlineUsers');
|
||||
INSERT INTO sys_menu(menuId, menuName, parentId, orderNum, path, component, isFrame, isCache, menuType, visible, status, perms, icon, create_by,create_time, menuName_key) VALUES (112, '服务监控', 2, 4, 'server', 'monitor/server/index', 0, 0, 'C', '0', '0', 'monitor:server:list', 'server', '', GETDATE(), 'menu.serviceMonitor');
|
||||
INSERT INTO sys_menu(menuId, menuName, parentId, orderNum, path, component, isFrame, isCache, menuType, visible, status, perms, icon, create_by,create_time, menuName_key) VALUES (113, '缓存监控', 2, 5, 'cache', 'monitor/cache/index', 0, 0, 'C', '1', '1', 'monitor:cache:list', 'redis', '', GETDATE(), 'menu.cacheMonitor');
|
||||
INSERT INTO sys_menu(menuId, menuName, parentId, orderNum, path, component, isFrame, isCache, menuType, visible, status, perms, icon, create_by,create_time, menuName_key) VALUES (114, '表单构建', 3, 1, 'build', 'tool/build/index', 0, 0, 'C', '0', '0', 'tool:build:list', 'build', '', GETDATE(), 'menu.formBuild');
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user