Exemple #1
0
        /// <summary>
        /// 公众号 微信获取code回调地址
        /// </summary>
        /// <param name="redirectUrl">redirectUrl</param>
        /// <param name="wxConfig"></param>
        /// <param name="isApp">是否是APP端</param>
        /// <returns></returns>
        public string GetWeChatCode(string redirectUrl, WechatAccount wxConfig, bool isApp = true)
        {
            if (wxConfig == null)
            {
                throw new ApiException(-1, "账号不存在");
            }
            var url = "";

            if (isApp)
            {
                url = string.Format(_config.WechatConfig.WechatAppConnect, wxConfig.AppID, redirectUrl, "snsapi_userinfo");
            }
            else
            {
                url = string.Format(_config.WechatConfig.WechatPCConnect, wxConfig.AppID, redirectUrl, "snsapi_login");
            }

            return(url);
        }
Exemple #2
0
        /// <summary>
        /// 公众号/小程序 获取AccessToken
        /// </summary>
        /// <param name="wxConfig"></param>
        /// <param name="code">为空时获取的是JSAPI临时Token</param>
        /// <returns></returns>
        public async Task <WeChatAccessTokenInfo> GetWeChatAccessTokenAsync(WechatAccount wxConfig, string code = "")
        {
            if (wxConfig == null)
            {
                throw new ApiException(-1, "账号不存在");
            }
            string cacheKey = $"wechat:{wxConfig.AppID}";

            if (!string.IsNullOrEmpty(code))
            {
                cacheKey = $"{cacheKey}:{code}";
            }
            var result = _memoryCache.Get <WeChatAccessTokenInfo>(cacheKey);

            if (result != null)
            {
                return(result);
            }
            string apiUrl = string.IsNullOrEmpty(code) ?
                            string.Format(_config.WechatConfig.MiniTokenUrl, wxConfig.AppID, wxConfig.AppSecret) :
                            string.Format(_config.WechatConfig.WechatTokenUrl, wxConfig.AppID, wxConfig.AppSecret, code);

            using (var request = new HttpRequestMessage(HttpMethod.Get, apiUrl))
            {
                var responseMessage = await _httpClient.SendAsync(request);

                responseMessage.EnsureSuccessStatusCode();
                var json = await responseMessage.Content.ReadAsStringAsync();

                result = json.FromJson <WeChatAccessTokenInfo>();
                if (result != null && !string.IsNullOrEmpty(result.Access_Token))
                {
                    _memoryCache.Set(cacheKey, result, TimeSpan.FromSeconds(result.Expires_In - 5));
                }
            }
            return(result);
        }
Exemple #3
0
        /// <summary>
        /// 小程序 临时登录凭证
        /// </summary>
        /// <param name="code">用户登录小程序获取到的Code</param>
        /// <param name="wxConfig"></param>
        /// <returns></returns>
        public async Task <WeChatLoginInfo> LoginAsync(string code, WechatAccount wxConfig)
        {
            if (string.IsNullOrEmpty(code))
            {
                throw new ApiException(-1, "小程序未授权");
            }
            if (wxConfig == null)
            {
                throw new ApiException(-1, "小程序账号不存在");
            }
            var cacheKey = $"wechat:{wxConfig.AppID}:{code}";
            var result   = _memoryCache.Get <WeChatLoginInfo>(cacheKey);

            if (result != null)
            {
                return(result);
            }
            string apiUrl = string.Format(_config.WechatConfig.MiniSessionUrl, wxConfig.AppID, wxConfig.AppSecret, code);

            using (var request = new HttpRequestMessage(HttpMethod.Get, apiUrl))
            {
                var responseMessage = await _httpClient.SendAsync(request);

                responseMessage.EnsureSuccessStatusCode();
                var json = await responseMessage.Content.ReadAsStringAsync();

                result = json.FromJson <WeChatLoginInfo>();
                if (result == null || string.IsNullOrEmpty(result.session_key))
                {
                    throw new ApiException(-1, "小程序授权异常");
                }
                //小程序登录Code有效期只有5分钟
                _memoryCache.Set(cacheKey, result, TimeSpan.FromMinutes(5));
            }
            return(result);
        }