外部用户 发送邮件更改

This commit is contained in:
2022-03-29 14:48:34 +08:00
parent c25783a63a
commit c46ba88d30
7 changed files with 309 additions and 153 deletions
@@ -8,13 +8,14 @@ using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using IRaCIS.Core.Application.Contracts.DTO;
using Newtonsoft.Json;
namespace IRaCIS.Core.Application.ViewModel
{
/// <summary> TrialExternalUserView 列表视图模型 </summary>
public class TrialExternalUserView: TrialExternalUserAddOrEdit
public class TrialExternalUserView : TrialExternalUserAddOrEdit
{
public DateTime CreateTime { get; set; }
public Guid CreateUserId { get; set; }
public DateTime UpdateTime { get; set; }
@@ -25,9 +26,33 @@ namespace IRaCIS.Core.Application.ViewModel
public Guid SystemUserId { get; set; }
[JsonIgnore]
public TrialExternalUserStateEnum InviteState { get; set; }
public TrialExternalUserStateEnum State
{
get
{
if (InviteState == TrialExternalUserStateEnum.HasSend && ExpireTime != null && ExpireTime < DateTime.Now)
{
return TrialExternalUserStateEnum.OverTime;
}
else
{
return InviteState;
}
}
}
public DateTime? ExpireTime { get; set; }
public bool? IsJoin { get; set; }
@@ -51,7 +76,7 @@ namespace IRaCIS.Core.Application.ViewModel
}
///<summary> TrialExternalUserAddOrEdit 列表查询参数模型</summary>
public class TrialExternalUserAddOrEdit: VerifyUserAdd
public class TrialExternalUserAddOrEdit : VerifyUserAdd
{
public Guid? Id { get; set; }
@@ -61,11 +86,10 @@ namespace IRaCIS.Core.Application.ViewModel
public string OrganizationName { get; set; } = String.Empty;
}
public class TrialExternalUserConfirm
{
public string BaseUrl { get; set; } = string.Empty;
[NotDefault]
public Guid Id { get; set; }
@@ -78,7 +102,7 @@ namespace IRaCIS.Core.Application.ViewModel
}
public class TrialSiteUserSurveyUserConfirm: TrialExternalUserConfirm
public class TrialSiteUserSurveyUserConfirm : TrialExternalUserConfirm
{
//[NotDefault]
//public Guid TrialId { get; set; }
@@ -88,7 +112,7 @@ namespace IRaCIS.Core.Application.ViewModel
}
public class TrialInfoWithPreparationInfo: TrialExternalUserConfirm
public class TrialInfoWithPreparationInfo : TrialExternalUserConfirm
{
public string ExperimentName { get; set; } = string.Empty;
@@ -31,7 +31,7 @@ namespace IRaCIS.Core.Application.Service
private readonly IRepository<TrialSiteUser> _trialSiteUserRepository;
public TrialExternalUserService(IRepository<TrialExternalUser> trialExternalUseRepository, IRepository<User> userRepository, IRepository<TrialUser> trialUserRepository,
IRepository<TrialSiteUserSurvey> trialSiteSurveyUserRepository,IRepository<TrialSiteUser> trialSiteUserRepository)
IRepository<TrialSiteUserSurvey> trialSiteSurveyUserRepository, IRepository<TrialSiteUser> trialSiteUserRepository)
{
_trialExternalUseRepository = trialExternalUseRepository;
_userRepository = userRepository;
@@ -54,28 +54,6 @@ namespace IRaCIS.Core.Application.Service
return await trialExternalUserQueryable.ToListAsync();
}
/// <summary>
/// 验证 在系统中是否存在该类型的账户 返回true 表示 不存在 可以添加和更新|存在但是信息一致,false 需要提示不一致项(前端 可以直接用我返回的错误信息,或者根据返回的用户信息实体,自己设置格式显示)
/// </summary>
/// <param name="addOrEditTrialExternalUser"></param>
/// <returns></returns>
[HttpPost]
public async Task<IResponseOutput> VerifyUserIsCanAddOrUpdate(VerifyUserAdd addOrEditTrialExternalUser)
{
var existUser = await _userRepository.FirstOrDefaultAsync(t => t.EMail == addOrEditTrialExternalUser.Email && t.UserTypeId == addOrEditTrialExternalUser.UserTypeId);
if (existUser != null)
{
if (existUser.LastName != addOrEditTrialExternalUser.LastName || existUser.FirstName != addOrEditTrialExternalUser.FirstName)
{
return ResponseOutput.NotOk($"该用户在系统中账户名为:{existUser.LastName + " / " + existUser.FirstName} 电话:{existUser.Phone},与填写信息存在不一致项, 现将界面信息修改为与系统一致,可进行保存", new { existUser.LastName, existUser.FirstName, existUser.Phone });
}
}
return ResponseOutput.Ok();
}
/// <summary>
@@ -233,22 +211,13 @@ namespace IRaCIS.Core.Application.Service
//收件地址
messageToSend.To.Add(new MailboxAddress(String.Empty, userInfo.Email));
//主题
messageToSend.Subject = "GRR External User survey (Verification Code)";
messageToSend.Subject = "GRR External User survey (Invite)";
var baseApiUrl = sendEmail.BaseUrl.Remove(sendEmail.BaseUrl.IndexOf("#")) + "api";
var sysUserInfo = await _userRepository.Where(t => t.Id == userInfo.SystemUserId).Include(t => t.UserTypeRole).FirstOrDefaultAsync();
var builder = new BodyBuilder();
int verificationCode = new Random().Next(100000, 1000000);
if (sysUserInfo.IsFirstAdd)
{
await _userRepository.UpdateFromQueryAsync(t => t.Id == sysUserInfo.Id,
u => new User() { Password = MD5Helper.Md5(verificationCode.ToString()) });
}
var sysUserInfo = await _userRepository.Where(t => t.Id == userInfo.SystemUserId).FirstOrDefaultAsync();
builder.HtmlBody = @$"<body style='font-family: 微软雅黑;padding: 0;margin: 0;'>
@@ -258,32 +227,10 @@ namespace IRaCIS.Core.Application.Service
{sysUserInfo.LastName + "/" + sysUserInfo.FirstName}:
</div>
<div style='line-height: 40px;padding-left: 40px;margin-bottom: 10px;'>
{trialInfo.ExperimentName} ,:
{trialInfo.ResearchProgramNo} IRC供应商IRC相关工作
</div>
<div style='border: 1px solid #eee;box-sizing:border-box;width: 80%;background: #fff;padding: 20px;line-height: 40px;font-size: 14px;border-radius: 5px;margin-left: 60px;margin-bottom: 30px;'>
<div>
: {trialInfo.TrialCode}
</div>
<div>
: {trialInfo.ResearchProgramNo}
</div>
<div>
: {trialInfo.ExperimentName}
</div>
<div>
: {sysUserInfo.UserName}
</div>
<div>
: {(sysUserInfo.IsFirstAdd ? verificationCode.ToString()+"(请在登录后进行修改)" : "***(您已有账号, 若忘记密码, 请通过邮箱找回)")}
</div>
<div>
: {sysUserInfo.UserTypeRole.UserTypeShortName}
</div>
<div>
: {sendEmail.BaseUrl} ()
</div>
</div>
<a href=' {sendEmail.RouteUrl + "?Id="+ userInfo.Id+ "&IsExternalUser=1"}' style='margin-left:60px;font-size:14px;text-decoration: none;display: inline-block;height: 40px;width: 140px;background: #00D1B2;color:#fff;border-radius: 5px;line-height: 40px;text-align: center;margin-bottom: 100px;'>
<a href=' {sendEmail.RouteUrl + "?Id=" + userInfo.Id + "&IsExternalUser=1"}' style='margin-left:60px;font-size:14px;text-decoration: none;display: inline-block;height: 40px;width: 140px;background: #00D1B2;color:#fff;border-radius: 5px;line-height: 40px;text-align: center;margin-bottom: 100px;'>
</a>
</div>
@@ -291,6 +238,7 @@ namespace IRaCIS.Core.Application.Service
</body>";
messageToSend.Body = builder.ToMessageBody();
using (var smtp = new MailKit.Net.Smtp.SmtpClient())
@@ -334,7 +282,6 @@ namespace IRaCIS.Core.Application.Service
var needUpdate = await _trialExternalUseRepository.FirstOrDefaultAsync(t => t.Id == editTrialUserPreparation.Id);
if (DateTime.Now > needUpdate.ExpireTime)
{
return ResponseOutput.NotOk("邀请加入时间已过期,重新被邀请后才可以进行确认操作");
@@ -342,7 +289,7 @@ namespace IRaCIS.Core.Application.Service
_mapper.Map(editTrialUserPreparation, needUpdate);
needUpdate.InviteState = TrialExternalUserStateEnum.UserConfirmed;
needUpdate.InviteState = editTrialUserPreparation.IsJoin == true ? TrialExternalUserStateEnum.UserConfirmed : TrialExternalUserStateEnum.UserReject;
var trialId = needUpdate.TrialId;
@@ -354,8 +301,81 @@ namespace IRaCIS.Core.Application.Service
await _trialUserRepository.AddAsync(new TrialUser() { TrialId = trialId, UserId = userId });
await _trialExternalUseRepository.UpdateFromQueryAsync(t => t.TrialId == trialId && t.SystemUserId == userId,
u => new TrialExternalUser() { InviteState = TrialExternalUserStateEnum.UserConfirmed });
var messageToSend = new MimeMessage();
//发件地址
messageToSend.From.Add(new MailboxAddress("GRR", "iracis_grr@163.com"));
//收件地址
messageToSend.To.Add(new MailboxAddress(String.Empty, needUpdate.Email));
//主题
messageToSend.Subject = "GRR External User survey (Trial Join Success)";
var builder = new BodyBuilder();
var trialInfo = await _repository.FirstOrDefaultAsync<Trial>(t => t.Id == needUpdate.TrialId);
var sysUserInfo = await _userRepository.Where(t => t.Id == needUpdate.SystemUserId).Include(t => t.UserTypeRole).FirstOrDefaultAsync();
int verificationCode = new Random().Next(100000, 1000000);
if (sysUserInfo.IsFirstAdd)
{
await _userRepository.UpdateFromQueryAsync(t => t.Id == sysUserInfo.Id,
u => new User() { Password = MD5Helper.Md5(verificationCode.ToString()) });
}
builder.HtmlBody = @$"<body style='font-family: 微软雅黑;padding: 0;margin: 0;'>
<div style='padding-left: 40px;background: #f6f6f6'>
<div style='padding-top: 20px;'>
<div style='line-height: 40px;font-size: 18px'>
{sysUserInfo.LastName + "/" + sysUserInfo.FirstName}:
</div>
<div style='line-height: 40px;padding-left: 40px;margin-bottom: 10px;'>
{trialInfo.ResearchProgramNo} IRC相关工作
</div>
<div style='border: 1px solid #eee;box-sizing:border-box;width: 80%;background: #fff;padding: 20px;line-height: 40px;font-size: 14px;border-radius: 5px;margin-left: 60px;margin-bottom: 30px;'>
<div>
: {trialInfo.TrialCode}
</div>
<div>
: {trialInfo.ResearchProgramNo}
</div>
<div>
: {trialInfo.ExperimentName}
</div>
<div>
: {sysUserInfo.UserName}
</div>
<div>
: {(sysUserInfo.IsFirstAdd ? verificationCode.ToString() + "(请在登录后进行修改)" : "***(您已有账号, 若忘记密码, 请通过邮箱找回)")}
</div>
<div>
: {sysUserInfo.UserTypeRole.UserTypeShortName}
</div>
<div>
: {editTrialUserPreparation.BaseUrl}
</div>
</div>
</div>
</div>
</body>";
messageToSend.Body = builder.ToMessageBody();
using (var smtp = new MailKit.Net.Smtp.SmtpClient())
{
smtp.ServerCertificateValidationCallback = (s, c, h, e) => true;
await smtp.ConnectAsync("smtp.163.com", 25, SecureSocketOptions.StartTls);
await smtp.AuthenticateAsync("iracis_grr@163.com", "XLWVQKZAEKLDWOAH");
await smtp.SendAsync(messageToSend);
await smtp.DisconnectAsync(true);
}
}
await _userRepository.UpdateFromQueryAsync(t => t.Id == userId, u => new User() { Status = UserStateEnum.Enable });
@@ -375,7 +395,7 @@ namespace IRaCIS.Core.Application.Service
public async Task<IResponseOutput> TrialSiteSurveyUserJoinTrial(TrialExternalUserConfirm editInfo)
{
var needUpdate = await _trialSiteSurveyUserRepository.Where(t => t.Id == editInfo.Id).Include(t=>t.TrialSiteSurvey).FirstOrDefaultAsync();
var needUpdate = await _trialSiteSurveyUserRepository.Where(t => t.Id == editInfo.Id).Include(t => t.TrialSiteSurvey).FirstOrDefaultAsync();
if (DateTime.Now > needUpdate.ExpireTime)
@@ -385,30 +405,108 @@ namespace IRaCIS.Core.Application.Service
_mapper.Map(editInfo, needUpdate);
//needUpdate.InviteState = TrialExternalUserStateEnum.UserConfirmed;
needUpdate.InviteState = editInfo.IsJoin == true ? TrialSiteUserStateEnum.UserConfirmed : TrialSiteUserStateEnum.UserReject;
if (needUpdate.SystemUserId==null)
if (needUpdate.SystemUserId == null)
{
return ResponseOutput.NotOk("调研表系统用户Id 存储有问题");
}
var trialId = needUpdate.TrialSiteSurvey.TrialId;
var siteId= needUpdate.TrialSiteSurvey.SiteId;
var siteId = needUpdate.TrialSiteSurvey.SiteId;
var userId = (Guid)needUpdate.SystemUserId;
if (!_trialUserRepository.Where(t => t.TrialId == trialId && t.UserId == userId).Any())
if (! await _trialUserRepository.AnyAsync(t => t.TrialId == trialId && t.UserId == userId))
{
await _trialUserRepository.AddAsync(new TrialUser() { TrialId = trialId, UserId = userId });
await _trialSiteUserRepository.AddAsync(new TrialSiteUser() { TrialId = trialId, SiteId = siteId, UserId = userId });
var messageToSend = new MimeMessage();
//发件地址
messageToSend.From.Add(new MailboxAddress("GRR", "iracis_grr@163.com"));
//收件地址
messageToSend.To.Add(new MailboxAddress(String.Empty, needUpdate.Email));
//主题
messageToSend.Subject = "GRR Site survey (Trial Join Success)";
var builder = new BodyBuilder();
var trialInfo = await _repository.FirstOrDefaultAsync<Trial>(t => t.Id == needUpdate.TrialSiteSurvey.TrialId);
var sysUserInfo = await _userRepository.Where(t => t.Id == needUpdate.SystemUserId).Include(t => t.UserTypeRole).FirstOrDefaultAsync();
int verificationCode = new Random().Next(100000, 1000000);
if (sysUserInfo.IsFirstAdd)
{
await _userRepository.UpdateFromQueryAsync(t => t.Id == sysUserInfo.Id,
u => new User() { Password = MD5Helper.Md5(verificationCode.ToString()) });
}
builder.HtmlBody = @$"<body style='font-family: 微软雅黑;padding: 0;margin: 0;'>
<div style='padding-left: 40px;background: #f6f6f6'>
<div style='padding-top: 20px;'>
<div style='line-height: 40px;font-size: 18px'>
{sysUserInfo.LastName + "/" + sysUserInfo.FirstName}:
</div>
<div style='line-height: 40px;padding-left: 40px;margin-bottom: 10px;'>
{trialInfo.ResearchProgramNo} IRC相关工作
</div>
<div style='border: 1px solid #eee;box-sizing:border-box;width: 80%;background: #fff;padding: 20px;line-height: 40px;font-size: 14px;border-radius: 5px;margin-left: 60px;margin-bottom: 30px;'>
<div>
: {trialInfo.TrialCode}
</div>
<div>
: {trialInfo.ResearchProgramNo}
</div>
<div>
: {trialInfo.ExperimentName}
</div>
<div>
: {sysUserInfo.UserName}
</div>
<div>
: {(sysUserInfo.IsFirstAdd ? verificationCode.ToString() + "(请在登录后进行修改)" : "***(您已有账号, 若忘记密码, 请通过邮箱找回)")}
</div>
<div>
: {sysUserInfo.UserTypeRole.UserTypeShortName}
</div>
<div>
: {editInfo.BaseUrl}
</div>
</div>
</div>
</div>
</body>";
messageToSend.Body = builder.ToMessageBody();
using (var smtp = new MailKit.Net.Smtp.SmtpClient())
{
smtp.ServerCertificateValidationCallback = (s, c, h, e) => true;
await smtp.ConnectAsync("smtp.163.com", 25, SecureSocketOptions.StartTls);
await smtp.AuthenticateAsync("iracis_grr@163.com", "XLWVQKZAEKLDWOAH");
await smtp.SendAsync(messageToSend);
await smtp.DisconnectAsync(true);
}
}
await _userRepository.UpdateFromQueryAsync(t => t.Id == needUpdate.SystemUserId, u => new User() { Status = UserStateEnum.Enable });
var success =await _trialExternalUseRepository.SaveChangesAsync();
var success = await _trialExternalUseRepository.SaveChangesAsync();
return ResponseOutput.Ok();
@@ -422,7 +520,7 @@ namespace IRaCIS.Core.Application.Service
/// <param name="id"></param>
/// <returns></returns>
[AllowAnonymous]
public async Task<TrialInfoWithPreparationInfo> JoinBasicInfo(Guid id,bool isExternalUser)
public async Task<TrialInfoWithPreparationInfo> JoinBasicInfo(Guid id, bool isExternalUser)
{
if (isExternalUser)
{
@@ -434,7 +532,7 @@ namespace IRaCIS.Core.Application.Service
return (await _trialSiteSurveyUserRepository.Where(t => t.Id == id)
.ProjectTo<TrialInfoWithPreparationInfo>(_mapper.ConfigurationProvider).FirstOrDefaultAsync()).IfNullThrowException();
}
}