菜单到前端路由转换
This commit is contained in:
@@ -31,7 +31,13 @@
|
||||
</member>
|
||||
<member name="M:Yi.Framework.ApiMicroservice.Controllers.AccountController.GetUserAllInfo">
|
||||
<summary>
|
||||
通过已登录的用户获取用户信息及菜单
|
||||
通过已登录的用户获取用户信息
|
||||
</summary>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:Yi.Framework.ApiMicroservice.Controllers.AccountController.GetRouterInfo">
|
||||
<summary>
|
||||
获取当前登录用户的前端路由
|
||||
</summary>
|
||||
<returns></returns>
|
||||
</member>
|
||||
|
||||
@@ -5,6 +5,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Common.Enum;
|
||||
using Yi.Framework.Common.Helper;
|
||||
using Yi.Framework.Common.Models;
|
||||
using Yi.Framework.Core;
|
||||
@@ -27,12 +28,12 @@ namespace Yi.Framework.ApiMicroservice.Controllers
|
||||
{
|
||||
private IUserService _iUserService;
|
||||
private JwtInvoker _jwtInvoker;
|
||||
private ILogger _logger;
|
||||
private ILogger _logger;
|
||||
public AccountController(ILogger<UserEntity> logger, IUserService iUserService, JwtInvoker jwtInvoker)
|
||||
{
|
||||
_iUserService = iUserService;
|
||||
_jwtInvoker = jwtInvoker;
|
||||
_logger = logger;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -51,12 +52,14 @@ namespace Yi.Framework.ApiMicroservice.Controllers
|
||||
UserEntity user = new();
|
||||
if (await _iUserService.Login(loginDto.UserName, loginDto.Password, o => user = o))
|
||||
{
|
||||
var userRoleMenu= await _iUserService.GetUserAllInfo(user.Id);
|
||||
return Result.Success("登录成功!").SetData(new { token = _jwtInvoker.GetAccessToken(userRoleMenu.User,userRoleMenu.Menus) });
|
||||
var userRoleMenu = await _iUserService.GetUserAllInfo(user.Id);
|
||||
return Result.Success("登录成功!").SetData(new { token = _jwtInvoker.GetAccessToken(userRoleMenu.User, userRoleMenu.Menus) });
|
||||
}
|
||||
return Result.Error("登录失败!用户名或者密码错误!");
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 没啥说,注册
|
||||
/// </summary>
|
||||
@@ -79,13 +82,13 @@ namespace Yi.Framework.ApiMicroservice.Controllers
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public Result Logout()
|
||||
public Result Logout()
|
||||
{
|
||||
return Result.Success("安全登出成功!");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过已登录的用户获取用户信息及菜单
|
||||
/// 通过已登录的用户获取用户信息
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
@@ -99,6 +102,20 @@ namespace Yi.Framework.ApiMicroservice.Controllers
|
||||
return Result.Success().SetData(data);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前登录用户的前端路由
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public async Task<Result> GetRouterInfo()
|
||||
{
|
||||
var userId = HttpContext.GetCurrentUserEntityInfo(out _).Id;
|
||||
var data = await _iUserService.GetUserAllInfo(userId);
|
||||
|
||||
//将后端菜单转换成前端路由,组件级别需要过滤
|
||||
List<VueRouterModel> routers = _iUserService.RouterBuild(data.Menus.ToList());
|
||||
return Result.Success().SetData(routers);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -37,9 +37,7 @@ namespace Yi.Framework.ApiMicroservice.Controllers
|
||||
[HttpGet]
|
||||
public async Task<Result> GetList([FromQuery] MenuEntity menu)
|
||||
{
|
||||
var p= await _iMenuService.SelctGetList(menu);
|
||||
p.ForEach(m => m.Children = new List<MenuEntity>());
|
||||
return Result.Success().SetData(p);
|
||||
return Result.Success().SetData(await _iMenuService.SelctGetList(menu));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Binary file not shown.
@@ -27,7 +27,7 @@ namespace Yi.Framework.Common.Models
|
||||
{
|
||||
public string Title { get; set; }
|
||||
public string Icon { get; set; }
|
||||
public string NoCache { get; set; }
|
||||
public bool NoCache { get; set; }
|
||||
public string link { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,5 +84,12 @@ namespace Yi.Framework.Interface
|
||||
/// <param name="page"></param>
|
||||
/// <returns></returns>
|
||||
Task<PageModel<List<UserEntity>>> SelctPageList(UserEntity user, PageParModel page);
|
||||
|
||||
/// <summary>
|
||||
/// 菜单构建前端路由
|
||||
/// </summary>
|
||||
/// <param name="menus"></param>
|
||||
/// <returns></returns>
|
||||
List<VueRouterModel> RouterBuild(List<MenuEntity> menus);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Common.Enum;
|
||||
using Yi.Framework.Common.Helper;
|
||||
using Yi.Framework.Common.Models;
|
||||
using Yi.Framework.DTOModel;
|
||||
@@ -129,13 +130,13 @@ namespace Yi.Framework.Service
|
||||
foreach (var menu in role.Menus)
|
||||
{
|
||||
|
||||
|
||||
if (!string.IsNullOrEmpty(menu.PermissionCode))
|
||||
{
|
||||
userRoleMenu.PermissionCodes.Add(menu.PermissionCode);
|
||||
userRoleMenu.Menus.Add(menu);
|
||||
}
|
||||
|
||||
|
||||
if (!string.IsNullOrEmpty(menu.PermissionCode))
|
||||
{
|
||||
userRoleMenu.PermissionCodes.Add(menu.PermissionCode);
|
||||
userRoleMenu.Menus.Add(menu);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -178,5 +179,48 @@ namespace Yi.Framework.Service
|
||||
|
||||
return new PageModel<List<UserEntity>>(data, total);
|
||||
}
|
||||
|
||||
|
||||
public List<VueRouterModel> RouterBuild(List<MenuEntity> menus)
|
||||
{
|
||||
menus = menus.Where(m => m.MenuType != null && m.MenuType != MenuTypeEnum.Component.GetHashCode()).ToList();
|
||||
List<VueRouterModel> routers = new();
|
||||
foreach (var m in menus)
|
||||
{
|
||||
var r = new VueRouterModel();
|
||||
var routerName = m.Router.Split("/").LastOrDefault();
|
||||
//开头大写
|
||||
r.Name = routerName.First().ToString().ToUpper() + routerName.Substring(1);
|
||||
r.Path = m.Router;
|
||||
r.Hidden = (bool)!m.IsShow;
|
||||
|
||||
|
||||
if (m.MenuType == MenuTypeEnum.Catalogue.GetHashCode())
|
||||
{
|
||||
r.Redirect = "noRedirect";
|
||||
r.AlwaysShow = true;
|
||||
r.Component = "Layout";
|
||||
}
|
||||
if (m.MenuType == MenuTypeEnum.Menu.GetHashCode())
|
||||
{
|
||||
r.Component = m.Component;
|
||||
}
|
||||
|
||||
|
||||
r.Meta = new Meta
|
||||
{
|
||||
Title = m.MenuName,
|
||||
Icon = m.MenuIcon,
|
||||
NoCache = (bool)!m.IsCache
|
||||
};
|
||||
if ((bool)m.IsLink)
|
||||
{
|
||||
r.Meta.link = m.Router;
|
||||
}
|
||||
|
||||
routers.Add(r);
|
||||
}
|
||||
return routers;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,7 +158,7 @@
|
||||
<el-input-number v-model="form.orderNum" controls-position="right" :min="0" />
|
||||
</el-form-item>
|
||||
</el-col>
|
||||
<el-col :span="12" v-if="form.menuType != 1">
|
||||
<el-col :span="12" v-if="form.menuType != 2">
|
||||
<el-form-item>
|
||||
<template #label>
|
||||
<span>
|
||||
@@ -175,7 +175,7 @@
|
||||
</el-col>
|
||||
|
||||
|
||||
<el-col :span="12" v-if="form.menuType != 1">
|
||||
<el-col :span="12" v-if="form.menuType != 2">
|
||||
<el-form-item prop="router">
|
||||
<template #label>
|
||||
<span>
|
||||
@@ -188,7 +188,7 @@
|
||||
<el-input v-model="form.router" placeholder="请输入路由地址" />
|
||||
</el-form-item>
|
||||
</el-col>
|
||||
<el-col :span="12" v-if="form.menuType == 2">
|
||||
<el-col :span="12" v-if="form.menuType == 1">
|
||||
<el-form-item prop="component">
|
||||
<template #label>
|
||||
<span>
|
||||
@@ -214,7 +214,7 @@
|
||||
</template>
|
||||
</el-form-item>
|
||||
</el-col>
|
||||
<el-col :span="12" v-if="form.menuType == 2">
|
||||
<el-col :span="12" v-if="form.menuType == 1">
|
||||
<el-form-item>
|
||||
<el-input v-model="form.query" placeholder="请输入路由参数" maxlength="255" />
|
||||
<template #label>
|
||||
@@ -227,7 +227,7 @@
|
||||
</template>
|
||||
</el-form-item>
|
||||
</el-col>
|
||||
<el-col :span="12" v-if="form.menuType == 2">
|
||||
<el-col :span="12" v-if="form.menuType == 1">
|
||||
<el-form-item>
|
||||
<template #label>
|
||||
<span>
|
||||
@@ -243,7 +243,7 @@
|
||||
</el-radio-group>
|
||||
</el-form-item>
|
||||
</el-col>
|
||||
<el-col :span="12" v-if="form.menuType != 1">
|
||||
<el-col :span="12" v-if="form.menuType != 2">
|
||||
<el-form-item>
|
||||
<template #label>
|
||||
<span>
|
||||
@@ -262,7 +262,7 @@
|
||||
</el-radio-group>
|
||||
</el-form-item>
|
||||
</el-col>
|
||||
<el-col :span="12" v-if="form.menuType != 1">
|
||||
<el-col :span="12" v-if="form.menuType != 2">
|
||||
<el-form-item>
|
||||
<template #label>
|
||||
<span>
|
||||
|
||||
Reference in New Issue
Block a user