public async Task <Unit> Handle(SendRegEmailCommand command, CancellationToken cancellationToken) { var email = command.Email; var user = await _userDomainService.Get(x => x.Email == email && x.HasVerifiedEmail); if (user != null) { await _bus.RaiseEvent(new DomainNotification("该邮箱已注册,请使用其他邮箱注册")); return(Unit.Value); } int expiryMin = 30; string key = string.Format(RedisKey.RegEmail, email); //$"regemail_{email}"; if (await _redisDb.KeyTimeToLive(key) > 0) { await _bus.RaiseEvent(new DomainNotification($"同一邮箱每{expiryMin}分钟只能发送一次,请稍后")); return(Unit.Value); } string code = GenerateRandom(4); try { string url = $"{_appConfig.Site.Url}/user/reg?email={email}&code={code}"; await _mail.Send(new MailModel { Content = $"<p>您好,您正在使用该邮箱注册<a href='{_appConfig.Site.Url}'>{_appConfig.Site.Name}</a>账号,请在验证码输入框中输入此次验证码: {code},验证码有效期{expiryMin}分钟。</p><p>您也可以<a href='{url}'>点击这里</a>或复制以下链接到浏览器打开</p><p>{url}</p>", Address = email, Subject = $"【{_appConfig.Site.Name}】请继续完成您的注册" }); } catch (Exception ex) { _logger.LogError($"邮件发送失败 {ex}"); await _bus.RaiseEvent(new DomainNotification($"邮件发送失败,请稍后重试")); return(Unit.Value); } await _redisDb.StringSet(key, code, DateTime.Now.AddMinutes(expiryMin)); if (await Commit()) { //TODO } return(Unit.Value); }
public async Task <IActionResult> OnPostAsync([FromBody] SendRegEmailDto dto) { var userId = _accountContext.UserId; var command = new SendRegEmailCommand(dto.Email); await _bus.SendCommand(command); if (_notifications.HasNotifications()) { var errorMessage = string.Join(";", _notifications.GetNotifications().Select(x => x.Content)); return(await Task.FromResult(new JsonResult(new { status = false, errorMessage }))); } return(await Task.FromResult(new JsonResult(new { status = true }))); }