lu
2025-04-05 d70880a3d9ae6c9f99ec380ccd16f5524bb622e6
DevApp/Gs.DevApp/DevFrm/Sys/SysMenu.cs
@@ -1,32 +1,36 @@
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using Gs.DevApp.Entity;
using Gs.DevApp.ToolBox;
using Newtonsoft.Json;
using System;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Reflection;
using System.Resources;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraTreeList;
using Gs.DevApp.Entity;
using Gs.DevApp.Properties;
using Gs.DevApp.ToolBox;
using Newtonsoft.Json;
namespace Gs.DevApp.DevFrm.User
{
    public partial class SysMenu : DevExpress.XtraEditors.XtraForm
    public partial class SysMenu : XtraForm
    {
        public SysMenu()
        {
            InitializeComponent();
            getImg();
            txt_upGuid.Properties.PopupFormSize = new System.Drawing.Size(txt_upGuid.Width, 300);
            txt_upGuid.Properties.TextEditStyle = TextEditStyles.DisableTextEditor;
            this.toolBarMenu1.btnAddClick += ToolBarMenu1_btnAddClick;
            this.toolBarMenu1.btnEdtClick += ToolBarMenu1_btnEdtClick;
            this.toolBarMenu1.btnSaveClick += ToolBarMenu1_btnSaveClick;
            this.toolBarMenu1.btnLoadClick += ToolBarMenu1_btnLoadClick;
            this.toolBarMenu1.btnDelClick += ToolBarMenu1_btnDelClick1;
            this.toolBarMenu1.btnEscClick += ToolBarMenu1_btnEscClick;
            txt_upGuid.Properties.PopupFormSize =
                new Size(txt_upGuid.Width, 300);
            txt_upGuid.Properties.TextEditStyle =
                TextEditStyles.DisableTextEditor;
            toolBarMenu1.btnAddClick += ToolBarMenu1_btnAddClick;
            toolBarMenu1.btnEdtClick += ToolBarMenu1_btnEdtClick;
            toolBarMenu1.btnSaveClick += ToolBarMenu1_btnSaveClick;
            toolBarMenu1.btnLoadClick += ToolBarMenu1_btnLoadClick;
            toolBarMenu1.btnDelClick += ToolBarMenu1_btnDelClick1;
            toolBarMenu1.btnEscClick += ToolBarMenu1_btnEscClick;
            txt_category.TextChanged += Txt_category_TextChanged;
            tlMenu.MouseDoubleClick += TlMenu_MouseDoubleClick;
            txt_upGuid.Properties.TreeList.IndicatorWidth = 50;
@@ -53,7 +57,7 @@
        }
        /// <summary>
        /// 菜单类型改变事件
        ///     菜单类型改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
@@ -61,247 +65,298 @@
        {
            if (txt_category.SelectedIndex == 1)
            {
                this.txt_formPath.Enabled = true;
                this.txt_icon.Enabled = true;
                txt_formPath.Enabled = true;
                txt_icon.Enabled = true;
            }
            else
            {
                this.txt_formPath.Enabled = false;
                this.txt_icon.Enabled = false;
                this.txt_formPath.Text = "";
                txt_formPath.Enabled = false;
                txt_icon.Enabled = false;
                txt_formPath.Text = "";
            }
        }
        /// <summary>
        /// 双击行事件
        ///     双击行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TlMenu_MouseDoubleClick(object sender, System.Windows.Forms.MouseEventArgs e)
        private void TlMenu_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            string rowGuid = Gs.DevApp.ToolBox.UtilityHelper.GetCurrentDoubleRow(tlMenu, e, "guid");
            var rowGuid = UtilityHelper.GetCurrentDoubleRow(tlMenu, e, "guid");
            if (!string.IsNullOrEmpty(rowGuid))
                getModel(rowGuid, false, 999);
                getModel(rowGuid);
        }
        /// <summary>
        /// 取消事件
        ///     取消事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolBarMenu1_btnEscClick(object sender, EventArgs e)
        {
            UtilityHelper.JumpToTab(xtraTabControl1, 0);
            Gs.DevApp.ToolBox.UtilityHelper.JumpTab(xtraTabControl1, 1);
        }
        /// <summary>
        /// 删除事件
        ///     删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolBarMenu1_btnDelClick1(object sender, EventArgs e)
        {
            string rowGuid, rowName;
            (rowGuid, rowName) = UtilityHelper.GetCurrentRow(xtraTabControl1, lbGuid, txt_name, tlMenu);
            (rowGuid, rowName) = UtilityHelper.GetCurrentRow(xtraTabControl1,
                lbGuid, txt_name, tlMenu);
            if (string.IsNullOrEmpty(rowGuid))
            {
                ToolBox.MsgHelper.Warning("请先选择你要操作的行!");
                MsgHelper.Warning("请先选择你要操作的行!");
                return;
            }
            if (!MsgHelper.AskQuestion("你选择了【" + rowName + "】,确定删除吗?"))
            {
                return;
            }
            List<string> lst = new List<string>();
            if (!MsgHelper.AskQuestion("你选择了【" + rowName + "】,确定删除吗?")) return;
            var lst = new List<string>();
            lst.Add(rowGuid);
            var _obj = lst;
            try
            {
                string strJson = UtilityHelper.HttpPost("", "MenuAction/DeleteModel", JsonConvert.SerializeObject(_obj));
                ReturnModel<dynamic> _rtn = ToolBox.UtilityHelper.ReturnToDynamic(strJson);
                var strJson = UtilityHelper.HttpPost("",
                    "MenuAction/DeleteModel",
                    JsonConvert.SerializeObject(_obj));
                var _rtn = UtilityHelper.ReturnToDynamic(strJson);
                if (_rtn.rtnCode > 0)
                {
                    UtilityHelper.JumpToTab(xtraTabControl1, 0);
                    if (xtraTabControl1.SelectedTabPageIndex == 0)
                    { }
                    else
                        Gs.DevApp.ToolBox.UtilityHelper.JumpTab(xtraTabControl1, 2);
                    getTree();
                }
                ToolBox.MsgHelper.Warning("提示:" + _rtn.rtnMsg);
                MsgHelper.Warning("提示:" + _rtn.rtnMsg);
            }
            catch (Exception ex)
            {
                ToolBox.MsgHelper.Warning("提示:" + ex.Message);
                MsgHelper.Warning("提示:" + ex.Message);
            }
        }
        /// <summary>
        /// 刷新事件
        ///     刷新事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolBarMenu1_btnLoadClick(object sender, EventArgs e)
        {
            UtilityHelper.JumpToTab(xtraTabControl1, 0);
            getTree();
            if (xtraTabControl1.SelectedTabPageIndex == 1)
                getModel(lbGuid.Text.Trim());
            else
            {
                //_filterList.Clear();
                //if (gridView1.ActiveFilter.Count > 0)
                //{
                //    gridView1.ColumnFilterChanged -= GridView1_ColumnFilterChanged;
                //    gridView1.ActiveFilter.Clear();
                //    gridView1.ColumnFilterChanged += GridView1_ColumnFilterChanged;
                //}
                //getPageList(1);
                getTree();
            }
        }
        /// <summary>
        /// 修改事件
        ///     修改事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolBarMenu1_btnEdtClick(object sender, EventArgs e)
        {
            string rowGuid, rowName;
            string rowGuid = "", rowName = "";
            (rowGuid, rowName) = UtilityHelper.GetCurrentRow(xtraTabControl1, lbGuid, txt_name, tlMenu);
            if (string.IsNullOrEmpty(rowGuid))
            {
                ToolBox.MsgHelper.Warning("请先选择你要操作的行!");
                return;
            }
            getModel(rowGuid, true, 1);
            if (txt_category.SelectedIndex == 1)
            Gs.DevApp.ToolBox.UtilityHelper.JumpTab(xtraTabControl1, 3);
            if (xtraTabControl1.SelectedTabPageIndex == 1)
            {
                this.txt_formPath.Enabled = true;
                this.txt_icon.Enabled = true;
                getModel(rowGuid);
                if (txt_category.SelectedIndex == 1)
                {
                    txt_formPath.Enabled = true;
                    txt_icon.Enabled = true;
                }
                else
                {
                    txt_formPath.Enabled = false;
                    txt_icon.Enabled = false;
                }
            }
            else
            {
                this.txt_formPath.Enabled = false;
                this.txt_icon.Enabled = false;
                UtilityHelper.ChangeEnableByControl(this.panel1.Controls, true);
            }
        }
        /// <summary>
        /// 新增事件
        ///     新增事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolBarMenu1_btnAddClick(object sender, EventArgs e)
        {
            UtilityHelper.JumpToTab(xtraTabControl1, 1);
            lbGuid.Text = "";
            UtilityHelper.CleanValueByControl(this.panel1.Controls, true);
            Gs.DevApp.ToolBox.UtilityHelper.JumpTab(xtraTabControl1, 4);
            UtilityHelper.CleanValueByControl(panel1.Controls, true);
        }
        /// <summary>
        /// 保存事件
        ///     保存事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolBarMenu1_btnSaveClick(object sender, EventArgs e)
        {
            toolBarMenu1.isSetBtn = false;
            if (string.IsNullOrEmpty(txt_category.Text.Trim()) || txt_category.SelectedIndex == 0)
            if (string.IsNullOrEmpty(txt_category.Text.Trim()) ||
                txt_category.SelectedIndex == 0)
            {
                Gs.DevApp.ToolBox.MsgHelper.Warning("菜单类型不能为空!");
                MsgHelper.Warning("菜单类型不能为空!");
                txt_category.Focus();
                return;
            }
            if (string.IsNullOrEmpty(txt_name.Text.Trim()))
            {
                Gs.DevApp.ToolBox.MsgHelper.Warning("菜单名称不能为空!");
                MsgHelper.Warning("菜单名称不能为空!");
                txt_name.Focus();
                return;
            }
            string _upGuid = txt_upGuid.EditValue.ToString();
            if (txt_category.SelectedIndex == 2 && string.IsNullOrEmpty(_upGuid))
            var _upGuid = txt_upGuid.EditValue.ToString();
            if (txt_category.SelectedIndex == 2 &&
                string.IsNullOrEmpty(_upGuid))
            {
                Gs.DevApp.ToolBox.MsgHelper.Warning("按钮类型必须选择父级菜单!");
                MsgHelper.Warning("按钮类型必须选择父级菜单!");
                txt_upGuid.Focus();
                return;
            }
            var _obj = new
            {
                guid = lbGuid.Text.Trim(),//主建
                upGuid = _upGuid,//上级的主建
                name = txt_name.Text.Trim(),//名称
                icon = txt_icon.Text,//菜单图标
                isStatus = txt_isStatus.Checked,//状态
                formPath = txt_formPath.Text.Trim(),//窗体路径
                idx = int.Parse(txt_idx.Value.ToString()),//排序
                category = txt_category.SelectedIndex,//类型
                guid = UtilityHelper.ToGuid(lbGuid.Text.Trim()), //主建
                upGuid = _upGuid, //上级的主建
                name = txt_name.Text.Trim(), //名称
                icon = txt_icon.Text, //菜单图标
                isStatus = txt_isStatus.Checked, //状态
                formPath = txt_formPath.Text.Trim(), //窗体路径
                idx = int.Parse(txt_idx.Value.ToString()), //排序
                category = txt_category.SelectedIndex //类型
            };
            try
            {
                string strJson = UtilityHelper.HttpPost("", "MenuAction/EditModel", JsonConvert.SerializeObject(_obj));
                ReturnModel<dynamic> _rtn = ToolBox.UtilityHelper.ReturnToDynamic(strJson);
                ToolBox.MsgHelper.Warning("提示:" + _rtn.rtnMsg);
                var strJson = UtilityHelper.HttpPost("", "MenuAction/EditModel",
                    JsonConvert.SerializeObject(_obj));
                var _rtn = UtilityHelper.ReturnToDynamic(strJson);
                MsgHelper.Warning("提示:" + _rtn.rtnMsg);
                if (_rtn.rtnCode > 0)
                {
                    lbGuid.Text = _rtn.rtnData;
                    toolBarMenu1.isSetBtn = true;
                    UtilityHelper.ChangeEnableByControl(this.panel1.Controls, false);
                    UtilityHelper.ChangeEnableByControl(panel1.Controls, false);
                    toolBarMenu1.currentAction = "";
                    Gs.DevApp.ToolBox.UtilityHelper.JumpTab(xtraTabControl1, 6);
                }
            }
            catch (Exception ex)
            {
                ToolBox.MsgHelper.Warning("提示:" + ex.Message);
                MsgHelper.Warning("提示:" + ex.Message);
            }
        }
        /// <summary>
        /// 读取列表
        ///     读取列表
        /// </summary>
        private void getTree()
        {
            PageQueryModel pgq = new PageQueryModel(1, 999999, "idx", "asc", "", "");
            string json = JsonConvert.SerializeObject(pgq);
            var pgq = new PageQueryModel(1, 999999, "a.idx");
            var json = JsonConvert.SerializeObject(pgq);
            try
            {
                string strReturn = UtilityHelper.HttpPost("", "MenuAction/GetListPage", json);
                ReturnModel<PageListModel> dd = UtilityHelper.ReturnToTablePage(strReturn);
                DataTable dt = dd.rtnData.list;
                this.tlMenu.DataSource = dt;
                this.tlMenu.KeyFieldName = "guid";
                this.tlMenu.ParentFieldName = "upGuid";
                this.tlMenu.Tag = "name";
                this.tlMenu.EndUpdate();
                // this.tlMenu.ExpandAll();
                tlMenu.OptionsView.CheckBoxStyle = DevExpress.XtraTreeList.DefaultNodeCheckBoxStyle.Default;
                var strReturn =
                    UtilityHelper.HttpPost("", "MenuAction/GetListPage", json);
                var dd = UtilityHelper.ReturnToTablePage(strReturn);
                var dt = dd.rtnData.list;
                tlMenu.DataSource = dt;
                tlMenu.KeyFieldName = "guid";
                tlMenu.ParentFieldName = "upGuid";
                tlMenu.Tag = "name";
                tlMenu.EndUpdate();
                this.tlMenu.CollapseAll();
                tlMenu.OptionsView.CheckBoxStyle =
                    DefaultNodeCheckBoxStyle.Default;
            }
            catch (Exception ex)
            {
                ToolBox.MsgHelper.Warning("提示:" + ex.Message);
                MsgHelper.Warning("提示:" + ex.Message);
            }
        }
        private void getModel(string strGuid, bool isEdit, int tabIdx)
        private void getModel(string strGuid)
        {
            bool isEdit = false;
            if (toolBarMenu1.currentAction == "add") return;
            if (toolBarMenu1.currentAction == "edit") isEdit = true;
            if (string.IsNullOrEmpty(strGuid))
            {
                ToolBox.MsgHelper.Warning("请先选择你要操作的行!");
                MsgHelper.Warning("请先选择你要操作的行!");
                return;
            }
            UtilityHelper.JumpToTab(xtraTabControl1, tabIdx);
            var _obj = new
            {
                guid = strGuid,//主建
                guid = strGuid //主建
            };
            try
            {
                string strJson = UtilityHelper.HttpPost("", "MenuAction/GetModel", JsonConvert.SerializeObject(_obj));
                ReturnModel<dynamic> _rtn = ToolBox.UtilityHelper.ReturnToDynamic(strJson);
                var strJson = UtilityHelper.HttpPost("", "MenuAction/GetModel",
                    JsonConvert.SerializeObject(_obj));
                var _rtn = UtilityHelper.ReturnToDynamic(strJson);
                if (_rtn.rtnCode > 0)
                {
                    dynamic dy = _rtn.rtnData;
                    var dy = _rtn.rtnData;
                    lbGuid.Text = strGuid;
                    UtilityHelper.SetValueByObj(this.panel1.Controls, dy, isEdit);
                    UtilityHelper.SetValueByObj(panel1.Controls, dy, isEdit);
                    txt_upGuid.EditValue = dy.upGuid.ToString();
                }
                else
                    ToolBox.MsgHelper.Warning("提示:" + _rtn.rtnMsg);
                {
                    MsgHelper.Warning("提示:" + _rtn.rtnMsg);
                }
            }
            catch (Exception ex)
            {
                ToolBox.MsgHelper.Warning("提示:" + ex.Message);
                MsgHelper.Warning("提示:" + ex.Message);
            }
        }
        /// <summary>
        /// 读取上级树
        ///     读取上级树
        /// </summary>
        private void getDownList()
        {
            PageQueryModel pgq = new PageQueryModel(1, 999999, "idx", "asc", "", " and category=1");
            string json = JsonConvert.SerializeObject(pgq);
            var pgq = new PageQueryModel(1, 999999, "a.idx", "asc", "",
                " and a.category=1");
            var json = JsonConvert.SerializeObject(pgq);
            try
            {
                string strReturn = UtilityHelper.HttpPost("", "MenuAction/GetListPage", json);
                ReturnModel<PageListModel> dd = UtilityHelper.ReturnToTablePage(strReturn);
                DataTable dt = dd.rtnData.list;
                var strReturn =
                    UtilityHelper.HttpPost("", "MenuAction/GetListPage", json);
                var dd = UtilityHelper.ReturnToTablePage(strReturn);
                var dt = dd.rtnData.list;
                txt_upGuid.Properties.TreeList.KeyFieldName = "guid";
                txt_upGuid.Properties.TreeList.ParentFieldName = "upGuid";
                txt_upGuid.Properties.ValueMember = "guid";
@@ -310,46 +365,44 @@
            }
            catch (Exception ex)
            {
                ToolBox.MsgHelper.Warning("提示:" + ex.Message);
                MsgHelper.Warning("提示:" + ex.Message);
            }
        }
        private void getActionList()
        {
            List<string> lst = new List<string>();
            var lst = new List<string>();
            lst = toolBarMenu1.actions;
            txt_name.Properties.Items.AddRange(toolBarMenu1.actions);
            Assembly assembly = Assembly.GetExecutingAssembly();
            Type[] types = assembly.GetTypes();
            foreach (Type type in types)
            {
            var assembly = Assembly.GetExecutingAssembly();
            var types = assembly.GetTypes();
            foreach (var type in types)
                if (type.IsSubclassOf(typeof(Form)))
                {
                    txt_formPath.Properties.Items.Add(type.FullName);
                }
            }
        }
        private void getImg()
        {
            Assembly assembly = Assembly.GetExecutingAssembly();
            ResourceManager resourceManager = new ResourceManager("Gs.DevApp.Properties.Resources", assembly);
            int i = 0;
            DevExpress.Utils.ImageCollection dicImg = new DevExpress.Utils.ImageCollection();
            Properties.Resources res = new Properties.Resources();
            PropertyInfo[] properInfo = res.GetType().GetProperties(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Instance);
            foreach (PropertyInfo item in properInfo)
            {
            var assembly = Assembly.GetExecutingAssembly();
            var resourceManager =
                new ResourceManager("Gs.DevApp.Properties.Resources", assembly);
            var i = 0;
            var dicImg = new ImageCollection();
            var res = new Resources();
            var properInfo = res.GetType().GetProperties(BindingFlags.Static |
                BindingFlags.NonPublic | BindingFlags.Instance);
            foreach (var item in properInfo)
                if (item.Name.Contains("_"))
                {
                    Image image = resourceManager.GetObject(item.Name) as Image;
                    var image = resourceManager.GetObject(item.Name) as Image;
                    if (image != null)
                    {
                        dicImg.AddImage(image);
                        txt_icon.Properties.Items.Add(new DevExpress.XtraEditors.Controls.ImageComboBoxItem(item.Name, i, i));
                        txt_icon.Properties.Items.Add(
                            new ImageComboBoxItem(item.Name, i, i));
                        i++;
                    }
                }
            }
            txt_icon.Properties.LargeImages = dicImg;
            txt_icon.Properties.SmallImages = dicImg;
        }