205 lines
6.5 KiB
C#
205 lines
6.5 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel;
|
||
using System.Data;
|
||
using System.Drawing;
|
||
using System.Linq;
|
||
using System.Reflection;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Windows.Forms;
|
||
using AntdUI;
|
||
using DH.RBAC.Common;
|
||
using DH.RBAC.Logic.Sys;
|
||
using DH.RBAC.Model.Sys;
|
||
using DH.RBAC.Models.Base;
|
||
using DH.RBAC.Page;
|
||
using Sunny.UI;
|
||
|
||
|
||
|
||
namespace DH.RBAC
|
||
{
|
||
public partial class RBACWindow : Window
|
||
{
|
||
private SysUserLogic userLogic;
|
||
private SysPermissionLogic permissionLogic;
|
||
private List<Menu> pageList = new List<Menu>();
|
||
public RBACWindow()
|
||
{
|
||
InitializeComponent();
|
||
userLogic = new SysUserLogic();
|
||
permissionLogic = new SysPermissionLogic();
|
||
Load += RBACWindow_Load;
|
||
menu1.SelectChanged += Menu1_SelectChanged;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 窗体对象实例
|
||
/// </summary>
|
||
private static RBACWindow _instance;
|
||
|
||
public static RBACWindow Instance
|
||
{
|
||
get
|
||
{
|
||
if (_instance == null || _instance.IsDisposed)
|
||
_instance = new RBACWindow();
|
||
return _instance;
|
||
}
|
||
}
|
||
|
||
private void RBACWindow_Load(object sender, EventArgs e)
|
||
{
|
||
lbName.Text = GlobalConfig.CurrentUser.Account;
|
||
//设置关联
|
||
//Aside.TabControl = MainTabControl;
|
||
//获得用户权限
|
||
List<SysPermission> list;
|
||
if (userLogic.ContainsUser("admin", GlobalConfig.CurrentUser.Id))
|
||
{
|
||
list = permissionLogic.GetList();
|
||
}
|
||
else
|
||
{
|
||
list = permissionLogic.GetList(GlobalConfig.CurrentUser.Id);
|
||
}
|
||
if (list.IsNullOrEmpty())
|
||
{
|
||
AntdUI.Message.warn(this, "网络或服务器异常,请稍后重试!", autoClose: 3);
|
||
|
||
return;
|
||
}
|
||
GlobalConfig.PermissionList = list;
|
||
|
||
//取出一级菜单
|
||
List<SysPermission> root = list.Where(it => it.ParentId == "0").ToList();
|
||
foreach (SysPermission permission in root)
|
||
{
|
||
MenuItem menuItem=new MenuItem();
|
||
menuItem.Text = permission.Name;
|
||
menu1.Items.Add(menuItem);
|
||
|
||
List<SysPermission> childList = list.Where(it => it.ParentId == permission.Id).ToList();
|
||
|
||
foreach (SysPermission child in childList)
|
||
{
|
||
MenuItem childmenuItem = new MenuItem();
|
||
childmenuItem.Text = child.Name;
|
||
childmenuItem.ID = child.EnCode;
|
||
childmenuItem.Tag=child.Id.ToString();
|
||
menuItem.Sub.Add(childmenuItem);
|
||
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
|
||
private void Menu1_SelectChanged(object sender, MenuSelectEventArgs e)
|
||
{
|
||
var clickedItem = e.Value as MenuItem;
|
||
|
||
|
||
|
||
|
||
if (clickedItem?.ID == null) return;
|
||
|
||
string pageCode = clickedItem.ID; // 获取PageCode(如"sys-user")
|
||
string menuText = clickedItem.Text;
|
||
string permissionId= clickedItem.Tag.ToString();
|
||
// 查找是否已存在对应的TabPage
|
||
var existingTab = FindTabByPageCode(pageCode);
|
||
if (existingTab != null)
|
||
{
|
||
tabs1.SelectedTab = existingTab;
|
||
return;
|
||
}
|
||
|
||
// 动态创建控件并添加TabPage
|
||
MyPage control = CreateControlByPageCode(pageCode);
|
||
try
|
||
{
|
||
|
||
|
||
control.ButtonPermissionList = GlobalConfig.PermissionList.Where(it => it.ParentId == permissionId).ToList();
|
||
}
|
||
catch
|
||
{
|
||
|
||
}
|
||
if (control == null)
|
||
{
|
||
AntdUI.Message.warn(this, "该菜单仅权限配置,无显示页面!", autoClose: 3);
|
||
|
||
return;
|
||
}
|
||
|
||
AddNewTab(control, menuText, pageCode);
|
||
|
||
}
|
||
private MyPage CreateControlByPageCode(string pageCode)
|
||
{
|
||
Type controlType = FindControlTypeByPageCode(pageCode);
|
||
if (controlType == null) return null;
|
||
|
||
try
|
||
{
|
||
// 获取接受 Form 参数的构造函数
|
||
var constructor = controlType.GetConstructor(new[] { typeof(Window) });
|
||
if (constructor != null)
|
||
{
|
||
return constructor.Invoke(new object[] { this }) as MyPage; // 传递当前窗体实例
|
||
}
|
||
else
|
||
{
|
||
// 如果未找到带 Form 参数的构造函数,尝试无参构造
|
||
return Activator.CreateInstance(controlType) as MyPage;
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show($"创建页面失败: {ex.Message}");
|
||
return null;
|
||
}
|
||
}
|
||
private Type FindControlTypeByPageCode(string pageCode)
|
||
{
|
||
// 反射查找所有带有PageCodeAttribute的UserControl类型
|
||
var controlTypes = Assembly.GetExecutingAssembly().GetTypes()
|
||
.Where(t => t.IsSubclassOf(typeof(UserControl)) && !t.IsAbstract);
|
||
|
||
foreach (var type in controlTypes)
|
||
{
|
||
var attr = type.GetCustomAttribute<PageCodeAttribute>();
|
||
if (attr != null && attr.Encode == pageCode)
|
||
{
|
||
return type;
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
private void AddNewTab(UserControl control, string menuText, string pageCode)
|
||
{
|
||
AntdUI.TabPage tabPage = new AntdUI.TabPage
|
||
{
|
||
Text = menuText,
|
||
Tag = pageCode, // 可选:存储PageCode用于后续查找
|
||
ReadOnly = false
|
||
};
|
||
control.Dock = DockStyle.Fill;
|
||
AutoDpi(control); // DPI适配(如果有)
|
||
tabPage.Controls.Add(control);
|
||
tabs1.Pages.Add(tabPage);
|
||
tabs1.SelectedTab = tabPage;
|
||
}
|
||
|
||
private AntdUI.TabPage FindTabByPageCode(string pageCode)
|
||
{
|
||
return tabs1.Pages.OfType<AntdUI.TabPage>()
|
||
.FirstOrDefault(t => t.Tag?.ToString() == pageCode);
|
||
}
|
||
|
||
}
|
||
}
|