public static JsapiTicket GetJsapiTicket(bool isQueryAgin = false) { string cacheKey = GetJsapiTicketCacheKey(); JsapiTicket ticket = CacheEx.Instance.Get <JsapiTicket>(cacheKey); if (!isQueryAgin && ticket != null && !ticket.IsExpired) { return(ticket); } ticket = GetJsapiTicketByWx(cacheKey, isQueryAgin); return(ticket); }
private static JsapiTicket GetJsapiTicketByWx(string cacheKey, bool isQueryAgin = false) { lock (lockObj) { JsapiTicket ticket = CacheEx.Instance.Get <JsapiTicket>(cacheKey); if (!isQueryAgin && ticket != null && !ticket.IsExpired) { return(ticket); } var jsapiTicketRepository = GetJsapiTicketRepository(); ticket = jsapiTicketRepository.Get(); if (ticket == null || (ticket != null && ticket.IsExpired)) { var res = new GetJsapiTicketRequest().Request(); //如果报错,重新获取Accesstoken if (res.ErrCode == "40001") { WechatAPI.Public.Wechat.AccessToken.AccessTokenManager.GetAccessToken(true); res = new GetJsapiTicketRequest().Request(); } if (res.IsSuccess) { ticket = new JsapiTicket(res); CacheEx.Instance.Add(cacheKey, ticket, ticket.ExpireTime); jsapiTicketRepository.Update(ticket); } } else { CacheEx.Instance.Add(cacheKey, ticket, ticket.ExpireTime); } return(ticket); } }