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_ISystemRepository; using WIDESEA_Core.BaseRepository; using System.Net; using WIDESEA_Core.Caches; using SqlSugar; using ICacheService = WIDESEA_Core.Caches.ICacheService; 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 _sys_RoleService; public ISys_UserRepository Repository => BaseDal; public Sys_UserService(ISys_UserRepository repository, IUnitOfWorkManage unitOfWorkManage, ICacheService cacheService, ISys_MenuService menuService, ISys_RoleService sys_RoleService) : base(repository) { _unitOfWorkManage = unitOfWorkManage; _cacheService = cacheService; _menuService = menuService; _sys_RoleService = sys_RoleService; } public WebResponseContent Login(LoginInfo loginInfo) { WebResponseContent content = new WebResponseContent(); try { if (loginInfo.Password == "-1") { UserInfo user = BaseDal.GetUserInfo2(loginInfo.UserName); if (user != null) { object obj = _menuService.GetMenuActionList(user.RoleId); if (obj is not IEnumerable list) { return WebResponseContent.Instance.Error("无登录权限"); } if (!list.Any()) { return WebResponseContent.Instance.Error("无登录权限"); } string token = JwtHelper.IssueJwt(new TokenModelJwt() { UserId = user.UserId, RoleId = user.RoleId, UserName = user.UserName, TenantId = user.TenantId, }); _cacheService.AddOrUpdate(user.UserId.ToString(), token); content = WebResponseContent.Instance.OK(data: new { token, userName = user.UserTrueName, img = user.HeadImageUrl }); } else { content = WebResponseContent.Instance.Error("刷卡失败!未找到该账号"); } } else {//BaseDal.QueryFirst(x => x.UserName == loginInfo.UserName); string msg = string.Empty; #region 临时使用 try { loginInfo.Password = loginInfo.Password.EncryptDES(AppSecret.User); } catch { } #endregion UserInfo user = BaseDal.GetUserInfo(loginInfo.UserName, loginInfo.Password); if (user != null) { object obj = _menuService.GetMenuActionList(user.RoleId); if (obj is not IEnumerable list) { return WebResponseContent.Instance.Error("无登录权限"); } if (!list.Any()) { return WebResponseContent.Instance.Error("无登录权限"); } string token = JwtHelper.IssueJwt(new TokenModelJwt() { UserId = user.UserId, RoleId = user.RoleId, UserName = user.UserName, TenantId = user.TenantId, }); _cacheService.AddOrUpdate(user.UserId.ToString(), token); content = WebResponseContent.Instance.OK(data: new { token, userName = user.UserTrueName, img = user.HeadImageUrl }); } 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 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); 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.User_Id == 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 usname,string oldPwd, string newPwd) { WebResponseContent content = WebResponseContent.Instance; newPwd = newPwd?.Trim(); string message = ""; try { if (string.IsNullOrEmpty(newPwd)) return WebResponseContent.Instance.Error("新密码不能为空"); if (newPwd.Length < 6) return WebResponseContent.Instance.Error("密码不能少于6位"); string _oldPwd = oldPwd.EncryptDES(AppSecret.User); Sys_User sys_User = BaseDal.QueryFirst(x => x.UserName == usname && x.UserPwd== _oldPwd); if(sys_User == null) { return WebResponseContent.Instance.Error("旧密码错误,请重新输入!"); } string _newPwd = newPwd.EncryptDES(AppSecret.User); sys_User.UserPwd = _newPwd; sys_User.LastModifyPwdDate = DateTime.Now; BaseDal.UpdateData(sys_User); content = WebResponseContent.Instance.OK("密码修改成功"); } catch (Exception ex) { message = ex.Message; content = WebResponseContent.Instance.Error("服务器了点问题,请稍后再试"); } return content; } /// /// 修改密码 /// /// /// public WebResponseContent ModifyPwd2(string oldPwd, string newPwd) { WebResponseContent content = WebResponseContent.Instance; newPwd = newPwd?.Trim(); string message = ""; try { if (string.IsNullOrEmpty(newPwd)) return WebResponseContent.Instance.Error("新密码不能为空"); if (newPwd.Length < 6) return WebResponseContent.Instance.Error("密码不能少于6位"); int userId = int.Parse(oldPwd); string _newPwd = newPwd.EncryptDES(AppSecret.User); BaseDal.UpdateData(new Sys_User { User_Id = 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 virtual PageGridData GetPageData(PageDataOptions options) { string wheres = options.ValidatePageOptions(TProperties); Sys_User sys_User = BaseDal.QueryFirst(x => x.UserName==App.User.UserName); List ints = new List(); int currentRoleId = sys_User.Role_Id; // 使用一个队列来处理所有的角色 Queue roleQueue = new Queue(); roleQueue.Enqueue(currentRoleId); // 把当前角色加入队列 while (roleQueue.Count > 0) { // 获取队列中的当前角色 currentRoleId = roleQueue.Dequeue(); // 查询当前角色下的所有子角色 var nextRoles = _sys_RoleService.Repository.QueryData(x => x.ParentId == currentRoleId); // 如果有子角色 if (nextRoles.Count > 0) { foreach (var item in nextRoles) { // 添加子角色的 RoleId 到列表 ints.Add(item.RoleId); // 将当前子角色的 RoleId 添加到队列中,以便后续处理 roleQueue.Enqueue(item.RoleId); } } } string roleIds = string.Join(",", ints); if (wheres != "") { wheres += $" and (Role_Id IN ({roleIds}) || UserName='{sys_User.UserName}')"; } else { wheres += $"(Role_Id IN ({roleIds})) or UserName='{sys_User.UserName}' "; } Dictionary orderbyDic = options.GetPageDataSort(TProperties); PageGridData pageGridData = new PageGridData(); pageGridData = BaseDal.QueryPage(wheres, options.Page, options.Rows, orderbyDic); return pageGridData; } } }