コード例 #1
0
 public Menu GetMenuById(int id)
 {
     return(dal.GetMenu(id));
 }
コード例 #2
0
        public static JArray GetAllMenus()
        {
            JArray retValue = new JArray();

            using (MySqlConnection conn = DBUtility.OpenConnection())
            {
                try
                {
                    Menu[] dbMenus = MenuDal.GetAllMenus(conn);

                    if (dbMenus != null && dbMenus.Length > 0)
                    {
                        //dbMenus为单级权限菜单,需要组装成二级菜单
                        Dictionary <int, List <Menu> > menuDict = new Dictionary <int, List <Menu> >();
                        foreach (Menu dbmenu in dbMenus)
                        {
                            List <Menu> menuGroup = null;
                            if (menuDict.ContainsKey(dbmenu.parent_id))
                            {
                                menuGroup = menuDict[dbmenu.parent_id];
                                //选择合适位置插入
                                int insertIndex = 0;
                                for (; insertIndex < menuGroup.Count; insertIndex++)
                                {
                                    if (string.Compare(menuGroup[insertIndex].menu_no, dbmenu.menu_no, StringComparison.OrdinalIgnoreCase) > 0)
                                    {
                                        break;
                                    }
                                }
                                menuGroup.Insert(insertIndex, dbmenu);
                            }
                            else
                            {
                                Menu parent = MenuDal.GetMenu(dbmenu.parent_id, conn);
                                if (parent != null)
                                {
                                    menuGroup = new List <Menu>()
                                    {
                                        parent, dbmenu
                                    };
                                    menuDict.Add(dbmenu.parent_id, menuGroup);
                                }
                            }
                        }
                        //生成json数组
                        int[] keys = menuDict.Keys.ToArray();
                        for (int i = 0; i < keys.Length; i++)
                        {
                            Menu   parent    = menuDict[keys[i]][0];
                            JArray jChildren = new JArray();
                            for (int j = 1; j < menuDict[keys[i]].Count; j++)
                            {
                                Menu child = menuDict[keys[i]][j];
                                jChildren.Add(new JObject(new JProperty("id", child.menu_no),
                                                          new JProperty("name", child.name),
                                                          new JProperty("icon", child.icon_class),
                                                          new JProperty("url", child.action_url)));
                            }
                            JObject jMenuGroup = new JObject(new JProperty("id", parent.menu_no),
                                                             new JProperty("icon", parent.icon_class),
                                                             new JProperty("name", parent.name),
                                                             new JProperty("children", jChildren));
                            //父级菜单还需一次排序
                            int insertIndex = 0;
                            for (; insertIndex < retValue.Count; insertIndex++)
                            {
                                JObject item = (JObject)retValue[insertIndex];
                                if (string.Compare(item["id"].ToString(), jMenuGroup["id"].ToString(), StringComparison.OrdinalIgnoreCase) > 0)
                                {
                                    break;
                                }
                            }
                            retValue.Insert(insertIndex, jMenuGroup);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            return(retValue);
        }