Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说微信公众平台对接C#-获取用户详情,希望能够帮助你!!!。
#region 获取到用户的详情
/// <summary>
/// 获取到用户的详细信息
/// </summary>
/// <param name="uid"></param>
/// <param name="aid"></param>
/// <returns></returns>
public static void GetMore(int aid, ref CKXD.WeiXinManage.Model.wx_user userModel, string guid, string LogName)
{
try
{
CommonApi common = new CommonApi();
string openID = userModel.WXU_OpenID;
CKXD.WeiXinManage.Model.wx_account accountModel = accountBiz.GetModel(aid);
string accessToken = CommonApi.getAccessToken(accountModel.WXA_AppId, accountModel.WXA_Secret, accountModel.WXA_OtherWSURL, LogName, null, guid);
//向微信服务器发送数据
string Url = string.Format("https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}&lang=zh_CN", accessToken, openID);
string returnJson = CommonApi.WXGetInfo(Url, accountModel, guid, LogName);
T9.Util.LogUtil.WriteLog("【获取用户" + userModel.ID + "信息】:/r/n" + returnJson, guid, LogName);
//有可能报错信息
if (returnJson.Contains("errcode"))
{
WxJsonResult result = T9.Util.JSONUtil.JsonDeserialize<WxJsonResult>(returnJson);
T9.Util.LogUtil.WriteLog(result.errmsg, guid, LogName);
}
WXUserInfo userInfotest = null;
try
{
userInfotest = T9.Util.JSONUtil.JsonDeserialize<WXUserInfo>(returnJson);
}
catch (Exception ex)
{
T9.Util.LogUtil.WriteLog("【获取用户" + userModel.ID + "信息】" + ex.Message + "\r\n" + ex.StackTrace, guid, LogName);
userInfotest = JsonConvert.DeserializeObject<WXUserInfo>(returnJson);
}
WXUserInfo userInfo = userInfotest;
if (userInfo != null && userInfo.subscribe != 0)
{
userModel.WXU_NickName = Regex.Replace(userInfo.nickname, @"\p{Cs}", " ");
userModel.WXU_Sex = userInfo.sex;
userModel.WXU_Country = userInfo.country;
userModel.WXU_City = userInfo.city;
userModel.WXU_Headimgurl = userInfo.headimgurl;
userModel.WXU_Province = userInfo.province;
}
//获取到用户的分组id
string json = "{\n";
json += "\"openid\":\"" + openID + "\",\n";
json += "}\n";
string url = string.Format("https://api.weixin.qq.com/cgi-bin/groups/getid?access_token={0}", accessToken);
string GJson = CommonApi.WXPostInfo(url, json, accountModel, guid, "weblog");
//有可能报错信息
if (GJson.Contains("errcode"))
{
WxJsonResult result = T9.Util.JSONUtil.JsonDeserialize<WxJsonResult>(returnJson);
T9.Util.LogUtil.WriteLog(result.errmsg, guid, LogName);
}
GetGroupId groupInfo = T9.Util.JSONUtil.JsonDeserialize<GetGroupId>(GJson);
int groupID = groupInfo.groupid;
userModel.WXU_GroupID = groupBiz.ExistsGroup(groupID, aid);
userModel.UpdateTime = DateTime.Now;
userBiz.Update(userModel);
}
catch (Exception ex)
{
T9.Util.LogUtil.WriteLog(ex.Message + "\r\n" + ex.StackTrace, guid, LogName);
}
}
#endregion
今天的分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
上一篇
已是最后文章
下一篇
已是最新文章