wenxiangchao
3 天以前 b544d5cccd76735e034c8055918a50927b437474
HMI.Base/PluginLoader.cs
@@ -4,7 +4,6 @@
using log4net;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Reflection;
using System.Threading;
@@ -13,25 +12,11 @@
namespace HMI.Base
{
    public enum LoadMsgType
    {
        INIT, LOADING, FINISHED
    }
    public class LoadMsg
    {
        public string Message { get; set; }
        public bool? Result { get; set; }
        public LoadMsgType Type { get; set; }
        public int NumData { get; set; }
    }
    public class PluginLoader
    {
        private static ILog log = LogManager.GetLogger("PluginLoader");
        private static string PluginFolder = PathHelper.PluginFolder();
        public bool Completed { get; private set; } = false;
        public SqliteHelper sqliteHelper;
        public List<PluginInfo> Menus { get; private set; }
        public List<PluginInfo> Plugins { get; private set; }
@@ -50,6 +35,7 @@
            {
                Instance = null;
            }
            GC.Collect();
        }
        public static PluginLoader GetLoader()
@@ -62,6 +48,7 @@
        }
        private IPluginContainer pluginContainer;
        public void LoadMenuAndPlugin(IPluginContainer container, Dispatcher dispatcher)
        {
            pluginContainer = container;
@@ -70,22 +57,28 @@
                Plugins = new List<PluginInfo>();
                Menus = LoadTreeMenu(dispatcher);
                sqliteHelper.Close();
                dispatcher.Invoke(() =>
                {
                    LoadMessageAppend?.Invoke(this, new LoadMsg { Type = LoadMsgType.FINISHED });
                });
                LoadMessageAppend?.Invoke(this, new LoadMsg { Type = LoadMsgType.FINISHED });
            }
            pluginContainer.DoCloseEvent += PluginContainer_DoCloseEvent;
        }
        private void PluginContainer_DoCloseEvent(object sender, EventArgs e)
        {
            Reset();
        }
        private void LoadChildrenMenu()
        {
            foreach (var menu in Plugins)
            {
                CountdownEvent c = new CountdownEvent(1);
                Thread.Sleep(80);
                Thread.Sleep(50);
                Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    try
                    {
                        var start = DateTime.Now;
                        log.Info(this.lang("App.IsLoadingMenu", menu.Name));
                        LoadMessageAppend?.Invoke(this,
                            new LoadMsg
                            {
@@ -93,6 +86,7 @@
                                Message = this.lang("App.IsLoadingMenu", menu.Name)
                            }
                        );
                        if (!string.IsNullOrEmpty(menu.DllPath))
                        {
                            string dllPath = Path.Combine(PluginFolder, menu.DllPath);
@@ -129,6 +123,7 @@
                                menu.SettingControl = SettingInstance;
                            }
                        }
                        log.Info($"{this.lang("App.MenuLoadSuccess", menu.Name)} Cost: {(DateTime.Now - start).TotalMilliseconds}ms");
                        LoadMessageAppend?.Invoke(this,
                            new LoadMsg
                            {
@@ -139,7 +134,7 @@
                    }
                    catch (Exception ex)
                    {
                        log.Error("Load Plugin Failed.", ex);
                        log.Error($"Load Plugin [{menu.Name}] Failed.", ex);
                        LoadMessageAppend?.Invoke(this, new LoadMsg
                        {
                            Type = LoadMsgType.LOADING,
@@ -156,50 +151,68 @@
            }
        }
        private PluginInfo ConvertPermission2PluginInfo(PermissionData data)
        {
            if (data.Type == "权限") return null;
            PluginInfo info = new PluginInfo
            {
                Code = data.Code,
                Name = data.Name,
                Type = data.Type,
                ParentCode = data.ParentCode,
                DllPath = data.DllPath,
                ClassName = data.ClassName,
                IconFont = data.IconFont
                //Icon = new BitmapImage(new Uri(Path.Combine(PathHelper.IconFolder, dr["iconPath"].ToString())))
            };
            if (data.Children != null && data.Children.Count > 0)
            {
                foreach (var child in data.Children)
                {
                    var childInfo = ConvertPermission2PluginInfo(child);
                    if (childInfo != null) info.Children.Add(childInfo);
                }
            }
            return info;
        }
        public List<PluginInfo> LoadTreeMenu(Dispatcher dispatcher)
        {
            //LoadMessageAppend?.Invoke(this, "开始加载菜单...");
            List<PluginInfo> data = new List<PluginInfo>();
            List<PluginInfo> menus = new List<PluginInfo>();
            Dictionary<string, PluginInfo> cache = new Dictionary<string, PluginInfo>();
            try
            {
                UserInfo user = UserInfo.User;
                string sql = $@"SELECT m.* FROM menu AS m INNER JOIN role_menu_rel AS rrel ON m.code = rrel.menuCode
INNER JOIN role AS r ON rrel.roleCode = r.code
INNER JOIN user_role_rel AS urel ON r.code = urel.roleCode
AND urel.username = '{user.Username}' ORDER BY m.level,m.sort, m.id;";
                DataTable dt = sqliteHelper.GetDataTable(sql);
                if (dt != null)
                List<PermissionData> allPermissions = UserInfo.User?.AllPermissions;
                if (allPermissions != null)
                {
                    Application.Current.Dispatcher.Invoke(() =>
                    Application.Current.Dispatcher.Invoke(new Action(() =>
                    {
                        log.Info(this.lang("App.MenuCount", allPermissions.Count));
                        LoadMessageAppend?.Invoke(this,
                          new LoadMsg
                          {
                              Type = LoadMsgType.INIT,
                              NumData = dt.Rows.Count,
                              Message = this.lang("App.MenuCount", dt.Rows.Count),
                              NumData = allPermissions.Count,
                              Message = this.lang("App.MenuCount", allPermissions.Count),
                          });
                        foreach (DataRow dr in dt.Rows)
                        foreach (PermissionData data in allPermissions)
                        {
                            string path = Path.Combine(PathHelper.IconFolder, dr["iconPath"].ToString());
                            if (data.Type == "Permission") continue;
                            var menu = new PluginInfo
                            {
                                Code = dr["code"].ToString(),
                                Name = dr["name"].ToString(),
                                Type = dr["type"].ToString(),
                                ParentCode = dr["parentCode"].ToString(),
                                DllPath = dr["dllPath"].ToString(),
                                ClassName = dr["className"].ToString(),
                                IconFont = dr["icon"].ToString()
                                //Icon = new BitmapImage(new Uri(Path.Combine(PathHelper.IconFolder, dr["iconPath"].ToString())))
                                Code = data.Code,
                                Name = data.Name,
                                Type = data.Type,
                                ParentCode = data.ParentCode,
                                DllPath = data.DllPath,
                                ClassName = data.ClassName,
                                IconFont = data.IconFont
                            };
                            cache[menu.Code] = menu;
                            if (!cache.ContainsKey(menu.ParentCode))
                            {
                                data.Add(menu);
                                menus.Add(menu);
                            }
                            else
                            {
@@ -207,7 +220,7 @@
                            }
                            Plugins.Add(menu);
                        }
                    });
                    }));
                    LoadChildrenMenu();
                }
            }
@@ -215,8 +228,7 @@
            {
                //LoadMessageAppend?.Invoke(this, ex.Message);
            }
            return data;
            return menus;
        }
    }
}