1
heshaofeng
2026-03-25 37454e625df68d40897112b2e8c2e3cf4d7163e3
ÏîÄ¿´úÂë/WMSÎÞ²Ö´¢°æ/WIDESEA_WMSServer/WIDESEA_SystemService/Sys_UserService.cs
@@ -18,6 +18,8 @@
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
@@ -28,24 +30,29 @@
        private readonly ICacheService _cacheService;
        private readonly ISys_MenuService _menuService;
        private readonly ISys_RoleService _roleService;
        private readonly IPasswordPolicyConfigService _passwordPolicyConfigService;
        public IRepository<Sys_User> Repository => BaseDal;
        public Sys_UserService(IRepository<Sys_User> repository, IUnitOfWorkManage unitOfWorkManage, ICacheService cacheService, ISys_MenuService menuService, ISys_RoleService roleService) : base(repository)
        public Sys_UserService(IRepository<Sys_User> 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 ä¸´æ—¶ä½¿ç”¨
@@ -59,9 +66,51 @@
                }
                #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 });
                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<object> list)
                    {
@@ -72,7 +121,7 @@
                        return WebResponseContent.Instance.Error("无登录权限");
                    }
                    string token = JwtHelper.IssueJwt(new TokenModelJwt()
                     token = JwtHelper.IssueJwt(new TokenModelJwt()
                    {
                        UserId = user.UserId,
                        RoleId = user.RoleId,
@@ -84,7 +133,12 @@
                    //if (PermissionDataHostService.UserRoles.FirstOrDefault(x => x.UserId == user.UserId) == null)
                    //    PermissionDataHostService.UserRoles.AddRange(PermissionDataHostService.GetUserRoles(Db, user.UserId));
                    content = WebResponseContent.Instance.OK(data: new { token, userName = user.UserName, img = user.HeadImageUrl, user.UserTrueName });
                    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
                {
@@ -151,6 +205,9 @@
            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)
@@ -244,6 +301,7 @@
                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)
                {
@@ -265,5 +323,63 @@
            }
            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;
        }
    }
}