Files
zszq-trs/YLErpDAL/Modules/SubAppModule/ClientApp/ClientUserPasswordService.cs
T
2024-05-09 14:06:26 +08:00

147 lines
6.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using BaseOUDAL;
using YLErp.Helpers;
namespace YLErp.Modules.SubAppModule.ClientApp
{
/// <summary>
/// 客户用户密码服务
/// </summary>
public class ClientUserPasswordService : BaseService<ClientDBContext>
{
public ClientUserPasswordService(OptUserInfo optUser) : base(optUser)
{
}
/// <summary>
/// 重置密码
/// </summary>
public string ResetPassword(int clientId)
{
var clientDB = DbContextFactory.GetClientDbContext(UserInfo);
var client = clientDB.client.FirstOrDefault(s => s.id == clientId);
if (client == null)
{
throw new ServiceException("该客户不存在");
}
if (string.IsNullOrEmpty(client.DefaultLoginName))
{
throw new ServiceException("请先设置该客户的默认登录名");
}
if (DbContext.ClientUser.Any(x => x.ClientId != clientId && x.LoginName == client.DefaultLoginName))
{
return "该客户默认登录名已经被占用,请修改后再确认";
}
if (string.IsNullOrEmpty(client.Guid))
{
client.Guid = Guid.NewGuid().ToString();
}
var password = DataHelper.GenerateCheckCode(6);
var clientUser = DbContext.ClientUser.FirstOrDefault(x => x.ClientId == client.id);
if (clientUser == null)
{
clientUser = new ClientUser()
{
ClientId = client.id,
LoginName = client.DefaultLoginName,
Password = DataHelper.EncryptPassword(client.DefaultLoginName.ToLower(), password)
};
SetDBModelOpt(clientUser);
DbContext.ClientUser.Add(clientUser);
}
else
{
clientUser.Password = DataHelper.EncryptPassword(client.DefaultLoginName.ToLower(), password);
}
// 要向该客户的所有订阅了邮件通知的人员发送邮件
var emails = ClientModule.ClientDataQueryService.GetClientEmails(client.id);
if (!emails.Any(n => !string.IsNullOrEmpty(n)))
{
throw new ServiceException("未设置邮箱,无法发送用户名密码");
}
var sendMail = EmailHelper.SendMail(string.Join(";", emails), PS.Config.CompanyFullName + "-场外衍生品客户服务用户密码设置"
, string.Format("<p>{1}</p><p><br/></p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;您在我司场外衍生品客户服务系统的登录账号和密码信息如下:</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;登录用户名:{2}, 密码:{3}</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;建议使用Chrome浏览器!</p><p><br/></p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{0}</p>"
, PS.Config.CompanyFullName, client.Name, client?.DefaultLoginName ?? client.DefaultLoginName, password));
if (string.IsNullOrEmpty(sendMail))
{
clientDB.ClientAuditLog.Add(new ClientAuditLog
{
ClientId = client.id,
OptType = "重置密码",
Changes = string.Empty,
DataType = "00",
OptId = UserId,
OptName = UserName,
OptDate = DateTime.Now
});
clientDB.SaveChanges();
DbContext.SaveChanges();
return "发送用户名密码成功";
}
else
{
LogFactory.GetLogger("客户密码重置").Error("发送邮件失败:" + sendMail);
throw new ServiceException("发送用户名密码失败");
}
}
/// <summary>
/// 变更密码
/// </summary>
public void ChangePassword(ClientChangePasswordRequest reqModel)
{
if (reqModel is null)
{
throw new ArgumentNullException(nameof(reqModel));
}
if (string.IsNullOrWhiteSpace(reqModel.Password))
{
throw new ServiceException("输入错误,缺少当前密码");
}
if (string.IsNullOrWhiteSpace(reqModel.NewPassword))
{
throw new ServiceException("输入错误,缺少新密码");
}
if (reqModel.NewPassword == null || reqModel.NewPassword.Length < 6 || !reqModel.NewPassword.Any(n => (n >= 'a' && n <= 'z') || (n >= 'A' && n <= 'Z')) || !reqModel.NewPassword.Any(n => n >= '0' && n <= '9'))
{
throw new ServiceException("密码长度至少6位并且包含英文字母和数字");
}
var clientDB = DbContextFactory.GetClientDbContext(UserInfo);
if (!clientDB.client.Any(c => c.id == reqModel.ClientId))
{
throw new ServiceException("客户信息不存在");
}
var cuser = DbContext.ClientUser.FirstOrDefault(c => c.id == reqModel.ClientId);
if (cuser == null)
{
throw new ServiceException("用户不存在");
}
if (!DataHelper.EncryptPassword(cuser.LoginName.ToLowerInvariant(), reqModel.Password).Equals(cuser.Password, StringComparison.OrdinalIgnoreCase))
{
throw new ServiceException("当前密码错误");
}
cuser.Password = DataHelper.EncryptPassword(cuser.LoginName.ToLowerInvariant(), reqModel.NewPassword);
DbContext.SaveChanges();
}
}
}