LoginGUID.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.IO;
  6. using System.Timers;
  7. namespace SysDataLibs
  8. {
  9. public class LoginGUID : IDisposable
  10. {
  11. Timer _Timer = null;
  12. WSLoginSessionList _LoginSessonList = null;
  13. int _SessionExpiredInterval = AppEnv.SysSetObj.GetInt("SessionExpiredInterval");
  14. public LoginGUID()
  15. {
  16. _LoginSessonList = new WSLoginSessionList();
  17. _SessionExpiredInterval = AppEnv.SysSetObj.GetInt("SessionExpiredInterval");
  18. if (_SessionExpiredInterval <= 0)
  19. _SessionExpiredInterval = 20;
  20. _Timer = new Timer();
  21. _Timer.AutoReset = true;
  22. _Timer.Enabled = true;
  23. _Timer.Interval = 5000;
  24. _Timer.Elapsed += new ElapsedEventHandler(_Timer_Elapsed);
  25. _Timer.Start();
  26. }
  27. void _Timer_Elapsed(object sender, ElapsedEventArgs e)
  28. {
  29. lock (_LoginSessonList)
  30. {
  31. WSLoginSessionList loList = _LoginSessonList.GetExpiredLogin(_SessionExpiredInterval);
  32. if (loList.Count > 0)
  33. {
  34. foreach (WSLoginSession loSession in loList)
  35. {
  36. _LoginSessonList.Remove(loSession);
  37. }
  38. }
  39. }
  40. }
  41. public void AddOne(string guid, UserSession session)
  42. {
  43. _LoginSessonList.Add(guid, session);
  44. }
  45. public UserSession GetOne(string guid)
  46. {
  47. UserSession loRetVal = null;
  48. if (_LoginSessonList.ContainsGUID(guid))
  49. {
  50. _LoginSessonList[guid].RefreshLastAccessTime();
  51. loRetVal = _LoginSessonList[guid].UserSession;
  52. }
  53. return loRetVal;
  54. }
  55. public bool RemoveGUID(string guid)
  56. {
  57. bool lbRetVal = false;
  58. if (_LoginSessonList.ContainsGUID(guid))
  59. {
  60. lbRetVal = _LoginSessonList.Remove(_LoginSessonList[guid]);
  61. }
  62. return lbRetVal;
  63. }
  64. // 根据用户名称删除
  65. public void RemoveUser(string pcUserCode)
  66. {
  67. lock (_LoginSessonList)
  68. {
  69. WSLoginSessionList loList = _LoginSessonList.GetByUser(pcUserCode);
  70. if (loList.Count > 0)
  71. {
  72. foreach (WSLoginSession loSession in loList)
  73. {
  74. _LoginSessonList.Remove(loSession);
  75. }
  76. }
  77. }
  78. }
  79. #region IDisposable 成员
  80. public void Dispose()
  81. {
  82. if (_Timer != null)
  83. {
  84. _Timer.Close();
  85. _Timer.Dispose();
  86. }
  87. }
  88. #endregion
  89. // 单键模式
  90. private static LoginGUID _Instance = null;
  91. public static LoginGUID Instance
  92. {
  93. get
  94. {
  95. if (_Instance == null)
  96. {
  97. _Instance = new LoginGUID();
  98. }
  99. return _Instance;
  100. }
  101. }
  102. }
  103. public class WSLoginSessionList : List<WSLoginSession>
  104. {
  105. public void Add(string pcGUID, UserSession poUserSession)
  106. {
  107. if (!this.ContainsGUID(pcGUID))
  108. {
  109. this.Add(new WSLoginSession(pcGUID, poUserSession));
  110. }
  111. }
  112. public new void Add(WSLoginSession item)
  113. {
  114. if (!this.ContainsGUID(item.GUID))
  115. base.Add(item);
  116. }
  117. public WSLoginSession this[string pcGUID]
  118. {
  119. get
  120. {
  121. foreach (WSLoginSession loSession in this)
  122. {
  123. if (loSession.GUID == pcGUID)
  124. {
  125. return loSession;
  126. }
  127. }
  128. return null;
  129. }
  130. }
  131. public bool ContainsGUID(string pcGUID)
  132. {
  133. foreach (WSLoginSession loSession in this)
  134. {
  135. if (loSession.GUID == pcGUID)
  136. {
  137. return true;
  138. }
  139. }
  140. return false;
  141. }
  142. public WSLoginSessionList GetExpiredLogin(int pdExpiredTime)
  143. {
  144. WSLoginSessionList loRetVal = new WSLoginSessionList();
  145. foreach (WSLoginSession loLoginSession in this)
  146. {
  147. if (loLoginSession.IsExpired(pdExpiredTime))
  148. {
  149. loRetVal.Add(loLoginSession);
  150. }
  151. }
  152. return loRetVal;
  153. }
  154. public WSLoginSessionList GetByUser(string pcUserCode)
  155. {
  156. WSLoginSessionList loRetVal = new WSLoginSessionList();
  157. foreach (WSLoginSession loLoginSession in this)
  158. {
  159. if ((loLoginSession.UserSession != null) &&
  160. (loLoginSession.UserSession.UserInfo != null) &&
  161. (loLoginSession.UserSession.UserInfo.UserCode == pcUserCode))
  162. {
  163. loRetVal.Add(loLoginSession);
  164. }
  165. }
  166. return loRetVal;
  167. }
  168. }
  169. public class WSLoginSession
  170. {
  171. public WSLoginSession(string pcGUID, UserSession poUserSession)
  172. {
  173. _GUID = pcGUID;
  174. _UserSession = poUserSession;
  175. }
  176. string _GUID = "";
  177. public string GUID
  178. {
  179. get { return _GUID; }
  180. }
  181. DateTime _CreateTime = DateTime.Now;
  182. /// <summary>
  183. /// 创建时间
  184. /// </summary>
  185. public DateTime CreateTime
  186. {
  187. get { return _CreateTime; }
  188. }
  189. DateTime _LastTime = DateTime.Now;
  190. /// <summary>
  191. /// 最近一次访问时间
  192. /// </summary>
  193. public DateTime LastTime
  194. {
  195. get { return _LastTime; }
  196. }
  197. UserSession _UserSession = null;
  198. public UserSession UserSession
  199. {
  200. get { return _UserSession; }
  201. }
  202. public bool IsExpired(int pdExpiredTime)
  203. {
  204. TimeSpan loSpan = DateTime.Now - LastTime;
  205. return loSpan.Minutes > pdExpiredTime;
  206. }
  207. public void RefreshLastAccessTime()
  208. {
  209. _LastTime = DateTime.Now;
  210. }
  211. }
  212. }