From b6aab652a8ce2619385f5eab8089cc3228e80128 Mon Sep 17 00:00:00 2001 From: youngq Date: Thu, 22 Aug 2024 17:19:35 +0800 Subject: [PATCH] 1 --- WGShare.API/Controllers/AuthController.cs | 48 +------------------ .../Controllers/Basic/BasicController.cs | 9 ++-- .../Controllers/Frontend/RoomController.cs | 25 +++++++--- WGShare.API/Hubs/IMessageClient.cs | 14 ++++++ WGShare.API/Hubs/SessionManageHub.cs | 11 +++-- WGShare.API/WGShare.API.xml | 16 ++++--- WGShare.Domain/Entities/ChannelUserInfo.cs | 1 + 7 files changed, 58 insertions(+), 66 deletions(-) diff --git a/WGShare.API/Controllers/AuthController.cs b/WGShare.API/Controllers/AuthController.cs index ef84431..cd9353b 100644 --- a/WGShare.API/Controllers/AuthController.cs +++ b/WGShare.API/Controllers/AuthController.cs @@ -194,53 +194,7 @@ namespace WGShare.API.Controllers }); } - /// - /// 匿名登录,直接进入会议室 - /// - /// - [HttpPost("anon-login"), Obsolete] - public async Task Login([FromBody] AnonymousLoginDTO loginDTO) - { - - var room = await _sqlSugar.Queryable().FirstAsync(x => x.Id == loginDTO.RoomId); - if (room == null) - { - throw Oops.Oh("会议号无效"); - } - var anonRoleId = "2"; - - // 匿名登录使用普通用户身份 - var perms = await _sqlSugar.Queryable() - .InnerJoin((m, rm) => m.Id == rm.PermId) - .Where((m, rm) => rm.RoleId == anonRoleId) - .Distinct() - .ToListAsync(); - - var tenant = await _sqlSugar.Queryable().FirstAsync(x => x.Id == room.TenantId); - if (tenant == null || tenant.IsDelete == true) - { - throw Oops.Oh("该区域账号已停用,请联系管理员"); - } - - - var btnAutn = new List(); - btnAutn.Add(new Claim("perm", perms.Sum(x => x.PermValue).ToString())); - btnAutn.Add(new Claim("roleid", anonRoleId)); - btnAutn.Add(new Claim("tenant", room.TenantId)); - btnAutn.Add(new Claim("mac", loginDTO.Mac)); - btnAutn.Add(new Claim("machine", loginDTO.MachineName)); - btnAutn.Add(new Claim("nickName", loginDTO.NickName)); - - return Ok(new - { - perms = perms.Sum(x => x.PermValue), - token = _jwtHelper.CreateToken("0", btnAutn), - roleId = anonRoleId, - userName = loginDTO.NickName, - tenantName = tenant.TenantName - }); - } - + /// /// 登出(暂未处理任何业务逻辑) /// diff --git a/WGShare.API/Controllers/Basic/BasicController.cs b/WGShare.API/Controllers/Basic/BasicController.cs index c1f609c..83160b1 100644 --- a/WGShare.API/Controllers/Basic/BasicController.cs +++ b/WGShare.API/Controllers/Basic/BasicController.cs @@ -4,6 +4,8 @@ using SqlSugar.Extensions; using System.Security.Claims; using WGShare.API.Helpers; using WGShare.Domain.Constant; +using WGShare.Domain.Entities; +using WGShare.Domain.Enums; using WGShare.Domain.FriendlyException; namespace WGShare.API.Controllers.Basic @@ -52,16 +54,17 @@ namespace WGShare.API.Controllers.Basic } - public string RoleId + public RoleEnums RoleId { get { var roleId = HttpContext.User.Claims.FirstOrDefault(x => x.Type == "roleid").Value; - if (string.IsNullOrWhiteSpace(roleId)) + if (string.IsNullOrWhiteSpace(roleId) || !int.TryParse(roleId, out int role) || role == 0) { throw Oops.Oh("用户信息有误,请重新登录"); } - return roleId; + + return (RoleEnums)role; } } diff --git a/WGShare.API/Controllers/Frontend/RoomController.cs b/WGShare.API/Controllers/Frontend/RoomController.cs index 8ac8d48..34cff6a 100644 --- a/WGShare.API/Controllers/Frontend/RoomController.cs +++ b/WGShare.API/Controllers/Frontend/RoomController.cs @@ -397,8 +397,8 @@ namespace WGShare.API.Controllers.Frontend ConnectId = ConnectionId, Account = Account, ScreenShareId = ScreenShareId, - RoleId = RoleId, - RoleName = ((RoleEnums)RoleId.ToInt32()).GetDescription(), + RoleId = ((int)RoleId).ToString(), + RoleName = RoleId.GetDescription(), IsRoomManager = false }; using (var pipe = RedisHelper.Instance.StartPipe()) @@ -486,7 +486,7 @@ namespace WGShare.API.Controllers.Frontend var polledUserIds = RedisHelper.Instance.SMembers(RedisKeyConstant.Data.GetPolledUserId(TenantId, roomNum)); // 排除已轮询的用户 - var userInfos = channelUserInfos.Where(x => !polledUserIds.Contains(x.UID)).Take(count).ToList(); + var userInfos = channelUserInfos.Where(x => !polledUserIds.Contains(x.UID) && x.RoleId != ((int)RoleEnums.Admin).ToString()).Take(count).ToList(); if (userInfos.Count < count) { // 数量不足,则从全部用户中取足 @@ -496,8 +496,11 @@ namespace WGShare.API.Controllers.Frontend RedisHelper.Instance.Del(RedisKeyConstant.Data.GetPolledUserId(TenantId, roomNum)); } - RedisHelper.Instance.SAdd(RedisKeyConstant.Data.GetPolledUserId(TenantId, roomNum), userInfos.Select(x => x.UID).ToArray()); - return userInfos; + var watchUids = userInfos.Select(x => x.UID).ToArray(); + RedisHelper.Instance.SAdd(RedisKeyConstant.Data.GetPolledUserId(TenantId, roomNum), watchUids); + _hubContext.Clients.Group(roomNum).Watch(watchUids); + + return userInfos; } /// @@ -505,9 +508,19 @@ namespace WGShare.API.Controllers.Frontend /// /// [HttpGet("apply-speak")] - public async Task ApplyToSpeak([FromQuery] string roomNum) + public async Task ApplySpeak([FromQuery] string roomNum) { + var channelUsers = RedisHelper.Instance.HVals(RedisKeyConstant.SessionManage.GetChannelUserKey(TenantId, roomNum)); + if (channelUsers.IsNullOrEmpty()) + { + return; + } + var connectionIds = channelUsers.Where(x => x.RoleId == ((int)RoleEnums.Admin).ToString()).Select(x => x.ConnectId); + if (connectionIds.Any()) + { + await _hubContext.Clients.Clients(connectionIds).ApplyToSpeak(UId, UserName); + } } diff --git a/WGShare.API/Hubs/IMessageClient.cs b/WGShare.API/Hubs/IMessageClient.cs index a512e75..8801d2a 100644 --- a/WGShare.API/Hubs/IMessageClient.cs +++ b/WGShare.API/Hubs/IMessageClient.cs @@ -110,5 +110,19 @@ namespace WGShare.API.Hubs /// /// Task ForceLogout(string msg); + + /// + /// 用户申请发言 + /// + /// + /// + /// + Task ApplyToSpeak(string uid, string uname); + + /// + /// 接受监控用户 + /// + /// + void Watch(string[] watchUids); } } diff --git a/WGShare.API/Hubs/SessionManageHub.cs b/WGShare.API/Hubs/SessionManageHub.cs index 380c424..ca3f5d1 100644 --- a/WGShare.API/Hubs/SessionManageHub.cs +++ b/WGShare.API/Hubs/SessionManageHub.cs @@ -12,6 +12,7 @@ using WGShare.API.Helpers; using WGShare.Domain.Constant; using WGShare.Domain.Entities; using WGShare.Domain.Enums; +using ZstdSharp.Unsafe; namespace WGShare.API.Hubs { @@ -34,7 +35,7 @@ namespace WGShare.API.Hubs Console.WriteLine($"{DateTime.Now}连接成功 当前租户:{tenant} account:{account} uid:{uid} connectId:{Context.ConnectionId}"); - await ClearUserChannel(uid, tenant, account, ssid); + await ClearUserChannel(uid, tenant, account, ssid, false); // 存储在线信息 RedisHelper.Instance.HSet(RedisKeyConstant.SessionManage.GetOnlineUserKey(tenant), uid, Context.ConnectionId); } @@ -53,13 +54,13 @@ namespace WGShare.API.Hubs { Console.WriteLine($"{DateTime.Now}断开连接 未重连,开始删除用户频道信息"); // 断开后未重连则清退频道 - await ClearUserChannel(uid, tenant, account, ssid); + await ClearUserChannel(uid, tenant, account, ssid, true); } } - private async Task ClearUserChannel(string uid, string tenant, string account, string ssid) + private async Task ClearUserChannel(string uid, string tenant, string account, string ssid, bool isclose) { Console.WriteLine($"{DateTime.Now} 执行删除开始"); // 获取用户参加得频道 @@ -90,6 +91,10 @@ namespace WGShare.API.Hubs pipe.EndPipe(); } + if (!string.IsNullOrEmpty(roomNum)) + // 通知房间其他用户,该用户退出 + await Clients.Group(roomNum).UserLeave(uid); + Console.WriteLine($"{DateTime.Now} 执行删除完成"); } diff --git a/WGShare.API/WGShare.API.xml b/WGShare.API/WGShare.API.xml index 6355caf..11aec50 100644 --- a/WGShare.API/WGShare.API.xml +++ b/WGShare.API/WGShare.API.xml @@ -24,12 +24,6 @@ - - - 匿名登录,直接进入会议室 - - - 登出(暂未处理任何业务逻辑) @@ -210,7 +204,7 @@ - + 申请发言 @@ -476,6 +470,14 @@ + + + 用户申请发言 + + + + + 加入频道 diff --git a/WGShare.Domain/Entities/ChannelUserInfo.cs b/WGShare.Domain/Entities/ChannelUserInfo.cs index 5c799e4..cead967 100644 --- a/WGShare.Domain/Entities/ChannelUserInfo.cs +++ b/WGShare.Domain/Entities/ChannelUserInfo.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using WGShare.Domain.Enums; namespace WGShare.Domain.Entities {