邮件根据接收人工作语言,动态设置邮件模板

This commit is contained in:
2026-05-13 11:05:22 +08:00
parent 030aa36f94
commit fdb124b12e
7 changed files with 403 additions and 350 deletions
@@ -66,7 +66,7 @@ namespace IRaCIS.Core.Application.Service
Task AddUserSendEmailAsync(Guid userId, string baseUrl, string routeUrl);
Task AdminResetPwdSendEmailAsync(Guid userId, string pwdNotMd5 = "123456", string baseUrl="");
Task AdminResetPwdSendEmailAsync(Guid userId, string pwdNotMd5 = "123456", string baseUrl = "");
Task SiteSurveyUserJoinEmail(Guid trialId, Guid userId, string userTypes, string baseUrl, string rootUrl);
@@ -104,10 +104,18 @@ namespace IRaCIS.Core.Application.Service
{
private readonly SystemEmailSendConfig _systemEmailConfig = systemEmailConfig.CurrentValue;
/// <summary>
/// 获取邮件主题和html 通用封装 以用户语言为主,没有的就用当前的请求语言_userInfo.IsEn_Us
/// </summary>
/// <param name="scenario"></param>
/// <param name="messageToSend"></param>
/// <param name="emailFunc"></param>
/// <param name="userWorkLanguage"></param>
/// <returns></returns>
/// <exception cref="BusinessValidationFailedException"></exception>
private async Task<EmailNoticeConfig> GetEmailSubejctAndHtmlInfoAndBuildAsync(EmailBusinessScenario scenario, MimeMessage messageToSend,
Func<(string topicStr, string htmlBodyStr), (string topicStr, string htmlBodyStr)> emailFunc)
Func<(string topicStr, string htmlBodyStr), (string topicStr, string htmlBodyStr)> emailFunc, UserWorkLanguage? userWorkLanguage = null)
{
var configInfo = await _emailNoticeConfigrepository.Where(t => t.BusinessScenarioEnum == scenario).FirstOrDefaultAsync();
@@ -119,7 +127,9 @@ namespace IRaCIS.Core.Application.Service
}
var (topicStr, htmlBodyStr) = _userInfo.IsEn_Us ? (configInfo.EmailTopic, configInfo.EmailHtmlContent) : (configInfo.EmailTopicCN, configInfo.EmailHtmlContentCN);
bool isEn_Us = userWorkLanguage.HasValue ? userWorkLanguage.Value == UserWorkLanguage.US : _userInfo.IsEn_Us;
var (topicStr, htmlBodyStr) = isEn_Us ? (configInfo.EmailTopic, configInfo.EmailHtmlContent) : (configInfo.EmailTopicCN, configInfo.EmailHtmlContentCN);
try
{
@@ -355,8 +365,7 @@ namespace IRaCIS.Core.Application.Service
var redirectUrl = $"{domain}/api/User/UserRedirect?url={System.Web.HttpUtility.UrlEncode(routeUrl)}";
var companyName = _userInfo.IsEn_Us ? _systemEmailConfig.CompanyShortName : _systemEmailConfig.CompanyShortNameCN;
var companyName = sysUserInfo.UserWorkLanguage == UserWorkLanguage.US ? _systemEmailConfig.CompanyShortName : _systemEmailConfig.CompanyShortNameCN;
Func<(string topicStr, string htmlBodyStr), (string topicStr, string htmlBodyStr)> emailConfigFunc = input =>
{
@@ -374,7 +383,7 @@ namespace IRaCIS.Core.Application.Service
};
await GetEmailSubejctAndHtmlInfoAndBuildAsync(EmailBusinessScenario.SysCreateUser, messageToSend, emailConfigFunc);
await GetEmailSubejctAndHtmlInfoAndBuildAsync(EmailBusinessScenario.SysCreateUser, messageToSend, emailConfigFunc, sysUserInfo.UserWorkLanguage);
await SendEmailHelper.SendEmailAsync(messageToSend, _systemEmailConfig);
@@ -393,7 +402,7 @@ namespace IRaCIS.Core.Application.Service
//主题
//---[来自展影IRC] 关于重置账户密码的提醒
var companyName = _userInfo.IsEn_Us ? _systemEmailConfig.CompanyShortName : _systemEmailConfig.CompanyShortNameCN;
var companyName = sysUserInfo.UserWorkLanguage == UserWorkLanguage.US ? _systemEmailConfig.CompanyShortName : _systemEmailConfig.CompanyShortNameCN;
Func<(string topicStr, string htmlBodyStr), (string topicStr, string htmlBodyStr)> emailConfigFunc = input =>
{
@@ -413,7 +422,7 @@ namespace IRaCIS.Core.Application.Service
};
await GetEmailSubejctAndHtmlInfoAndBuildAsync(EmailBusinessScenario.SysResetPassword, messageToSend, emailConfigFunc);
await GetEmailSubejctAndHtmlInfoAndBuildAsync(EmailBusinessScenario.SysResetPassword, messageToSend, emailConfigFunc, sysUserInfo.UserWorkLanguage);
await SendEmailHelper.SendEmailAsync(messageToSend, _systemEmailConfig);
}
@@ -910,7 +919,7 @@ namespace IRaCIS.Core.Application.Service
{
await SendEmailHelper.SendEmailAsync(messageToSend, trialinfo);
}
}
@@ -935,7 +944,7 @@ namespace IRaCIS.Core.Application.Service
{
var topicStr = string.Format(input.topicStr, companyName);
var htmlBodyStr = string.Format(ReplacePlatformName(ReplaceCompanyName(input.htmlBodyStr)) ,
var htmlBodyStr = string.Format(ReplacePlatformName(ReplaceCompanyName(input.htmlBodyStr)),
sysUserInfo.FullName
);
@@ -985,7 +994,7 @@ namespace IRaCIS.Core.Application.Service
public async Task SiteSuervyUpdateUser(Guid trialSiteId, string email, string name, string url)
{
var siteInfo = await _trialSiteRepository.Where(t => t.Id == trialSiteId).Include(x=>x.Trial).FirstOrDefaultAsync();
var siteInfo = await _trialSiteRepository.Where(t => t.Id == trialSiteId).Include(x => x.Trial).FirstOrDefaultAsync();
var trialInfo = siteInfo.Trial;
var messageToSend = new MimeMessage();
@@ -173,23 +173,16 @@ namespace IRaCIS.Core.Application.Services
if (newUserTypeIds.Any()&& newUserTypeIds.Count()>0)
{
// 发送邮件给新增的角色
Console.WriteLine("开始 发送系统文档更新邮件给新增角色");
Console.WriteLine(string.Join(",", newUserTypeIds));
Log.Logger.Warning("开始 发送系统文档更新邮件给新增角色");
Log.Logger.Warning(string.Join(",", newUserTypeIds));
Task.Run(async () =>
// 只发送给新增的角色
await _mediatorScoped.Publish(new SystemDocumentPublishEvent
{
// 创建独立作用域
using (var scope = serviceScopeFactory.CreateScope())
{
// 从新作用域解析服务
var mediator = scope.ServiceProvider.GetRequiredService<IMediator>();
// 只发送给新增的角色
await mediator.Publish(new SystemDocumentPublishEvent {
Ids = new List<Guid> { document.Id },
NewUserTypeIds = newUserTypeIds
});
}
Ids = new List<Guid> { document.Id },
NewUserTypeIds = newUserTypeIds
});
}
}
@@ -214,18 +207,10 @@ namespace IRaCIS.Core.Application.Services
IsDeleted = false,
});
Console.WriteLine("开始 发布系统文档");
Log.Logger.Warning("开始 发布系统文档");
await _mediatorScoped.Publish(new SystemDocumentPublishEvent { Ids = inDto.Ids });
Task.Run(async () =>
{
// 创建独立作用域
using (var scope = serviceScopeFactory.CreateScope())
{
// 从新作用域解析服务
var mediator = scope.ServiceProvider.GetRequiredService<IMediator>();
await mediator.Publish(new SystemDocumentPublishEvent { Ids = inDto.Ids });
}
});
return ResponseOutput.Result(true);
@@ -27,6 +27,7 @@ namespace IRaCIS.Core.Application.Services
[ApiExplorerSettings(GroupName = "Trial")]
public class TrialDocumentService(IRepository<TrialDocument> _trialDocumentRepository,
IRepository<Trial> _trialRepository,
IScopedMediator _mediatorScoped,
IRepository<AuditRecord> _auditRecordRepository,
IRepository<TrialDocumentAttachment> _trialDocumentAttachmentRepository,
ISystemDocumentService _systemDocumentService,
@@ -105,18 +106,11 @@ namespace IRaCIS.Core.Application.Services
IsDeleted = false,
}, false, true);
await _trialDocumentRepository.SaveChangesAsync();
Console.WriteLine("开始 发布项目文档");
Serilog.Log.Logger.Warning("开始 发布项目文档");
Task.Run(async () =>
{
// 创建独立作用域
using (var scope = serviceScopeFactory.CreateScope())
{
// 从新作用域解析服务
var mediator = scope.ServiceProvider.GetRequiredService<IMediator>();
await mediator.Publish(new TrialDocumentPublishEvent { Ids = inDto.Ids });
}
});
await _mediatorScoped.Publish(new TrialDocumentPublishEvent { Ids = inDto.Ids });
return ResponseOutput.Result(true);
@@ -128,32 +122,20 @@ namespace IRaCIS.Core.Application.Services
/// <returns></returns>
public async Task<IResponseOutput> TestPush()
{
Task.Run(async () =>
{
// 创建独立作用域
using (var scope = serviceScopeFactory.CreateScope())
{
// 从新作用域解析服务
var mediator = scope.ServiceProvider.GetRequiredService<IMediator>();
await mediator.Publish(new TrialDocumentErverDayEvent { });
}
});
await _mediatorScoped.Publish(new TrialDocumentErverDayEvent { });
return ResponseOutput.Result(true);
}
public async Task<IResponseOutput> TestSendEmail()
{
Task.Run(async () =>
{
// 创建独立作用域
using (var scope = serviceScopeFactory.CreateScope())
{
// 从新作用域解析服务
var mediator = scope.ServiceProvider.GetRequiredService<IMediator>();
await mediator.Publish(new ImageQCRecurringEvent { TrialId = Guid.Parse("08de2254-5d7d-581a-0242-0a0001000000") });
}
});
await _mediatorScoped.Publish(new ImageQCRecurringEvent { TrialId = Guid.Parse("08de2254-5d7d-581a-0242-0a0001000000") });
return ResponseOutput.Result(true);
}
@@ -1170,21 +1152,13 @@ namespace IRaCIS.Core.Application.Services
Console.WriteLine("开始 发送项目文档更新邮件给新增角色");
Console.WriteLine(string.Join(",", newUserTypeIds));
Task.Run(async () =>
await _mediatorScoped.Publish(new TrialDocumentPublishEvent
{
// 创建独立作用域
using (var scope = serviceScopeFactory.CreateScope())
{
// 从新作用域解析服务
var mediator = scope.ServiceProvider.GetRequiredService<IMediator>();
// 只发送给新增的角色
await mediator.Publish(new TrialDocumentPublishEvent
{
Ids = new List<Guid> { document.Id },
NewUserTypeIds = newUserTypeIds
});
}
Ids = new List<Guid> { document.Id },
NewUserTypeIds = newUserTypeIds
});
}
}
return ResponseOutput.Ok(document.Id.ToString());