public ActionResult EditNavigation(NavigationEditModel model) { if (!ModelState.IsValid) { return(Json(new MessageData(false, "操作失败"))); } Navigation navigation; if (model.Id > 0) { navigation = _navigationService.FindById(model.Id); if (navigation == null) { return(Json(new MessageData(false, "导航不存在"))); } var nameExists = _navigationService.Navigations.FirstOrDefault(n => n.Id != model.Id && n.Name == model.Name) != null; if (nameExists) { return(Json(new MessageData(false, "导航名称已存在"))); } var permissionExists = _navigationService.Navigations.FirstOrDefault(n => n.Id != model.Id && n.PermissionName == model.PermissionName) != null; if (permissionExists) { return(Json(new MessageData(false, "权限名称已存在"))); } navigation.ParentId = model.ParentId; navigation.Name = model.Name; navigation.RouteName = model.RouteName; navigation.Url = model.Url; navigation.PermissionName = model.PermissionName; navigation.IconName = model.IconName; navigation.DisplayOrder = model.DisplayOrder; navigation.IsEnabled = model.IsEnabled; try { _navigationService.Update(navigation); //改变权限,保持一致 Permission permission = _adminService.FindPermissionByName(navigation.PermissionName); if (permission == null) { return(Json(new MessageData(true, "编辑成功,角色权限修改失败"))); } if (permission.ParentId.HasValue) { Navigation parentNavigation = _adminService.FindNavigationById(navigation.ParentId.Value); Permission parentPermission = _adminService.FindPermissionByName(parentNavigation.PermissionName); permission.ParentId = parentPermission.Id; _adminService.UpdatePermission(permission); } return(Json(new MessageData(true, "编辑成功"))); } catch (Exception ex) { _logger.Error(ex); return(Json(new MessageData(false, "编辑失败,请重新编辑"))); } } else { if (_navigationService.IsExists(model.Name)) { return(Json(new MessageData(false, "导航名称已存在"))); } var permissionExists = _navigationService.Navigations.FirstOrDefault(n => n.Id != model.Id && n.PermissionName == model.PermissionName) != null; if (permissionExists) { return(Json(new MessageData(false, "权限名称已存在"))); } //添加权限 Permission permission = new Permission { Name = model.PermissionName, ParentId = model.ParentId, Description = model.PermissionName, IsBuiltIn = true, IsButton = false, RolePermissonEnumType = RolePermissonEnum.Navigation }; _adminService.InsertPermission(permission); //添加用户权限关联 var userId = UserContext.CurrentUserId; _permissionService.AddPermissionToUser(userId, permission.Id); navigation = new Navigation() { ParentId = model.ParentId, Name = model.Name, RouteName = model.RouteName, Url = model.Url, PermissionName = model.PermissionName, IconName = model.IconName, DisplayOrder = model.DisplayOrder, IsEnabled = model.IsEnabled, }; try { _navigationService.Create(navigation); return(Json(new MessageData(true, "添加成功"))); } catch (Exception ex) { _logger.Error(ex); return(Json(new MessageData(false, "添加失败"))); } } }