添加登录用户全部信息查询
This commit is contained in:
@@ -9,6 +9,12 @@
|
|||||||
账户管理
|
账户管理
|
||||||
</summary>
|
</summary>
|
||||||
</member>
|
</member>
|
||||||
|
<member name="M:Yi.Framework.ApiMicroservice.Controllers.AccountController.GetUserAllInfo">
|
||||||
|
<summary>
|
||||||
|
通过已登录的用户获取用户信息及菜单
|
||||||
|
</summary>
|
||||||
|
<returns></returns>
|
||||||
|
</member>
|
||||||
<member name="T:Yi.Framework.ApiMicroservice.Controllers.BaseCrudController`1">
|
<member name="T:Yi.Framework.ApiMicroservice.Controllers.BaseCrudController`1">
|
||||||
<summary>
|
<summary>
|
||||||
Json To Sql 类比模式,通用模型
|
Json To Sql 类比模式,通用模型
|
||||||
|
|||||||
@@ -55,5 +55,20 @@ namespace Yi.Framework.ApiMicroservice.Controllers
|
|||||||
}
|
}
|
||||||
return Result.SuccessError("注册失败!用户名已存在!");
|
return Result.SuccessError("注册失败!用户名已存在!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 通过已登录的用户获取用户信息及菜单
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpGet]
|
||||||
|
public async Task<Result> GetUserAllInfo()
|
||||||
|
{
|
||||||
|
//通过鉴权jwt获取到用户的id
|
||||||
|
var userId=HttpContext.GetCurrentUserEntityInfo(out _).Id;
|
||||||
|
|
||||||
|
return Result.Success().SetData(await _iUserService.GetUserAllInfo(userId));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
17
Yi.Framework.Net6/Yi.Framework.DTOModel/UserRoleMenuDto.cs
Normal file
17
Yi.Framework.Net6/Yi.Framework.DTOModel/UserRoleMenuDto.cs
Normal file
@@ -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<RoleEntity> Roles { get; set; } = new();
|
||||||
|
public HashSet<MenuEntity> Menus { get; set; }=new();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Yi.Framework.DTOModel;
|
||||||
using Yi.Framework.Model.Models;
|
using Yi.Framework.Model.Models;
|
||||||
using Yi.Framework.Repository;
|
using Yi.Framework.Repository;
|
||||||
|
|
||||||
@@ -59,5 +60,12 @@ namespace Yi.Framework.Interface
|
|||||||
/// <param name="userId"></param>
|
/// <param name="userId"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task<List<RoleEntity>> GetRoleListByUserId(long userId);
|
Task<List<RoleEntity>> GetRoleListByUserId(long userId);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取当前登录用户的所有信息
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="userId"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<UserRoleMenuDto> GetUserAllInfo(long userId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Yi.Framework.DTOModel;
|
||||||
using Yi.Framework.Interface;
|
using Yi.Framework.Interface;
|
||||||
using Yi.Framework.Model.Models;
|
using Yi.Framework.Model.Models;
|
||||||
using Yi.Framework.Repository;
|
using Yi.Framework.Repository;
|
||||||
@@ -102,5 +103,37 @@ namespace Yi.Framework.Service
|
|||||||
{
|
{
|
||||||
return (await _repository._Db.Queryable<UserEntity>().Includes(u => u.Roles).InSingleAsync(userId)).Roles;
|
return (await _repository._Db.Queryable<UserEntity>().Includes(u => u.Roles).InSingleAsync(userId)).Roles;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<UserRoleMenuDto> GetUserAllInfo(long userId)
|
||||||
|
{
|
||||||
|
|
||||||
|
var userRoleMenu = new UserRoleMenuDto();
|
||||||
|
//首先获取到该用户全部信息,导航到角色、菜单,(菜单需要去重,完全交给Set来处理即可)
|
||||||
|
|
||||||
|
//得到用户
|
||||||
|
var user = await _repository._Db.Queryable<UserEntity>().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;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ using System.Security.Claims;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Yi.Framework.Model.Models;
|
using Yi.Framework.Model.Models;
|
||||||
|
using System.IdentityModel.Tokens.Jwt;
|
||||||
|
|
||||||
namespace Yi.Framework.WebCore
|
namespace Yi.Framework.WebCore
|
||||||
{
|
{
|
||||||
@@ -32,9 +33,21 @@ namespace Yi.Framework.WebCore
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static UserEntity GetCurrentUserEntityInfo(this HttpContext httpContext, out List<Guid> menuIds)
|
public static UserEntity GetCurrentUserEntityInfo(this HttpContext httpContext, out List<Guid> menuIds)
|
||||||
{
|
{
|
||||||
IEnumerable<Claim> claimlist = httpContext.AuthenticateAsync().Result.Principal.Claims;
|
IEnumerable<Claim> 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鉴权失败!");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
long.TryParse(claimlist.FirstOrDefault(u => u.Type == ClaimTypes.Sid).Value,out var resId) ;
|
|
||||||
|
|
||||||
menuIds = claimlist.Where(u => u.Type == "menuIds").ToList().Select(u => new Guid(u.Value)).ToList();
|
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()
|
return new UserEntity()
|
||||||
{
|
{
|
||||||
Id = resId,
|
Id = resId,
|
||||||
Name = claimlist.FirstOrDefault(u => u.Type == ClaimTypes.Name).Value
|
//Name = claimlist.FirstOrDefault(u => u.Type == JwtRegisteredClaimNames.Name).Value
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,7 +48,7 @@
|
|||||||
item-text="menuName"
|
item-text="menuName"
|
||||||
>
|
>
|
||||||
<template v-slot:append="{ item }">
|
<template v-slot:append="{ item }">
|
||||||
<v-btn>id:{{ item.id }}</v-btn>
|
<v-btn>权限:{{ item.permissionCode }}</v-btn>
|
||||||
</template>
|
</template>
|
||||||
</v-treeview>
|
</v-treeview>
|
||||||
</v-card></v-col
|
</v-card></v-col
|
||||||
|
|||||||
Reference in New Issue
Block a user