/// <summary> /// 获取用户的信息 /// </summary> /// <param name="Code"></param> /// <returns></returns> public WeChatUser GetUserInfo(string AppId, string Code) { WeChatUser UModel = new WeChatUser(); Senparc.Weixin.MP.AdvancedAPIs.OAuth.OAuthAccessTokenResult TokenResult = Senparc.Weixin.MP.AdvancedAPIs.OAuthApi.GetAccessToken(AppId, WeChatDeploy.GetDeploy()[AppId], Code); Senparc.Weixin.MP.AdvancedAPIs.OAuth.OAuthUserInfo Model = Senparc.Weixin.MP.AdvancedAPIs.OAuthApi.GetUserInfo(TokenResult.access_token, TokenResult.openid); UModel.HeadImgUrl = Model.headimgurl ?? ""; UModel.NickName = Model.nickname ?? ""; UModel.OpenID = Model.openid; UModel.Sex = Model.sex; UModel.City = Model.city ?? ""; UModel.Country = Model.country ?? ""; UModel.Privilege = Model.privilege ?? new string[0]; UModel.Province = Model.province ?? ""; UModel.UnionID = Model.unionid ?? ""; return(UModel); }
/// <summary> /// 解析数据_服务号和订阅号 /// </summary> private static void GetUserBaseInfo_General(Controller controller, string code, out string OpenId) { //初始化结果 OpenId = String.Empty; //参数检查 if (string.IsNullOrEmpty(code)) { return; } //判断Session是否有存储,有则直接返回 string openIdInSession = controller.Session[SESSION_NAME_OPENID] as string; if (String.IsNullOrEmpty(openIdInSession) == false) { OpenId = openIdInSession; } //目标:只获取OpenID Senparc.Weixin.MP.AdvancedAPIs.OAuth.OAuthAccessTokenResult result = null; try { result = Senparc.Weixin.MP.AdvancedAPIs.OAuth.OAuthApi.GetAccessToken(WeiXinConfiguration.appID, WeiXinConfiguration.appsecret, code); } catch (Exception ex) { //如果获取不到,返回NULL,外部进行错误处理。 return; } if (result != null) { //存储到Session controller.Session[SESSION_NAME_OPENID] = result.openid; //返回结果 OpenId = result.openid; } }
/// <summary> /// 获取授权用户的openid /// </summary> /// <param name="Code"></param> /// <returns></returns> public string GetAuthorizeUserOpenID(string AppId, string Code) { Senparc.Weixin.MP.AdvancedAPIs.OAuth.OAuthAccessTokenResult TokenResult = Senparc.Weixin.MP.AdvancedAPIs.OAuthApi.GetAccessToken(AppId, WeChatDeploy.GetDeploy()[AppId], Code); return(TokenResult.openid); }