From 713e57c4244c1b1ccfbafad01b16812653afbe79 Mon Sep 17 00:00:00 2001 From: lihanbo Date: Tue, 29 Oct 2024 15:46:44 +0800 Subject: [PATCH] =?UTF-8?q?105040=20Add=20=E7=94=A8=E6=88=B7=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controller/UserManagementController.cs | 30 ++++++++ .../LFlow.UserManagement.csproj | 17 +++++ LFlow.UserManagement/Model/UserDto.cs | 27 ++++++++ LFlow.UserManagement/Model/UserModel.cs | 31 +++++++++ .../Repository/UserManagementRepo.cs | 10 +++ .../Service/IUserManagementService.cs | 14 ++++ .../Service/UserManagementService.cs | 69 +++++++++++++++++++ LFlow.UserManagement/UserManegementModule.cs | 24 +++++++ LFlow.UserManagement/Util/PasswordHelper.cs | 20 ++++++ .../VersionManagementModule.cs | 6 +- LFlow.sln | 8 ++- 11 files changed, 252 insertions(+), 4 deletions(-) create mode 100644 LFlow.UserManagement/Controller/UserManagementController.cs create mode 100644 LFlow.UserManagement/LFlow.UserManagement.csproj create mode 100644 LFlow.UserManagement/Model/UserDto.cs create mode 100644 LFlow.UserManagement/Model/UserModel.cs create mode 100644 LFlow.UserManagement/Repository/UserManagementRepo.cs create mode 100644 LFlow.UserManagement/Service/IUserManagementService.cs create mode 100644 LFlow.UserManagement/Service/UserManagementService.cs create mode 100644 LFlow.UserManagement/UserManegementModule.cs create mode 100644 LFlow.UserManagement/Util/PasswordHelper.cs diff --git a/LFlow.UserManagement/Controller/UserManagementController.cs b/LFlow.UserManagement/Controller/UserManagementController.cs new file mode 100644 index 0000000..2954b24 --- /dev/null +++ b/LFlow.UserManagement/Controller/UserManagementController.cs @@ -0,0 +1,30 @@ +using LFlow.Base.Interfaces; +using LFlow.Base.Utils; +using LFlow.UserManagement.Model; +using LFlow.UserManagement.Service; +using Microsoft.AspNetCore.Mvc; + +namespace LFlow.UserManagement.Controller +{ + public class UserManagementController(IUserManagementService service) : BaseController + { + [HttpPost] + public ApiResult? Login(UserDto user) + { + return Success(service.Login(user)); + } + [HttpPost] + public ApiResult Register(UserDto user) + { + try + { + return Success(service.Register(user)); + } + catch (Exception ex) + { + return Fail(null, ex.Message, 500); + } + + } + } +} diff --git a/LFlow.UserManagement/LFlow.UserManagement.csproj b/LFlow.UserManagement/LFlow.UserManagement.csproj new file mode 100644 index 0000000..fe015fa --- /dev/null +++ b/LFlow.UserManagement/LFlow.UserManagement.csproj @@ -0,0 +1,17 @@ + + + + net8.0 + enable + enable + ../LFlow_Bin/Services/ + false + false + true + + + + + + + diff --git a/LFlow.UserManagement/Model/UserDto.cs b/LFlow.UserManagement/Model/UserDto.cs new file mode 100644 index 0000000..e58e385 --- /dev/null +++ b/LFlow.UserManagement/Model/UserDto.cs @@ -0,0 +1,27 @@ +using LFlow.Base.Interfaces; + +namespace LFlow.UserManagement.Model +{ + public class UserDto : IModel + { + public string? ID + { + get; + set; + } + + public string? UserName + { + get; + set; + } + public string? UserEmail + { + get; set; + } + public string? UserPassword + { + get; set; + } + } +} diff --git a/LFlow.UserManagement/Model/UserModel.cs b/LFlow.UserManagement/Model/UserModel.cs new file mode 100644 index 0000000..ef10388 --- /dev/null +++ b/LFlow.UserManagement/Model/UserModel.cs @@ -0,0 +1,31 @@ +using LFlow.Base.Interfaces; +using SqlSugar; + +namespace LFlow.UserManagement.Model +{ + [SugarTable("T_U_USERINFO")] + public class UserModel : IDataModel + { + [SugarColumn(IsPrimaryKey = true)] + public string ID + { + get; + set; + } + + public string UserName + { + get; + set; + } + public string UserEmail + { + get; set; + } + public string UserPassword + { + get; set; + } + } + +} diff --git a/LFlow.UserManagement/Repository/UserManagementRepo.cs b/LFlow.UserManagement/Repository/UserManagementRepo.cs new file mode 100644 index 0000000..75629e8 --- /dev/null +++ b/LFlow.UserManagement/Repository/UserManagementRepo.cs @@ -0,0 +1,10 @@ +using LFlow.Base.Default; +using LFlow.UserManagement.Model; +using SqlSugar; + +namespace LFlow.UserManagement.Repository +{ + internal class UserManagementRepo(ISqlSugarClient client) : DefaultCurdRepo(client) + { + } +} diff --git a/LFlow.UserManagement/Service/IUserManagementService.cs b/LFlow.UserManagement/Service/IUserManagementService.cs new file mode 100644 index 0000000..1544e36 --- /dev/null +++ b/LFlow.UserManagement/Service/IUserManagementService.cs @@ -0,0 +1,14 @@ +using LFlow.Base.Interfaces; +using LFlow.UserManagement.Model; + +namespace LFlow.UserManagement.Service +{ + public interface IUserManagementService : IService// + { + UserDto? Login(UserDto user); + Task? LoginAsync(UserDto user); + + UserDto? Register(UserDto user); + Task? RegisterAsync(UserDto user); + } +} diff --git a/LFlow.UserManagement/Service/UserManagementService.cs b/LFlow.UserManagement/Service/UserManagementService.cs new file mode 100644 index 0000000..1f5c364 --- /dev/null +++ b/LFlow.UserManagement/Service/UserManagementService.cs @@ -0,0 +1,69 @@ +using LFlow.Base.Interfaces; +using LFlow.Base.Utils; +using LFlow.UserManagement.Model; +using LFlow.UserManagement.Util; + +namespace LFlow.UserManagement.Service +{ + /// + /// 在线管理服务 + /// + public class UserManagementService(IRepo repo) : IUserManagementService + { + public UserDto? Login(UserDto user) + { + var userPwd = PasswordHelper.HashPassword(user.UserPassword); + + var userModel = repo.WhereSearchId(new UserModel + { + UserName = user.UserName, + UserPassword = userPwd + }); + + if (userModel == null) + { + return null; + } + var loginedUser = repo.Get(userModel.FirstOrDefault()); + loginedUser.UserPassword = ""; + return loginedUser.MapTo(); + } + + public Task? LoginAsync(UserDto user) + { + return Task.FromResult(this.Login(user)); + } + + public UserDto? Register(UserDto user) + { + if (user.UserEmail == null || user.UserName == null || user.UserPassword == null) + { + throw new Exception("用户名、密码、邮箱不能为空"); + } + var userPwd = PasswordHelper.HashPassword(user.UserPassword); + var ids = repo.WhereSearchId(new UserModel + { + UserName = user.UserName + }); + if (ids != null && ids.Count > 0) + { + throw new Exception("用户名已存在"); + } + var userModel = new UserModel + { + ID = Guid.NewGuid().ToString(), + UserName = user.UserName, + UserEmail = user.UserEmail, + UserPassword = userPwd + }; + var savedUser = repo.SaveOrUpdate(userModel, false).MapTo(); + savedUser.UserPassword = ""; + return savedUser; + } + + public Task RegisterAsync(UserDto user) + { + return Task.FromResult(this.Register(user)); + } + } +} diff --git a/LFlow.UserManagement/UserManegementModule.cs b/LFlow.UserManagement/UserManegementModule.cs new file mode 100644 index 0000000..d342dbd --- /dev/null +++ b/LFlow.UserManagement/UserManegementModule.cs @@ -0,0 +1,24 @@ +using LFlow.Base.Interfaces; +using LFlow.Base.Utils; +using LFlow.UserManagement.Model; +using Microsoft.Extensions.DependencyInjection; +using Serilog; +using System.Reflection; + +namespace LFlow.UserManagement +{ + public class UserManegementModule : IModule + { + public void ConfigureModule(IServiceCollection services) + { + CodeFirst.AddType(typeof(UserModel)); + var assembly = Assembly.GetAssembly(typeof(UserManegementModule))!; + var types = assembly.GetTypes().ToList(); + RegisterModule.RegisterAllService(types, services); + RegisterModule.RegisterAllRepo(types, services); + RegisterModule.RegisterAllModel(types, services); + services.AddControllers().AddApplicationPart(assembly); + Log.Logger?.Information("UserManegementModule ConfigureModule done"); + } + } +} diff --git a/LFlow.UserManagement/Util/PasswordHelper.cs b/LFlow.UserManagement/Util/PasswordHelper.cs new file mode 100644 index 0000000..11dcfdd --- /dev/null +++ b/LFlow.UserManagement/Util/PasswordHelper.cs @@ -0,0 +1,20 @@ +using System.Security.Cryptography; +using System.Text; + +namespace LFlow.UserManagement.Util +{ + public class PasswordHelper + { + public static string HashPassword(string password) + { + byte[] data = Encoding.Default.GetBytes(password); + byte[] hashData = MD5.HashData(data); + StringBuilder sb = new(); + foreach (byte b in hashData) + { + sb.Append(b.ToString("x2")); + } + return sb.ToString(); + } + } +} diff --git a/LFlow.VersionManagement/VersionManagementModule.cs b/LFlow.VersionManagement/VersionManagementModule.cs index d590d47..45a3bb7 100644 --- a/LFlow.VersionManagement/VersionManagementModule.cs +++ b/LFlow.VersionManagement/VersionManagementModule.cs @@ -5,7 +5,7 @@ using Microsoft.Extensions.DependencyInjection; using Serilog; using System.Reflection; -namespace LFlow.User; +namespace LFlow.VersionManagement; public class VersionManagementModule : IModule { @@ -14,8 +14,8 @@ public class VersionManagementModule : IModule CodeFirst.AddType(typeof(VersionModel)); var assembly = Assembly.GetAssembly(typeof(VersionManagementModule))!; var types = assembly.GetTypes().ToList(); - RegisterModule.RegisterAllService(types, services); - RegisterModule.RegisterAllRepo(types, services); + types.RegisterAllService(services); + types.RegisterAllRepo(services); //RegisterModule.RegisterAllModel(types, services); services.AddControllers().AddApplicationPart(assembly); Log.Logger?.Information("VersionManagementModule ConfigureModule done"); diff --git a/LFlow.sln b/LFlow.sln index e66d28b..0fdf5f0 100644 --- a/LFlow.sln +++ b/LFlow.sln @@ -7,7 +7,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LFlow.Base", "LFlow.Base\LF EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LFlow.VersionManagement", "LFlow.VersionManagement\LFlow.VersionManagement.csproj", "{D52CC594-B10C-4FC0-8B7A-68CE0645A95D}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LFlow.OnlineManegement", "LFlow.OnlineManegement\LFlow.OnlineManegement.csproj", "{E849310F-64AD-453A-A2AA-557731508BF1}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LFlow.OnlineManegement", "LFlow.OnlineManegement\LFlow.OnlineManegement.csproj", "{E849310F-64AD-453A-A2AA-557731508BF1}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LFlow.UserManagement", "LFlow.UserManagement\LFlow.UserManagement.csproj", "{DA68CE22-AC53-40BD-AB2A-5C52DFDDD548}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -27,6 +29,10 @@ Global {E849310F-64AD-453A-A2AA-557731508BF1}.Debug|Any CPU.Build.0 = Debug|Any CPU {E849310F-64AD-453A-A2AA-557731508BF1}.Release|Any CPU.ActiveCfg = Release|Any CPU {E849310F-64AD-453A-A2AA-557731508BF1}.Release|Any CPU.Build.0 = Release|Any CPU + {DA68CE22-AC53-40BD-AB2A-5C52DFDDD548}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DA68CE22-AC53-40BD-AB2A-5C52DFDDD548}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DA68CE22-AC53-40BD-AB2A-5C52DFDDD548}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DA68CE22-AC53-40BD-AB2A-5C52DFDDD548}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE