示例#1
0
        public AccessToken GetAccessTokenSync()
        {
            var connectProvider = ServiceLocator.GetInstance <IWeixinConnectProvider>();

            if (connectProvider == null)
            {
                throw new Exception("未注册IWeixinConnectProvider接口的实现类");
            }
            var token = connectProvider.GetAccessToken(CurMPAccount.AppId);

            if (token != null && DateTime.Now < token.ExpireTime)
            {
                return(token);
            }

            //单线程执行
            lock (TokenRefLock)
            {
                //检查是否已被其他线程刷新
                token = connectProvider.GetAccessToken(CurMPAccount.AppId);
                if (token != null && DateTime.Now < token.ExpireTime)
                {
                    return(token);
                }

                //get the new token
                var    url    = $"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={CurMPAccount.AppId}&secret={CurMPAccount.AppSecret}";
                string error  = null;
                var    resStr = WebReqUtil.GetRequest(url, out error);
                if (!string.IsNullOrEmpty(error) || string.IsNullOrEmpty(resStr))
                {
                    LogService.ErrorFormat("get access token error:{0}", error);
                    return(null);
                }
                LogService.DebugFormat("get access token result:{0}", resStr);

                var resObj = JsonConvert.DeserializeObject <ErrorDes>(resStr);
                if (resObj.errcode > 0)
                {
                    LogService.DebugFormat("get access token failed:{0}", resStr);
                }

                var res = JsonConvert.DeserializeAnonymousType(resStr, new { access_token = string.Empty, expires_in = 0 });
                if (res == null)
                {
                    return(null);
                }
                //store new token
                token = new AccessToken
                {
                    AppId      = CurMPAccount.AppId,
                    Token      = res.access_token,
                    ExpireTime = DateTime.Now.AddSeconds(res.expires_in)
                };
                connectProvider.StoreAccessToken(token);
            }

            return(token);
        }
示例#2
0
        public string GetJsApiTicket()
        {
            var connectProvider = ServiceLocator.GetInstance <IWeixinConnectProvider>();

            if (connectProvider == null)
            {
                throw new Exception("未注册IWeixinConnectProvider接口的实现类");
            }
            var token = connectProvider.GetJsApiTicket(CurMPAccount.AppId);

            if (token != null && DateTime.Now < token.ExpireTime)
            {
                return(token.Token);
            }

            //单线程执行
            lock (JsTicketRefLock)
            {
                //检查是否已被其他线程刷新
                token = connectProvider.GetJsApiTicket(CurMPAccount.AppId);
                if (token != null && DateTime.Now < token.ExpireTime)
                {
                    return(token.Token);
                }

                //get the new ticket
                var    url = string.Format("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={0}&type=jsapi", GetAccessToken());
                string error;
                var    resStr = WebReqUtil.GetRequest(url, out error);
                if (!string.IsNullOrEmpty(error) || string.IsNullOrEmpty(resStr))
                {
                    LogService.ErrorFormat("Wechat fail get js ticket:{0}", error);
                    return(null);
                }
                var resObj = JsonConvert.DeserializeObject <ErrorDes>(resStr);
                if (resObj.errcode > 0)
                {
                    LogService.DebugFormat("Wechat fail get js ticket:{0}", resStr);
                }

                var res = JsonConvert.DeserializeAnonymousType(resStr, new { ticket = string.Empty, expires_in = 0 });
                if (res == null)
                {
                    return(null);
                }
                //store new ticket
                token = new AccessToken
                {
                    AppId      = CurMPAccount.AppId,
                    Token      = res.ticket,
                    ExpireTime = DateTime.Now.AddSeconds(res.expires_in)
                };
                connectProvider.StoreJsApiTicket(token);
            }

            return(token.Token);
        }