| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- using BizCom;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Web;
- namespace SiteCore
- {
- public class WebUser
- {
- public WebUser()
- {
- }
- //public WebUser(int uid)
- //{
- // UserID = uid;
- //}
- //Cache最后更新时间
- private static string LastCacheTime = "";
- public static Dictionary<string, WebUser> UserCache = new Dictionary<string, WebUser>();
- private static readonly List<string> userKeyList = new List<string>();
- private static readonly object usercache_Flag = 1;
- /// <summary>
- /// 添加用户Cache
- /// </summary>
- /// <param name="key"></param>
- /// <param name="value"></param>
- public static void AddUserCache(string key, WebUser value)
- {
- lock (usercache_Flag)
- {
- if (UserCache.Count > 2000)
- {
- UserCache.Remove(userKeyList[0]);
- userKeyList.RemoveAt(0);
- }
- if (!UserCache.ContainsKey(key))
- {
- UserCache.Add(key, value);
- if (!userKeyList.Contains(key)) userKeyList.Add(key);
- }
- else
- {
- UserCache[key] = value;
- }
- }
- }
- public static void RemoveUserCache(string key)
- {
- if (UserCache.ContainsKey(key))
- {
- UserCache.Remove(key);
- if (userKeyList.Contains(key)) userKeyList.Remove(key);
- }
- }
- //更新缓存时间,以便重新加载数据
- public static void RefresCacheTime()
- {
- LastCacheTime = DateTime.Now.ToString("yyMMddhhmm");
- }
- #region user
- /// <summary>
- /// 用户ID
- /// </summary>
- public int UserID
- {
- get
- {
- if (User != null)
- return User.ID;
- return 0;
- }
- }
- public string UserAcc
- {
- get
- {
- if (User != null)
- return User.Account;
- return "";
- }
- }
- public string UserName
- {
- get
- {
- if (User != null)
- return User.Name;
- return "";
- }
- }
- /// <summary>
- /// 会员实体
- /// </summary>
- public CeErpUser User { get; set; }
- public CeErpUserPost UserPost { get; set; }
- #endregion
- /// <summary>
- /// 缓存时间
- /// </summary>
- public string CachekTicks { get; set; }
- private static WebUser InitUser()
- {
- WebUser webUser = null;
- //获取COOKIES
- HttpCookie lcCookie = HttpContext.Current.Request.Cookies[webConfig.CookieName];
- if (lcCookie != null)
- {
- string name = HttpUtility.UrlDecode(lcCookie.Values["User"]);
- string ticket = lcCookie.Values["Ticket"];
- //string dl_ticket = erpRedis.RedisHelper.StringGet("dl_"+name);
- //string mullogin = lcCookie.Values["mullogin"];
- //if (mullogin == "0" || mullogin == "")
- //{
- // if (ticket != dl_ticket) return null;//被别的人登录了该账号
- //}
- //if (WebHelper.getLoginTicket(name) != ticket) return null;
- try
- {
- //判断缓存是否存在
- if (!string.IsNullOrEmpty(name) && UserCache.ContainsKey(name))
- {
- webUser = UserCache[name];
- }
- else
- {
- return SetUser(name, ticket);
- }
- }
- catch (Exception ex)
- {
- //SyLog.WriteLog("获取会员名发生错误(Name:" + name+",Ticket:"+ticket+")", ex);
- }
- }
- return webUser;
- }
- public static WebUser SetUser(string name, string ticket)
- {
- CeErpUser SUser = CeErpUser.GetUserByCookie(name, ticket);
- if (SUser != null)
- {
- WebUser webUser = new WebUser();
- //职位
- CeErpUserPost userPost = CeErpUserPost.GetByUserID(SUser.ID);
- //人员
- webUser.UserPost = userPost;
- //商家信息
- webUser.User = SUser;
- AddUserCache(name, webUser);
- return webUser;
- }
- return null;
- }
- public static WebUser GetUser()
- {
- WebUser user = null;
- //HttpContext.Current.Session["WEBUSER"] == null;
- if (HttpContext.Current.Items["WEBUSER"] == null)
- {
- user = InitUser();
- HttpContext.Current.Items["WEBUSER"] = user;
- }
- else
- {
- user = (WebUser)HttpContext.Current.Items["WEBUSER"];
- }
- return user;
- }
- #region 权限缓存
- private static object premissioncache_Flag = 1;
- private static Dictionary<string, DataTable> PermissionCache = new Dictionary<string, DataTable>();
- public static void ClearPermissionCache()
- {
- lock (premissioncache_Flag)
- {
- PermissionCache.Clear();
- }
- }
- public static void AddPermissionCache(string key, DataTable value)
- {
- lock (premissioncache_Flag)
- {
- if (!PermissionCache.ContainsKey(key))
- {
- PermissionCache.Add(key, value);
- }
- else
- {
- PermissionCache[key] = value;
- }
- }
- }
- public static void RemovePermissionCache(string key)
- {
- if (PermissionCache.ContainsKey(key))
- {
- PermissionCache.Remove(key);
- }
- }
- public static void UpdatePermissionCache(string key, DataTable dt)
- {
- if (PermissionCache.ContainsKey(key))
- {
- PermissionCache[key] = dt;
- }
- }
- public static DataTable GetPermission(string _pkey)
- {
- if (_pkey == "") return null;
- //string[] pArr = _pkey.Split('|');
- //string key = pArr[1].ToString();
- //if (pArr[0] == "5") key = "admin_5";
- if (PermissionCache.ContainsKey(_pkey))
- return PermissionCache[_pkey];
- else
- {
- lock (premissioncache_Flag)
- {
- DataTable dt = null;
- if (_pkey == "admin") dt = CeErpPermission.GetErpAllPermission();
- else dt = CeErpPermission.GetErpPermission(_pkey);
- AddPermissionCache(_pkey, dt);
- return dt;
- }
- }
- }
- #endregion
- }
- }
|