日期:2014-05-18 浏览次数:20459 次
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; using System.Configuration; using SDT.Info.Active; using SDT.Info; namespace SDT.Active.ActiveUser { /// <summary> /// PassPort 存储在线用户列表。 /// </summary> public class PassPort { private static SortedList<int,ActiveUserInfo> _activeusers; private static List<UserEmployeeInfo> _userInfos; private static PassPort _instance; private static readonly object obj = new object(); /// <summary> /// 初始化在线用户表。 /// </summary> private void userstableFormat() { if (_activeusers == null) { _activeusers = new SortedList<int, ActiveUserInfo>(); _userInfos = new List<UserEmployeeInfo>(); } } public static PassPort Instance() { if (_instance == null) { _instance = new PassPort(); } return _instance; } public int ActiveTimeout { get { return Config.SysBaseConfig.GetInstance().ServoActiveTimeout; } } public int RefreshTimeout { get { return Config.SysBaseConfig.GetInstance().ServoActiveTimeout * 2; } } private PassPort() { userstableFormat(); } /// <summary> /// 获取全部用户列表 /// </summary> /// <returns></returns> public List<UserEmployeeInfo> GetActiveUsers() { return _userInfos; } /// <summary> /// 获取在线用户 /// </summary> /// <param name="sysNo">用户SysNo</param> /// <returns></returns> public UserEmployeeInfo GetActiveUserBySysNo(int sysNo) { return _userInfos.First( p => p.SysNo == sysNo); } /// <summary> /// 用户集合 /// </summary> /// <param name="userAttr">用户属性</param> /// <returns></returns> public List<UserEmployeeInfo> GetActiveUsersByUserAttr(AppEnum.UserAttr userAttr) { return _userInfos.Where(p => p.Attr == userAttr).ToList<UserEmployeeInfo>(); } /// <summary> /// 新用户登陆。 /// </summary> public void Login(UserEmployeeInfo userInfo) { DelTimeOut(); //清除超时用户 //若是单人登陆则注销原来登陆的用户 this.Logout(userInfo.SysNo); try { ActiveUserInfo activeUserInfo = new ActiveUserInfo(userInfo.SysNo,userInfo); _activeusers.Add(userInfo.SysNo, activeUserInfo); _userInfos.Add(userInfo); } catch (Exception e) { throw (new Exception(e.Message)); } } /// <summary> ///用户注销,根据Ticket或UserName。 /// </summary> public void Logout(int userSysNo) { DelTimeOut(); //清除超时用户 _activeusers.Remove(userSysNo); } /// <summary> ///清除超时用户。 /// </summary> private bool DelTimeOut() { DateTime dtNow = DateTime.Now; List<ActiveUserInfo> removeActiveUser = _activeusers.Values.Where(p => DateTime.Compare(p.RefreshTime, dtNow.AddSeconds(- RefreshTimeout)) < 0).ToList<ActiveUserInfo>(); foreach (ActiveUserInfo activeUser in removeActiveUser)