Exemplo n.º 1
0
        /// <summary>
        /// 根据员工账号查询菜单
        /// </summary>
        /// <param name="userKID">员工账号ID</param>
        /// <returns>
        /// UserAuthorMenu.
        /// </returns>
        public static UserAuthorMenu GetMenuByUserID(int userKID)
        {
            //递归查询得到菜单信息
            UserAuthorMenu ret = new UserAuthorMenu()
            {
                UserMenuList = new List <zTreeModel>()
            };

            var allmenu = GetAllList().OrderByDescending(t => t.Menusort);

            var roleList = new List <Sysrole>();

            var dic = new Dictionary <string, object>();

            if (userKID > 1)
            {
                dic.Add(nameof(Sysuserrole.Userid), userKID);
                var roleLst = SysuserroleLogic.GetList(dic);

                if (roleLst.Count() == 0)
                {
                    return(new UserAuthorMenu()
                    {
                        Message = "未分配权限角色,请联系管理员开通权限"
                    });
                }

                var allRoleIds = string.Join(",", roleLst.Select(p => p.Roleid).ToArray());

                if (allRoleIds.Count() == 0)
                {
                    return(new UserAuthorMenu()
                    {
                        Message = "未分配权限角色,请联系管理员开通权限"
                    });
                }

                dic = new Dictionary <string, object>();
                dic.Add(nameof(Sysrole.KID) + "|i", allRoleIds);

                dic.Add(nameof(Sysrole.IsDeleted), 0);

                roleList = SysroleLogic.GetList(dic);

                if (roleList.Count() == 0)
                {
                    return(new UserAuthorMenu()
                    {
                        Message = "不存在对应角色,请联系管理员开通权限"
                    });
                }
                List <string> removelst = new List <string>();

                var allAuthMenu = string.Join(",", roleList.Select(p => p.MenuList).ToArray()).Split(',').ToList();

                foreach (var item in allmenu.Where(p => allAuthMenu.Contains(p.KID.ToString())).OrderByDescending(k => k.Menusort))
                {
                    if (item.IsNull())
                    {
                        continue;
                    }
                    if (item.IsNull() || removelst.Contains(item.KID.ToString()))
                    {
                        continue;
                    }
                    removelst.Add(item.KID.ToString());

                    ret.UserMenuList.Add(new zTreeModel()
                    {
                        id   = item.KID.ToString(),
                        pId  = item.Fatherid.ToString(),
                        name = item.Menuname,
                        url  = item.MenuUrl,
                        sort = item.Menusort,
                        ico  = item.Menuicon,
                        open = true
                    });
                }
            }
            else
            {
                foreach (var item in allmenu.OrderByDescending(p => p.Menusort))
                {
                    ret.UserMenuList.Add(new zTreeModel()
                    {
                        id   = item.KID.ToString(),
                        pId  = item.Fatherid.ToString(),
                        name = item.Menuname,
                        url  = item.MenuUrl,
                        sort = item.Menusort,
                        ico  = item.Menuicon,
                        open = true
                    });
                }
            }
            if (ret.UserMenuList.Count() > 0)
            {
                ret.IsSucceed = true;
                ret.Message   = "菜单获取成功";
            }
            else
            {
                ret.Message = "未找到对应菜单,请联系管理员";
            }

            return(ret);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 更根据用户id获取用户menu
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public static UserAuthorMenu GetMenulistByUserid(int userid)
        {
            var UserAuthorMenu = new UserAuthorMenu()
            {
                UserMenuList = new List <zTreeModel>()
            };

            try
            {
                //所有的menus
                var allmenus = SysmenuLogic.GetAllList();
                if (userid <= 0)
                {
                    UserAuthorMenu.IsSucceed = false;
                    UserAuthorMenu.Message   = "主键值不存在";
                    return(UserAuthorMenu);
                }
                //1是超管
                if (userid == 1)
                {
                    foreach (var item in allmenus)
                    {
                        UserAuthorMenu.UserMenuList.Add(new zTreeModel
                        {
                            id         = item.KID.ToString(),
                            pId        = item.Fatherid.ToString(),
                            name       = item.Menuname,
                            ico        = item.Menuicon,
                            url        = item.MenuUrl,
                            sort       = item.Menusort,
                            open       = true,
                            schecked   = false,
                            subMenuLst = new List <zTreeModel>()
                        });
                    }
                }
                else
                {
                    var userrole = SysuserroleLogic.GetList(new Dictionary <string, object>()
                    {
                        { nameof(Sysuserrole.Userid), userid }
                    });
                    if (userrole.Count <= 0)
                    {
                        return(new UserAuthorMenu()
                        {
                            Message = "暂无可用角色", IsSucceed = false
                        });
                    }
                    else
                    {
                        var roleids = string.Join(",", userrole.Select(x => x.Roleid));
                        var roles   = SysroleLogic.GetList(new Dictionary <string, object>()
                        {
                            { $"{nameof(Sysrole.KID)}|i", roleids }
                        });
                        var menulists = string.Join(",", roles.Select(x => x.MenuList));
                        var menuids   = menulists.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        foreach (var item in menuids)
                        {
                            var menu = allmenus.FirstOrDefault(x => x.KID == item.Toint());
                            if (menu != null)
                            {
                                UserAuthorMenu.UserMenuList.Add(new zTreeModel
                                {
                                    id         = menu.KID.ToString(),
                                    pId        = menu.Fatherid.ToString(),
                                    name       = menu.Menuname,
                                    ico        = menu.Menuicon,
                                    url        = menu.MenuUrl,
                                    sort       = menu.Menusort,
                                    open       = true,
                                    schecked   = false,
                                    subMenuLst = new List <zTreeModel>()
                                });
                            }
                        }
                    }
                }
                if (UserAuthorMenu.UserMenuList.Count > 0)
                {
                    UserAuthorMenu.IsSucceed = true;
                    UserAuthorMenu.Message   = "获取菜单权限成功";
                }
                else
                {
                    UserAuthorMenu.IsSucceed = false;
                    UserAuthorMenu.Message   = "获取菜单权限失败";
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(ex, "");
                UserAuthorMenu.IsSucceed = false;
                UserAuthorMenu.Message   = "获取菜单权限出错";
            }

            return(UserAuthorMenu);
        }