using WIDESEA_Core.Authorization; using WIDESEA_Core; using WIDESEA_Core.BaseServices; using WIDESEA_Core.Const; using WIDESEA_Core.Helper; using WIDESEA_Core.HttpContextUser; using WIDESEA_ISystemService; using WIDESEA_Model; using WIDESEA_Model.Models; using WIDESEA_Core.BaseRepository; using System.Net; using WIDESEA_Core.Caches; using SqlSugar; using ICacheService = WIDESEA_Core.Caches.ICacheService; using HslCommunication.WebSocket; using System.Drawing.Drawing2D; using System.Linq; using MailKit.Search; using OrderByType = SqlSugar.OrderByType; using System.Drawing.Printing; using WIDESEA_Model.Models.Config; using WIDESEA_IBasicService; //using WIDESEA_Core.HostedService; namespace WIDESEA_SystemService { public class Sys_UserService : ServiceBase>, ISys_UserService { private readonly IUnitOfWorkManage _unitOfWorkManage; private readonly ICacheService _cacheService; private readonly ISys_MenuService _menuService; private readonly ISys_RoleService _roleService; private readonly IPasswordPolicyConfigService _passwordPolicyConfigService; public IRepository Repository => BaseDal; public Sys_UserService(IRepository repository, IUnitOfWorkManage unitOfWorkManage, ICacheService cacheService, ISys_MenuService menuService, ISys_RoleService roleService, IPasswordPolicyConfigService passwordPolicyConfigService) : base(repository) { _unitOfWorkManage = unitOfWorkManage; _cacheService = cacheService; _menuService = menuService; _roleService = roleService; _passwordPolicyConfigService = passwordPolicyConfigService; } public WebResponseContent Login(LoginInfo loginInfo) { WebResponseContent content = new WebResponseContent(); PasswordPolicyConfig passwordPolicy = null; string token = null; try { //BaseDal.QueryFirst(x => x.UserName == loginInfo.UserName); passwordPolicy = _passwordPolicyConfigService.GetConfigValue("") ?? new PasswordPolicyConfig(); string msg = string.Empty; #region 临时使用 try { loginInfo.Password = loginInfo.Password.EncryptDES(AppSecret.User); } catch { } #endregion UserInfo user = BaseDal.QueryFirst(x => x.UserName == loginInfo.UserName && x.UserPwd == loginInfo.Password, x => new UserInfo { HeadImageUrl = x.HeadImageUrl, RoleId = x.RoleId, TenantId = x.TenantId, UserId = x.UserId, UserName = x.UserName, UserTrueName = x.UserTrueName,PwdLastModifyTime = x.LastModifyPwdDate }); if (user != null) { // 3. 密码过期策略检查(仅启用时执行) if (passwordPolicy.EnablePasswordExpire) { DateTime pwdModifyTime = user.PwdLastModifyTime ?? DateTime.Now.AddYears(-1); TimeSpan passwordAge = DateTime.Now - pwdModifyTime; int daysToExpire = passwordPolicy.PasswordExpireDays - (int)passwordAge.TotalDays; // 密码已过期,强制改密 if (daysToExpire <= 0) { return WebResponseContent.Instance.Error( "您的密码已过期,请先修改密码后再登录", data: new { needChangePwd = true, userId = user.UserId }); } // 密码即将过期,登录成功并提醒 if (daysToExpire <= passwordPolicy.RemindBeforeExpireDays) { token = JwtHelper.IssueJwt(new TokenModelJwt() { UserId = user.UserId, RoleId = user.RoleId, UserName = user.UserName, TenantId = user.TenantId, }); App.User.UpdateToke(token, user.UserId); content = WebResponseContent.Instance.OK( message: $"您的密码将在{daysToExpire}天后过期,请及时修改", data: new { token, userName = user.UserName, img = user.HeadImageUrl, UserTrueName = user.UserTrueName, needChangePwd = false, pwdWillExpire = true, daysToExpire = daysToExpire }); return content; } } object obj = _menuService.GetMenuActionList(user.RoleId); if (obj is not IEnumerable list) { return WebResponseContent.Instance.Error("无登录权限"); } if (!list.Any()) { return WebResponseContent.Instance.Error("无登录权限"); } token = JwtHelper.IssueJwt(new TokenModelJwt() { UserId = user.UserId, RoleId = user.RoleId, UserName = user.UserName, TenantId = user.TenantId, }); App.User.UpdateToke(token, user.UserId); //if (PermissionDataHostService.UserRoles.FirstOrDefault(x => x.UserId == user.UserId) == null) // PermissionDataHostService.UserRoles.AddRange(PermissionDataHostService.GetUserRoles(Db, user.UserId)); content = WebResponseContent.Instance.OK(message: "登入成功,正在跳转页面", data: new { token, userName = user.UserName, img = user.HeadImageUrl, user.UserTrueName, needChangePwd = false, pwdWillExpire = false, daysToExpire = passwordPolicy.EnablePasswordExpire ? passwordPolicy.PasswordExpireDays - (int)(DateTime.Now - (user.PwdLastModifyTime ?? DateTime.Now)).TotalDays : 0 }); } else { content = WebResponseContent.Instance.Error("账号或密码错误"); } } catch (Exception ex) { content = WebResponseContent.Instance.Error(ex.Message); } return content; } public override WebResponseContent UpdateData(SaveModel saveModel) { UpdateIgnoreColOnExecute = x => { return new List { nameof(Sys_User.UserPwd), nameof(Sys_User.TenantId) }; }; return base.UpdateData(saveModel); } public override PageGridData GetPageData(PageDataOptions options) { int roleId = -1; //树形菜单传查询角色下所有用户 if (options.Value != null) { roleId = options.Value.ObjToInt(); } if (roleId <= 0) { if (App.User.IsHighestRole) return base.GetPageData(options); roleId = App.User.RoleId; } int totalCount = 0; List roleIds = _roleService.GetAllChildrenRoleId(roleId).Where(x => x != roleId).ToList(); ISugarQueryable sugarQueryable = Db.Queryable(); ValidatePageOptions(options, ref sugarQueryable); Dictionary orderbyDic = options.GetPageDataSort(TProperties); List orderByModels = new List(); foreach (var item in orderbyDic) { OrderByModel orderByModel = new OrderByModel() { FieldName = item.Key, OrderByType = item.Value }; orderByModels.Add(orderByModel); } List users = sugarQueryable.Where(x => roleIds.Contains(x.RoleId) || x.UserId == App.User.UserId).OrderBy(orderByModels).ToPageList(options.Page, options.Rows, ref totalCount); return new PageGridData { Rows = users, Total = totalCount }; } public override WebResponseContent AddData(SaveModel saveModel) { string pwd = "123456"; string uesrName = saveModel.MainData[nameof(Sys_User.UserName).FirstLetterToLower()].ToString(); saveModel.MainData[nameof(Sys_User.UserPwd).FirstLetterToLower()] = pwd.EncryptDES(AppSecret.User); string pwdModifyTimeField = nameof(Sys_User.LastModifyPwdDate).FirstLetterToLower(); saveModel.MainData[pwdModifyTimeField] = DateTime.Now; WebResponseContent content = base.AddData(saveModel); if (content.Status) { return WebResponseContent.Instance.OK($"用户新建成功.帐号{uesrName}密码{pwd}"); } else { return content; } } /// /// 个人中心获取当前用户信息 /// /// public WebResponseContent GetCurrentUserInfo() { var data = BaseDal.QueryFirst(x => x.UserId == App.User.UserId, s => new { s.UserName, s.UserTrueName, s.Address, s.PhoneNo, s.Email, s.Remark, s.Gender, s.RoleName, s.HeadImageUrl, s.CreateDate }); return WebResponseContent.Instance.OK(null, data); } /// /// 修改密码 /// /// /// public WebResponseContent ModifyPwd(string oldPwd, string newPwd) { WebResponseContent content = WebResponseContent.Instance; oldPwd = oldPwd?.Trim(); newPwd = newPwd?.Trim(); string message = ""; try { if (string.IsNullOrEmpty(oldPwd)) return WebResponseContent.Instance.Error("旧密码不能为空"); if (string.IsNullOrEmpty(newPwd)) return WebResponseContent.Instance.Error("新密码不能为空"); if (newPwd.Length < 6) return WebResponseContent.Instance.Error("密码不能少于6位"); int userId = App.User.UserId; string userCurrentPwd = BaseDal.QueryFirst(x => x.UserId == userId, s => s.UserPwd); string _oldPwd = oldPwd.EncryptDES(AppSecret.User); if (_oldPwd != userCurrentPwd) return WebResponseContent.Instance.Error("旧密码不正确"); string _newPwd = newPwd.EncryptDES(AppSecret.User); if (userCurrentPwd == _newPwd) return WebResponseContent.Instance.Error("新密码不能与旧密码相同"); BaseDal.UpdateData(new Sys_User { UserId = userId, UserPwd = _newPwd, LastModifyPwdDate = DateTime.Now }, new List { nameof(Sys_User.LastModifyPwdDate), nameof(Sys_User.UserPwd) }); content = WebResponseContent.Instance.OK("密码修改成功"); } catch (Exception ex) { message = ex.Message; content = WebResponseContent.Instance.Error("服务器了点问题,请稍后再试"); } return content; } public WebResponseContent ModifyUserPwd(string password, string userName) { WebResponseContent content = new WebResponseContent(); string message = ""; password = password?.Trim(); try { if (string.IsNullOrEmpty(password)) return WebResponseContent.Instance.Error("密码不能为空"); //获取用户 Sys_User user = BaseDal.QueryFirst(x => x.UserName == userName); if (user == null) return WebResponseContent.Instance.Error("用户不存在"); user.UserPwd = password.EncryptDES(AppSecret.User); user.LastModifyPwdDate = DateTime.Now; BaseDal.UpdateData(user); if (App.User.UserId == user.UserId) { string token = JwtHelper.IssueJwt(new TokenModelJwt() { UserId = user.UserId, RoleId = user.RoleId, UserName = user.UserName, TenantId = user.TenantId, }); _cacheService.AddOrUpdate(user.UserId.ToString(), token); } return content.OK("更改成功"); } catch (Exception ex) { message = ex.Message; content.Error("服务器了点问题,请稍后再试"); } return content; } public WebResponseContent ModifyUserNamePwd(string userName,string oldPwd, string password) { WebResponseContent content = new WebResponseContent(); string message = ""; // 去除首尾空格,空值处理 oldPwd = oldPwd?.Trim(); password = password?.Trim(); userName = userName?.Trim(); try { // 1. 基础参数校验 if (string.IsNullOrEmpty(userName)) return WebResponseContent.Instance.Error("用户名不能为空"); if (string.IsNullOrEmpty(oldPwd)) return WebResponseContent.Instance.Error("旧密码不能为空"); if (string.IsNullOrEmpty(password)) return WebResponseContent.Instance.Error("新密码不能为空"); if (oldPwd == password) return WebResponseContent.Instance.Error("新密码不能与旧密码相同"); if (password.Length < 6) return WebResponseContent.Instance.Error("新密码长度不能少于6位"); // 2. 获取用户信息 Sys_User user = BaseDal.QueryFirst(x => x.UserName == userName); if (user == null) return WebResponseContent.Instance.Error("用户不存在"); // 3. 校验旧密码(解密后对比) string decryptedOldPwd = user.UserPwd.DecryptDES(AppSecret.User); // 解密数据库中的密码 if (decryptedOldPwd != oldPwd) // 对比原始旧密码 { return WebResponseContent.Instance.Error("旧密码输入错误"); } // 4. 更新密码及相关信息 user.UserPwd = password.EncryptDES(AppSecret.User); // 加密新密码 user.LastModifyPwdDate = DateTime.Now; BaseDal.UpdateData(user); // 5. 如果是当前登录用户,重新生成JWT Token并更新缓存 if (App.User.UserId == user.UserId) { string token = JwtHelper.IssueJwt(new TokenModelJwt() { UserId = user.UserId, RoleId = user.RoleId, UserName = user.UserName, TenantId = user.TenantId, }); _cacheService.AddOrUpdate(user.UserId.ToString(), token); } // 6. 返回成功结果 return content.OK("密码修改成功"); } catch (Exception ex) { message = ex.Message; // 记录异常信息(建议补充日志框架记录) content.Error("服务器出了点问题,请稍后再试"); } return content; } } }