Skip to content

Commit

Permalink
Optimize profile api json structure
Browse files Browse the repository at this point in the history
  • Loading branch information
lampo1024 committed May 5, 2019
1 parent 526af50 commit 56a3c35
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 57 deletions.
57 changes: 19 additions & 38 deletions DncZeus.Api/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
******************************************/

using DncZeus.Api.Entities;
using DncZeus.Api.Entities.Enums;
using DncZeus.Api.Extensions;
using DncZeus.Api.Extensions.AuthContext;
using DncZeus.Api.Extensions.DataAccess;
using DncZeus.Api.ViewModels.Rbac.DncMenu;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
Expand Down Expand Up @@ -66,32 +64,15 @@ public IActionResult Profile()
WHERE P.IsDeleted=0 AND P.Status=1";
}
var permissions = _dbContext.DncPermissionWithMenu.FromSql(sqlPermission, user.Guid).ToList();
var allowPages = new List<string> { };

if (user.UserType == UserType.SuperAdministrator)
{
allowPages.AddRange(menus.Select(x => x.Alias));
}
else
{
allowPages.AddRange(menus.Where(x => x.IsDefaultRouter == YesOrNo.Yes).Select(x => x.Alias));
foreach (var permission in permissions.Where(x => x.PermissionType == PermissionType.Menu))
{
allowPages.AddRange(FindParentMenuAlias(menus, permission.MenuGuid));
}
}

//var allowPages = FindParentMenuAlias(menus);
var pages = allowPages.Distinct().ToList();
var pagePermissions = permissions.GroupBy(x => x.MenuAlias).ToDictionary(g => g.Key, g => g.Select(x => x.PermissionActionCode));
var pagePermissions = permissions.GroupBy(x => x.MenuAlias).ToDictionary(g => g.Key, g => g.Select(x => x.PermissionActionCode).Distinct());
response.SetData(new
{
access = new string[] { },
avator = user.Avatar,
user_guid = user.Guid,
user_name = user.DisplayName,
user_type = user.UserType,
pages, // =new[] { "rbac", "rbac_user_page", "rbac_menu_page", "rbac_role_page", "rbac_permission_page", "rbac_role_permission_page" },
permissions = pagePermissions

});
Expand Down Expand Up @@ -141,17 +122,15 @@ public IActionResult Menu()
strSql = @"SELECT * FROM DncMenu WHERE IsDeleted=0 AND Status=1";
}
var menus = _dbContext.DncMenu.FromSql(strSql, AuthContextService.CurrentUser.Guid).ToList();
var rootMenus = _dbContext.DncMenu.Where(x => x.IsDeleted == IsDeleted.No && x.Status == Status.Normal && x.ParentGuid==Guid.Empty).ToList();
var rootMenus = _dbContext.DncMenu.Where(x => x.IsDeleted == IsDeleted.No && x.Status == Status.Normal && x.ParentGuid == Guid.Empty).ToList();
foreach (var root in rootMenus)
{
if (menus.Exists(x => x.Guid == root.Guid))
{
continue;
}
}
menus.Add(root);
}
//var menus = _dbContext.DncMenu.Where(x => x.IsDeleted == IsDeleted.No && x.Status == Status.Normal).ToList();
//menus.AddRange(rootMenus);
var menu = MenuItemHelper.LoadMenuTree(menus, "0");
return Ok(menu);
}
Expand Down Expand Up @@ -180,20 +159,19 @@ public static List<MenuItem> BuildTree(this List<MenuItem> menus, string selecte
Guid = x.Guid,
ParentId = x.ParentId,
Children = build(x.Guid),
Component = x.Component,
Component = x.Component ?? "Main",
Name = x.Name,
Path = x.Path,
Meta = new MenuMeta
{
ConfirmBeforeClose = x.MetaConfirmBeforeClose,
HideInMenu = x.MetaHideInMenu,
Icon = x.MetaIcon,
NotCache = x.MetaNotCache,
Title = x.MetaTitle,
Permission = x.MetaPermission
BeforeCloseFun = x.Meta.BeforeCloseFun,
HideInMenu = x.Meta.HideInMenu,
Icon = x.Meta.Icon,
NotCache = x.Meta.NotCache,
Title = x.Meta.Title,
Permission = x.Meta.Permission
}
})
.ToList();
}).ToList();
};
var result = build(selectedGuid);
return result;
Expand All @@ -208,11 +186,14 @@ public static List<MenuItem> LoadMenuTree(List<DncMenu> menus, string selectedGu
Name = x.Alias,
Path = $"/{x.Url}",
Component = x.Component,
MetaTitle = x.Name,
MetaIcon = x.Icon,
MetaHideInMenu = x.HideInMenu== YesOrNo.Yes,
MetaConfirmBeforeClose = false,
MetaNotCache = x.NotCache== YesOrNo.Yes
Meta = new MenuMeta
{
BeforeCloseFun = x.BeforeCloseFun ?? "",
HideInMenu = x.HideInMenu == YesOrNo.Yes,
Icon = x.Icon,
NotCache = x.NotCache == YesOrNo.Yes,
Title = x.Name
}
}).ToList();
var tree = temp.BuildTree(selectedGuid);
return tree;
Expand Down
17 changes: 2 additions & 15 deletions DncZeus.Api/ViewModels/Rbac/DncMenu/MenuItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,13 @@ public MenuItem()
{
Meta = new MenuMeta();
Children = new List<MenuItem>();
MetaPermission = new List<string>();
}
public string Guid { get; set; }
public string Name { get; set; }
public string Path { get; set; }
public string Component { get; set; }
public string ParentId { get; set; }

public bool MetaHideInMenu { get; set; }
public string MetaIcon { get; set; }
public bool MetaNotCache { get; set; }
public string MetaTitle { get; set; }
public List<string> MetaPermission { get; set; }
/// <summary>
/// 关闭前是否确认
/// </summary>
public bool MetaConfirmBeforeClose { get; set; }

public MenuMeta Meta { get; set; }
public List<MenuItem> Children { get; set; }
}
Expand All @@ -35,15 +24,13 @@ public class MenuMeta
public MenuMeta()
{
Permission = new List<string>();
BeforeCloseFun = "";
}
public bool HideInMenu { get; set; }
public string Icon { get; set; }
public bool NotCache { get; set; }
public string Title { get; set; }
public List<string> Permission { get; set; }
/// <summary>
/// 关闭前是否确认
/// </summary>
public bool ConfirmBeforeClose { get; set; }
public string BeforeCloseFun { get; set; }
}
}
7 changes: 3 additions & 4 deletions DncZeus.App/src/store/module/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export default {
messageReadedList: [],
messageTrashList: [],
messageContentStore: {},
pages: [],
permissions: {},
profile: {}
},
Expand All @@ -55,7 +54,7 @@ export default {
},
//设置用户可以访问的页面编码列表
setPages(state, pages) {
state.pages = pages;
//state.pages = pages;
},
//设置用户可以访问页面的权限集合
setPermissions(state, permissions) {
Expand Down Expand Up @@ -149,7 +148,7 @@ export default {
// 如果你的退出登录无需请求接口,则可以直接使用下面三行代码而无需使用logout调用接口
commit('setToken', '')
commit('setAccess', [])
commit('setPages', [])
//commit('setPages', [])
commit('setPermissions', {})
setTagNavListInLocalstorage([]);
resolve()
Expand All @@ -169,7 +168,7 @@ export default {
commit('setUserName', data.user_name)
commit('setUserGuid', data.user_guid)
commit('setAccess', data.access)
commit('setPages', getUnion(data.pages, staticRouters))
//commit('setPages', getUnion(data.pages, staticRouters))
commit('setPermissions', data.permissions)
commit("setUserType", data.user_type);
commit('setHasGetInfo', true)
Expand Down

0 comments on commit 56a3c35

Please sign in to comment.