feat 用户创建

This commit is contained in:
2024-03-21 21:02:52 +08:00
parent d25909a235
commit ed5196d1a1
4 changed files with 62 additions and 1 deletions

View File

@@ -63,6 +63,18 @@ public class UsersController : ControllerBase
return await _userService.GetUserByIdsAsync(ids); return await _userService.GetUserByIdsAsync(ids);
} }
/// <summary>
///
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
[Authorize(Roles = "admin")]
[HttpPost("create")]
public async Task CreateUserAsync(UserCreateRequest request)
{
await _userService.CreateUserAsync(request);
}
/// <summary> /// <summary>
/// 上传头像 /// 上传头像
/// </summary> /// </summary>

View File

@@ -0,0 +1,25 @@
using System.ComponentModel.DataAnnotations;
namespace Dlhis.Sys.Identity.Request;
public class UserCreateRequest
{
/// <summary>
/// 用户名
/// </summary>
[Required]
public string Username { get; set; }
/// <summary>
/// 密码
/// </summary>
[MinLength(6)]
[Required]
public string Password { get; set; }
/// <summary>
/// 厂区
/// </summary>
[Required]
public string Tenant { get; set; }
}

View File

@@ -18,4 +18,6 @@ public interface IUserService : IScopeDependency
Task<List<UserResponse>> GetUserByIdsAsync(List<int> ids); Task<List<UserResponse>> GetUserByIdsAsync(List<int> ids);
Task CreateUserAsync(UserCreateRequest request);
} }

View File

@@ -5,6 +5,7 @@ using Kurisu.AspNetCore.Utils.Extensions;
using Kurisu.Core.DataAccess.Dto; using Kurisu.Core.DataAccess.Dto;
using Kurisu.SqlSugar.Extensions; using Kurisu.SqlSugar.Extensions;
using Kurisu.SqlSugar.Services; using Kurisu.SqlSugar.Services;
using Microsoft.AspNetCore.Identity;
namespace Dlhis.Sys.Identity.Services.Implements; namespace Dlhis.Sys.Identity.Services.Implements;
@@ -67,4 +68,25 @@ public class UserService : IUserService
var users = await _dbContext.Queryable<UserEntity>().Where(x => ids.Contains(x.Id)).Select<UserResponse>().ToListAsync(); var users = await _dbContext.Queryable<UserEntity>().Where(x => ids.Contains(x.Id)).Select<UserResponse>().ToListAsync();
return users; return users;
} }
public async Task CreateUserAsync(UserCreateRequest request)
{
var hasher = new PasswordHasher<UserEntity>();
var user = new UserEntity
{
UserName = request.Username,
NormalizedUserName = request.Username.ToUpper(),
};
request.Password = hasher.HashPassword(user, request.Password);
user.PasswordHash = request.Password;
var id = await _dbContext.InsertReturnIdentityAsync(user);
await _dbContext.InsertAsync(new UserTenantEntity
{
TenantCode = request.Tenant,
UserId = (int)id
});
}
} }