From 50ee6aa7a2011b9d3c73f600d04d57ebec110c6c Mon Sep 17 00:00:00 2001 From: youngq Date: Fri, 28 Jun 2024 17:56:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E7=94=A8=E6=88=B7=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Backend/RoleController.cs | 6 +- .../Controllers/Frontend/UserController.cs | 106 ++++++++++++++++++ WGShare.API/Controllers/PublicController.cs | 39 +++++++ WGShare.API/Program.cs | 35 +----- .../SwaggerServiceExtensions.cs | 67 +++++++++++ 5 files changed, 214 insertions(+), 39 deletions(-) create mode 100644 WGShare.API/Controllers/Frontend/UserController.cs create mode 100644 WGShare.API/Controllers/PublicController.cs create mode 100644 WGShare.API/ServiceConfigs/SwaggerServiceExtensions.cs diff --git a/WGShare.API/Controllers/Backend/RoleController.cs b/WGShare.API/Controllers/Backend/RoleController.cs index cdaf972..62f8ca8 100644 --- a/WGShare.API/Controllers/Backend/RoleController.cs +++ b/WGShare.API/Controllers/Backend/RoleController.cs @@ -58,11 +58,7 @@ namespace WGShare.API.Controllers.Backend .Where(x => ids.Contains(x.Id)).ExecuteCommandHasChangeAsync(); } - [HttpGet("dp-list")] - public async Task> GetDropDownList() - { - return await _sqlSugar.Queryable().Where(x => x.IsDelete == false).ToListAsync(); - } + [HttpPost("auth-perm")] public async Task PermAuth([FromQuery] string roleId, [FromBody] List inputDTO) diff --git a/WGShare.API/Controllers/Frontend/UserController.cs b/WGShare.API/Controllers/Frontend/UserController.cs new file mode 100644 index 0000000..95c2ad0 --- /dev/null +++ b/WGShare.API/Controllers/Frontend/UserController.cs @@ -0,0 +1,106 @@ +using Mapster; +using Microsoft.AspNetCore.Mvc; +using SqlSugar; +using WGShare.API.Controllers.Basic; +using WGShare.Domain.DTOs.User; +using WGShare.Domain.Entities; +using WGShare.Domain.FriendlyException; +using WGShare.Domain.GeneralModel; +using Yitter.IdGenerator; + +namespace WGShare.API.Controllers.Frontend +{ + [ApiExplorerSettings(GroupName = "frontend")] + [Route("user")] + public class UserController : BasicController + { + private readonly ISqlSugarClient _sqlSugar; + + public UserController(ISqlSugarClient sqlSugar) + { + this._sqlSugar = sqlSugar; + } + + + [HttpGet("list")] + public async Task> GetUserList([FromQuery] string? searchKeywod, [FromQuery] PagedBaseDto pagedBaseDto) + { + RefAsync total = 0; + + var list = await _sqlSugar.Queryable() + .InnerJoin((u, r) => u.RoleId == r.Id) + .WhereIF(!string.IsNullOrWhiteSpace(searchKeywod), u => u.UserName.Contains(searchKeywod) || u.Account.Contains(searchKeywod)) + .ToPageListAsync(pagedBaseDto.PageIndex, pagedBaseDto.PageSize, total); + + return list.Adapt>(); + } + + + /// + /// 新增用户 + /// + /// + /// + [HttpPost] + public async Task AddUser([FromQuery] UserInputDTO inputDTO) + { + var user = inputDTO.Adapt(); + user.Id = YitIdHelper.NextId().ToString(); + user.TenantId = TenantId; + + if (await _sqlSugar.Queryable().AnyAsync(x => x.Account == user.Account)) + { + throw Oops.Oh("账号已存在!"); + } + + + return await _sqlSugar.Insertable(user).ExecuteCommandAsync() > 0; + } + + /// + /// 修改用户信息 + /// + /// + /// + [HttpPut] + public async Task Modify([FromBody] UserInputDTO inputDTO) + { + var entity = inputDTO.Adapt(); + + if (await _sqlSugar.Queryable().AnyAsync(x => x.Account == entity.Account && x.Id != inputDTO.Id)) + { + throw Oops.Oh("账号已存在!"); + } + + return await _sqlSugar.Updateable(entity) + .IgnoreColumns(x => new { x.Pwd, x.TenantId }).ExecuteCommandAsync() > 0; + } + + /// + /// 更改密码 + /// + /// + /// + [HttpPut("pwd")] + public async Task ModifyPassword([FromBody] UserChangePwdDTO inputDTO) + { + var entity = inputDTO.Adapt(); + + return await _sqlSugar.Updateable(entity) + .UpdateColumns(x => new { x.Pwd }).ExecuteCommandAsync() > 0; + } + + /// + /// 删除用户 + /// + /// + /// + [HttpDelete] + public async Task Delete([FromBody] params string[] ids) + { + return await _sqlSugar.Updateable() + .SetColumns(x => x.IsDelete == true) + .Where(x => ids.Contains(x.Id)).ExecuteCommandHasChangeAsync(); + } + } +} diff --git a/WGShare.API/Controllers/PublicController.cs b/WGShare.API/Controllers/PublicController.cs new file mode 100644 index 0000000..2822b89 --- /dev/null +++ b/WGShare.API/Controllers/PublicController.cs @@ -0,0 +1,39 @@ +using Masuit.Tools; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using SqlSugar; +using System.Configuration; +using System.Security.Claims; +using WGShare.API.Controllers.Basic; +using WGShare.API.Helpers; +using WGShare.Domain.DTOs.Login; +using WGShare.Domain.Entities; +using WGShare.Domain.FriendlyException; + +namespace WGShare.API.Controllers +{ + /// + /// 前后端共用接口 + /// + [ApiExplorerSettings(GroupName = "public")] + [Route("pub")] + public class PublicController : BasicController + { + private readonly ISqlSugarClient _sqlSugar; + + public PublicController(ISqlSugarClient sqlSugar) + { + _sqlSugar = sqlSugar; + } + + /// + /// 角色列表下拉框 + /// + /// + [HttpGet("role-dp-list")] + public async Task> GetDropDownList() + { + return await _sqlSugar.Queryable().Where(x => x.IsDelete == false).ToListAsync(); + } + } +} diff --git a/WGShare.API/Program.cs b/WGShare.API/Program.cs index 39f714f..2df77da 100644 --- a/WGShare.API/Program.cs +++ b/WGShare.API/Program.cs @@ -37,40 +37,7 @@ namespace WGShare.API }); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); - builder.Services.AddSwaggerGen(w => - { - w.SwaggerDoc("frontend", new OpenApiInfo { Title = "ǰ", Version = "frontend" }); - w.SwaggerDoc("backend", new OpenApiInfo { Title = "", Version = "backend" }); - w.SwaggerDoc("public", new OpenApiInfo { Title = "ӿ", Version = "public" }); - w.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme - { - Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"", - Name = "Authorization", - In = ParameterLocation.Header, - Type = SecuritySchemeType.ApiKey, - Scheme = "Bearer" - }); - - w.AddSecurityRequirement(new OpenApiSecurityRequirement - { - { - new OpenApiSecurityScheme - { - Reference = new OpenApiReference - { - Type = ReferenceType.SecurityScheme, - Id = "Bearer" - } - }, - new string[] {} - } - }); - - //string xmlPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SwaggerGroup.xml"); - //w.IncludeXmlComments(xmlPath, true); - } - ); - + builder.Services.AddSwagger(); builder.Services.AddSingleton(new JwtHelper(configuration)); builder.Services.AddAuth(configuration["Jwt:Issuer"], configuration["Jwt:Audience"], diff --git a/WGShare.API/ServiceConfigs/SwaggerServiceExtensions.cs b/WGShare.API/ServiceConfigs/SwaggerServiceExtensions.cs new file mode 100644 index 0000000..caf7cb8 --- /dev/null +++ b/WGShare.API/ServiceConfigs/SwaggerServiceExtensions.cs @@ -0,0 +1,67 @@ +using Mapster; +using MapsterMapper; +using Newtonsoft.Json.Converters; +using Newtonsoft.Json.Serialization; +using Newtonsoft.Json; +using Microsoft.AspNetCore.Authentication.JwtBearer; +using Microsoft.IdentityModel.Tokens; +using System.Text; +using System.Reflection.Metadata; +using Microsoft.OpenApi.Models; + +namespace WGShare.API.ServiceConfigs +{ + public static class SwaggerServiceExtensions + { + /// + /// 添加认证和授权 + /// + /// 服务集合 + /// + public static IServiceCollection AddSwagger(this IServiceCollection services) + { + services.AddSwaggerGen(w => + { + w.SwaggerDoc("frontend", new OpenApiInfo { Title = "前端", Version = "frontend" }); + w.SwaggerDoc("backend", new OpenApiInfo { Title = "后端", Version = "backend" }); + w.SwaggerDoc("public", new OpenApiInfo { Title = "公共接口", Version = "public" }); + w.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme + { + Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"", + Name = "Authorization", + In = ParameterLocation.Header, + Type = SecuritySchemeType.ApiKey, + Scheme = "Bearer" + }); + + w.AddSecurityRequirement(new OpenApiSecurityRequirement + { + { + new OpenApiSecurityScheme + { + Reference = new OpenApiReference + { + Type = ReferenceType.SecurityScheme, + Id = "Bearer" + } + }, + new string[] {} + } + }); + + //string xmlPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SwaggerGroup.xml"); + //w.IncludeXmlComments(xmlPath, true); + }); + + + return services; + //services.AddAuthorization(options => + //{ + // options.AddPolicy(Constant.Policy.FreePolicyName, + // policy => policy.RequireClaim(Constant.Auth.PermissionsKey, Constant.Auth.FreeClaimValue, Constant.Auth.VipClaimValue)); + // options.AddPolicy(Constant.Policy.VipPolicyName, + // policy => policy.RequireClaim(Constant.Auth.PermissionsKey, Constant.Auth.VipClaimValue)); + //}); + } + } +}