From 769e2cb897437e139425b142af6769776e51730b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A9=99=E5=AD=90?= <454313500@qq.com> Date: Wed, 29 Mar 2023 22:39:20 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=8C=BA=E5=88=86=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SmsAliyunManager.cs | 35 +++++++----- .../SmsAliyunOptions.cs | 4 ++ .../YiFrameworkSmsAliyunModule.cs | 7 ++- .../project/BBS/Yi.BBS.Web/Yi.BBS.Web.csproj | 6 ++ .../project/BBS/Yi.BBS.Web/appsettings.json | 9 ++- .../project/bbs/Yi.BBS.Web/Yi.BBS.Web.csproj | 6 ++ .../project/bbs/Yi.BBS.Web/appsettings.json | 9 ++- .../Identity/Dtos/Account/CaptchaImageDto.cs | 3 +- .../Identity/AccountService.cs | 56 +++++++++++++++---- .../Yi.RBAC.ApplicationSwaggerDoc.xml | 14 ++++- .../rbac/Yi.RBAC.Domain/Yi.RBAC.Domain.csproj | 2 + .../rbac/Yi.RBAC.Domain/YiRBACDomainModule.cs | 6 +- 12 files changed, 125 insertions(+), 32 deletions(-) diff --git a/Yi.Framework.Net6/src/module/Yi.Framework.Sms.Aliyun/SmsAliyunManager.cs b/Yi.Framework.Net6/src/module/Yi.Framework.Sms.Aliyun/SmsAliyunManager.cs index 9af7d3d9..1de33228 100644 --- a/Yi.Framework.Net6/src/module/Yi.Framework.Sms.Aliyun/SmsAliyunManager.cs +++ b/Yi.Framework.Net6/src/module/Yi.Framework.Sms.Aliyun/SmsAliyunManager.cs @@ -4,12 +4,24 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using AlibabaCloud.SDK.Dysmsapi20170525; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; using Tea; namespace Yi.Framework.Sms.Aliyun { public class SmsAliyunManager { + public Client AliyunClient { get; set; } + private ILogger _logger; + private SmsAliyunOptions Options { get; set; } + public SmsAliyunManager(ILogger logger,IOptions options) + { + _logger = logger; + AliyunClient = CreateClient(Options.AccessKeyId, Options.AccessKeySecret); + Options = options.Value; + } + private static Client CreateClient(string accessKeyId, string accessKeySecret) { AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config @@ -24,12 +36,13 @@ namespace Yi.Framework.Sms.Aliyun return new Client(config); } - public Client AliyunClient { get; set; } - public SmsAliyunManager() { - - AliyunClient = CreateClient("accessKeyId", "accessKeySecret"); - } + /// + /// 发送短信 + /// + /// + /// + /// public async Task Send(string phoneNumbers, string code) { try @@ -37,21 +50,17 @@ namespace Yi.Framework.Sms.Aliyun AlibabaCloud.SDK.Dysmsapi20170525.Models.SendSmsRequest sendSmsRequest = new AlibabaCloud.SDK.Dysmsapi20170525.Models.SendSmsRequest { PhoneNumbers = phoneNumbers, - SignName = "", + SignName = Options.SignName, TemplateCode = code, }; - var response= await AliyunClient.SendSmsAsync(sendSmsRequest); + var response = await AliyunClient.SendSmsAsync(sendSmsRequest); } - catch (TeaException error) - { - Console.WriteLine(error.Message); - } catch (Exception _error) { - Console.WriteLine(_error.Message); + _logger.LogError(_error, _error.Message); } } - } + } } \ No newline at end of file diff --git a/Yi.Framework.Net6/src/module/Yi.Framework.Sms.Aliyun/SmsAliyunOptions.cs b/Yi.Framework.Net6/src/module/Yi.Framework.Sms.Aliyun/SmsAliyunOptions.cs index 1bc41dc1..afe9ca7f 100644 --- a/Yi.Framework.Net6/src/module/Yi.Framework.Sms.Aliyun/SmsAliyunOptions.cs +++ b/Yi.Framework.Net6/src/module/Yi.Framework.Sms.Aliyun/SmsAliyunOptions.cs @@ -8,5 +8,9 @@ namespace Yi.Framework.Sms.Aliyun { public class SmsAliyunOptions { + public string AccessKeyId { get; set; } + public string AccessKeySecret { get; set; } + + public string SignName { get; set; } } } diff --git a/Yi.Framework.Net6/src/module/Yi.Framework.Sms.Aliyun/YiFrameworkSmsAliyunModule.cs b/Yi.Framework.Net6/src/module/Yi.Framework.Sms.Aliyun/YiFrameworkSmsAliyunModule.cs index 9493e7f6..049a5d33 100644 --- a/Yi.Framework.Net6/src/module/Yi.Framework.Sms.Aliyun/YiFrameworkSmsAliyunModule.cs +++ b/Yi.Framework.Net6/src/module/Yi.Framework.Sms.Aliyun/YiFrameworkSmsAliyunModule.cs @@ -1,9 +1,13 @@ using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; using StartupModules; +using Yi.Framework.Core; +using Yi.Framework.Core.Attributes; +using Yi.Framework.Core.Configuration; namespace Yi.Framework.Sms.Aliyun { + [DependsOn(typeof(YiFrameworkCoreModule))] public class YiFrameworkSmsAliyunModule : IStartupModule { public void Configure(IApplicationBuilder app, ConfigureMiddlewareContext context) @@ -13,7 +17,8 @@ namespace Yi.Framework.Sms.Aliyun public void ConfigureServices(IServiceCollection services, ConfigureServicesContext context) { - + services.Configure(Appsettings.appConfiguration("SmsAliyunOptions")); + services.AddSingleton(); } } } \ No newline at end of file diff --git a/Yi.Framework.Net6/src/project/BBS/Yi.BBS.Web/Yi.BBS.Web.csproj b/Yi.Framework.Net6/src/project/BBS/Yi.BBS.Web/Yi.BBS.Web.csproj index 9332c133..f25f49fd 100644 --- a/Yi.Framework.Net6/src/project/BBS/Yi.BBS.Web/Yi.BBS.Web.csproj +++ b/Yi.Framework.Net6/src/project/BBS/Yi.BBS.Web/Yi.BBS.Web.csproj @@ -16,6 +16,12 @@ + + Always + + + Always + Always diff --git a/Yi.Framework.Net6/src/project/BBS/Yi.BBS.Web/appsettings.json b/Yi.Framework.Net6/src/project/BBS/Yi.BBS.Web/appsettings.json index 2788576d..dfccfb75 100644 --- a/Yi.Framework.Net6/src/project/BBS/Yi.BBS.Web/appsettings.json +++ b/Yi.Framework.Net6/src/project/BBS/Yi.BBS.Web/appsettings.json @@ -36,5 +36,12 @@ }, //开启种子数据 - "EnabledDataSeed": true + "EnabledDataSeed": false, + + //阿里云短信 + "SmsAliyunOptions": { + "AccessKeyId": "", + "AccessKeySecret": "", + "SignName": "" + } } diff --git a/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Web/Yi.BBS.Web.csproj b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Web/Yi.BBS.Web.csproj index 9332c133..f25f49fd 100644 --- a/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Web/Yi.BBS.Web.csproj +++ b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Web/Yi.BBS.Web.csproj @@ -16,6 +16,12 @@ + + Always + + + Always + Always diff --git a/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Web/appsettings.json b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Web/appsettings.json index 2788576d..dfccfb75 100644 --- a/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Web/appsettings.json +++ b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Web/appsettings.json @@ -36,5 +36,12 @@ }, //开启种子数据 - "EnabledDataSeed": true + "EnabledDataSeed": false, + + //阿里云短信 + "SmsAliyunOptions": { + "AccessKeyId": "", + "AccessKeySecret": "", + "SignName": "" + } } diff --git a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application.Contracts/Identity/Dtos/Account/CaptchaImageDto.cs b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application.Contracts/Identity/Dtos/Account/CaptchaImageDto.cs index 362ac0e3..9e71ced7 100644 --- a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application.Contracts/Identity/Dtos/Account/CaptchaImageDto.cs +++ b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application.Contracts/Identity/Dtos/Account/CaptchaImageDto.cs @@ -8,7 +8,8 @@ namespace Yi.RBAC.Application.Contracts.Identity.Dtos.Account { public class CaptchaImageDto { - public string Uuid { get; set; }=string.Empty; + public string Code { get; set; } = string.Empty; + public Guid Uuid { get; set; } = Guid.Empty; public byte[] Img { get; set; } } } diff --git a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application/Identity/AccountService.cs b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application/Identity/AccountService.cs index 2c5f056d..9814f2d8 100644 --- a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application/Identity/AccountService.cs +++ b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application/Identity/AccountService.cs @@ -30,6 +30,7 @@ using Yi.RBAC.Domain.Shared.Identity.Dtos; using Yi.RBAC.Domain.Shared.Identity.Etos; using System.Net.WebSockets; using Yi.Framework.Uow; +using Yi.Framework.Caching; namespace Yi.RBAC.Application.Identity { @@ -67,6 +68,26 @@ namespace Yi.RBAC.Application.Identity [Autowired] private IRepository _roleRepository { get; set; } + + [Autowired] + private CacheManager _cacheManager { get; set; } + + /// + /// 效验图片登录验证码 + /// + private void ValidationCaptcha() + { + + } + + /// + /// 效验电话验证码 + /// + private void ValidationPhone() + { + + } + /// /// 登录 /// @@ -74,6 +95,14 @@ namespace Yi.RBAC.Application.Identity /// public async Task PostLoginAsync(LoginInputVo input) { + if (string.IsNullOrEmpty(input.Password) || string.IsNullOrEmpty(input.UserName)) + { + throw new UserFriendlyException("请输入合理数据!"); + } + + //效验验证码 + ValidationCaptcha(); + UserEntity user = new(); //登录成功 await _accountManager.LoginValidationAsync(input.UserName, input.Password, x => user = x); @@ -105,13 +134,15 @@ namespace Yi.RBAC.Application.Identity /// 注册 手机验证码 /// /// - public async Task PostPhoneCaptchaImage(PhoneCaptchaImageDto input) + public object PostPhoneCaptchaImage(PhoneCaptchaImageDto input) { - + var code = _securityCode.GetRandomEnDigitalText(4); + var uuid = Guid.NewGuid(); + _cacheManager.Set($"Yi:Phone:{input.Phone}", $"{code}:{uuid}", new TimeSpan(0, 10, 0)); //生成一个4位数的验证码 //发送短信,同时生成uuid //key: 电话号码 value:验证码+uuid - return new { uuid = Guid.NewGuid() }; + return new { Uuid = uuid }; } /// @@ -130,19 +161,20 @@ namespace Yi.RBAC.Application.Identity { throw new UserFriendlyException("账号名需大于等于2位!"); } - if (input.Password.Length<6) + if (input.Password.Length < 6) { throw new UserFriendlyException("密码需大于等于6位!"); } //效验验证码,根据电话号码获取 value,比对验证码已经uuid + ValidationPhone(); + + //输入的用户名与电话号码都不能在数据库中存在 UserEntity user = new(); var isExist = await _userRepository.IsAnyAsync(x => x.UserName == input.UserName - || x.Phone == input.Phone - || x.UserName == input.Phone.ToString() - || x.Phone.ToString() == input.UserName); + || x.Phone == input.Phone); if (isExist) { throw new UserFriendlyException("用户已存在,注册失败"); @@ -178,7 +210,7 @@ namespace Yi.RBAC.Application.Identity //通过鉴权jwt获取到用户的id var userId = _currentUser.Id; //此处从缓存中获取即可 - //var data = _cacheDb.Get($"Yi:UserInfo:{userId}"); + //var data = _cacheManager.Get($"Yi:UserInfo:{userId}"); var data = await _userRepository.GetUserAllInfoAsync(userId); //系统用户数据被重置,老前端访问重新授权 if (data is null) @@ -233,9 +265,9 @@ namespace Yi.RBAC.Application.Identity var code = _securityCode.GetRandomEnDigitalText(4); //将uuid与code,Redis缓存中心化保存起来,登录根据uuid比对即可 //10分钟过期 - //_cacheDb.Set($"Yi:Captcha:{uuid}", code, new TimeSpan(0, 10, 0)); + _cacheManager.Set($"Yi:Captcha:{code}", uuid, new TimeSpan(0, 10, 0)); var imgbyte = _securityCode.GetEnDigitalCodeByte(code); - return new CaptchaImageDto { Img = imgbyte, Uuid = code }; + return new CaptchaImageDto { Img = imgbyte, Code = code, Uuid = uuid }; } /// @@ -271,9 +303,9 @@ namespace Yi.RBAC.Application.Identity } /// - /// 更新头像 + /// 更新头像 /// - /// + /// /// public async Task UpdateIconAsync(UpdateIconDto input) { diff --git a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application/Yi.RBAC.ApplicationSwaggerDoc.xml b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application/Yi.RBAC.ApplicationSwaggerDoc.xml index d0a445c9..f701746a 100644 --- a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application/Yi.RBAC.ApplicationSwaggerDoc.xml +++ b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application/Yi.RBAC.ApplicationSwaggerDoc.xml @@ -4,6 +4,16 @@ Yi.RBAC.Application + + + 效验图片登录验证码 + + + + + 效验电话验证码 + + 登录 @@ -66,9 +76,9 @@ - 更新头像 + 更新头像 - + diff --git a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/Yi.RBAC.Domain.csproj b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/Yi.RBAC.Domain.csproj index c71fbc8a..a1010035 100644 --- a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/Yi.RBAC.Domain.csproj +++ b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/Yi.RBAC.Domain.csproj @@ -16,10 +16,12 @@ + + diff --git a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/YiRBACDomainModule.cs b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/YiRBACDomainModule.cs index 85d0f744..20865540 100644 --- a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/YiRBACDomainModule.cs +++ b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/YiRBACDomainModule.cs @@ -9,10 +9,12 @@ using System.Text; using System.Threading.Tasks; using Yi.Framework.Core.Attributes; using Yi.Framework.Data; +using Yi.Framework.Ddd; using Yi.Framework.DictionaryManager; using Yi.Framework.EventBus; using Yi.Framework.FileManager; using Yi.Framework.OperLogManager; +using Yi.Framework.Sms.Aliyun; using Yi.Framework.ThumbnailSharp; using Yi.RBAC.Domain.Logs; using Yi.RBAC.Domain.Shared; @@ -26,7 +28,9 @@ namespace Yi.RBAC.Domain typeof(YiFrameworkEventBusModule), typeof(YiFrameworkOperLogManagerModule), typeof(YiFrameworkFileManagerModule), - typeof(YiFrameworkDictionaryManagerModule) + typeof(YiFrameworkDictionaryManagerModule), + typeof(YiFrameworkCachingMemoryCacheModule), + typeof(YiFrameworkSmsAliyunModule) )] public class YiRBACDomainModule : IStartupModule {