Exemple #1
0
        /// <summary>
        /// 获取第三方平台的全局唯一票据。
        /// </summary>
        /// <param name="ignoreCached">是否忽略缓存。</param>
        /// <returns>第三方用户唯一凭证密钥,即appsecret。</returns>
        public AccessTokenModel GetAccessToken(bool ignoreCached = false)
        {
            Func <AccessTokenModel> get = () => WeiXinHttpHelper.PostResultByJson <AccessTokenModel>(
                "https://api.weixin.qq.com/cgi-bin/component/api_component_token",
                new
            {
                component_appid         = _accountModel.AppId,
                component_appsecret     = _accountModel.AppSecret,
                component_verify_ticket = _accountModel.GetVerifyTicket()
            });

            //是否需要重新获取(无效、忽略缓存、过期)
            Func <bool> needGet = () => _accessTokenModel == null || ignoreCached || _accessTokenModel.IsExpired();

            if (needGet())
            {
                lock (this)
                {
                    if (needGet())
                    {
                        /*                        var newModel = get();
                         *                      if (_accessTokenModel != null && _accessTokenModel.AccessToken == newModel.AccessToken)
                         *                          return _accessTokenModel;
                         *                      return _accessTokenModel = newModel;*/
                        return(_accessTokenModel = get());
                    }
                }
            }

            return(_accessTokenModel);
        }
        /// <summary>
        /// 获取第三方平台的全局唯一票据。
        /// </summary>
        /// <param name="ignoreCached">是否忽略缓存。</param>
        /// <returns>第三方用户唯一凭证密钥,即appsecret。</returns>
        public AccessTokenModel GetAccessToken(bool ignoreCached = false)
        {
            Func <AccessTokenModel> get = () => WeiXinHttpHelper.PostResultByJson <AccessTokenModel>(
                "https://api.weixin.qq.com/cgi-bin/component/api_component_token",
                new
            {
                component_appid         = _accountModel.AppId,
                component_appsecret     = _accountModel.AppSecret,
                component_verify_ticket = _accountModel.GetVerifyTicket()
            });

            if (_accessTokenModel == null || _accessTokenModel.IsExpired() || ignoreCached)
            {
                var newModel = get();
                if (_accessTokenModel != null && _accessTokenModel.AccessToken == newModel.AccessToken)
                {
                    return(_accessTokenModel);
                }
                return(_accessTokenModel = newModel);
            }

            return(_accessTokenModel);
        }