Ejemplo n.º 1
0
        /// <summary>
        /// 通过OpenID来获取用户基本信息
        /// </summary>
        /// <param name="openID">openID</param>
        /// <returns></returns>
        public static WXUserInfo GetUserInfoByopenID(string openID)
        {
            WXUserInfo userInfo = null;

            try
            {
                var access_token = AccessTokenHandler.ManageAccessToken();
                var url          = string.Format(WXUrlConfig.UserInfo_Url, access_token, openID);
                LogHelper.Info(typeof(UserHandler), "获取用户基本信息,url:" + url);
                var jsonText = HttpHandler.GetUrl(url);
                LogHelper.Info(typeof(UserHandler), "获取用户基本信息,userinfo:" + jsonText);

                var jsonUser = JObject.Parse(jsonText);
                if (jsonUser["errcode"] != null)
                {
                    LogHelper.Info(typeof(UserHandler), "获取用户基本信息发生返回错误:" + jsonUser["errmsg"].ToString());
                }
                else
                {
                    userInfo = new WXUserInfo
                    {
                        subscribe      = jsonUser.Value <int>("subscribe"),
                        nickname       = jsonUser.Value <string>("nickname"),
                        headimgurl     = jsonUser.Value <string>("headimgurl"),
                        openid         = jsonUser.Value <string>("openid"),
                        city           = jsonUser.Value <string>("city"),
                        country        = jsonUser.Value <string>("country"),
                        province       = jsonUser.Value <string>("province"),
                        language       = jsonUser.Value <string>("language"),
                        unionid        = jsonUser.Value <string>("unionid"),
                        remark         = jsonUser.Value <string>("remark"),
                        sex            = jsonUser.Value <int>("sex"),
                        groupid        = jsonUser.Value <int>("groupid"),
                        tagid_list     = null,
                        subscribe_time = jsonUser.Value <int>("subscribe_time").ToDateTime(),
                    };
                    var tagidJArray = JArray.Parse(jsonUser["tagid_list"].ToString());
                    userInfo.tagid_list = new int[tagidJArray.Count];
                    for (var i = 0; i < tagidJArray.Count; i++)
                    {
                        userInfo.tagid_list[i] = (int)tagidJArray[i];
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(typeof(UserHandler), "通过OpenID来获取用户基本信息出错:" + ex.Message + ex.InnerException.Message);
            }
            return(userInfo);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 刷新网页授权的Access_Token
 /// </summary>
 /// <returns></returns>
 public static JObject RefreshAuthAccessToken(string appid, string refresh_Access_Token)
 {
     try
     {
         var jsonText = HttpHandler.GetUrl(string.Format(WXUrlConfig.Auth_Refresh_AccessToken_Url, appid, refresh_Access_Token));
         LogHelper.Info(typeof(AccessTokenHandler), "刷新网页授权的Access_Token:" + jsonText);
         var jsonObject = JObject.Parse(jsonText);
         return(jsonObject);
     }
     catch (Exception ex)
     {
         LogHelper.Error(typeof(AccessTokenHandler), "刷新网页授权的Access_Token发生代码错误!" + ex.Message);
     }
     return(null);
 }
Ejemplo n.º 3
0
        /// <summary>
        /// 获取基础Access_Token
        /// </summary>
        /// <returns></returns>
        private static string GetAccessToken()
        {
            string accessToken = null;

            try
            {
                var jsonText = HttpHandler.GetUrl(string.Format(WXUrlConfig.AccessToken_Url, BaseConfigurationManager.GetAppSettingsKeyValue("AppID"), BaseConfigurationManager.GetAppSettingsKeyValue("AppSecret")));
                LogHelper.Info(typeof(AccessTokenHandler), "获取基础Access_Token:" + jsonText);
                var jsonObject = JObject.Parse(jsonText);
                if (jsonObject["errcode"] != null)
                {
                    LogHelper.Info(typeof(AccessTokenHandler), "获取基础Access_Token发生返回错误:" + jsonObject["errmsg"].ToString());
                }
                else
                {
                    accessToken = jsonObject["access_token"].ToString();
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(typeof(AccessTokenHandler), "获取基础Access_Token发生代码错误!" + ex.Message);
            }
            return(accessToken);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 获取网页授权用户基本信息
        /// </summary>
        /// <param name="appid"></param>
        /// <param name="appsecret"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static string GetAuthUserInfo(string appid, string appsecret, string code)
        {
            try
            {
                var access_token_Object = AuthAccessTokenHandler.GetAuthAccessToken(appid, appsecret, code);
                if (access_token_Object != null)
                {
                    var access_token         = string.Empty;
                    var openid               = string.Empty;
                    var refresh_access_token = string.Empty;
                    if (access_token_Object["errcode"] != null)
                    {
                        LogHelper.Info(typeof(AccessTokenHandler), "获取网页授权Access_Token发生返回错误:" + access_token_Object["errmsg"].ToString());
                    }
                    else
                    {
                        openid               = access_token_Object["openid"].ToString();
                        access_token         = access_token_Object["access_token"].ToString();
                        refresh_access_token = access_token_Object["refresh_token"].ToString();
                    }

                    if (!string.IsNullOrEmpty(access_token))
                    {
                        var url = string.Format(WXUrlConfig.Auth_UserInfo_Url, access_token, openid);
                        LogHelper.Info(typeof(UserHandler), "获取网页授权用户基本信息,url:" + url);
                        var jsonText = HttpHandler.GetUrl(url);
                        LogHelper.Info(typeof(UserHandler), "获取网页授权用户基本信息,userinfo:" + jsonText);

                        var jsonUser = JObject.Parse(jsonText);
                        if (jsonUser["errcode"] != null)
                        {//刷新一次access_token
                            LogHelper.Info(typeof(UserHandler), "获取网页授权用户发生返回错误:" + jsonUser["errmsg"].ToString());
                            access_token_Object = AuthAccessTokenHandler.RefreshAuthAccessToken(appid, refresh_access_token);
                            LogHelper.Info(typeof(UserHandler), "刷新access_token");

                            if (access_token_Object["errcode"] != null)
                            {
                                LogHelper.Info(typeof(AccessTokenHandler), "获取网页授权Access_Token发生返回错误:" + access_token_Object["errmsg"].ToString());
                            }
                            else
                            {
                                openid               = access_token_Object["openid"].ToString();
                                access_token         = access_token_Object["access_token"].ToString();
                                refresh_access_token = access_token_Object["refresh_token"].ToString();
                            }

                            if (!string.IsNullOrEmpty(access_token))
                            {
                                var _url = string.Format(WXUrlConfig.Auth_UserInfo_Url, access_token, openid);
                                LogHelper.Info(typeof(UserHandler), "获取网页授权用户基本信息,url:" + _url);
                                var _jsonText = HttpHandler.GetUrl(_url);
                                LogHelper.Info(typeof(UserHandler), "获取网页授权用户基本信息,userinfo:" + _jsonText);
                            }
                        }

                        return(jsonText);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(typeof(UserHandler), "获取网页授权用户基本信息出错:" + ex.Message + ex.InnerException.Message);
            }
            return(string.Empty);
        }