using AutoMapper;
|
using MailKit.Net.Smtp;
|
using Microsoft.AspNetCore.Components.Forms;
|
using MimeKit;
|
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
|
using System.Net;
|
using System.Reflection;
|
using WIDESEA_Core;
|
using WIDESEA_Core.Authorization;
|
using WIDESEA_Core.BaseRepository;
|
using WIDESEA_Core.BaseServices;
|
using WIDESEA_Core.Const;
|
using WIDESEA_Core.Helper;
|
using WIDESEA_Core.HttpContextUser;
|
using WIDESEA_DTO.System;
|
using WIDESEA_IRepository;
|
using WIDESEA_IServices;
|
using WIDESEA_Model;
|
using WIDESEA_Model.Models;
|
|
namespace WIDESEA_Services
|
{
|
public class Sys_CompanyRegistrationService : ServiceBase<Sys_CompanyRegistration, ISys_CompanyRegistrationRepository>, ISys_CompanyRegistrationService
|
{
|
private readonly IUnitOfWorkManage _unitOfWorkManage;
|
private readonly IMapper _mapper;
|
private readonly ISys_ConfigService _configService;
|
|
public Sys_CompanyRegistrationService(ISys_CompanyRegistrationRepository repository,
|
IUnitOfWorkManage unitOfWorkManage,
|
IMapper mapper,
|
ISys_ConfigService configService) : base(repository)
|
{
|
_unitOfWorkManage = unitOfWorkManage;
|
_mapper = mapper;
|
_configService = configService;
|
}
|
|
public WebResponseContent RegisterCompany(RegistrationDTO registrationDto)
|
{
|
WebResponseContent content = new WebResponseContent();
|
var invalidFields = new List<string>();
|
|
var properties = typeof(RegistrationDTO).GetProperties();
|
foreach (var property in properties)
|
{
|
var value = property.GetValue(registrationDto);
|
// 检查字段值是否为空
|
if ((value == null || string.IsNullOrWhiteSpace(value.ToString())))
|
{
|
// 将空字段名称添加到 invalidFields 集合中
|
invalidFields.Add(property.Name);
|
}
|
}
|
Sys_CompanyRegistration registration = _mapper.Map<Sys_CompanyRegistration>(registrationDto);
|
// 如果存在空字段,则返回错误消息
|
if (invalidFields.Any())
|
{
|
string invalidFieldsMessage = string.Join(", ", invalidFields);
|
return content.Error($"以下字段不能为空: {invalidFieldsMessage}");
|
}
|
|
var isResult = base.Db.Queryable<Sys_CompanyRegistration>().Any(x => x.CompanyName == registrationDto.CompanyName);
|
if (isResult)
|
{
|
return content.Error("已提交注册,请勿重复提交");
|
}
|
|
List<Sys_Config> sysConfigs = _configService.GetConfigsByCategory(CateGoryConst.CONFIG_SYS_RegExmail);
|
if (sysConfigs == null)
|
{
|
return content.Error("尚未配置邮箱服务,请联系管理员配置邮箱转发");
|
}
|
|
// 设置默认值
|
registration.RegistrationStatus = "待审核";
|
registration.Creater = "System";
|
|
// 创建 HTML 格式的邮件内容
|
var bodyBuilder = new BodyBuilder();
|
bodyBuilder.HtmlBody = $@"
|
<html>
|
<body>
|
<h1>新用户注册通知</h1>
|
<h2>公司详情</h2>
|
<p><strong>公司名称:</strong> {registration.CompanyName}</p>
|
<p><strong>公司类型:</strong> {registration.CompanyType}</p>
|
<p><strong>行业类别:</strong> {registration.Industry}</p>
|
<h2>联系人明细</h2>
|
<p><strong>联系人姓名:</strong> {registration.ContactName}</p>
|
<p><strong>联系人电话:</strong> {registration.ContactPhone}</p>
|
<p><strong>联系人电子邮箱:</strong> {registration.ContactEmail}</p>
|
<h2>隐私政策</h2>
|
<p><strong>使用条款和隐私政策:</strong> {(registration.AgreeTerms ? "同意" : "不同意")}</p>
|
<br/>
|
<br/>
|
<p>发件人,<br/>湖南宽海智能</p>
|
</body>
|
</html>";
|
//"chenmeimei@hnkhzn.com"
|
isResult = SendEmail(sysConfigs, registrationDto, bodyBuilder);
|
// 保存到数据库
|
BaseDal.AddData(registration);
|
return content.OK();
|
}
|
|
/// <summary>
|
/// 发送邮箱
|
/// </summary>
|
/// <param name="configs"></param>
|
/// <param name="registration"></param>
|
/// <returns></returns>
|
public bool SendEmail(List<Sys_Config> configs, RegistrationDTO registration, BodyBuilder bodyBuilder)
|
{
|
List<Sys_Config> sysConfigs = _configService.GetConfigsByCategory(CateGoryConst.CONFIG_SYS_BaseExmail);
|
if (sysConfigs == null)
|
{
|
return false;
|
}
|
|
// 获取 smtp 配置
|
string _smtpServer = sysConfigs.FirstOrDefault(x => x.ConfigKey == SysConfigConst.SMTP_Server).ConfigValue ?? "";
|
int _smtpPort = sysConfigs.FirstOrDefault(x => x.ConfigKey == SysConfigConst.SMTP_Port).ConfigValue.ObjToInt();
|
string _smtpUser = sysConfigs.FirstOrDefault(x => x.ConfigKey == SysConfigConst.SMTP_User).ConfigValue ?? "";
|
string _smtpPass = sysConfigs.FirstOrDefault(x => x.ConfigKey == SysConfigConst.SMTP_Pass).ConfigValue ?? "";
|
|
// 获取 邮件配置
|
string _smtpTitle = configs.FirstOrDefault(x => x.ConfigKey == SysConfigConst.SMTP_Title).ConfigValue ?? "";
|
string _smtpConTitle = configs.FirstOrDefault(x => x.ConfigKey == SysConfigConst.SMTP_ContentTitle).ConfigValue ?? "";
|
string _smtpRegUser = configs.FirstOrDefault(x => x.ConfigKey == SysConfigConst.SMTP_RegUser).ConfigValue ?? "";
|
|
// 创建邮件
|
var message = new MimeMessage();
|
message.From.Add(new MailboxAddress(_smtpTitle, _smtpUser));
|
message.To.Add(new MailboxAddress("", _smtpRegUser));
|
message.Subject = _smtpConTitle;
|
|
message.Body = bodyBuilder.ToMessageBody();
|
|
// 发送邮件
|
try
|
{
|
using (var client = new SmtpClient())
|
{
|
client.Connect(_smtpServer, _smtpPort, true);
|
client.Authenticate(_smtpUser, _smtpPass);
|
client.Send(message);
|
client.Disconnect(true);
|
}
|
|
Console.WriteLine("邮件发送成功!");
|
return true;
|
}
|
catch (Exception ex)
|
{
|
Console.WriteLine($"发送邮件时发生未知错误:{ex.Message}");
|
return false;
|
}
|
}
|
}
|
}
|