各个功能的增删改查接口
角色
/// <summary> /// 角色管理 /// </summary> [Route("api/[controller]/[action]")] [ApiController] public class RoleController : ControllerBase { IBaseServices<SysRole> _roleServices; public RoleController(IBaseServices<SysRole> roleServices) { _roleServices = roleServices; } /// <summary> /// 分页获取角色 /// </summary> /// <param name="page"></param> /// <param name="key"></param> /// <returns></returns> [HttpGet] public async Task<MessageModel<PageModel<SysRole>>> Get(int page = 1, string key = "") { if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key)) { key = ""; } int intPageSize = 50; var data = await _roleServices.QueryPage(a => a.IsDeleted != true && (a.RoleName != null && a.RoleName.Contains(key)), page, intPageSize, a => a.Id, false); return new MessageModel<PageModel<SysRole>>() { msg = "获取成功", success = data.dataCount >= 0, response = data }; } /// <summary> /// 新增角色 /// </summary> /// <param name="role"></param> /// <returns></returns> [HttpPost] public async Task<MessageModel<string>> Post([FromBody] SysRole role) { var data = new MessageModel<string>(); var m = (await _roleServices.Add(role)); data.success = m.Id > 0; if (data.success) { data.response = m.Id.ObjToString(); data.msg = "添加成功"; } return data; } /// <summary> /// 修改角色 /// </summary> /// <param name="role"></param> /// <returns></returns> [HttpPut] public async Task<MessageModel<string>> Put([FromBody] SysRole role) { var data = new MessageModel<string>(); if (role != null && role.Id > 0) { data.success = await _roleServices.Update(role); if (data.success) { data.msg = "更新成功"; } } return data; } /// <summary> /// 删除角色 /// </summary> /// <param name="id"></param> /// <returns></returns> [HttpDelete] public async Task<MessageModel<string>> Delete(int id) { var data = new MessageModel<string>(); if (id > 0) { var model = await _roleServices.QueryBy(a => a.Id == id); model.IsDeleted = true; data.success = await _roleServices.Update(model); if (data.success) { data.msg = "删除成功"; } } return data; } }
用户
/// <summary> /// 用户管理、用户和角色关系 /// </summary> [Route("api/[controller]/[action]")] [ApiController] public class UserController : ControllerBase { readonly IBaseServices<SysUser> _sysUserInfoServices; readonly IBaseServices<SysUserRole> _userRoleServices; readonly IBaseServices<SysRole> _roleServices; private readonly ILogger<UserController> _logger; public UserController(IBaseServices<SysUser> sysUserInfoServices, IBaseServices<SysUserRole> userRoleServices, IBaseServices<SysRole> roleServices, ILogger<UserController> logger) { _sysUserInfoServices = sysUserInfoServices; _userRoleServices = userRoleServices; _roleServices = roleServices; _logger = logger; } /// <summary> /// 分页获取所有用户 /// </summary> /// <param name="page"></param> /// <param name="key"></param> /// <returns></returns> [HttpGet] public async Task<MessageModel<PageModel<SysUser>>> Get(int page = 1, string key = "") { if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key)) { key = ""; } int intPageSize = 50; var data = await _sysUserInfoServices.QueryPage(a => a.IsDelete != true && a.Status >= 0 && ((a.LoginName != null && a.LoginName.Contains(key)) || (a.RealName != null && a.RealName.Contains(key))), page, intPageSize, m => m.Id, false); var allUserRoles = await _userRoleServices.Query(d => d.IsDeleted == false); var allRoles = await _roleServices.Query(d => d.IsDeleted == false); var sysUserInfos = data.data; foreach (var item in sysUserInfos) { var currentUserRoles = allUserRoles.Where(d => d.UserId == item.Id).Select(d => d.RoleId).ToList(); item.RIDs = currentUserRoles; item.RoleNames = allRoles.Where(d => currentUserRoles.Contains(d.Id)).Select(d => d.RoleName).ToList(); } data.data = sysUserInfos; _logger.LogInformation("Get Users"); return new MessageModel<PageModel<SysUser>>() { msg = "获取成功", success = data.dataCount >= 0, response = data }; } /// <summary> /// 添加用户 /// </summary> /// <param name="sysUserInfo"></param> /// <returns></returns> [HttpPost] public async Task<MessageModel<string>> Post([FromBody] SysUser sysUserInfo) { var data = new MessageModel<string>(); sysUserInfo.LoginPWD = MD5Helper.MD5Encrypt32(sysUserInfo.LoginPWD); var user = await _sysUserInfoServices.Add(sysUserInfo); data.success = user.Id > 0; if (data.success) { data.response = user.Id.ObjToString(); data.msg = "添加成功"; } return data; } /// <summary> /// 更新用户与角色 /// </summary> /// <param name="sysUserInfo"></param> /// <returns></returns> [HttpPut] public async Task<MessageModel<string>> Put([FromBody] SysUser sysUserInfo) { var data = new MessageModel<string>(); try { if (sysUserInfo != null && sysUserInfo.Id > 0) { if (sysUserInfo.RIDs.Count > 0) { var usreroles = (await _userRoleServices.Query(d => d.UserId == sysUserInfo.Id)).Select(d => d.Id.ToString()).ToArray(); if (usreroles.Count() > 0) { var isAllDeleted = await _userRoleServices.DeleteBy(d => d.UserId == sysUserInfo.Id); } var userRolsAdd = new List<SysUserRole>(); sysUserInfo.RIDs.ForEach(rid => { userRolsAdd.Add(new SysUserRole(sysUserInfo.Id, rid)); }); await _userRoleServices.Add(userRolsAdd); } data.success = await _sysUserInfoServices.Update(sysUserInfo); if (data.success) { data.msg = "更新成功"; } } } catch (Exception e) { _logger.LogError(e, e.Message); } return data; } /// <summary> /// 删除用户 /// </summary> /// <param name="id"></param> /// <returns></returns> [HttpDelete] public async Task<MessageModel<string>> Delete(int id) { var data = new MessageModel<string>(); if (id > 0) { var model = await _sysUserInfoServices.QueryBy(a => a.Id == id); model.IsDelete = true; data.success = await _sysUserInfoServices.Update(model); if (data.success) { data.msg = "删除成功"; } } return data; } }
接口
/// <summary> /// 接口管理 /// </summary> [Route("api/[controller]/[action]")] [ApiController] public class ActionController : ControllerBase { readonly IBaseServices<SysAction> _actionServices; public ActionController(IBaseServices<SysAction> actionServices) { _actionServices = actionServices; } /// <summary> /// 分页获取所有接口 /// </summary> /// <param name="page"></param> /// <param name="key"></param> /// <returns></returns> [HttpGet] public async Task<MessageModel<PageModel<SysAction>>> Get(int page = 1, string key = "") { if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key)) { key = ""; } int intPageSize = 50; Expression<Func<SysAction, bool>> whereExpression = a => a.IsDeleted != true && (a.Name != null && a.Name.Contains(key)); var data = await _actionServices.QueryPage(whereExpression, page, intPageSize, a => a.Id, false); return new MessageModel<PageModel<SysAction>>() { msg = "获取成功", success = data.dataCount >= 0, response = data }; } /// <summary> /// 新增一个API /// </summary> /// <param name="model"></param> /// <returns></returns> [HttpPost] public async Task<MessageModel<string>> Post([FromBody] SysAction model) { var data = new MessageModel<string>(); var m = (await _actionServices.Add(model)); data.success = m.Id > 0; if (data.success) { data.response = m.Id.ObjToString(); data.msg = "添加成功"; } return data; } /// <summary> /// 更新接口信息 /// </summary> /// <param name="model"></param> /// <returns></returns> [HttpPut] public async Task<MessageModel<string>> Put([FromBody] SysAction model) { var data = new MessageModel<string>(); if (model != null && model.Id > 0) { data.success = await _actionServices.Update(model); if (data.success) { data.msg = "更新成功"; } } return data; } /// <summary> /// 删除接口 /// </summary> /// <param name="id"></param> /// <returns></returns> [HttpDelete] public async Task<MessageModel<string>> Delete(int id) { var data = new MessageModel<string>(); if (id > 0) { var model = await _actionServices.QueryBy(a => a.Id == id); model.IsDeleted = true; data.success = await _actionServices.Update(model); if (data.success) { data.msg = "删除成功"; } } return data; } }
菜单及授权
/// <summary> /// 菜单管理、菜单授权 /// </summary> [Route("api/[controller]/[action]")] [ApiController] public class MenuController : ControllerBase { readonly IBaseServices<SysMenu> _sysMenuService; readonly IBaseServices<SysAction> _sysActionService; readonly IBaseServices<SysRoleMenu> _sysRoleMenuService; readonly IBaseServices<SysUserRole> _sysUserRoleService; public MenuController(IBaseServices<SysMenu> sysMenuService, IBaseServices<SysAction> sysActionService, IBaseServices<SysRoleMenu> sysRoleMenuService, IBaseServices<SysUserRole> sysUserRoleService) { _sysMenuService = sysMenuService; _sysActionService = sysActionService; _sysRoleMenuService = sysRoleMenuService; _sysUserRoleService = sysUserRoleService; } #region 菜单 /// <summary> /// 分页查询菜单 /// </summary> /// <param name="page"></param> /// <param name="key"></param> /// <returns></returns> [HttpGet] public async Task<MessageModel<PageModel<SysMenu>>> Get(int page = 1, string key = "") { PageModel<SysMenu> permissions = new PageModel<SysMenu>(); int intPageSize = 60; if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key)) { key = ""; } permissions = await _sysMenuService.QueryPage(a => a.IsDeleted != true && (a.Name != null && a.Name.Contains(key)), page, intPageSize, a => a.Id, false); var apis = await _sysActionService.Query(d => d.IsDeleted == false); var permissionsView = permissions.data; var permissionAll = await _sysMenuService.Query(d => d.IsDeleted != true); foreach (var item in permissionsView) { List<int> pidarr = new List<int> { item.Pid }; if (item.Pid > 0) { pidarr.Add(0); } var parent = permissionAll.FirstOrDefault(d => d.Id == item.Pid); while (parent != null) { pidarr.Add(parent.Id); parent = permissionAll.FirstOrDefault(d => d.Id == parent.Pid); } item.PidArr = pidarr.OrderBy(d => d).Distinct().ToList(); foreach (var pid in item.PidArr) { var per = permissionAll.FirstOrDefault(d => d.Id == pid); item.PnameArr.Add((per != null ? per.Name : "根节点") + "/"); } } permissions.data = permissionsView; return new MessageModel<PageModel<SysMenu>>() { msg = "获取成功", success = permissions.dataCount >= 0, response = permissions }; } /// <summary> /// 菜单树 /// </summary> /// <param name="f"></param> /// <param name="key"></param> /// <returns></returns> [HttpGet] public async Task<MessageModel<List<SysMenu>>> GetTreeTable(int f = 0, string key = "") { List<SysMenu> permissions = new List<SysMenu>(); var apiList = await _sysActionService.Query(d => d.IsDeleted == false); var permissionsList = await _sysMenuService.Query(d => d.IsDeleted == false); if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key)) { key = ""; } if (key != "") { permissions = permissionsList.Where(a => a.Name.Contains(key)).OrderBy(a => a.OrderSort).ToList(); } else { permissions = permissionsList.Where(a => a.Pid == f).OrderBy(a => a.OrderSort).ToList(); } foreach (var item in permissions) { List<int> pidarr = new List<int> { }; var parent = permissionsList.FirstOrDefault(d => d.Id == item.Pid); while (parent != null) { pidarr.Add(parent.Id); parent = permissionsList.FirstOrDefault(d => d.Id == parent.Pid); } pidarr.Reverse(); pidarr.Insert(0, 0); item.PidArr = pidarr; item.hasChildren = permissionsList.Where(d => d.Pid == item.Id).Any(); } return new MessageModel<List<SysMenu>>() { msg = "获取成功", success = true, response = permissions }; } [HttpPost] public async Task<MessageModel<string>> Post([FromBody] SysMenu model) { var data = new MessageModel<string>(); var id = (await _sysMenuService.Add(model)); data.success = id.Id > 0; if (data.success) { data.response = id.ObjToString(); data.msg = "添加成功"; } return data; } /// <summary> /// 更新菜单 /// </summary> /// <param name="model"></param> /// <returns></returns> [HttpPut] public async Task<MessageModel<string>> Put([FromBody] SysMenu model) { var data = new MessageModel<string>(); if (model != null && model.Id > 0) { data.success = await _sysMenuService.Update(model); if (data.success) { data.msg = "更新成功"; } } return data; } /// <summary> /// 删除菜单 /// </summary> /// <param name="id"></param> /// <returns></returns> [HttpDelete] public async Task<MessageModel<string>> Delete(int id) { var data = new MessageModel<string>(); if (id > 0) { var model = await _sysMenuService.QueryBy(a => a.Id == id); model.IsDeleted = true; data.success = await _sysMenuService.Update(model); if (data.success) { data.msg = "删除成功"; } } return data; } #endregion #region 菜单授权 /// <summary> /// 获取菜单树 /// </summary> /// <param name="pid"></param> /// <param name="needbtn"></param> /// <returns></returns> [HttpGet] public async Task<MessageModel<PermissionTree>> GetPermissionTree(int pid = 0, bool needbtn = false) { var data = new MessageModel<PermissionTree>(); var permissions = await _sysMenuService.Query(d => d.IsDeleted == false); var permissionTrees = (from child in permissions where child.IsDeleted == false orderby child.Id select new PermissionTree { value = child.Id, label = child.Name, Pid = child.Pid, order = child.OrderSort, }).ToList(); PermissionTree rootRoot = new PermissionTree { value = 0, Pid = 0, label = "根节点" }; permissionTrees = permissionTrees.OrderBy(d => d.order).ToList(); RecursionHelper.LoopToAppendChildren(permissionTrees, rootRoot, pid, needbtn); data.success = true; if (data.success) { data.response = rootRoot; data.msg = "获取成功"; } return data; } [HttpPost] public async Task<MessageModel<string>> Assign([FromBody] AssignView assignView) { var data = new MessageModel<string>(); if (assignView.rid > 0) { data.success = true; var RoleMenus = await _sysRoleMenuService.Query(d => d.RoleId == assignView.rid); var remove = RoleMenus.Where(d => !assignView.pids.Contains(d.MenuId.ObjToInt())).Select(c => (object)c.Id); data.success &= remove.Any() ? await _sysRoleMenuService.DeleteBy(d => !assignView.pids.Contains(d.MenuId.ObjToInt())) : true; foreach (var item in assignView.pids) { var rmpitem = RoleMenus.Where(d => d.MenuId == item); if (!rmpitem.Any()) { SysRoleMenu SysRoleMenu = new SysRoleMenu() { IsDeleted = false, RoleId = assignView.rid, MenuId = item, }; data.success &= (await _sysRoleMenuService.Add(SysRoleMenu)).Id > 0; } } if (data.success) { data.response = ""; data.msg = "保存成功"; } } return data; } /// <summary> /// 获取路由树 /// </summary> /// <param name="uid"></param> /// <returns></returns> [HttpGet] public async Task<MessageModel<NavigationBar>> GetNavigationBar(int uid) { var data = new MessageModel<NavigationBar>(); var uidInHttpcontext1 = 0; var roleIds = new List<int>(); if (uid > 0 && uid == uidInHttpcontext1) { if (roleIds.Any()) { var pids = (await _sysRoleMenuService.Query(d => d.IsDeleted == false && roleIds.Contains(d.RoleId))).Select(d => d.MenuId.ObjToInt()).Distinct(); if (pids.Any()) { var rolePermissionMoudles = (await _sysMenuService.Query(d => pids.Contains(d.Id))).OrderBy(c => c.OrderSort); var permissionTrees = (from child in rolePermissionMoudles where child.IsDeleted == false orderby child.Id select new NavigationBar { id = child.Id, name = child.Name, pid = child.Pid, order = child.OrderSort, path = child.Code, iconCls = child.Icon, meta = new NavigationBarMeta { requireAuth = true, title = child.Name } }).ToList(); NavigationBar rootRoot = new NavigationBar() { id = 0, pid = 0, order = 0, name = "根节点", path = "", iconCls = "", meta = new NavigationBarMeta(), }; permissionTrees = permissionTrees.OrderBy(d => d.order).ToList(); RecursionHelper.LoopNaviBarAppendChildren(permissionTrees, rootRoot); data.success = true; if (data.success) { data.response = rootRoot; data.msg = "获取成功"; } } } } return data; } #endregion } public class AssignView { public List<int> pids { get; set; } public int rid { get; set; } } public class AssignShow { public List<int> permissionids { get; set; } public List<string> assignbtns { get; set; } }