添加登录用户全部信息查询

This commit is contained in:
橙子
2022-04-30 21:48:18 +08:00
parent d8fe983b9d
commit d6b0c56c35
7 changed files with 97 additions and 5 deletions

View File

@@ -9,6 +9,12 @@
账户管理
</summary>
</member>
<member name="M:Yi.Framework.ApiMicroservice.Controllers.AccountController.GetUserAllInfo">
<summary>
通过已登录的用户获取用户信息及菜单
</summary>
<returns></returns>
</member>
<member name="T:Yi.Framework.ApiMicroservice.Controllers.BaseCrudController`1">
<summary>
Json To Sql 类比模式,通用模型

View File

@@ -55,5 +55,20 @@ namespace Yi.Framework.ApiMicroservice.Controllers
}
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));
}
}
}

View 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();
}
}

View File

@@ -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
/// <param name="userId"></param>
/// <returns></returns>
Task<List<RoleEntity>> GetRoleListByUserId(long userId);
/// <summary>
/// 获取当前登录用户的所有信息
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
Task<UserRoleMenuDto> GetUserAllInfo(long userId);
}
}

View File

@@ -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<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;
}
}
}

View File

@@ -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
/// <returns></returns>
public static UserEntity GetCurrentUserEntityInfo(this HttpContext httpContext, out List<Guid> menuIds)
{
IEnumerable<Claim> claimlist = httpContext.AuthenticateAsync().Result.Principal.Claims;
long.TryParse(claimlist.FirstOrDefault(u => u.Type == ClaimTypes.Sid).Value,out var resId) ;
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鉴权失败");
}
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
};
}
}

View File

@@ -48,7 +48,7 @@
item-text="menuName"
>
<template v-slot:append="{ item }">
<v-btn>id:{{ item.id }}</v-btn>
<v-btn>权限:{{ item.permissionCode }}</v-btn>
</template>
</v-treeview>
</v-card></v-col