OperatorProvider.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using LeaRun.Cache.Factory;
  2. using LeaRun.Util;
  3. using System;
  4. namespace LeaRun.Application.Code
  5. {
  6. /// <summary>
  7. /// 版 本 6.1
  8. /// Copyright (c) 2013-2016 上海力软信息技术有限公司
  9. /// 创建人:佘赐雄
  10. /// 日 期:2015.10.10
  11. /// 描 述:当前操作者回话
  12. /// </summary>
  13. public class OperatorProvider : OperatorIProvider
  14. {
  15. #region 静态实例
  16. /// <summary>
  17. /// 当前提供者
  18. /// </summary>
  19. public static OperatorIProvider Provider
  20. {
  21. get { return new OperatorProvider(); }
  22. }
  23. /// <summary>
  24. /// 给app调用
  25. /// </summary>
  26. public static string AppUserId
  27. {
  28. set;
  29. get;
  30. }
  31. #endregion
  32. /// <summary>
  33. /// 秘钥
  34. /// </summary>
  35. private string LoginUserKey = "Learun_LoginUserKey_2016_V6.1";
  36. /// <summary>
  37. /// 登陆提供者模式:Session、Cookie
  38. /// </summary>
  39. private string LoginProvider = Config.GetValue("LoginProvider");
  40. /// <summary>
  41. /// 写入登录信息
  42. /// </summary>
  43. /// <param name="user">成员信息</param>
  44. public virtual void AddCurrent(Operator user)
  45. {
  46. try
  47. {
  48. if (LoginProvider == "Cookie")
  49. {
  50. #region 解决cookie时,设置数据权限较多时无法登陆的bug
  51. CacheFactory.Cache().WriteCache(user.DataAuthorize, LoginUserKey, user.LogTime.AddHours(12));
  52. user.DataAuthorize = null;
  53. #endregion
  54. WebHelper.WriteCookie(LoginUserKey, DESEncrypt.Encrypt(user.ToJson()));
  55. }
  56. else
  57. {
  58. WebHelper.WriteSession(LoginUserKey, DESEncrypt.Encrypt(user.ToJson()));
  59. }
  60. CacheFactory.Cache().WriteCache(user.Token, user.UserId, user.LogTime.AddHours(12));
  61. }
  62. catch (Exception ex)
  63. {
  64. throw new Exception(ex.Message);
  65. }
  66. }
  67. /// <summary>
  68. /// 当前用户
  69. /// </summary>
  70. /// <returns></returns>
  71. public virtual Operator Current()
  72. {
  73. try
  74. {
  75. Operator user = new Operator();
  76. if (LoginProvider == "Cookie")
  77. {
  78. user = DESEncrypt.Decrypt(WebHelper.GetCookie(LoginUserKey).ToString()).ToObject<Operator>();
  79. #region 解决cookie时,设置数据权限较多时无法登陆的bug
  80. AuthorizeDataModel dataAuthorize = CacheFactory.Cache().GetCache<AuthorizeDataModel>(LoginUserKey);
  81. user.DataAuthorize = dataAuthorize;
  82. #endregion
  83. }
  84. else if (LoginProvider == "AppClient")
  85. {
  86. user = CacheFactory.Cache().GetCache<Operator>(AppUserId);
  87. }
  88. else
  89. {
  90. user = DESEncrypt.Decrypt(WebHelper.GetSession(LoginUserKey).ToString()).ToObject<Operator>();
  91. }
  92. return user;
  93. }
  94. catch (Exception ex)
  95. {
  96. throw new Exception(ex.Message);
  97. }
  98. }
  99. /// <summary>
  100. /// 删除登录信息
  101. /// </summary>
  102. public virtual void EmptyCurrent()
  103. {
  104. if (LoginProvider == "Cookie")
  105. {
  106. WebHelper.RemoveCookie(LoginUserKey.Trim());
  107. #region 解决cookie时,设置数据权限较多时无法登陆的bug
  108. CacheFactory.Cache().RemoveCache(LoginUserKey);
  109. #endregion
  110. }
  111. else
  112. {
  113. WebHelper.RemoveSession(LoginUserKey.Trim());
  114. }
  115. }
  116. /// <summary>
  117. /// 是否过期
  118. /// </summary>
  119. /// <returns></returns>
  120. public virtual bool IsOverdue()
  121. {
  122. try
  123. {
  124. object str = "";
  125. AuthorizeDataModel dataAuthorize = null;
  126. if (LoginProvider == "Cookie")
  127. {
  128. str = WebHelper.GetCookie(LoginUserKey);
  129. #region 解决cookie时,设置数据权限较多时无法登陆的bug
  130. dataAuthorize = CacheFactory.Cache().GetCache<AuthorizeDataModel>(LoginUserKey);
  131. if (dataAuthorize == null)
  132. {
  133. return true;
  134. }
  135. #endregion
  136. }
  137. else
  138. {
  139. str = WebHelper.GetSession(LoginUserKey);
  140. }
  141. if (str != null && str.ToString() != "")
  142. {
  143. return false;
  144. }
  145. else
  146. {
  147. return true;
  148. }
  149. }
  150. catch (Exception)
  151. {
  152. return true;
  153. }
  154. }
  155. /// <summary>
  156. /// 是否已登录
  157. /// </summary>
  158. /// <returns></returns>
  159. public virtual int IsOnLine()
  160. {
  161. Operator user = new Operator();
  162. if (LoginProvider == "Cookie")
  163. {
  164. user = DESEncrypt.Decrypt(WebHelper.GetCookie(LoginUserKey).ToString()).ToObject<Operator>();
  165. #region 解决cookie时,设置数据权限较多时无法登陆的bug
  166. AuthorizeDataModel dataAuthorize = CacheFactory.Cache().GetCache<AuthorizeDataModel>(LoginUserKey);
  167. user.DataAuthorize = dataAuthorize;
  168. #endregion
  169. }
  170. else
  171. {
  172. user = DESEncrypt.Decrypt(WebHelper.GetSession(LoginUserKey).ToString()).ToObject<Operator>();
  173. }
  174. object token = CacheFactory.Cache().GetCache<string>(user.UserId);
  175. if (token == null)
  176. {
  177. return -1;//过期
  178. }
  179. if (user.Token == token.ToString())
  180. {
  181. return 1;//正常
  182. }
  183. else
  184. {
  185. return 0;//已登录
  186. }
  187. }
  188. }
  189. }