/// <summary> /// 获取AccessToken /// </summary> /// <returns></returns> public static string GetAccessToken() { QyAccessToken accessToken = null; bool isGet = false; if (CacheUtil.Exists("QyAccessToken")) { accessToken = CacheUtil.Get <QyAccessToken>("QyAccessToken"); if (accessToken.Expire_Time >= DateTime.Now.AddMinutes(1)) { isGet = true; } } if (!isGet) { string url = $"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={CorpID}&corpsecret={Secret}"; //请求获取 string res = RequestUtil.HttpGet(url); accessToken = JsonConvert.DeserializeObject <QyAccessToken>(res); if (accessToken != null && accessToken.ErrCode == 0) { accessToken.Expire_Time = DateTime.Now.AddSeconds(accessToken.Expires_In); CacheUtil.Set("QyAccessToken", accessToken, new TimeSpan(2, 0, 0)); } else { LogUtil.Error($"获取accesstoken失败——{accessToken.ErrCode},{accessToken.ErrMsg}"); } } return(accessToken.Access_Token); }
/// <summary> /// 多个登录设置缓存 /// </summary> /// <param name="token"></param> /// <param name="userid"></param> public static void SetUserToken(string token, int userid) { Dictionary <int, List <string> > dicusers = CacheUtil.Get <Dictionary <int, List <string> > >("UserToken"); if (dicusers == null) { dicusers = new Dictionary <int, List <string> >(); } List <string> listtokens = new List <string>(); if (dicusers.ContainsKey(userid)) { listtokens = dicusers[userid]; if (listtokens.Count <= 0) { listtokens.Add(token); } else { if (!AprilConfig.AllowMuiltiLogin) { foreach (var item in listtokens) { RemoveToken(item); } listtokens.Add(token); } else { bool isAdd = true; foreach (var item in listtokens) { if (item == token) { isAdd = false; } } if (isAdd) { listtokens.Add(token); } } } } else { listtokens.Add(token); dicusers.Add(userid, listtokens); } CacheUtil.Add("UserToken", dicusers, new TimeSpan(6, 0, 0), true); }
/// <summary> /// 多个登录获取 /// </summary> /// <param name="userid"></param> /// <returns></returns> public static List <string> GetUserToken(int userid) { Dictionary <int, List <string> > dicusers = CacheUtil.Get <Dictionary <int, List <string> > >("UserToken"); List <string> lists = new List <string>(); if (dicusers != null && dicusers.Count > 0) { foreach (var item in dicusers) { if (item.Key == userid) { lists = dicusers[userid]; break; } } } return(lists); }
/// <summary> /// 多个登录删除缓存 /// </summary> /// <param name="userid"></param> public static void RemoveUserToken(int userid) { Dictionary <int, List <string> > dicusers = CacheUtil.Get <Dictionary <int, List <string> > >("UserToken"); if (dicusers != null && dicusers.Count > 0) { if (dicusers.ContainsKey(userid)) { //删除所有token var listtokens = dicusers[userid]; foreach (var token in listtokens) { RemoveToken(token); } dicusers.Remove(userid); } } }
private static AdminEntity Get(string token) { AdminEntity admin = null; switch (AprilConfig.TokenCacheType) { case AprilEnums.TokenCacheType.Cookie: admin = CookieUtil.Get <AdminEntity>(token); break; case AprilEnums.TokenCacheType.Cache: admin = CacheUtil.Get <AdminEntity>(token); break; case AprilEnums.TokenCacheType.Session: admin = SessionUtil.Get <AdminEntity>(token); break; case AprilEnums.TokenCacheType.Redis: admin = RedisUtil.Get <AdminEntity>(token); break; } return(admin); }