using Learun.Application.Base.AuthorizeModule; using Learun.Application.Base.SystemModule; using Learun.Application.Organization; using Learun.Cache.Base; using Learun.Cache.Factory; using System; using System.Collections.Generic; using System.Web; namespace Learun.Util.Operat { /// /// 版 本 Learun-ADMS V7.0.3 力软敏捷开发框架 /// Copyright (c) 2013-2018 上海力软信息技术有限公司 /// 创建人:力软-框架开发组 /// 日 期:2017.03.08 /// 描 述:当前连接用户信息处理类 /// public class OperatorHelper { #region 基础数据类 private UserIBLL userIBLL = new UserBLL(); private UserRelationIBLL userRelationIBLL = new UserRelationBLL(); private CompanyIBLL companyIBLL = new CompanyBLL(); private DepartmentIBLL departmentIBLL = new DepartmentBLL(); #endregion /// /// 缓存操作类 /// private ICache redisCache = CacheFactory.CaChe(); private string cacheKeyOperator = "learun_adms_operator_";// +登录者token private string cacheKeyToken = "learun_adms_token_";// +登录者token private string cacheKeyError = "learun_adms_error_";// + Mark private string cacheKeyInfo = "learun_adms_info_";// + Mark /// /// 秘钥 /// private string LoginUserToken = "Learun_ADMS_V7_Token"; /// /// 标记登录的浏览器 /// private string LoginUserMarkKey = "Learun_ADMS_V7_Mark"; /// /// 获取实例 /// public static OperatorHelper Instance { get { return new OperatorHelper(); } } /// /// 获取浏览器设配号 /// /// public string GetMark() { string cookieMark = WebHelper.GetCookie(LoginUserMarkKey).ToString(); if (string.IsNullOrEmpty(cookieMark)) { cookieMark = Guid.NewGuid().ToString(); WebHelper.WriteCookie(LoginUserMarkKey, cookieMark); } return cookieMark; } /// /// 登录者信息添加到缓存中 /// /// 账号 /// 应用id /// 设备标识 /// 是否保存cookie,默认是 /// public string AddLoginUser(string account, string mobileCode, string appId, string loginMark, bool cookie = true) { string token = Guid.NewGuid().ToString(); try { // 填写登录信息 Operator operatorInfo = new Operator(); operatorInfo.appId = appId; operatorInfo.account = account; operatorInfo.logTime = DateTime.Now; operatorInfo.iPAddress = Net.Ip; operatorInfo.browser = Net.Browser; operatorInfo.token = token; if (cookie) { string cookieMark = WebHelper.GetCookie(LoginUserMarkKey).ToString(); if (string.IsNullOrEmpty(cookieMark)) { operatorInfo.loginMark = Guid.NewGuid().ToString(); WebHelper.WriteCookie(LoginUserMarkKey, operatorInfo.loginMark); } else { operatorInfo.loginMark = cookieMark; } WebHelper.WriteCookie(LoginUserToken, token); } else { operatorInfo.loginMark = loginMark; } Dictionary tokenMarkList = redisCache.Read>(cacheKeyToken + account, CacheId.loginInfo); if (tokenMarkList == null)// 此账号第一次登录 { tokenMarkList = new Dictionary(); tokenMarkList.Add(operatorInfo.loginMark, token); } else { if (tokenMarkList.ContainsKey(operatorInfo.loginMark)) { tokenMarkList[operatorInfo.loginMark] = token; } else { tokenMarkList.Add(operatorInfo.loginMark, token); } } redisCache.Write>(cacheKeyToken + account, tokenMarkList, CacheId.loginInfo); redisCache.Write(cacheKeyOperator + operatorInfo.loginMark, operatorInfo, CacheId.loginInfo); return token; } catch (Exception) { throw; } } /// /// 登录者信息添加到缓存中 /// /// 账号 /// 应用id /// 设备标识 /// 是否保存cookie,默认是 /// public string AddLoginUser(string account, string appId, string loginMark, bool cookie = true) { string token = Guid.NewGuid().ToString(); try { // 填写登录信息 Operator operatorInfo = new Operator(); operatorInfo.appId = appId; operatorInfo.account = account; operatorInfo.logTime = DateTime.Now; operatorInfo.iPAddress = Net.Ip; operatorInfo.browser = Net.Browser; operatorInfo.token = token; //登录时间记录 redisCache.Write("logintime" + account, operatorInfo.logTime.ToDateTimeString(), CacheId.loginInfo); if (cookie) { string cookieMark = WebHelper.GetCookie(LoginUserMarkKey).ToString(); if (string.IsNullOrEmpty(cookieMark)) { operatorInfo.loginMark = Guid.NewGuid().ToString(); WebHelper.WriteCookie(LoginUserMarkKey, operatorInfo.loginMark); } else { operatorInfo.loginMark = cookieMark; } WebHelper.WriteCookie(LoginUserToken, token); //登录时间记录 WebHelper.WriteCookie("logintime" + account, operatorInfo.logTime.ToDateTimeString()); } else { operatorInfo.loginMark = loginMark; } Dictionary tokenMarkList = redisCache.Read>(cacheKeyToken + account, CacheId.loginInfo); if (tokenMarkList == null)// 此账号第一次登录 { tokenMarkList = new Dictionary(); tokenMarkList.Add(operatorInfo.loginMark, token); } else { if (tokenMarkList.ContainsKey(operatorInfo.loginMark)) { tokenMarkList[operatorInfo.loginMark] = token; } else { tokenMarkList.Add(operatorInfo.loginMark, token); } } redisCache.Write>(cacheKeyToken + account, tokenMarkList, CacheId.loginInfo); redisCache.Write(cacheKeyOperator + operatorInfo.loginMark, operatorInfo, CacheId.loginInfo); return token; } catch (Exception) { throw; } } /// /// 清空当前登录信息 /// public void EmptyCurrent() { try { string token = WebHelper.GetCookie(LoginUserToken).ToString(); string loginMark = WebHelper.GetCookie(LoginUserMarkKey).ToString(); EmptyCurrent(token, loginMark); } catch (Exception) { } } /// /// 清空当前登录信息 /// /// 登录票据 /// 登录设备标识 public void EmptyCurrent(string token, string loginMark) { try { Operator operatorInfo = redisCache.Read(cacheKeyOperator + loginMark, CacheId.loginInfo); if (operatorInfo != null && operatorInfo.token == token) { Dictionary tokenMarkList = redisCache.Read>(cacheKeyToken + operatorInfo.account, CacheId.loginInfo); tokenMarkList.Remove(loginMark); redisCache.Remove(cacheKeyOperator + loginMark, CacheId.loginInfo); //登录时间清除 redisCache.Remove("logintime" + operatorInfo.account, CacheId.loginInfo); WebHelper.RemoveCookie("logintime" + operatorInfo.account); redisCache.Write>(cacheKeyToken + operatorInfo.account, tokenMarkList, CacheId.loginInfo); } } catch (Exception) { } } /// /// 判断登录状态 /// /// -1未登录,1登录成功,0登录过期 public OperatorResult IsOnLine(string account) { try { string token = WebHelper.GetCookie(LoginUserToken).ToString(); string loginMark = WebHelper.GetCookie(LoginUserMarkKey).ToString(); return IsOnLine(token, loginMark, account); } catch (Exception) { return new OperatorResult { stateCode = -1 }; } } /// /// 判断登录状态 /// /// 登录票据 /// 登录设备标识 /// -1未登录,1登录成功,0登录过期 public OperatorResult IsOnLine(string token, string loginMark, string account = "") { OperatorResult operatorResult = new OperatorResult(); operatorResult.stateCode = -1; // -1未登录,1登录成功,0登录过期 try { if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(loginMark)) { return operatorResult; } Operator operatorInfo = redisCache.Read(cacheKeyOperator + loginMark, CacheId.loginInfo); if (operatorInfo != null && operatorInfo.token == token) { TimeSpan span = (TimeSpan)(DateTime.Now - operatorInfo.logTime); if (span.TotalHours >= 1200)// 登录操作过12小时移除 { operatorResult.stateCode = 0; Dictionary tokenMarkList = redisCache.Read>(cacheKeyToken + operatorInfo.account, CacheId.loginInfo); tokenMarkList.Remove(loginMark); redisCache.Write>(cacheKeyToken + operatorInfo.account, tokenMarkList, CacheId.loginInfo); redisCache.Remove(cacheKeyOperator + loginMark, CacheId.loginInfo); //登录时间清除 redisCache.Remove("logintime" + operatorInfo.account, CacheId.loginInfo); WebHelper.RemoveCookie("logintime" + operatorInfo.account); } else { string verifyonelogin = Util.Config.GetValue("verifyonelogin"); if (!string.IsNullOrEmpty(verifyonelogin) && verifyonelogin == "true" && operatorInfo.account != "") { if (operatorInfo.appId.Contains("PC")) { //多客户端登录判断 string logintime = redisCache.Read("logintime" + operatorInfo.account, CacheId.loginInfo); string cookielogintime = WebHelper.GetCookie("logintime" + operatorInfo.account); if (string.IsNullOrEmpty(logintime) || string.IsNullOrEmpty(cookielogintime) || logintime != cookielogintime) { operatorResult.stateCode = 0; return operatorResult; } } } if (!string.IsNullOrEmpty(account) && account != operatorInfo.account) { operatorResult.stateCode = 2; return operatorResult; } UserInfo userInfo = redisCache.Read(cacheKeyInfo + operatorInfo.account, CacheId.loginInfo); if (userInfo == null || userInfo.loadTime == null || userInfo.loadTime <= DateTime.Now.AddMinutes(-5)) { userInfo = new UserInfo(); userInfo.appId = operatorInfo.appId; userInfo.logTime = operatorInfo.logTime; userInfo.iPAddress = operatorInfo.iPAddress; userInfo.browser = operatorInfo.browser; userInfo.loginMark = operatorInfo.loginMark; userInfo.token = operatorInfo.token; userInfo.account = operatorInfo.account; UserEntity userEntity = userIBLL.GetEntityByAccount(operatorInfo.account); if (userEntity != null) { userInfo.userId = userEntity.F_UserId; userInfo.enCode = userEntity.F_EnCode; userInfo.password = userEntity.F_Password; userInfo.secretkey = userEntity.F_Secretkey; userInfo.realName = userEntity.F_RealName; userInfo.nickName = userEntity.F_NickName; userInfo.headIcon = userEntity.F_HeadIcon; userInfo.gender = userEntity.F_Gender; userInfo.mobile = userEntity.F_Mobile; userInfo.telephone = userEntity.F_Telephone; userInfo.email = userEntity.F_Email; userInfo.oICQ = userEntity.F_OICQ; userInfo.weChat = userEntity.F_WeChat; userInfo.companyId = userEntity.F_CompanyId; userInfo.departmentId = userEntity.F_DepartmentId; userInfo.openId = userEntity.F_OpenId; userInfo.isSystem = userEntity.F_SecurityLevel == 1 ? true : false; userInfo.Description = userEntity.F_Description; userInfo.roleIds = userRelationIBLL.GetObjectIds(userEntity.F_UserId, 1); if (!string.IsNullOrEmpty(userInfo.roleIds)) { var rolelist = userRelationIBLL.GetRoleListByUserId(userEntity.F_UserId); foreach (var roleEntity in rolelist) { userInfo.roleName += roleEntity.F_FullName + ","; } } userInfo.postIds = userRelationIBLL.GetObjectIds(userEntity.F_UserId, 2); userInfo.companyIds = companyIBLL.GetSubNodes(userEntity.F_CompanyId); userInfo.departmentIds = departmentIBLL.GetSubNodes(userEntity.F_CompanyId, userEntity.F_DepartmentId); userInfo.loadTime = DateTime.Now; if (HttpContext.Current != null) { HttpContext.Current.Items.Add("LoginUserInfo", userInfo); } operatorResult.userInfo = userInfo; operatorResult.stateCode = 1; redisCache.Write(cacheKeyInfo + operatorInfo.account, userInfo, CacheId.loginInfo); } else { operatorResult.stateCode = 0; } } else { userInfo.logTime = operatorInfo.logTime; if (HttpContext.Current != null) { HttpContext.Current.Items.Add("LoginUserInfo", userInfo); } operatorResult.userInfo = userInfo; operatorResult.stateCode = 1; } operatorResult.userInfo.token = operatorInfo.token; } } return operatorResult; } catch (Exception) { return operatorResult; } } #region 登录错误次数记录 /// /// 获取当前登录错误次数 /// /// public int GetCurrentErrorNum() { int res = 0; try { string cookieMark = WebHelper.GetCookie(LoginUserMarkKey).ToString(); if (string.IsNullOrEmpty(cookieMark)) { cookieMark = Guid.NewGuid().ToString(); WebHelper.WriteCookie(LoginUserMarkKey, cookieMark); } string num = redisCache.Read(cacheKeyError + cookieMark, CacheId.loginInfo); if (!string.IsNullOrEmpty(num)) { res = Convert.ToInt32(num); } } catch (Exception) { } return res; } /// /// 增加错误次数 /// /// public int AddCurrentErrorNum() { int res = 0; try { string cookieMark = WebHelper.GetCookie(LoginUserMarkKey).ToString(); if (string.IsNullOrEmpty(cookieMark)) { cookieMark = Guid.NewGuid().ToString(); WebHelper.WriteCookie(LoginUserMarkKey, cookieMark); } string num = redisCache.Read(cacheKeyError + cookieMark, CacheId.loginInfo); if (!string.IsNullOrEmpty(num)) { res = Convert.ToInt32(num); } res++; num = res + ""; redisCache.Write(cacheKeyError + cookieMark, num, CacheId.loginInfo); } catch (Exception) { } return res; } /// /// 清除当前登录错误次数 /// public void ClearCurrentErrorNum() { try { string cookieMark = WebHelper.GetCookie(LoginUserMarkKey).ToString(); if (string.IsNullOrEmpty(cookieMark)) { cookieMark = Guid.NewGuid().ToString(); WebHelper.WriteCookie(LoginUserMarkKey, cookieMark); } redisCache.Remove(cacheKeyError + cookieMark, CacheId.loginInfo); } catch (Exception) { } } #endregion #region 写入操作日志 /// /// 写操作日志 /// public void WriteOperateLog(OperateLogModel operateLogModel) { try { if (operateLogModel.userInfo == null) { operateLogModel.userInfo = LoginUserInfo.Get(); } LogEntity logEntity = new LogEntity(); logEntity.F_CategoryId = 3; logEntity.F_OperateTypeId = ((int)operateLogModel.type).ToString(); logEntity.F_OperateType = EnumAttribute.GetDescription(operateLogModel.type); logEntity.F_OperateAccount = operateLogModel.userInfo?.account + "(" + operateLogModel.userInfo.realName + ")"; logEntity.F_OperateUserId = operateLogModel.userInfo?.userId; logEntity.F_Module = operateLogModel.title; logEntity.F_ExecuteResult = 1; logEntity.F_ExecuteResultJson = "访问地址:" + operateLogModel.url; logEntity.F_SourceObjectId = operateLogModel.sourceObjectId; logEntity.F_SourceContentJson = operateLogModel.sourceContentJson; logEntity.F_Description = "PC端"; logEntity.WriteLog(); } catch (Exception) { } } /// /// 写操作日志 /// public void WriteOperateLog(OperateLogModel operateLogModel, string source) { try { if (operateLogModel.userInfo == null) { operateLogModel.userInfo = LoginUserInfo.Get(); } LogEntity logEntity = new LogEntity(); logEntity.F_CategoryId = 3; logEntity.F_OperateTypeId = ((int)operateLogModel.type).ToString(); logEntity.F_OperateType = EnumAttribute.GetDescription(operateLogModel.type); logEntity.F_OperateAccount = operateLogModel.userInfo?.account + "(" + operateLogModel.userInfo.realName + ")"; logEntity.F_OperateUserId = operateLogModel.userInfo.userId; logEntity.F_Module = operateLogModel.title; logEntity.F_ExecuteResult = 1; logEntity.F_ExecuteResultJson = "访问地址:" + operateLogModel.url; logEntity.F_SourceObjectId = operateLogModel.sourceObjectId; logEntity.F_SourceContentJson = operateLogModel.sourceContentJson; logEntity.F_Description = "PC端"; logEntity.WriteLog(); } catch (Exception) { } } #endregion } }