xxyy
2025-03-10 ffe8ac13ba8bc9426f8b5f5b88f094a05b31b7ff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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;
            }
        }
    }
}