Beispiel #1
0
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="taskId">任务标识</param>
        /// <param name="userInfo">用户</param>
        /// <param name="recordCount">记录数</param>
        /// <param name="pageNo">当前页</param>
        /// <param name="pageSize">每页显示</param>
        /// <param name="condition">条件</param>
        /// <param name="dbParameters">参数</param>
        /// <param name="order">排序</param>
        /// <returns>数据表</returns>
        public DataTable GetDataTableByPage(string taskId, BaseUserInfo userInfo, out int recordCount, int pageNo, int pageSize, string condition, List <KeyValuePair <string, object> > dbParameters, string order = null)
        {
            var result        = new DataTable(BaseLogonLogEntity.CurrentTableName);
            var myRecordCount = 0;

            var parameter = ServiceInfo.Create(taskId, userInfo, MethodBase.GetCurrentMethod());

            // 这里需要连接到登录日志数据库服务器
            ServiceUtil.ProcessLogonLogDb(userInfo, parameter, (dbHelper) =>
            {
                if (SecretUtil.IsSqlSafe(condition))
                {
                    var loginLogManager = new BaseLogonLogManager(dbHelper, userInfo);
                    result           = loginLogManager.GetDataTableByPage(out myRecordCount, pageNo, pageSize, condition, dbHelper.MakeParameters(dbParameters), order);
                    result.TableName = BaseLogonLogEntity.CurrentTableName;
                }
                else
                {
                    // 记录注入日志
                    LogUtil.WriteLog("userInfo:" + userInfo.Serialize() + " " + condition, "SqlSafe");
                }
            });
            recordCount = myRecordCount;

            return(result);
        }
        private static void AddLogTask(object param)
        {
            var tuple = param as Tuple <string, string, string, string, string, string, string, Tuple <string> >;

            if (tuple != null)
            {
                var systemCode    = tuple.Item1;
                var userId        = tuple.Item2;
                var userName      = tuple.Item3;
                var nickName      = tuple.Item4;
                var ipAddress     = tuple.Item5;
                var ipAddressName = tuple.Item6;
                var macAddress    = tuple.Item7;
                var loginStatus   = string.Empty;
                if (tuple.Rest != null)
                {
                    loginStatus = tuple.Rest.Item1;
                }

                var entity = new BaseLogonLogEntity
                {
                    SystemCode = systemCode,
                    UserId     = userId.ToInt(),
                    UserName   = userName,
                    //Troy.Cui 20160927
                    NickName      = nickName,
                    IpAddress     = ipAddress,
                    IpAddressName = ipAddressName,
                    MacAddress    = macAddress,
                    LogonStatus   = loginStatus,
                    LogLevel      = LogonStatusToLogLevel(loginStatus),
                    CreateTime    = DateTime.Now
                };

                var tableName = GetSplitTableName();
                using (var dbHelper = DbHelperFactory.Create(BaseSystemInfo.UserCenterDbType, BaseSystemInfo.UserCenterDbConnection))
                {
                    var loginLogManager = new BaseLogonLogManager(tableName);
                    try
                    {
                        // 2015-07-13 把登录日志无法正常写入的,进行日志记录
                        loginLogManager.Add(entity);
                    }
                    catch (Exception ex)
                    {
                        LogUtil.WriteLog("AddLogTask: 异常信息:" + ex.Message
                                         + Environment.NewLine + "错误源:" + ex.Source
                                         + Environment.NewLine + "堆栈信息:" + ex.StackTrace, "Log");
                    }
                }
            }
        }
        private static void AddLogTaskByBaseUserInfo(object param)
        {
            var tuple = param as Tuple <string, BaseUserInfo, string, string, string, string>;

            if (tuple != null)
            {
                var systemCode    = tuple.Item1;
                var userInfo      = tuple.Item2;
                var ipAddress     = tuple.Item3;
                var ipAddressName = tuple.Item4;
                var macAddress    = tuple.Item5;
                var loginStatus   = tuple.Item6;

                var entity = new BaseLogonLogEntity
                {
                    SystemCode    = systemCode,
                    UserId        = userInfo.UserId,
                    UserName      = userInfo.UserName,
                    NickName      = userInfo.NickName,
                    RealName      = userInfo.RealName,
                    CompanyId     = userInfo.CompanyId.ToInt(),
                    CompanyName   = userInfo.CompanyName,
                    CompanyCode   = userInfo.CompanyCode,
                    IpAddress     = ipAddress,
                    IpAddressName = ipAddressName,
                    MacAddress    = macAddress,
                    LogonStatus   = loginStatus,
                    LogLevel      = LogonStatusToLogLevel(loginStatus),
                    CreateTime    = DateTime.Now
                };

                var tableName = GetSplitTableName(userInfo);

                var loginLogManager = new BaseLogonLogManager(tableName);
                try
                {
                    // 2015-07-13 把登录日志无法正常写入的,进行日志记录
                    loginLogManager.Add(entity);
                }
                catch (Exception ex)
                {
                    LogUtil.WriteLog("AddLogTask: ipAddress:" + ipAddress + "macAddress:" + macAddress + "userName:"******"异常信息:" + ex.Message
                                     + Environment.NewLine + "错误源:" + ex.Source
                                     + Environment.NewLine + "堆栈信息:" + ex.StackTrace, "Log");
                }
            }
        }
        /// <summary>
        /// 用户退出
        /// </summary>
        /// <param name="openId">信令</param>
        /// <param name="systemCode">系统编码</param>
        /// <param name="ipAddress">IP地址</param>
        /// <param name="macAddress">MAC地址</param>
        /// <returns>影响行数</returns>
        public bool SignOut(string openId, string systemCode = "Base", string ipAddress = null, string macAddress = null)
        {
            var result = 0;

            // 应该进行一次日志记录
            // 从缓存读取、效率高
            if (!string.IsNullOrWhiteSpace(openId))
            {
                var userEntity = BaseUserManager.GetEntityByOpenIdByCache(openId);
                if (userEntity != null && userEntity.Id > 0)
                {
                    var ipAddressName = string.Empty;
                    if (!string.IsNullOrEmpty(ipAddress))
                    {
                        ipAddressName = IpUtil.GetInstance().FindName(ipAddress);
                    }

                    BaseLogonLogManager.AddLog(systemCode, userEntity, ipAddress, ipAddressName, macAddress, Status.SignOut.ToDescription(), 0, 1);

                    // 是否更新访问日期信息
                    if (!BaseSystemInfo.UpdateVisit)
                    {
                        return(result > 0);
                    }
                    // 最后一次登录时间
                    var sql = "UPDATE " + BaseUserLogonEntity.CurrentTableName + " SET " + BaseUserLogonEntity.FieldPreviousVisitTime + " = " + BaseUserLogonEntity.FieldLastVisitTime;
                    //Troy.Cui 2020-02-29用户退出时也强制OpenId重新生成,和登录时一样强制生成OpenId
                    sql += " , " + BaseUserLogonEntity.FieldOpenId + " = '" + Guid.NewGuid().ToString("N") + "'";
                    sql += ", " + BaseUserLogonEntity.FieldOpenIdTimeoutTime + " = " + DbHelper.GetDbNow();
                    sql += " , " + BaseUserLogonEntity.FieldUserOnline + " = 0 "
                           + " , " + BaseUserLogonEntity.FieldLastVisitTime + " = " + DbHelper.GetDbNow();
                    sql += "  WHERE " + BaseUserLogonEntity.FieldUserId + " = " + DbHelper.GetParameter(BaseUserEntity.FieldId);

                    var dbParameters = new List <IDbDataParameter>
                    {
                        DbHelper.MakeParameter(BaseUserEntity.FieldId, userEntity.Id)
                    };
                    result = ExecuteNonQuery(sql, dbParameters.ToArray());
                }
            }

            return(result > 0);
        }
        /// <summary>
        /// 增加日志
        /// </summary>
        /// <param name="systemCode">系统编码</param>
        /// <param name="userEntity"></param>
        /// <param name="ipAddress"></param>
        /// <param name="ipAddressName"></param>
        /// <param name="macAddress"></param>
        /// <param name="loginStatus"></param>
        /// <param name="operationType"></param>
        /// <param name="loginResult"></param>
        /// <param name="sourceType"></param>
        /// <param name="targetApplication"></param>
        /// <param name="targetIp"></param>
        /// <returns></returns>
        public static string AddLog(string systemCode, BaseUserEntity userEntity, string ipAddress, string ipAddressName, string macAddress, string loginStatus, int operationType = 1, int loginResult = 1, string sourceType = null, string targetApplication = null, string targetIp = null)
        {
            if (!BaseSystemInfo.RecordLogonLog)
            {
                return(string.Empty);
            }

            if (userEntity == null)
            {
                return(null);
            }

            var result = string.Empty;

            var entity = new BaseLogonLogEntity
            {
                SystemCode  = systemCode,
                UserId      = userEntity.Id,
                UserName    = userEntity.UserName,
                NickName    = userEntity.NickName,
                RealName    = userEntity.RealName,
                CompanyId   = userEntity.CompanyId,
                CompanyName = userEntity.CompanyName
            };

            if (BaseSystemInfo.OnInternet && userEntity.CompanyId > 0)
            {
                entity.CompanyCode = BaseOrganizationManager.GetCodeByCache(userEntity.CompanyId.ToString());
            }

            entity.Province = userEntity.Province;
            entity.City     = userEntity.City;

            entity.TargetApplication = targetApplication;
            entity.TargetIp          = targetIp;
            entity.SourceType        = sourceType;

            entity.IpAddress     = ipAddress;
            entity.IpAddressName = ipAddressName;
            entity.MacAddress    = macAddress;
            entity.LogonStatus   = loginStatus;

            entity.OperationType = operationType;
            entity.Result        = loginResult;

            entity.LogLevel   = LogonStatusToLogLevel(loginStatus);
            entity.CreateTime = DateTime.Now;

            var tableName = GetSplitTableName(userEntity);

            var loginLogManager = new BaseLogonLogManager(tableName);

            try
            {
                // 2015-07-13 把登录日志无法正常写入的,进行日志记录
                result = loginLogManager.Add(entity);
            }
            catch (Exception ex)
            {
                LogUtil.WriteLog("AddLogTask: 异常信息:" + ex.Message + "userName:"******"错误源:" + ex.Source
                                 + Environment.NewLine + "堆栈信息:" + ex.StackTrace, "Log");
            }

            return(result);
        }