/// <summary> /// 通过openID获取用户信息 /// </summary> /// <param name="openID"></param> /// <returns></returns> public UserInfo getUserInfo(string openID, string AppID = "", int count = 3) { Config config = getConfig(AppID); UserInfo userInfo = new UserInfo(); do { try { string res = ""; res = HttpWebResponseUtility.CreateGetHttpResponse(string.Format(Const.user_info + "{0}&openid={1}&lang=zh_CN", config.accessToken, openID), 5000, null, null); userInfo = JsonConvert.DeserializeObject <UserInfo>(res); if (CheckSuccess(userInfo.errcode)) { count = 0; } } catch (Exception ex) { config = getConfig(AppID, true); log.Info(ex.Message); } } while (count-- > 0); return(userInfo); }
/// <summary> /// 通过code来获取openID /// </summary> /// <param name="code"></param> /// <returns></returns> public AnsapiUserInfo getOpenIDByCode(string code, string AppID = "", int count = 3) { log.Info("Code:" + code); Config config = getConfig(AppID); AnsapiUserInfo ansapiUserInfo = new AnsapiUserInfo(); do { try { string res = ""; res = HttpWebResponseUtility.CreateGetHttpResponse(string.Format(Const.access_token_code + "?appid={0}&secret={1}&code={2}&grant_type=authorization_code", config.AppID, config.AppSecret, code), 5000, null, null); ansapiUserInfo = JsonConvert.DeserializeObject <AnsapiUserInfo>(res); if (CheckSuccess(ansapiUserInfo.errcode)) { count = 0; } } catch (Exception ex) { log.Info(ex.Message); log.Info(ex); } count--; } while (count > 0); return(ansapiUserInfo); }
/// <summary> /// 更新AccessToken /// </summary> /// <param name="config"></param> /// <returns></returns> private Config updateAccessToken(Config config, bool force = false) { lock (obj) { if (force || config.accessTokenDate == null || config.accessTokenDate < DateTime.Now.AddMinutes(-100)) { string res = HttpWebResponseUtility.CreateGetHttpResponse(Const.access_token + String.Format("?grant_type=client_credential&appid={0}&secret={1}", config.AppID, config.AppSecret), 5000, null, null); AccessTokenModel accessTokenModel = JsonConvert.DeserializeObject <AccessTokenModel>(res); if (!accessTokenModel.errcode.HasValue) { config.accessToken = accessTokenModel.access_token; config.accessTokenDate = DateTime.Now; } } } return(config); }
/// <summary> /// 删除菜单 /// </summary> /// <returns></returns> public string DeleteMenu(string AppID = "", int count = 3) { Config config = getConfig(AppID); string res = ""; do { try { res = HttpWebResponseUtility.CreateGetHttpResponse(Const.menu_delete + config.accessToken, 5000, null, null); count = 0; } catch (Exception ex) { config = getConfig(AppID, true); } }while (count-- > 0); return(res); }