WebUser.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. using BizCom;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Web;
  6. namespace SiteCore
  7. {
  8. public class WebUser
  9. {
  10. public WebUser()
  11. {
  12. }
  13. //public WebUser(int uid)
  14. //{
  15. // UserID = uid;
  16. //}
  17. //Cache最后更新时间
  18. private static string LastCacheTime = "";
  19. public static Dictionary<string, WebUser> UserCache = new Dictionary<string, WebUser>();
  20. private static readonly List<string> userKeyList = new List<string>();
  21. private static readonly object usercache_Flag = 1;
  22. /// <summary>
  23. /// 添加用户Cache
  24. /// </summary>
  25. /// <param name="key"></param>
  26. /// <param name="value"></param>
  27. public static void AddUserCache(string key, WebUser value)
  28. {
  29. lock (usercache_Flag)
  30. {
  31. if (UserCache.Count > 2000)
  32. {
  33. UserCache.Remove(userKeyList[0]);
  34. userKeyList.RemoveAt(0);
  35. }
  36. if (!UserCache.ContainsKey(key))
  37. {
  38. UserCache.Add(key, value);
  39. if (!userKeyList.Contains(key)) userKeyList.Add(key);
  40. }
  41. else
  42. {
  43. UserCache[key] = value;
  44. }
  45. }
  46. }
  47. public static void RemoveUserCache(string key)
  48. {
  49. if (UserCache.ContainsKey(key))
  50. {
  51. UserCache.Remove(key);
  52. if (userKeyList.Contains(key)) userKeyList.Remove(key);
  53. }
  54. }
  55. //更新缓存时间,以便重新加载数据
  56. public static void RefresCacheTime()
  57. {
  58. LastCacheTime = DateTime.Now.ToString("yyMMddhhmm");
  59. }
  60. #region user
  61. /// <summary>
  62. /// 用户ID
  63. /// </summary>
  64. public int UserID
  65. {
  66. get
  67. {
  68. if (User != null)
  69. return User.ID;
  70. return 0;
  71. }
  72. }
  73. public string UserAcc
  74. {
  75. get
  76. {
  77. if (User != null)
  78. return User.Account;
  79. return "";
  80. }
  81. }
  82. public string UserName
  83. {
  84. get
  85. {
  86. if (User != null)
  87. return User.Name;
  88. return "";
  89. }
  90. }
  91. /// <summary>
  92. /// 会员实体
  93. /// </summary>
  94. public CeErpUser User { get; set; }
  95. public CeErpUserPost UserPost { get; set; }
  96. #endregion
  97. /// <summary>
  98. /// 缓存时间
  99. /// </summary>
  100. public string CachekTicks { get; set; }
  101. private static WebUser InitUser()
  102. {
  103. WebUser webUser = null;
  104. //获取COOKIES
  105. HttpCookie lcCookie = HttpContext.Current.Request.Cookies[webConfig.CookieName];
  106. if (lcCookie != null)
  107. {
  108. string name = HttpUtility.UrlDecode(lcCookie.Values["User"]);
  109. string ticket = lcCookie.Values["Ticket"];
  110. //string dl_ticket = erpRedis.RedisHelper.StringGet("dl_"+name);
  111. //string mullogin = lcCookie.Values["mullogin"];
  112. //if (mullogin == "0" || mullogin == "")
  113. //{
  114. // if (ticket != dl_ticket) return null;//被别的人登录了该账号
  115. //}
  116. //if (WebHelper.getLoginTicket(name) != ticket) return null;
  117. try
  118. {
  119. //判断缓存是否存在
  120. if (!string.IsNullOrEmpty(name) && UserCache.ContainsKey(name))
  121. {
  122. webUser = UserCache[name];
  123. }
  124. else
  125. {
  126. return SetUser(name, ticket);
  127. }
  128. }
  129. catch (Exception ex)
  130. {
  131. //SyLog.WriteLog("获取会员名发生错误(Name:" + name+",Ticket:"+ticket+")", ex);
  132. }
  133. }
  134. return webUser;
  135. }
  136. public static WebUser SetUser(string name, string ticket)
  137. {
  138. CeErpUser SUser = CeErpUser.GetUserByCookie(name, ticket);
  139. if (SUser != null)
  140. {
  141. WebUser webUser = new WebUser();
  142. //职位
  143. CeErpUserPost userPost = CeErpUserPost.GetByUserID(SUser.ID);
  144. //人员
  145. webUser.UserPost = userPost;
  146. //商家信息
  147. webUser.User = SUser;
  148. AddUserCache(name, webUser);
  149. return webUser;
  150. }
  151. return null;
  152. }
  153. public static WebUser GetUser()
  154. {
  155. WebUser user = null;
  156. //HttpContext.Current.Session["WEBUSER"] == null;
  157. if (HttpContext.Current.Items["WEBUSER"] == null)
  158. {
  159. user = InitUser();
  160. HttpContext.Current.Items["WEBUSER"] = user;
  161. }
  162. else
  163. {
  164. user = (WebUser)HttpContext.Current.Items["WEBUSER"];
  165. }
  166. return user;
  167. }
  168. #region 权限缓存
  169. private static object premissioncache_Flag = 1;
  170. private static Dictionary<string, DataTable> PermissionCache = new Dictionary<string, DataTable>();
  171. public static void ClearPermissionCache()
  172. {
  173. lock (premissioncache_Flag)
  174. {
  175. PermissionCache.Clear();
  176. }
  177. }
  178. public static void AddPermissionCache(string key, DataTable value)
  179. {
  180. lock (premissioncache_Flag)
  181. {
  182. if (!PermissionCache.ContainsKey(key))
  183. {
  184. PermissionCache.Add(key, value);
  185. }
  186. else
  187. {
  188. PermissionCache[key] = value;
  189. }
  190. }
  191. }
  192. public static void RemovePermissionCache(string key)
  193. {
  194. if (PermissionCache.ContainsKey(key))
  195. {
  196. PermissionCache.Remove(key);
  197. }
  198. }
  199. public static void UpdatePermissionCache(string key, DataTable dt)
  200. {
  201. if (PermissionCache.ContainsKey(key))
  202. {
  203. PermissionCache[key] = dt;
  204. }
  205. }
  206. public static DataTable GetPermission(string _pkey)
  207. {
  208. if (_pkey == "") return null;
  209. //string[] pArr = _pkey.Split('|');
  210. //string key = pArr[1].ToString();
  211. //if (pArr[0] == "5") key = "admin_5";
  212. if (PermissionCache.ContainsKey(_pkey))
  213. return PermissionCache[_pkey];
  214. else
  215. {
  216. lock (premissioncache_Flag)
  217. {
  218. DataTable dt = null;
  219. if (_pkey == "admin") dt = CeErpPermission.GetErpAllPermission();
  220. else dt = CeErpPermission.GetErpPermission(_pkey);
  221. AddPermissionCache(_pkey, dt);
  222. return dt;
  223. }
  224. }
  225. }
  226. #endregion
  227. }
  228. }