From d6b0c56c35535501afdde4d1be8a0a69c47b3b0d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=A9=99=E5=AD=90?= <454313500@qq.com>
Date: Sat, 30 Apr 2022 21:48:18 +0800
Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=99=BB=E5=BD=95=E7=94=A8?=
=?UTF-8?q?=E6=88=B7=E5=85=A8=E9=83=A8=E4=BF=A1=E6=81=AF=E6=9F=A5=E8=AF=A2?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../Config/SwaggerDoc.xml | 6 ++++
.../Controllers/AccountController.cs | 15 +++++++++
.../Yi.Framework.DTOModel/UserRoleMenuDto.cs | 17 ++++++++++
.../Yi.Framework.Interface/IUserService.cs | 8 +++++
.../Yi.Framework.Service/UserService.cs | 33 +++++++++++++++++++
.../Yi.Framework.WebCore/CommonExtend.cs | 21 +++++++++---
Yi.Vue2.x/src/views/AdmRoleMenu.vue | 2 +-
7 files changed, 97 insertions(+), 5 deletions(-)
create mode 100644 Yi.Framework.Net6/Yi.Framework.DTOModel/UserRoleMenuDto.cs
diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Config/SwaggerDoc.xml b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Config/SwaggerDoc.xml
index 030ce0cd..42b057aa 100644
--- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Config/SwaggerDoc.xml
+++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Config/SwaggerDoc.xml
@@ -9,6 +9,12 @@
账户管理
+
+
+ 通过已登录的用户获取用户信息及菜单
+
+
+
Json To Sql 类比模式,通用模型
diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/AccountController.cs b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/AccountController.cs
index 8ce42cb1..b71d5d22 100644
--- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/AccountController.cs
+++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/AccountController.cs
@@ -55,5 +55,20 @@ namespace Yi.Framework.ApiMicroservice.Controllers
}
return Result.SuccessError("注册失败!用户名已存在!");
}
+
+
+
+ ///
+ /// 通过已登录的用户获取用户信息及菜单
+ ///
+ ///
+ [HttpGet]
+ public async Task GetUserAllInfo()
+ {
+ //通过鉴权jwt获取到用户的id
+ var userId=HttpContext.GetCurrentUserEntityInfo(out _).Id;
+
+ return Result.Success().SetData(await _iUserService.GetUserAllInfo(userId));
+ }
}
}
diff --git a/Yi.Framework.Net6/Yi.Framework.DTOModel/UserRoleMenuDto.cs b/Yi.Framework.Net6/Yi.Framework.DTOModel/UserRoleMenuDto.cs
new file mode 100644
index 00000000..bebab074
--- /dev/null
+++ b/Yi.Framework.Net6/Yi.Framework.DTOModel/UserRoleMenuDto.cs
@@ -0,0 +1,17 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Yi.Framework.Model.Models;
+
+namespace Yi.Framework.DTOModel
+{
+ public class UserRoleMenuDto
+ {
+ public UserEntity User { get; set; }=new ();
+ public HashSet Roles { get; set; } = new();
+ public HashSet Menus { get; set; }=new();
+
+ }
+}
diff --git a/Yi.Framework.Net6/Yi.Framework.Interface/IUserService.cs b/Yi.Framework.Net6/Yi.Framework.Interface/IUserService.cs
index 432664a7..1017b157 100644
--- a/Yi.Framework.Net6/Yi.Framework.Interface/IUserService.cs
+++ b/Yi.Framework.Net6/Yi.Framework.Interface/IUserService.cs
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
+using Yi.Framework.DTOModel;
using Yi.Framework.Model.Models;
using Yi.Framework.Repository;
@@ -59,5 +60,12 @@ namespace Yi.Framework.Interface
///
///
Task> GetRoleListByUserId(long userId);
+
+ ///
+ /// 获取当前登录用户的所有信息
+ ///
+ ///
+ ///
+ Task GetUserAllInfo(long userId);
}
}
diff --git a/Yi.Framework.Net6/Yi.Framework.Service/UserService.cs b/Yi.Framework.Net6/Yi.Framework.Service/UserService.cs
index 886dcf38..a7c9e0f1 100644
--- a/Yi.Framework.Net6/Yi.Framework.Service/UserService.cs
+++ b/Yi.Framework.Net6/Yi.Framework.Service/UserService.cs
@@ -3,6 +3,7 @@ using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
+using Yi.Framework.DTOModel;
using Yi.Framework.Interface;
using Yi.Framework.Model.Models;
using Yi.Framework.Repository;
@@ -102,5 +103,37 @@ namespace Yi.Framework.Service
{
return (await _repository._Db.Queryable().Includes(u => u.Roles).InSingleAsync(userId)).Roles;
}
+
+ public async Task GetUserAllInfo(long userId)
+ {
+
+ var userRoleMenu = new UserRoleMenuDto();
+ //首先获取到该用户全部信息,导航到角色、菜单,(菜单需要去重,完全交给Set来处理即可)
+
+ //得到用户
+ var user = await _repository._Db.Queryable().Includes(u => u.Roles, r => r.Menus).InSingleAsync(userId);
+
+ //得到角色集合
+ var roleList = user.Roles;
+
+ //得到菜单集合
+ foreach (var role in roleList)
+ {
+ foreach (var menu in role.Menus)
+ {
+ userRoleMenu.Menus.Add(menu);
+ }
+ //刚好可以去除一下多余的导航属性
+ role.Menus = null;
+ userRoleMenu.Roles.Add(role);
+ }
+
+ user.Roles = null;
+ userRoleMenu.User = user;
+
+ return userRoleMenu;
+
+
+ }
}
}
diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/CommonExtend.cs b/Yi.Framework.Net6/Yi.Framework.WebCore/CommonExtend.cs
index 794371bf..6e3af2a7 100644
--- a/Yi.Framework.Net6/Yi.Framework.WebCore/CommonExtend.cs
+++ b/Yi.Framework.Net6/Yi.Framework.WebCore/CommonExtend.cs
@@ -8,6 +8,7 @@ using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Model.Models;
+using System.IdentityModel.Tokens.Jwt;
namespace Yi.Framework.WebCore
{
@@ -32,9 +33,21 @@ namespace Yi.Framework.WebCore
///
public static UserEntity GetCurrentUserEntityInfo(this HttpContext httpContext, out List menuIds)
{
- IEnumerable claimlist = httpContext.AuthenticateAsync().Result.Principal.Claims;
-
- long.TryParse(claimlist.FirstOrDefault(u => u.Type == ClaimTypes.Sid).Value,out var resId) ;
+ IEnumerable claimlist = null;
+ long resId = 0;
+ try
+ {
+
+ claimlist = httpContext.AuthenticateAsync().Result.Principal.Claims;
+ resId = Convert.ToInt64(claimlist.FirstOrDefault(u => u.Type == JwtRegisteredClaimNames.Sid).Value);
+
+ }
+ catch
+ {
+ throw new Exception("未授权,Token鉴权失败!");
+ }
+
+
menuIds = claimlist.Where(u => u.Type == "menuIds").ToList().Select(u => new Guid(u.Value)).ToList();
@@ -42,7 +55,7 @@ namespace Yi.Framework.WebCore
return new UserEntity()
{
Id = resId,
- Name = claimlist.FirstOrDefault(u => u.Type == ClaimTypes.Name).Value
+ //Name = claimlist.FirstOrDefault(u => u.Type == JwtRegisteredClaimNames.Name).Value
};
}
}
diff --git a/Yi.Vue2.x/src/views/AdmRoleMenu.vue b/Yi.Vue2.x/src/views/AdmRoleMenu.vue
index f83cfb3c..aa1d5690 100644
--- a/Yi.Vue2.x/src/views/AdmRoleMenu.vue
+++ b/Yi.Vue2.x/src/views/AdmRoleMenu.vue
@@ -48,7 +48,7 @@
item-text="menuName"
>
- id:{{ item.id }}
+ 权限:{{ item.permissionCode }}