Beispiel #1
0
        /// <summary>
        /// 获取授权码。
        /// </summary>
        /// <param name="ignoreCached">是否忽略缓存。</param>
        /// <returns>授权码结果。</returns>
        public AuthorizeCodeResult GetAuthorizeCode(bool ignoreCached = false)
        {
            Func <string, AuthorizeCodeResult> get = accessToken => WeiXinHttpHelper.PostResultByJson <AuthorizeCodeResult>(
                "https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=" +
                accessToken, new { component_appid = _accountModel.AppId });

            if (_authorizeCodeResult == null || _authorizeCodeResult.IsExpired() || ignoreCached)
            {
                AuthorizeCodeResult newModel;
                try
                {
                    newModel = get(GetAccessToken().AccessToken);
                }
                catch (WeiXinException exception)
                {
                    if (exception.ErrorCode == 40001 ||
                        exception.Message == "invalid credential, access_token is invalid or not latest")
                    {
                        newModel = get(GetAccessToken(true).AccessToken);
                    }
                    else
                    {
                        throw;
                    }
                }
                if (_authorizeCodeResult != null && _authorizeCodeResult.AuthCode == newModel.AuthCode)
                {
                    return(_authorizeCodeResult);
                }
                return(_authorizeCodeResult = newModel);
            }
            return(_authorizeCodeResult);
        }
Beispiel #2
0
        /// <summary>
        /// 获取授权码。
        /// </summary>
        /// <param name="ignoreCached">是否忽略缓存。</param>
        /// <returns>授权码结果。</returns>
        public AuthorizeCodeResult GetAuthorizeCode(bool ignoreCached = false)
        {
            Func<string, AuthorizeCodeResult> get = accessToken => WeiXinHttpHelper.PostResultByJson<AuthorizeCodeResult>(
                 "https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=" +
                 accessToken, new { component_appid = _accountModel.AppId });

            if (_authorizeCodeResult == null || _authorizeCodeResult.IsExpired() || ignoreCached)
            {
                AuthorizeCodeResult newModel;
                try
                {
                    newModel = get(GetAccessToken().AccessToken);
                }
                catch (WeiXinException exception)
                {
                    if (exception.ErrorCode == 40001 ||
                        exception.Message == "invalid credential, access_token is invalid or not latest")
                    {
                        newModel = get(GetAccessToken(true).AccessToken);
                    }
                    else
                    {
                        throw;
                    }
                }
                if (_authorizeCodeResult != null && _authorizeCodeResult.AuthCode == newModel.AuthCode)
                    return _authorizeCodeResult;
                return _authorizeCodeResult = newModel;
            }
            return _authorizeCodeResult;
        }