后台单用户在线,简单处理
/// <summary> /// 在线处理数据结构 /// </summary> [Serializable] public class OnlineData { /// <summary> /// 在线用户ID /// </summary> public int UserID { get; set; } /// <summary> /// 上线时间 /// </summary> public string Timer { get; set; } }
方法预览
1.当前程序所有的在线用户,单例存在
/// <summary> /// 后台单用户在线,简单处理 /// </summary> public class B_UserOnline { /// <summary> /// 存储已经登录的用户UserID,和对应的登录时间 /// </summary> private static List<OnlineData> _UserIDList = new List<OnlineData>(); private IB_User IB_User = ConcreteFactory.GetB_User(); #region 获取 /// <summary> /// 获取上线用户数量 /// </summary> /// <returns></returns> public int GetCount() { return _UserIDList.Count; } /// <summary> /// 获取登录用户的信息UserID+登录编号 /// </summary> /// <returns></returns> public static List<OnlineData> GetList() { return _UserIDList; } /// <summary> /// 获取上线用户ID列表 /// </summary> /// <returns></returns> public List<int> GetUserIDList() { return _UserIDList.Select(q => q.UserID).ToList() ; } /// <summary> /// 获取数据项 /// </summary> /// <param name="UserID">用户ID</param> /// <returns></returns> public static OnlineData GetByUserID(int UserID) { return _UserIDList.FirstOrDefault(q => q.UserID == UserID); } /// <summary> /// 获取在线用户对象列表 /// </summary> /// <returns></returns> public List<B_User> GetUserList() { List<B_User> userList = new List<B_User>(); foreach (var item in _UserIDList) { B_User user = IB_User.GetByID(item.UserID); if (user != null) userList.Add(user); } return userList; } #endregion #region 操作 /// <summary> /// 判断用户是否已经上线 /// </summary> /// <param name="UserID">上线用户ID</param> /// <returns></returns> public bool IsOnline(int UserID, string Timer) { return _UserIDList.Any(q => q.UserID == UserID && q.Timer == Timer); } /// <summary> /// 判断用户是否在线上 /// </summary> /// <param name="UserID"></param> /// <returns></returns> public bool IsOnline(int UserID) { return _UserIDList.Any(q => q.UserID == UserID); } /// <summary> /// 添加上线用户 /// </summary> /// <param name="UserID">上线用户ID</param> public bool Add(int UserID, string Timer) { if (IsOnline(UserID, Timer) == false) { _UserIDList.Add(new OnlineData() { UserID=UserID, Timer=Timer }); return true; } return false; } /// <summary> /// 添加上线用户 /// </summary> /// <param name="user">用户对象</param> /// <param name="auto">是否自动登录</param> /// <returns></returns> public bool Add(B_User user, string auto = "0") { lock (this.GetType()) { //如果已经在线,返回登录失败 if (IsOnline(user.UserID)) return false; //1.生成用户的登录编号,用户登录上线 //int Timer = Convert.ToInt32(DateTime.Now.ToString("MMddhhmmss")); string Timer = DateTime.Now.ToString("yyyyMMddhhmmssms"); Add(user.UserID, Timer); //2.保存信息,到客户端 //保存用户ID,用户名 到浏览器 B_UserHelper.SetCookie(user, Timer, auto); //3.添加登录记录 B_UserHelper.AddLoginInfo(user.UserID); return true; } } /// <summary> /// 删除用户,下线 /// </summary> /// <param name="UserID">下线用户ID</param> /// <param name="isClear">指定是否,清空用户的客户端信息</param> public bool Remove(int UserID, bool isClear = false) { if (IsOnline(UserID)) { //2.添加退出记录 B_UserHelper.AddLogoffInfo(UserID); //1.移除在线用户 _UserIDList.Remove(GetByUserID(UserID)); //3.清除cookie if (isClear) B_UserHelper.ClearCookie(); return true; } return false; } #endregion #region 网站启动操作 /// <summary> /// 网站停止,保存数据到磁盘 /// </summary> public void SaveList() { string filename = GetFileName(); try { XmlSerializerHelper _serialize = new XmlSerializerHelper(filename); _serialize.XmlSerialize<List<OnlineData>>(_UserIDList); } catch (Exception ex) { throw ex; } } /// <summary> /// 网站启动,从磁盘读取数据 /// </summary> public void LoadList() { string filename = GetFileName(); try { XmlSerializerHelper _serialize = new XmlSerializerHelper(filename); _UserIDList = _serialize.XmlDeserialize<List<OnlineData>>(); } catch (Exception) { _UserIDList = new List<OnlineData>(); } } /// <summary> /// 存储的文件位置 /// </summary> /// <returns></returns> private string GetFileName() { string filename = ConfigValue.DataPath + "userList.xml"; //string filename =""; if (File.Exists(filename) == false) { File.Create(filename).Close(); } return filename; } #endregion /// <summary> /// 在线处理数据机构 /// </summary> [Serializable] public class OnlineData { /// <summary> /// 在线用户ID /// </summary> public int UserID { get; set; } /// <summary> /// 上线时间 /// </summary> public string Timer { get; set; } } }