From 1bf57c20f4a1c57e206e7bb366e0d3bd2d7c385e Mon Sep 17 00:00:00 2001 From: hudingwen <765472804@qq.com> Date: Sat, 3 Feb 2024 22:23:14 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E9=85=8D=E5=90=88=E5=89=8D=E7=AB=AFvue3?= =?UTF-8?q?=E7=BB=86=E8=8A=82=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Blog.Core.Api/Blog.Core.Model.xml | 93 +++++- Blog.Core.Api/Blog.Core.xml | 183 +++++++++--- .../Controllers/DbFirst/MigrateController.cs | 6 +- Blog.Core.Api/Controllers/DicController.cs | 274 ++++++++++++++++++ .../Controllers/PermissionController.cs | 48 +-- Blog.Core.Api/Controllers/RoleController.cs | 9 +- Blog.Core.Api/Controllers/UserController.cs | 137 ++++++++- Blog.Core.Common/Helper/RecursionHelper.cs | 39 ++- Blog.Core.IServices/IDicService.cs | 12 + Blog.Core.Model/Models/DicData.cs | 44 +++ Blog.Core.Model/Models/DicType.cs | 34 +++ Blog.Core.Model/Models/Permission.cs | 2 +- Blog.Core.Model/Models/sysUserInfo.cs | 5 +- Blog.Core.Model/PaginationModel.cs | 12 +- Blog.Core.Services/BASE/BaseServices.cs | 4 +- Blog.Core.Services/DicService.cs | 80 +++++ 16 files changed, 875 insertions(+), 107 deletions(-) create mode 100644 Blog.Core.Api/Controllers/DicController.cs create mode 100644 Blog.Core.IServices/IDicService.cs create mode 100644 Blog.Core.Model/Models/DicData.cs create mode 100644 Blog.Core.Model/Models/DicType.cs create mode 100644 Blog.Core.Services/DicService.cs diff --git a/Blog.Core.Api/Blog.Core.Model.xml b/Blog.Core.Api/Blog.Core.Model.xml index 8de49a7b..16896249 100644 --- a/Blog.Core.Api/Blog.Core.Model.xml +++ b/Blog.Core.Api/Blog.Core.Model.xml @@ -386,6 +386,86 @@ Nullable:True + + + 字典数据表(多数据) + + + + + 父级字典code + + + + + 字典code + + + + + 字典名称 + + + + + 字典内容 + + + + + 字典内容2 + + + + + 字典内容3 + + + + + 字典描述 + + + + + 排序 + + + + + 字典类型表(单数据) + + + + + 字典code + + + + + 字典名称 + + + + + 字典内容 + + + + + 字典内容2 + + + + + 字典内容3 + + + + + 字典描述 + + 用户团队表 @@ -1922,22 +2002,27 @@ 时间:2020-4-3 20:31:26 - + 当前页 - + 每页大小 - + + + 搜索关键词 + + + 排序字段(例如:id desc,time asc) - + 查询条件( 例如:id = 1 and name = 小明) diff --git a/Blog.Core.Api/Blog.Core.xml b/Blog.Core.Api/Blog.Core.xml index dfe9950a..7afc0db7 100644 --- a/Blog.Core.Api/Blog.Core.xml +++ b/Blog.Core.Api/Blog.Core.xml @@ -385,7 +385,7 @@ - + 查询树形 Table @@ -468,11 +468,12 @@ 角色管理 - + 获取全部角色 + @@ -666,19 +667,7 @@ 用户管理 - - - 构造函数 - - - - - - - - - - + 获取全部用户 @@ -695,14 +684,14 @@ 令牌 - + 添加一个用户 - + 更新用户与角色 @@ -716,6 +705,34 @@ + + + 重置密码 + + + + + + + 更新头像 + + + + + + + 重置我的密码 + + + + + + + 更新我的资料 + + + + 用户角色关系 @@ -1520,6 +1537,47 @@ + + + Summary:全局路由权限公约 + Remarks:目的是针对不同的路由,采用不同的授权过滤器 + 如果 controller 上不加 [Authorize] 特性,默认都是 Permission 策略 + 否则,如果想特例其他授权机制的话,需要在 controller 上带上 [Authorize],然后再action上自定义授权即可,比如 [Authorize(Roles = "Admin")] + + + + + 全局权限过滤器【无效】 + + + + + 全局异常错误日志 + + + + + 自定义返回格式 + + + + + + + + 生产环境的消息 + + + + + 开发环境的消息 + + + + + 全局路由前缀公约 + + 自定义路由 /api/{version}/[controler]/[action] @@ -1543,46 +1601,99 @@ - + - Summary:全局路由权限公约 - Remarks:目的是针对不同的路由,采用不同的授权过滤器 - 如果 controller 上不加 [Authorize] 特性,默认都是 Permission 策略 - 否则,如果想特例其他授权机制的话,需要在 controller 上带上 [Authorize],然后再action上自定义授权即可,比如 [Authorize(Roles = "Admin")] + 字典管理 - + - 全局权限过滤器【无效】 + 获取字典类型 + + - + - 全局异常错误日志 + 获取字典类型列表 + + - + - 自定义返回格式 + 获取列表 - - + + + - + - 生产环境的消息 + 添加 + + - + - 开发环境的消息 + 更新 + + - + - 全局路由前缀公约 + 删除 + + + + + + 批量删除 + + + + + + + 获取列表 + + + + + + + + + + 添加 + + + + + + + 更新 + + + + + + + 删除 + + + + + + + 批量删除 + + + diff --git a/Blog.Core.Api/Controllers/DbFirst/MigrateController.cs b/Blog.Core.Api/Controllers/DbFirst/MigrateController.cs index 7865cc69..71d6671a 100644 --- a/Blog.Core.Api/Controllers/DbFirst/MigrateController.cs +++ b/Blog.Core.Api/Controllers/DbFirst/MigrateController.cs @@ -276,9 +276,9 @@ private void InitPermissionTree(List permissionsTree, List d.Pid == item.Id).ToList(); + item.children = all.Where(d => d.Pid == item.Id).ToList(); item.Module = apis.FirstOrDefault(d => d.Id == item.Mid); - InitPermissionTree(item.Children, all, apis); + InitPermissionTree(item.children, all, apis); } } @@ -342,7 +342,7 @@ private async Task SavePermissionTreeAsync(List permissionsTree, Lis } pms.Add(pm); - await SavePermissionTreeAsync(item.Children, pms, permissionId); + await SavePermissionTreeAsync(item.children, pms, permissionId); } } diff --git a/Blog.Core.Api/Controllers/DicController.cs b/Blog.Core.Api/Controllers/DicController.cs new file mode 100644 index 00000000..89235dac --- /dev/null +++ b/Blog.Core.Api/Controllers/DicController.cs @@ -0,0 +1,274 @@ +using Blog.Core; +using Blog.Core.Common.Caches; +using Blog.Core.Common.Extensions; +using Blog.Core.Common.Helper; +using Blog.Core.Controllers; +using Blog.Core.IServices; +using Blog.Core.IServices.BASE; +using Blog.Core.Model; +using Blog.Core.Model.Models; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace MyDotnet.Controllers.System +{ + /// + /// 字典管理 + /// + [Route("api/[controller]/[action]")] + [ApiController] + [Authorize] + public class DicController : BaseApiController + { + IDicService _dicService; + IBaseServices _dicType; + IBaseServices _dicData; + ICaching _caching; + + public DicController(IDicService dicService, IBaseServices dicType, IBaseServices dicData, ICaching caching) + { + _dicService = dicService; + _dicType = dicType; + _dicData = dicData; + _caching = caching; + } + /// + /// 获取字典类型 + /// + /// + /// + [HttpGet] + public async Task> GetDic(string code) + { + var data = await _dicService.GetDic(code); + return Success(data); + } + /// + /// 获取字典类型列表 + /// + /// + /// + [HttpGet] + public async Task>> GetDicData(string code) + { + var data = await _dicService.GetDicData(code); + return Success(data); + } + + + + + + + + + + + + /// + /// 获取列表 + /// + /// + /// + /// + /// + [HttpGet] + [Authorize(Permissions.Name)] + public async Task>> Get(int page = 1, int size = 10, string key = "") + { + var whereFind = LinqHelper.True(); + + if (!string.IsNullOrEmpty(key)) + { + whereFind = whereFind.And(t => t.code.Contains(key) + || t.name.Contains(key) + || t.content.Contains(key) + || t.content2.Contains(key) + || t.content3.Contains(key) + || t.description.Contains(key)); + } + var data = await _dicType.QueryPage(whereFind, page, size, " Id desc "); + return Success(data, "获取成功"); + } + + /// + /// 添加 + /// + /// + /// + [HttpPost] + [Authorize(Permissions.Name)] + public async Task> Post([FromBody] DicType data) + { + var id = await _dicType.Add(data); + await _caching.RemoveAsync(data.code); + return id > 0 ? Success(id.ObjToString(), "添加成功") : Failed(); + + } + + /// + /// 更新 + /// + /// + /// + [HttpPut] + [Authorize(Permissions.Name)] + public async Task> Put([FromBody] DicType data) + { + if (data == null || data.Id <= 0) + return Failed("缺少参数"); + await _caching.RemoveAsync(data.code); + return await _dicType.Update(data) ? Success(data.Id.ObjToString(), "更新成功") : Failed(); + } + + /// + /// 删除 + /// + /// + /// + [HttpDelete] + [Authorize(Permissions.Name)] + public async Task> Delete(long id) + { + if (id <= 0) + return Failed("缺少参数"); + var data = await _dicType.QueryById(id); + if(data == null) + return Failed("数据不存在"); + var isOk = await _dicType.DeleteById(id); + await _caching.RemoveAsync(data.code); + if (isOk) + return Success("","删除成功"); + return Failed(); + } + + /// + /// 批量删除 + /// + /// + /// + [HttpPost] + [Authorize(Permissions.Name)] + public async Task> Deletes([FromBody] object[] ids) + { + + var ls = await _dicType.QueryByIDs(ids); + var isOk = await _dicType.DeleteByIds(ids); + if (isOk) + return Success("", "删除成功"); + foreach (var data in ls) + { + await _caching.RemoveAsync(data.code); + } + return Failed(); + } + + + + + + + /// + /// 获取列表 + /// + /// + /// + /// + /// + /// + [HttpGet] + [Authorize(Permissions.Name)] + public async Task>> DicDataGet(string code,int page = 1, int size = 10, string key = "") + { + var whereFind = LinqHelper.True(); + if (string.IsNullOrEmpty(code)) + { + return MessageModel>.Fail("请选择一个要查询的字典"); + } + else + { + whereFind = whereFind.And(t => t.pCode.Equals(code)); + } + + if (!string.IsNullOrEmpty(key)) + { + whereFind = whereFind.And(t => t.name.Contains(key) + || t.content.Contains(key) + || t.content2.Contains(key) + || t.content3.Contains(key) + || t.description.Contains(key)); + } + var data = await _dicData.QueryPage(whereFind, page, size, "codeOrder asc"); + return Success(data, "获取成功"); + } + + /// + /// 添加 + /// + /// + /// + [HttpPost] + [Authorize(Permissions.Name)] + public async Task> DicDataPost([FromBody] DicData data) + { + var id = await _dicData.Add(data); + await _caching.RemoveAsync($"{data.pCode}_list"); + return id > 0 ? Success(id.ObjToString(), "添加成功") : Failed(); + + } + + /// + /// 更新 + /// + /// + /// + [HttpPut] + [Authorize(Permissions.Name)] + public async Task> DicDataPut([FromBody] DicData data) + { + if (data == null || data.Id <= 0) + return Failed("缺少参数"); + await _caching.RemoveAsync($"{data.pCode}_list"); + return await _dicData.Update(data) ? Success(data.Id.ObjToString(), "更新成功") : Failed(); + } + + /// + /// 删除 + /// + /// + /// + [HttpDelete] + [Authorize(Permissions.Name)] + public async Task> DicDataDelete(long id) + { + if (id <= 0) + return Failed("缺少参数"); + var data = await _dicData.QueryById(id); + await _caching.RemoveAsync($"{data.pCode}_list"); + var isOk = await _dicData.DeleteById(id); + if (isOk) + return Success("", "删除成功"); + return Failed(); + } + + /// + /// 批量删除 + /// + /// + /// + [HttpPost] + [Authorize(Permissions.Name)] + public async Task> DicDataDeletes([FromBody] object[] ids) + { + var isOk = await _dicData.DeleteByIds(ids); + var data = await _dicData.QueryById(ids[0]); + await _caching.RemoveAsync($"{data.pCode}_list"); + if (isOk) + return Success("", "删除成功"); + return Failed(); + } + + + + } +} diff --git a/Blog.Core.Api/Controllers/PermissionController.cs b/Blog.Core.Api/Controllers/PermissionController.cs index 13f8125a..540640a7 100644 --- a/Blog.Core.Api/Controllers/PermissionController.cs +++ b/Blog.Core.Api/Controllers/PermissionController.cs @@ -147,54 +147,30 @@ public async Task>> Get(int page = 1, string /// [HttpGet] [AllowAnonymous] - public async Task>> GetTreeTable(long f = 0, string key = "") + public async Task>> GetTreeTable() { List permissions = new List(); var apiList = await _moduleServices.Query(d => d.IsDeleted == false); + + var permissionsList = await _permissionServices.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 pidarr = new() { }; - var parent = permissionsList.FirstOrDefault(d => d.Id == item.Pid); - while (parent != null) - { - pidarr.Add(parent.Id); - parent = permissionsList.FirstOrDefault(d => d.Id == parent.Pid); - } + Permission rootRoot = new Permission + { + Id = 0, + Pid = 0, + Name = "根节点" + }; - //item.PidArr = pidarr.OrderBy(d => d).Distinct().ToList(); + permissionsList = permissionsList.OrderBy(d => d.OrderSort).ToList(); - pidarr.Reverse(); - pidarr.Insert(0, 0); - item.PidArr = pidarr; - item.MName = apiList.FirstOrDefault(d => d.Id == item.Mid)?.LinkUrl; - item.hasChildren = permissionsList.Where(d => d.Pid == item.Id).Any(); - } + RecursionHelper.LoopToAppendChildren(permissionsList, rootRoot, 0, apiList); - //return new MessageModel>() - //{ - // msg = "获取成功", - // success = true, - // response = permissions - //}; - return Success(permissions, "获取成功"); + return Success(rootRoot.children, "获取成功"); } /// diff --git a/Blog.Core.Api/Controllers/RoleController.cs b/Blog.Core.Api/Controllers/RoleController.cs index 0b93e943..2e614ef0 100644 --- a/Blog.Core.Api/Controllers/RoleController.cs +++ b/Blog.Core.Api/Controllers/RoleController.cs @@ -29,20 +29,19 @@ public RoleController(IRoleServices roleServices, IUser user) /// 获取全部角色 /// /// + /// /// /// // GET: api/User [HttpGet] - public async Task>> Get(int page = 1, string key = "") + public async Task>> Get(int page = 1, int size = 10, string key = "") { if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key)) { key = ""; } - - int intPageSize = 50; - - var data = await _roleServices.QueryPage(a => a.IsDeleted != true && (a.Name != null && a.Name.Contains(key)), page, intPageSize, " Id desc "); + + var data = await _roleServices.QueryPage(a => a.IsDeleted != true && (a.Name != null && a.Name.Contains(key)), page, size, " Id desc "); //return new MessageModel>() //{ diff --git a/Blog.Core.Api/Controllers/UserController.cs b/Blog.Core.Api/Controllers/UserController.cs index 95137c8e..3d89eedf 100644 --- a/Blog.Core.Api/Controllers/UserController.cs +++ b/Blog.Core.Api/Controllers/UserController.cs @@ -9,6 +9,7 @@ using Blog.Core.Repository.UnitOfWorks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using System.IdentityModel.Tokens.Jwt; namespace Blog.Core.Controllers { @@ -28,6 +29,7 @@ public class UserController : BaseApiController private readonly IUser _user; private readonly IMapper _mapper; private readonly ILogger _logger; + public IHttpContextAccessor _httpContext; /// /// 构造函数 @@ -39,12 +41,14 @@ public class UserController : BaseApiController /// /// /// - /// + /// public UserController(IUnitOfWorkManage unitOfWorkManage, ISysUserInfoServices sysUserInfoServices, IUserRoleServices userRoleServices, IRoleServices roleServices, IDepartmentServices departmentServices, - IUser user, IMapper mapper, ILogger logger) + IUser user, IMapper mapper, ILogger logger + ,IHttpContextAccessor httpContext) { _unitOfWorkManage = unitOfWorkManage; _sysUserInfoServices = sysUserInfoServices; @@ -54,6 +58,8 @@ public UserController(IUnitOfWorkManage unitOfWorkManage, ISysUserInfoServices s _user = user; _mapper = mapper; _logger = logger; + _httpContext = httpContext; + } /// @@ -64,7 +70,7 @@ public UserController(IUnitOfWorkManage unitOfWorkManage, ISysUserInfoServices s /// // GET: api/User [HttpGet] - public async Task>> Get(int page = 1, string key = "") + public async Task>> Get(int page = 1, string key = "") { if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key)) { @@ -100,7 +106,7 @@ public async Task>> Get(int page = 1, str #endregion - return Success(data.ConvertTo(_mapper)); + return Success(data.ConvertTo(_mapper)); } private (string, List) GetFullDepartmentName(List departments, long departmentId) @@ -137,9 +143,9 @@ public string Get(string id) /// [HttpGet] [AllowAnonymous] - public async Task> GetInfoByToken(string token) + public async Task> GetInfoByToken(string token) { - var data = new MessageModel(); + var data = new MessageModel(); if (!string.IsNullOrEmpty(token)) { var tokenModel = JwtHelper.SerializeJwt(token); @@ -148,7 +154,7 @@ public async Task> GetInfoByToken(string token) var userinfo = await _sysUserInfoServices.QueryById(tokenModel.Uid); if (userinfo != null) { - data.response = _mapper.Map(userinfo); + data.response = userinfo; data.success = true; data.msg = "获取成功"; } @@ -165,14 +171,14 @@ public async Task> GetInfoByToken(string token) /// // POST: api/User [HttpPost] - public async Task> Post([FromBody] SysUserInfoDto sysUserInfo) + public async Task> Post([FromBody] SysUserInfo sysUserInfo) { var data = new MessageModel(); - sysUserInfo.uLoginPWD = MD5Helper.MD5Encrypt32(sysUserInfo.uLoginPWD); - sysUserInfo.uRemark = _user.Name; + sysUserInfo.LoginPWD = MD5Helper.MD5Encrypt32(sysUserInfo.LoginPWD); + sysUserInfo.Remark = _user.Name; - var id = await _sysUserInfoServices.Add(_mapper.Map(sysUserInfo)); + var id = await _sysUserInfoServices.Add(sysUserInfo); data.success = id > 0; if (data.success) { @@ -190,12 +196,12 @@ public async Task> Post([FromBody] SysUserInfoDto sysUserIn /// // PUT: api/User/5 [HttpPut] - public async Task> Put([FromBody] SysUserInfoDto sysUserInfo) + public async Task> Put([FromBody] SysUserInfo sysUserInfo) { // 这里使用事务处理 var data = new MessageModel(); - var oldUser = await _sysUserInfoServices.QueryById(sysUserInfo.uID); + var oldUser = await _sysUserInfoServices.QueryById(sysUserInfo.Id); if (oldUser is not { Id: > 0 }) { return Failed("用户不存在或已被删除"); @@ -203,7 +209,7 @@ public async Task> Put([FromBody] SysUserInfoDto sysUserInf try { - if (sysUserInfo.uLoginPWD != oldUser.LoginPWD) + if (sysUserInfo.LoginPWD != oldUser.LoginPWD) { oldUser.CriticalModifyTime = DateTime.Now; } @@ -282,5 +288,108 @@ public async Task> Delete(long id) return data; } + + + /// + /// 重置密码 + /// + /// + /// + [HttpPut] + [Authorize(Permissions.Name)] + public async Task> ResetPass([FromBody] SysUserInfo sysUserInfo) + { + //重置密码 + + var oldUser = await _sysUserInfoServices.QueryById(sysUserInfo.Id); + if (oldUser == null || oldUser.IsDeleted) + { + return Failed("用户不存在或已被删除"); + } + oldUser.LoginPWD = MD5Helper.MD5Encrypt32(sysUserInfo.LoginPWD); + await _sysUserInfoServices.Update(oldUser); + return Success("重置成功"); + } + + /// + /// 更新头像 + /// + /// + /// + [HttpPut] + [Authorize] + public async Task> RefreshMyLogo([FromBody] SysUserInfo sysUserInfo) + { + //重置密码 登录既可修改 + var uid = _user.ID; + var oldUser = await _sysUserInfoServices.QueryById(uid); + if (oldUser == null || oldUser.IsDeleted) + { + return Failed("用户不存在或已被删除"); + } + oldUser.logo = sysUserInfo.logo; + await _sysUserInfoServices.Update(oldUser); + return Success("更新成功"); + } + + /// + /// 重置我的密码 + /// + /// + /// + [HttpPut] + [Authorize] + public async Task> ResetMyPass([FromBody] SysUserInfo sysUserInfo) + { + //重置密码 登录既可修改 + + var uid = _user.ID; + var oldUser = await _sysUserInfoServices.QueryById(uid); + if (oldUser == null || oldUser.IsDeleted) + { + return Failed("用户不存在或已被删除"); + } + oldUser.LoginPWD = MD5Helper.MD5Encrypt32(sysUserInfo.LoginPWD); + await _sysUserInfoServices.Update(oldUser); + return Success("重置成功"); + } + /// + /// 更新我的资料 + /// + /// + /// + [HttpPut] + [Authorize] + public async Task> PutMyInfo([FromBody] SysUserInfo sysUserInfo) + { + + var uid = _user.ID; + + var data = new MessageModel(); + + //登录账号判断 + var hasUser = await _sysUserInfoServices.Query(t => t.LoginName == sysUserInfo.LoginName && t.IsDeleted == false && t.Id != uid); + if (hasUser.Count > 0) + { + return Failed($"登录名:{sysUserInfo.LoginName}已存在,请重新填写!"); + } + + var oldUser = await _sysUserInfoServices.QueryById(uid); + if (oldUser == null || oldUser.IsDeleted) + { + return Failed("用户不存在或已被删除"); + } + + oldUser.RealName = sysUserInfo.RealName; + oldUser.LoginName = sysUserInfo.LoginName; + oldUser.Sex = sysUserInfo.Sex; + oldUser.Age = sysUserInfo.Age; + oldUser.Birth = sysUserInfo.Birth; + oldUser.Address = sysUserInfo.Address; + oldUser.Remark = sysUserInfo.Remark; + await _sysUserInfoServices.Update(oldUser); + + return Success("更新成功"); + } } } \ No newline at end of file diff --git a/Blog.Core.Common/Helper/RecursionHelper.cs b/Blog.Core.Common/Helper/RecursionHelper.cs index f223cd71..d46623fe 100644 --- a/Blog.Core.Common/Helper/RecursionHelper.cs +++ b/Blog.Core.Common/Helper/RecursionHelper.cs @@ -1,4 +1,5 @@ -using System; +using Blog.Core.Model.Models; +using System; using System.Collections.Generic; using System.Linq; @@ -78,6 +79,42 @@ public static void LoopToAppendChildren(List all, DepartmentTree LoopToAppendChildren(all, subItem, pid); } } + /// + /// 菜单列表 + /// + /// + /// + /// + /// + public static void LoopToAppendChildren(List all, Permission curItem, long pid, List allApi) + { + var subItems = all.Where(ee => ee.Pid == curItem.Id).ToList(); + curItem.MName = allApi.FirstOrDefault(d => d.Id == curItem.Mid)?.LinkUrl; + if (subItems.Count > 0) + { + + foreach (var subItem in subItems) + { + subItem.MName = allApi.FirstOrDefault(d => d.Id == subItem.Mid)?.LinkUrl; + } + + curItem.children = subItems; + } + else + { + curItem.children = new List(); + } + + foreach (var subItem in subItems) + { + if (subItem.Id == pid && pid > 0) + { + //subItem.disabled = true;//禁用当前节点 + } + + LoopToAppendChildren(all, subItem, pid, allApi); + } + } public static void LoopNaviBarAppendChildren(List all, NavigationBar curItem) diff --git a/Blog.Core.IServices/IDicService.cs b/Blog.Core.IServices/IDicService.cs new file mode 100644 index 00000000..ce341ccd --- /dev/null +++ b/Blog.Core.IServices/IDicService.cs @@ -0,0 +1,12 @@ +using Blog.Core.IServices.BASE; +using Blog.Core.Model.Models; + + +namespace Blog.Core.IServices +{ + public interface IDicService : IBaseServices + { + public Task GetDic(string code); + public Task> GetDicData(string code); + } +} diff --git a/Blog.Core.Model/Models/DicData.cs b/Blog.Core.Model/Models/DicData.cs new file mode 100644 index 00000000..eb046d35 --- /dev/null +++ b/Blog.Core.Model/Models/DicData.cs @@ -0,0 +1,44 @@ + + +namespace Blog.Core.Model.Models +{ + /// + /// 字典数据表(多数据) + /// + public class DicData: RootEntityTkey + { + /// + /// 父级字典code + /// + public string pCode { set; get; } + /// + /// 字典code + /// + public string code { set; get; } + /// + /// 字典名称 + /// + public string name { set; get; } + /// + /// 字典内容 + /// + public string content { set; get; } + /// + /// 字典内容2 + /// + public string content2 { set; get; } + /// + /// 字典内容3 + /// + public string content3 { set; get; } + /// + /// 字典描述 + /// + public string description { set; get; } + + /// + /// 排序 + /// + public int codeOrder { get; set; } + } +} diff --git a/Blog.Core.Model/Models/DicType.cs b/Blog.Core.Model/Models/DicType.cs new file mode 100644 index 00000000..1f3a82cb --- /dev/null +++ b/Blog.Core.Model/Models/DicType.cs @@ -0,0 +1,34 @@ + +namespace Blog.Core.Model.Models +{ + /// + /// 字典类型表(单数据) + /// + public class DicType: RootEntityTkey + { + /// + /// 字典code + /// + public string code { set; get; } + /// + /// 字典名称 + /// + public string name { set; get; } + /// + /// 字典内容 + /// + public string content { set; get; } + /// + /// 字典内容2 + /// + public string content2 { set; get; } + /// + /// 字典内容3 + /// + public string content3 { set; get; } + /// + /// 字典描述 + /// + public string description { set; get; } + } +} diff --git a/Blog.Core.Model/Models/Permission.cs b/Blog.Core.Model/Models/Permission.cs index 9dd6238d..41ba9b28 100644 --- a/Blog.Core.Model/Models/Permission.cs +++ b/Blog.Core.Model/Models/Permission.cs @@ -114,7 +114,7 @@ public Permission() public bool hasChildren { get; set; } = true; [SugarColumn(IsIgnore = true)] - public List Children { get; set; } = new List(); + public List children { get; set; } = new List(); [SugarColumn(IsIgnore = true)] public Modules Module { get; set; } diff --git a/Blog.Core.Model/Models/sysUserInfo.cs b/Blog.Core.Model/Models/sysUserInfo.cs index 1137d91c..5031d738 100644 --- a/Blog.Core.Model/Models/sysUserInfo.cs +++ b/Blog.Core.Model/Models/sysUserInfo.cs @@ -116,8 +116,11 @@ public SysUserInfo(string loginName, string loginPWD) // 地址 [SugarColumn(Length = 200, IsNullable = true)] public string Address { get; set; } + // 头像 + [SugarColumn(IsNullable = true)] + public string logo { get; set; } - [SugarColumn(DefaultValue = "1")] + [SugarColumn(DefaultValue = "1")] public bool Enable { get; set; } = true; [SugarColumn(IsNullable = true)] diff --git a/Blog.Core.Model/PaginationModel.cs b/Blog.Core.Model/PaginationModel.cs index f79b3316..692cf8c0 100644 --- a/Blog.Core.Model/PaginationModel.cs +++ b/Blog.Core.Model/PaginationModel.cs @@ -10,18 +10,22 @@ public class PaginationModel /// /// 当前页 /// - public int PageIndex { get; set; } = 1; + public int page { get; set; } = 1; /// /// 每页大小 /// - public int PageSize { get; set; } = 10; + public int size { get; set; } = 10; + /// + /// 搜索关键词 + /// + public string key { get; set; } /// /// 排序字段(例如:id desc,time asc) /// - public string OrderByFileds { get; set; } + public string orderByFileds { get; set; } /// /// 查询条件( 例如:id = 1 and name = 小明) /// - public string Conditions { get; set; } + public string conditions { get; set; } } } diff --git a/Blog.Core.Services/BASE/BaseServices.cs b/Blog.Core.Services/BASE/BaseServices.cs index 44042382..daacd654 100644 --- a/Blog.Core.Services/BASE/BaseServices.cs +++ b/Blog.Core.Services/BASE/BaseServices.cs @@ -338,8 +338,8 @@ public async Task> QueryPage(Expression> public async Task> QueryPage(PaginationModel pagination) { - var express = DynamicLinqFactory.CreateLambda(pagination.Conditions); - return await QueryPage(express, pagination.PageIndex, pagination.PageSize, pagination.OrderByFileds); + var express = DynamicLinqFactory.CreateLambda(pagination.conditions); + return await QueryPage(express, pagination.page, pagination.size, pagination.orderByFileds); } #region 分表 diff --git a/Blog.Core.Services/DicService.cs b/Blog.Core.Services/DicService.cs new file mode 100644 index 00000000..8c496dc8 --- /dev/null +++ b/Blog.Core.Services/DicService.cs @@ -0,0 +1,80 @@ + + +using Blog.Core.Common.Caches; +using Blog.Core.IServices; +using Blog.Core.Model.Models; +using Blog.Core.Repository.Base; +using Blog.Core.Services.BASE; + +namespace MyDotnet.Services.System +{ + /// + /// 字典服务类 + /// + public class DicService: BaseServices, IDicService + { + private BaseRepository _dicType; + private BaseRepository _dicData; + private ICaching _caching; + /// + /// 构造函数 + /// + /// + /// + /// + public DicService(BaseRepository baseRepository + , BaseRepository dicBaseRepository + , ICaching caching) : base(baseRepository) + { + _dicType = baseRepository; + _dicData = dicBaseRepository; + _caching = caching; + } + /// + /// 获取一个字典类型值 + /// + /// + /// + public async Task GetDic(string code) + { + var data = await _caching.GetAsync(code); + if(data == null) + { + //缓存穿透 + var ls = await _dicType.Query(t => t.code == code); + if (ls == null || ls.Count == 0) + { + throw new Exception($"字典[{code}]不存在"); + } + data = ls.FirstOrDefault(); + //设置缓存 + _caching.Set(code, data); + } + return data; + } + + /// + /// 获取一个字典类型列表值 + /// + /// + /// + public async Task> GetDicData(string code) + { + + var data = await _caching.GetAsync>(code); + if(data == null) + { + data = await _dicData.Query(t => t.pCode == code, "codeOrder asc"); + if (data == null || data.Count == 0) + { + throw new Exception($"字典[{code}]不存在"); + } + //设置缓存 + _caching.Set($"{code}_list", data); + } + return data; + } + + + } +} From 62aeb8baa88968169159a01dacae1452332c19c2 Mon Sep 17 00:00:00 2001 From: hudingwen <765472804@qq.com> Date: Sun, 18 Feb 2024 23:43:00 +0800 Subject: [PATCH 2/2] =?UTF-8?q?blogadmin=E7=9A=84v2=E5=92=8Cv3=E5=8C=BA?= =?UTF-8?q?=E5=88=86=E4=BA=86=E4=B8=80=E4=B8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Blog.Core.Api/Blog.Core.xml | 34 +++- Blog.Core.Api/Controllers/UserController.cs | 182 +++++++++++++++++++- 2 files changed, 210 insertions(+), 6 deletions(-) diff --git a/Blog.Core.Api/Blog.Core.xml b/Blog.Core.Api/Blog.Core.xml index 7afc0db7..1e379c49 100644 --- a/Blog.Core.Api/Blog.Core.xml +++ b/Blog.Core.Api/Blog.Core.xml @@ -668,6 +668,14 @@ + + + 获取全部用户 + + + + + 获取全部用户 @@ -676,6 +684,14 @@ + + + 获取用户详情根据token + 【无权限】 + + 令牌 + + 获取用户详情根据token @@ -684,14 +700,28 @@ 令牌 - + + + 添加一个用户 + + + + + 添加一个用户 - + + + 更新用户与角色 + + + + + 更新用户与角色 diff --git a/Blog.Core.Api/Controllers/UserController.cs b/Blog.Core.Api/Controllers/UserController.cs index 3d89eedf..4df5ce2a 100644 --- a/Blog.Core.Api/Controllers/UserController.cs +++ b/Blog.Core.Api/Controllers/UserController.cs @@ -70,7 +70,7 @@ public UserController(IUnitOfWorkManage unitOfWorkManage, ISysUserInfoServices s /// // GET: api/User [HttpGet] - public async Task>> Get(int page = 1, string key = "") + public async Task>> Get_V3(int page = 1, string key = "") { if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key)) { @@ -108,6 +108,53 @@ public async Task>> Get(int page = 1, string return Success(data.ConvertTo(_mapper)); } + /// + /// 获取全部用户 + /// + /// + /// + /// + // GET: api/User + [HttpGet] + public async Task>> Get(int page = 1, string key = "") + { + if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key)) + { + key = ""; + } + + int intPageSize = 50; + + + var data = await _sysUserInfoServices.QueryPage(a => a.IsDeleted != true && a.Status >= 0 && ((a.LoginName != null && a.LoginName.Contains(key)) || (a.RealName != null && a.RealName.Contains(key))), page, intPageSize, " Id desc "); + + + #region MyRegion + + // 这里可以封装到多表查询,此处简单处理 + var allUserRoles = await _userRoleServices.Query(d => d.IsDeleted == false); + var allRoles = await _roleServices.Query(d => d.IsDeleted == false); + var allDepartments = await _departmentServices.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.Name).ToList(); + var departmentNameAndIds = GetFullDepartmentName(allDepartments, item.DepartmentId); + item.DepartmentName = departmentNameAndIds.Item1; + item.Dids = departmentNameAndIds.Item2; + } + + data.data = sysUserInfos; + + #endregion + + + return Success(data.ConvertTo(_mapper)); + } + private (string, List) GetFullDepartmentName(List departments, long departmentId) { @@ -143,7 +190,7 @@ public string Get(string id) /// [HttpGet] [AllowAnonymous] - public async Task> GetInfoByToken(string token) + public async Task> GetInfoByToken_V3(string token) { var data = new MessageModel(); if (!string.IsNullOrEmpty(token)) @@ -163,6 +210,35 @@ public async Task> GetInfoByToken(string token) return data; } + // GET: api/User/5 + /// + /// 获取用户详情根据token + /// 【无权限】 + /// + /// 令牌 + /// + [HttpGet] + [AllowAnonymous] + public async Task> GetInfoByToken(string token) + { + var data = new MessageModel(); + if (!string.IsNullOrEmpty(token)) + { + var tokenModel = JwtHelper.SerializeJwt(token); + if (tokenModel != null && tokenModel.Uid > 0) + { + var userinfo = await _sysUserInfoServices.QueryById(tokenModel.Uid); + if (userinfo != null) + { + data.response = _mapper.Map(userinfo); + data.success = true; + data.msg = "获取成功"; + } + } + } + + return data; + } /// /// 添加一个用户 @@ -171,7 +247,7 @@ public async Task> GetInfoByToken(string token) /// // POST: api/User [HttpPost] - public async Task> Post([FromBody] SysUserInfo sysUserInfo) + public async Task> Post_V3([FromBody] SysUserInfo sysUserInfo) { var data = new MessageModel(); @@ -188,6 +264,30 @@ public async Task> Post([FromBody] SysUserInfo sysUserInfo) return data; } + /// + /// 添加一个用户 + /// + /// + /// + // POST: api/User + [HttpPost] + public async Task> Post([FromBody] SysUserInfoDto sysUserInfo) + { + var data = new MessageModel(); + + sysUserInfo.uLoginPWD = MD5Helper.MD5Encrypt32(sysUserInfo.uLoginPWD); + sysUserInfo.uRemark = _user.Name; + + var id = await _sysUserInfoServices.Add(_mapper.Map(sysUserInfo)); + data.success = id > 0; + if (data.success) + { + data.response = id.ObjToString(); + data.msg = "添加成功"; + } + + return data; + } /// /// 更新用户与角色 @@ -196,7 +296,7 @@ public async Task> Post([FromBody] SysUserInfo sysUserInfo) /// // PUT: api/User/5 [HttpPut] - public async Task> Put([FromBody] SysUserInfo sysUserInfo) + public async Task> Put_V3([FromBody] SysUserInfo sysUserInfo) { // 这里使用事务处理 var data = new MessageModel(); @@ -263,6 +363,80 @@ public async Task> Put([FromBody] SysUserInfo sysUserInfo) return data; } + /// + /// 更新用户与角色 + /// + /// + /// + // PUT: api/User/5 + [HttpPut] + public async Task> Put([FromBody] SysUserInfoDto sysUserInfo) + { + // 这里使用事务处理 + var data = new MessageModel(); + + var oldUser = await _sysUserInfoServices.QueryById(sysUserInfo.uID); + if (oldUser is not { Id: > 0 }) + { + return Failed("用户不存在或已被删除"); + } + + try + { + if (sysUserInfo.uLoginPWD != oldUser.LoginPWD) + { + oldUser.CriticalModifyTime = DateTime.Now; + } + + _mapper.Map(sysUserInfo, oldUser); + + _unitOfWorkManage.BeginTran(); + // 无论 Update Or Add , 先删除当前用户的全部 U_R 关系 + var usreroles = (await _userRoleServices.Query(d => d.UserId == oldUser.Id)); + if (usreroles.Any()) + { + var ids = usreroles.Select(d => d.Id.ToString()).ToArray(); + var isAllDeleted = await _userRoleServices.DeleteByIds(ids); + if (!isAllDeleted) + { + return Failed("服务器更新异常"); + } + } + + // 然后再执行添加操作 + if (sysUserInfo.RIDs.Count > 0) + { + var userRolsAdd = new List(); + sysUserInfo.RIDs.ForEach(rid => { userRolsAdd.Add(new UserRole(oldUser.Id, rid)); }); + + var oldRole = usreroles.Select(s => s.RoleId).OrderBy(i => i).ToArray(); + var newRole = userRolsAdd.Select(s => s.RoleId).OrderBy(i => i).ToArray(); + if (!oldRole.SequenceEqual(newRole)) + { + oldUser.CriticalModifyTime = DateTime.Now; + } + + await _userRoleServices.Add(userRolsAdd); + } + + data.success = await _sysUserInfoServices.Update(oldUser); + + _unitOfWorkManage.CommitTran(); + + if (data.success) + { + data.msg = "更新成功"; + data.response = oldUser.Id.ObjToString(); + } + } + catch (Exception e) + { + _unitOfWorkManage.RollbackTran(); + _logger.LogError(e, e.Message); + } + + return data; + } /// /// 删除用户