Skip to content

Commit

Permalink
最后更新时间。小于该更新时间的菜单设置将被覆盖。一般应用于区域类,表明代码已修改菜单参数,希望强行覆盖已有菜单设置
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Jan 17, 2024
1 parent 1488dd1 commit 53bf890
Show file tree
Hide file tree
Showing 18 changed files with 93 additions and 82 deletions.
2 changes: 1 addition & 1 deletion NewLife.Cube/Areas/Admin/AdminAreaRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace NewLife.Cube.Areas.Admin;

/// <summary>权限管理区域注册</summary>
[DisplayName("系统管理")]
[Menu(-1, true, Icon = "fa-desktop")]
[Menu(-1, true, Icon = "fa-desktop", LastUpdate = "20240118")]
public class AdminArea : AreaBase
{
/// <inheritdoc />
Expand Down
11 changes: 3 additions & 8 deletions NewLife.Cube/Areas/Admin/Controllers/IndexController.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
using System;
using System.ComponentModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Numerics;
using System.Reflection;
using System.Reflection.PortableExecutable;
using System.Runtime;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.DataProtection.KeyManagement;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NewLife.Cube.ViewModels;
using NewLife.Log;
using NewLife.Reflection;
using NewLife.Web;
using XCode.Membership;
using static System.Net.Mime.MediaTypeNames;

namespace NewLife.Cube.Areas.Admin.Controllers;

/// <summary>首页</summary>
[AdminArea]
[DisplayName("首页")]
[AdminArea]
[Menu(0, false, Icon = "fa-home")]
public class IndexController : ControllerBaseX
{
private readonly IManageProvider _provider;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.AspNetCore.Mvc;
using NewLife.Cube.Entity;
using NewLife.Cube.Entity;
using XCode.Membership;

namespace NewLife.Cube.Areas.Admin.Controllers;
Expand Down
1 change: 0 additions & 1 deletion NewLife.Cube/Areas/Admin/Controllers/OAuthLogController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.ComponentModel;
using Microsoft.AspNetCore.Mvc;
using NewLife.Cube.Entity;
using NewLife.Web;
using XCode;
Expand Down
2 changes: 1 addition & 1 deletion NewLife.Cube/Areas/Admin/Controllers/TenantController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace NewLife.Cube.Areas.Admin.Controllers;

/// <summary>租户管理</summary>
[AdminArea]
[Menu(20, false)]
[Menu(75, true, Icon = "fa-user-circle", Mode = MenuModes.Admin | MenuModes.Tenant)]
public class TenantController : EntityController<Tenant, TenantModel>
{
static TenantController()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace NewLife.Cube.Areas.Admin.Controllers;

/// <summary>租户关系</summary>
[AdminArea]
[Menu(10, false)]
[Menu(10, false, Icon = "fa-users", Mode = MenuModes.Admin | MenuModes.Tenant)]
public class TenantUserController : EntityController<TenantUser, TenantUserModel>
{
static TenantUserController()
Expand Down
2 changes: 1 addition & 1 deletion NewLife.Cube/Areas/Cube/CubeAreaRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace NewLife.Cube.Areas.Cube;

/// <summary>魔方管理区域注册</summary>
[DisplayName("魔方管理")]
[Menu(-2, true, Icon = "fa-tachometer")]
[Menu(-2, true, Icon = "fa-tachometer", LastUpdate = "20240118")]
public class CubeArea : AreaBase
{
/// <inheritdoc />
Expand Down
77 changes: 46 additions & 31 deletions NewLife.Cube/Common/MenuAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,51 @@
using System;
namespace NewLife.Cube;

namespace NewLife.Cube
/// <summary>菜单模式</summary>
[Flags]
public enum MenuModes
{
/// <summary>菜单特性</summary>
public class MenuAttribute : Attribute
/// <summary>管理后台可见</summary>
Admin = 1,

/// <summary>租户可见</summary>
Tenant = 2,
}

/// <summary>菜单特性</summary>
public class MenuAttribute : Attribute
{
#region 属性
/// <summary>
/// 顺序。较大者在前面
/// </summary>
public Int32 Order { get; set; }

/// <summary>
/// 可见
/// </summary>
public Boolean Visible { get; set; } = true;

/// <summary>菜单模式。控制在管理后台和租户模式下是否可见</summary>
public MenuModes Mode { get; set; }

/// <summary>
/// 图标
/// </summary>
public String Icon { get; set; }

/// <summary>最后更新时间。小于该更新时间的菜单设置将被覆盖。</summary>
/// <remarks>一般应用于区域类,表明代码已修改菜单参数,希望强行覆盖已有菜单设置</remarks>
public String LastUpdate { get; set; }
#endregion

/// <summary>
/// 设置菜单特性
/// </summary>
/// <param name="order"></param>
/// <param name="visible"></param>
public MenuAttribute(Int32 order, Boolean visible = true)
{
#region 属性
/// <summary>
/// 顺序。较大者在前面
/// </summary>
public Int32 Order { get; set; }

/// <summary>
/// 可见
/// </summary>
public Boolean Visible { get; set; } = true;

/// <summary>
/// 图标
/// </summary>
public String Icon { get; set; }
#endregion

/// <summary>
/// 设置菜单特性
/// </summary>
/// <param name="order"></param>
/// <param name="visible"></param>
public MenuAttribute(Int32 order, Boolean visible = true)
{
Order = order;
Visible = visible;
}
Order = order;
Visible = visible;
}
}
28 changes: 16 additions & 12 deletions NewLife.Cube/Membership/MenuHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public static IList<IMenu> ScanController(this IMenuFactory menuFactory, String
var controllerTypes = types.Where(e => e.Name.EndsWith("Controller") && e.Namespace == nameSpace).ToList();
if (controllerTypes.Count == 0) return list;

var attArea = areaType.GetCustomAttribute<MenuAttribute>();

// 如果根菜单不存在,则添加
var r = menuFactory.Root;
var root = menuFactory.FindByFullName(nameSpace);
Expand All @@ -40,12 +42,11 @@ public static IList<IMenu> ScanController(this IMenuFactory menuFactory, String
root = r.Add(rootName, null, nameSpace, "/" + rootName);
list.Add(root);

var att = areaType.GetCustomAttribute<MenuAttribute>();
if (att != null && (!root.Visible || !root.Necessary))
if (attArea != null && (!root.Visible || !root.Necessary))
{
root.Sort = att.Order;
root.Visible = att.Visible;
root.Icon = att.Icon;
root.Sort = attArea.Order;
root.Visible = attArea.Visible;
root.Icon = attArea.Icon;
}
}
if (root.FullName != nameSpace) root.FullName = nameSpace;
Expand Down Expand Up @@ -154,24 +155,27 @@ public static IList<IMenu> ScanController(this IMenuFactory menuFactory, String
if (att != null)
{
if (controller.Icon.IsNullOrEmpty()) controller.Icon = att.Icon;

// 小于该更新时间的菜单设置将被覆盖
if ((controller.UpdateTime < att.LastUpdate.ToDateTime() || controller.UpdateTime < attArea.LastUpdate.ToDateTime()) &&
(!controller.Visible || !controller.Necessary))
{
controller.Sort = att.Order;
controller.Visible = att.Visible;
}
}

// 排序
if (controller.Sort == 0)
{
if (att != null)
{
if (!root.Visible || !root.Necessary)
if (!controller.Visible || !controller.Necessary)
{
controller.Sort = att.Order;
controller.Visible = att.Visible;
}
}
else
{
var pi = type.GetPropertyEx("MenuOrder");
if (pi != null) controller.Sort = pi.GetValue(null).ToInt();
}
}
}

Expand Down Expand Up @@ -248,7 +252,7 @@ private static IDictionary<MethodInfo, Int32> ScanActionMenu(Type controllerType
else
{
//var attAuth = method.GetCustomAttribute<EntityAuthorizeAttribute>();
if (attAuth != null && attAuth.Permission > PermissionFlags.None)
if (attAuth != null && attAuth.Permission > PermissionFlags.None)
dic.Add(method, (Int32)attAuth.Permission);
}
}
Expand Down
2 changes: 1 addition & 1 deletion NewLife.CubeNC/Areas/Admin/AdminAreaRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace NewLife.Cube.Admin;

/// <summary>权限管理区域注册</summary>
[DisplayName("系统管理")]
[Menu(-1, true, Icon = "fa-desktop")]
[Menu(-1, true, Icon = "fa-desktop", LastUpdate = "20240118")]
public class AdminArea : AreaBase
{
/// <inheritdoc />
Expand Down
5 changes: 1 addition & 4 deletions NewLife.CubeNC/Areas/Admin/Controllers/LogController.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel;
using Microsoft.AspNetCore.Mvc.Filters;
using NewLife.Cube.ViewModels;
using NewLife.Web;
Expand Down
3 changes: 0 additions & 3 deletions NewLife.CubeNC/Areas/Admin/Controllers/MenuController.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System.ComponentModel;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using NewLife.Cube.ViewModels;
using XCode.Membership;

namespace NewLife.Cube.Admin.Controllers;
Expand Down
1 change: 0 additions & 1 deletion NewLife.CubeNC/Areas/Admin/Controllers/SysController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.ComponentModel;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using NewLife.Common;
using XCode.Membership;
Expand Down
2 changes: 0 additions & 2 deletions NewLife.CubeNC/Areas/Admin/Controllers/TenantController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.ComponentModel;
using Microsoft.AspNetCore.Mvc;
using NewLife.Cube.ViewModels;
using NewLife.Web;
using XCode.Membership;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace NewLife.Cube.Admin.Controllers;

/// <summary>租户关系</summary>
[AdminArea]
[Menu(10, true, Icon = "fa-users", Mode = MenuModes.Admin | MenuModes.Tenant)]
[Menu(10, false, Icon = "fa-users", Mode = MenuModes.Admin | MenuModes.Tenant)]
public class TenantUserController : EntityController<TenantUser>
{
static TenantUserController()
Expand Down
2 changes: 1 addition & 1 deletion NewLife.CubeNC/Areas/Cube/CubeAreaRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace NewLife.Cube.Cube;

/// <summary>魔方管理区域注册</summary>
[DisplayName("魔方管理")]
[Menu(-2, true, Icon = "fa-tachometer")]
[Menu(-2, true, Icon = "fa-tachometer", LastUpdate = "20240118")]
public class CubeArea : AreaBase
{
/// <inheritdoc />
Expand Down
4 changes: 4 additions & 0 deletions NewLife.CubeNC/Common/MenuAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public class MenuAttribute : Attribute
/// 图标
/// </summary>
public String Icon { get; set; }

/// <summary>最后更新时间。小于该更新时间的菜单设置将被覆盖。</summary>
/// <remarks>一般应用于区域类,表明代码已修改菜单参数,希望强行覆盖已有菜单设置</remarks>
public String LastUpdate { get; set; }
#endregion

/// <summary>
Expand Down
26 changes: 15 additions & 11 deletions NewLife.CubeNC/Membership/MenuHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public static IList<IMenu> ScanController(this IMenuFactory menuFactory, String
}
if (controllerTypes.Count == 0) return list;

var attArea = areaType.GetCustomAttribute<MenuAttribute>();

// 如果根菜单不存在,则添加
var r = menuFactory.Root;
var root = menuFactory.FindByFullName(nameSpace);
Expand All @@ -64,12 +66,11 @@ public static IList<IMenu> ScanController(this IMenuFactory menuFactory, String
root = r.Add(rootName, null, nameSpace, "/" + rootName);
list.Add(root);

var att = areaType.GetCustomAttribute<MenuAttribute>();
if (att != null && (!root.Visible || !root.Necessary))
if (attArea != null && (!root.Visible || !root.Necessary))
{
root.Sort = att.Order;
root.Visible = att.Visible;
root.Icon = att.Icon;
root.Sort = attArea.Order;
root.Visible = attArea.Visible;
root.Icon = attArea.Icon;
}
}
if (root.FullName != nameSpace) root.FullName = nameSpace;
Expand Down Expand Up @@ -178,24 +179,27 @@ public static IList<IMenu> ScanController(this IMenuFactory menuFactory, String
if (att != null)
{
if (controller.Icon.IsNullOrEmpty()) controller.Icon = att.Icon;

// 小于该更新时间的菜单设置将被覆盖
if ((controller.UpdateTime < att.LastUpdate.ToDateTime() || controller.UpdateTime < attArea.LastUpdate.ToDateTime()) &&
(!controller.Visible || !controller.Necessary))
{
controller.Sort = att.Order;
controller.Visible = att.Visible;
}
}

// 排序
if (controller.Sort == 0)
{
if (att != null)
{
if (!root.Visible || !root.Necessary)
if (!controller.Visible || !controller.Necessary)
{
controller.Sort = att.Order;
controller.Visible = att.Visible;
}
}
else
{
var pi = type.GetPropertyEx("MenuOrder");
if (pi != null) controller.Sort = pi.GetValue(null).ToInt();
}
}
}

Expand Down

0 comments on commit 53bf890

Please sign in to comment.