dengjunjie
2025-03-12 f43b7df8400f4fcffc9f19dca0888d61e2b33d5f
ÏîÄ¿´úÂë/WMS/WIDESEA_WMSServer/WIDESEA_SystemService/Sys_RoleService.cs
@@ -15,6 +15,7 @@
using WIDESEA_Model.Models;
using WIDESEA_Model.Models.System;
using WIDESEA_SystemRepository;
using WIDESEA_Core.Helper;
namespace WIDESEA_SystemService
{
@@ -45,7 +46,7 @@
            return list;
        }
        public  List<RoleNodes> GetAllRoleId()
        public List<RoleNodes> GetAllRoleId()
        {
            List<RoleNodes> roles = BaseDal.QueryData().Select(s => new RoleNodes() { Id = s.RoleId, ParentId = s.ParentId, RoleName = s.RoleName }).ToList();
@@ -92,7 +93,42 @@
        {
            return GetUserTreePermission(App.User.RoleId);
        }
        public override WebResponseContent UpdateData(SaveModel saveModel)
        {
            var ParentId = saveModel.MainData[nameof(Sys_Role.ParentId).FirstLetterToLower()].ObjToInt();//父级ID
            var RoleId = saveModel.MainData[nameof(Sys_Role.RoleId).FirstLetterToLower()].ObjToInt();//角色ID
            if (RoleId == ParentId) return WebResponseContent.Instance.Error("父级ID不能为自己");
            if (App.User.RoleId == RoleId) return WebResponseContent.Instance.Error("无操作权限");
            return base.UpdateData(saveModel);
        }
        public override PageGridData<Sys_Role> GetPageData(PageDataOptions options)
        {
            PageGridData<Sys_Role> pageGridData = new PageGridData<Sys_Role>();
            if (App.User.IsSuperAdmin)
            {
                var GridData = base.GetPageData(options);
                pageGridData = new PageGridData<Sys_Role>()
                {
                    Rows = options.Order?.ToLower() == OrderByType.Asc.ToString().ToLower() ? GridData.Rows.OrderBy(x => x.RoleId).ToList() : GridData.Rows,
                    Total = GridData.Total
                };
                return pageGridData;
            }
            var roleIds = GetAllChildren(App.User.RoleId).Select(x => x.Id);
            //树形菜单传查询角色下所有用户
            string where = ValidatePageOptions(options);
            int total = 0;
            pageGridData = new PageGridData<Sys_Role>()
            {
                Rows = BaseDal.Db.Queryable<Sys_Role>().Where(x => roleIds.Contains(x.RoleId)).Where(where).OrderBy(x => x.RoleId).ToPageList(options.Page, options.Rows),
                Total = total
            };
            return pageGridData;
        }
        /// <summary>
        /// ç¼–辑权限时,获取指定角色的所有菜单权限
        /// </summary>
@@ -110,7 +146,7 @@
            //获取用户权限
            List<Permissions> permissions = _MenuRepository.GetPermissions(roleId);
            //权限用户权限查询所有的菜单信息
            List<MenuDTO> menus =  _MenuService.GetUserMenuList(roleId);
            List<MenuDTO> menus = _MenuService.GetUserMenuList(roleId);
            //获取当前用户权限如:(Add,Search)对应的显示文本信息如:Add:添加,Search:查询
            var data = menus.Select(x => new
            {
@@ -135,7 +171,9 @@
            string message = "";
            try
            {
                if (!GetAllChildren(App.User.RoleId).Exists(x => x.Id == roleId))
                var RoleNodes = GetAllChildren(App.User.RoleId);
                RoleNodes = RoleNodes.Where(x => x.Id != App.User.RoleId).ToList();
                if (!RoleNodes.Exists(x => x.Id == roleId) /*&& !App.User.IsRoleIdSuperAdmin(App.User.RoleId)*/)
                    return WebResponseContent.Instance.Error("没有权限修改此角色的权限信息");
                //当前用户的权限
                List<Permissions> permissions = _MenuRepository.GetPermissions(App.User.RoleId);
@@ -143,6 +181,138 @@
                List<int> originalMeunIds = new List<int>();
                //被分配角色的权限
                List<Sys_RoleAuth> roleAuths = _RoleAuthRepository.QueryData(x => x.RoleId == roleId);
                List<Sys_RoleAuth> updateAuths = new List<Sys_RoleAuth>();
                foreach (UserPermissionDTO x in userPermissions)
                {
                    Permissions per = permissions.FirstOrDefault(p => p.MenuId == x.Id);
                    //不能分配超过当前用户的权限
                    if (per == null) continue;
                    //per.UserAuthArr.Contains(a.Value)校验权限范围
                    string[] arr = x.Actions == null || x.Actions.Count == 0
                      ? new string[0]
                      : x.Actions.Where(a => per.UserAuthArr.Contains(a.Value))
                      .Select(s => s.Value).ToArray();
                    //如果当前权限没有分配过,设置Auth_Id默认为0,表示新增的权限
                    var auth = roleAuths.Where(r => r.MenuId == x.Id).Select(s => new { s.AuthId, s.AuthValue, s.MenuId }).FirstOrDefault();
                    string newAuthValue = string.Join(",", arr);
                    //权限没有发生变化则不处理
                    if (auth == null || auth.AuthValue != newAuthValue)
                    {
                        updateAuths.Add(new Sys_RoleAuth()
                        {
                            RoleId = roleId,
                            MenuId = x.Id,
                            AuthValue = string.Join(",", arr),
                            AuthId = auth == null ? 0 : auth.AuthId,
                            ModifyDate = DateTime.Now,
                            Modifier = App.User.UserName,
                            CreateDate = DateTime.Now,
                            Creater = App.User.UserName
                        });
                    }
                    else
                    {
                        originalMeunIds.Add(auth.MenuId);
                    }
                }
                //更新权限
                _RoleAuthRepository.UpdateData(updateAuths.Where(x => x.AuthId > 0).ToList());
                //新增的权限
                _RoleAuthRepository.AddData(updateAuths.Where(x => x.AuthId <= 0).ToList());
                //获取权限取消的权限
                int[] authIds = roleAuths.Where(x => userPermissions.Select(u => u.Id)
                 .ToList().Contains(x.MenuId) || originalMeunIds.Contains(x.MenuId))
                .Select(s => s.AuthId)
                .ToArray();
                List<Sys_RoleAuth> delAuths = roleAuths.Where(x => x.AuthValue != "" && !authIds.Contains(x.AuthId)).ToList();
                delAuths.ForEach(x =>
                {
                    x.AuthValue = "";
                });
                //将取消的权限设置为""
                _RoleAuthRepository.DeleteData(delAuths);
                int addCount = updateAuths.Where(x => x.AuthId <= 0).Count();
                int updateCount = updateAuths.Where(x => x.AuthId > 0).Count();
                string _version = DateTime.Now.ToString("yyyyMMddHHMMssfff");
                content.OK($"保存成功:新增加配菜单权限{addCount}条,更新菜单{updateCount}条,删除权限{delAuths.Count}条");
            }
            catch (Exception ex)
            {
                message = "异常信息:" + ex.Message + ex.StackTrace + ",";
            }
            return content;
        }
        /// <summary>
        /// ç¼–辑权限时,获取当前用户的所有菜单权限
        /// </summary>
        /// <returns></returns>
        public WebResponseContent GetCurrentUserTreePermissionPDA()
        {
            return GetUserTreePermissionPDA(App.User.RoleId);
        }
        public WebResponseContent GetCurrentTreePermissionPDA()
        {
            WebResponseContent content = GetCurrentUserTreePermissionPDA();
            int roleId = App.User.RoleId;
            return WebResponseContent.Instance.OK(null, new
            {
                tree = content.Data,
                roles = GetAllChildren(roleId)
            });
        }
        public WebResponseContent GetUserTreePermissionPDA(int roleId)
        {
            if (!App.User.IsRoleIdSuperAdmin(roleId) && App.User.RoleId != roleId)
            {
                if (!(GetAllChildren(App.User.RoleId)).Exists(x => x.Id == roleId))
                {
                    return WebResponseContent.Instance.Error("没有权限获取此角色的权限信息");
                }
            }
            //获取用户权限
            List<Permissions> permissions = _MenuRepository.GetPermissions(roleId);
            //权限用户权限查询所有的菜单信息
            List<MenuDTO> menus = _MenuService.GetUserMenuListPDA(roleId);
            //获取当前用户权限如:(Add,Search)对应的显示文本信息如:Add:添加,Search:查询
            var data = menus.Where(x => x.MenuType == 1).Select(x => new
            {
                Id = x.MenuId,
                Pid = x.ParentId,
                Text = x.MenuName,
                IsApp = x.MenuType == 1,
                Actions = _MenuService.GetActions(x.MenuId, x.Actions, permissions, roleId)
            });
            return WebResponseContent.Instance.OK(null, data);
        }
        public WebResponseContent SavePermissionPDA(List<UserPermissionDTO> userPermissions, int roleId)
        {
            WebResponseContent content = new WebResponseContent();
            string message = "";
            try
            {
                if (!GetAllChildren(App.User.RoleId).Exists(x => x.Id == roleId))
                    return WebResponseContent.Instance.Error("没有权限修改此角色的权限信息");
                //当前用户的权限
                List<Permissions> permissions = _MenuRepository.GetPermissions(App.User.RoleId);
                List<int> originalMeunIds = new List<int>();
                List<int> menuIds = _MenuRepository.QueryData(x => x.MenuId, x => x.MenuType == 1);
                //被分配角色的权限
                List<Sys_RoleAuth> roleAuths = _RoleAuthRepository.QueryData(x => x.RoleId == roleId && menuIds.Contains(x.MenuId));
                List<Sys_RoleAuth> updateAuths = new List<Sys_RoleAuth>();
                foreach (UserPermissionDTO x in userPermissions)
                {
@@ -195,11 +365,11 @@
                    x.AuthValue = "";
                });
                //将取消的权限设置为""
                _RoleAuthRepository.UpdateData(delAuths);
                _RoleAuthRepository.DeleteData(delAuths);
                int addCount = updateAuths.Where(x => x.AuthId <= 0).Count();
                int updateCount = updateAuths.Where(x => x.AuthId > 0).Count();
                string _version = DateTime.Now.ToString("yyyyMMddHHMMssfff");