首页 > PHP资讯 > 工具库 > asp.net微信开发已关注用户管理步骤详解

asp.net微信开发已关注用户管理步骤详解

工具库
这篇文章主要介绍了asp.net微信开发中有关已关注用户管理的相关内容,需要的朋友可以参考下

公众号可通过本接口来获取帐号的关注者列表,关注者列表由一串OpenID(加密后的微信号,每个用户对每个公众号的OpenID是唯一的)组成。一次拉取调用最多拉取10000个关注者的OpenID,可以通过多次拉取的方式来满足需求。

接口调用请求说明

http请求方式: GET(请使用https协议)

返回结果:

{ "total":23000, "count":10000, "data":{" openid":[ "OPENID1", "OPENID2", ..., "OPENID10000" ] }, "next_openid":"OPENID10000"}


确定 >>| > < |<< 当前第 页/ 共搜索到 条记录.

获取用户列表绑定用户信息的后台代码,已包括,修改备注名,将用户移动到分组,新建分组代码

分组统计,用于显示每个分组的已存在人数,无其他作用

上代码:

 PagedDataSource pds = new PagedDataSource(); protected void Page_Load(object sender, EventArgs e) {  if(!Page.IsPostBack)  {  BindGroupList();  BindGetAllUserOpenIdList();  this.DataBind();  this.CheckAll.AutoPostBack = true;  this.DDlAddgroups.AutoPostBack = true;  }  //this.DDlAddgroups.Enabled = false;   } ///  /// 获取所有用户的openId列表 ///  private void BindGetAllUserOpenIdList() {  WeiXinServer wxs = new WeiXinServer();  ///从缓存读取accesstoken  string Access_token = Cache["Access_token"] as string;  if (Access_token == null)  {  //如果为空,重新获取  Access_token = wxs.GetAccessToken();  //设置缓存的数据7000秒后过期  Cache.Insert("Access_token", Access_token, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration);  }  string Access_tokento = Access_token.Substring(17, Access_token.Length - 37);  string jsonres = "";  string content = Cache["AllUserOpenList_content"] as string;  if (content == null)  {  jsonres = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + Access_tokento;  HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(jsonres);  myRequest.Method = "GET";  HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();  StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);  content = reader.ReadToEnd();  reader.Close();  //设置缓存的数据7000秒后过期  Cache.Insert("AllUserOpenList_content", content, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration);  }  //使用前需要引用Newtonsoft.json.dll文件  JObject jsonObj = JObject.Parse(content);  int totalnum = int.Parse(jsonObj["count"].ToString());  List openidlist = new List();  for (int i = 0; i < totalnum;i++ )  {  WxOpenIdInfo wxopeninfo = new WxOpenIdInfo();  wxopeninfo.WxopenId = jsonObj["data"]["openid"][i].ToString();  openidlist.Add(wxopeninfo);  }  pds.DataSource = openidlist;  pds.AllowPaging = true;  pds.PageSize = 20;//每页显示为20条  int CurrentPage;  if (!String.IsNullOrWhiteSpace(this.txtPageIndex.Text.ToString().Trim()))  {  CurrentPage = Convert.ToInt32(this.txtPageIndex.Text.ToString().Trim());  }  else if (Request.QueryString["Page"] != null)  {  CurrentPage = Convert.ToInt32(Request.QueryString["Page"]);  }  else  {  CurrentPage = 1;  }  pds.CurrentPageIndex = CurrentPage - 1;//当前页的索引就等于当前页码-1;  if (!pds.IsFirstPage)  {  //Request.CurrentExecutionFilePath 为当前请求的虚拟路径  this.lnkTop.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurrentPage - 1);  this.lnkFist.Enabled = this.lnkTop.Enabled = true;  this.lnkNext.Enabled = this.lnkLast.Enabled = true;  }  else  {  this.lnkFist.Enabled = this.lnkTop.Enabled = false;  this.lnkNext.Enabled = this.lnkLast.Enabled = true;  this.lnkFist.Attributes.Add("style", "color:#ced9df;");  this.lnkTop.Attributes.Add("style", "color:#ced9df;");  this.lnkNext.Attributes.Remove("style");  this.lnkLast.Attributes.Remove("style");  }  if (!pds.IsLastPage)  {  //Request.CurrentExecutionFilePath 为当前请求的虚拟路径  this.lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurrentPage + 1);  this.lnkFist.Enabled = this.lnkTop.Enabled = true;  this.lnkNext.Enabled = this.lnkLast.Enabled = true;  }  else  {  this.lnkNext.Enabled = this.lnkLast.Enabled = false;  this.lnkFist.Enabled = this.lnkTop.Enabled = true;  this.lnkNext.Attributes.Add("style", "color:#ced9df;");  this.lnkLast.Attributes.Add("style", "color:#ced9df;");  this.lnkFist.Attributes.Remove("style");  this.lnkTop.Attributes.Remove("style");  }  this.lnkFist.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(1);//跳转至首页  this.lnkLast.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(pds.PageCount);//跳转至末页  this.RepeaterWxUserList.DataSource = pds;  this.RepeaterWxUserList.DataBind();  this.lbCountData.Text = openidlist.Count.ToString();  this.lbPageIndex.Text = (pds.CurrentPageIndex + 1).ToString();  this.lbPageSize.Text = "每页" + pds.PageSize.ToString() + "条记录";  this.lbCountPage.Text = pds.PageCount.ToString();  this.txtPageIndex.Text = (pds.CurrentPageIndex + 1).ToString();  if (int.Parse(openidlist.Count.ToString()) <= int.Parse(pds.PageSize.ToString()))  {  this.lnkFist.Visible = this.lnkTop.Visible = this.lnkNext.Visible = this.lnkLast.Visible = this.txtPageIndex.Visible = this.LinkBtnToPage.Visible = false;  }  else  {  this.lnkFist.Visible = this.lnkTop.Visible = this.lnkNext.Visible = this.lnkLast.Visible = this.txtPageIndex.Visible = this.LinkBtnToPage.Visible = true;  }  this.lbsubscribeCount.Text = openidlist.Count.ToString(); } ///  /// 绑定分组列表 ///  private void BindGroupList() {  WeiXinServer wxs = new WeiXinServer();  ///从缓存读取accesstoken  string Access_token = Cache["Access_token"] as string;  if (Access_token == null)  {  //如果为空,重新获取  Access_token = wxs.GetAccessToken();  //设置缓存的数据7000秒后过期  Cache.Insert("Access_token", Access_token, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration);  }  string Access_tokento = Access_token.Substring(17, Access_token.Length - 37);  string jsonres = "";  string content = Cache["AllGroups_content"] as string;  if (content == null)  {  jsonres = "https://api.weixin.qq.com/cgi-bin/groups/get?access_token=" + Access_tokento;  HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(jsonres);  myRequest.Method = "GET";  HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();  StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);  content = reader.ReadToEnd();  reader.Close();  //设置缓存的数据7000秒后过期  Cache.Insert("AllGroups_content", content, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration);  }  //使用前需要引用Newtonsoft.json.dll文件  JObject jsonObj = JObject.Parse(content);  int groupsnum = jsonObj["groups"].Count();  this.DDLgroups.Items.Clear();//清除  this.DDlAddgroups.Items.Clear();  this.DDLgroups.Items.Insert(0, new ListItem("分组统计", "0"));//添加默认第一个提示  this.DDlAddgroups.Items.Insert(0, new ListItem("移动用户到分组", "0"));  for (int i = 0; i < groupsnum; i++)  {  this.DDLgroups.Items.Add(new ListItem(jsonObj["groups"][i]["name"].ToString() + "(" + jsonObj["groups"][i]["count"].ToString() + ")", jsonObj["groups"][i]["id"].ToString()));    this.DDlAddgroups.Items.Add(new ListItem(jsonObj["groups"][i]["name"].ToString(), jsonObj["groups"][i]["id"].ToString()));  } } ///  /// 输入页码提交跳转 ///  /// 
 protected void LinkBtnSet_Click(object sender, EventArgs e) {    String openid = Request.QueryString["id"].ToString();  WeiXinServer wxs = new WeiXinServer();  string res = "";  ///从缓存读取accesstoken  string Access_token = Cache["Access_token"] as string;  if (Access_token == null)  {  //如果为空,重新获取  Access_token = wxs.GetAccessToken();  //设置缓存的数据7000秒后过期  Cache.Insert("Access_token", Access_token, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration);  }  string Access_tokento = Access_token.Substring(17, Access_token.Length - 37);  string posturl = "https://api.weixin.qq.com/cgi-bin/user/info/updateremark?access_token=" + Access_tokento;  string postData = "{"openid":"" + openid.ToString().Trim() + "","remark":"" + this.txtRemarkName.Value.ToString() + ""}";  res = wxs.GetPage(posturl, postData);  //使用前需药引用Newtonsoft.json.dll文件  JObject jsonObj = JObject.Parse(res);  ///获取返回结果的正确|true|false,  string isright = jsonObj["errcode"].ToString();//0  string istrueorfalse = jsonObj["errmsg"].ToString();//ok  if (isright.Equals("0") && istrueorfalse.Equals("ok"))  {  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('修改备注成功!');location='WeiXinUserList.aspx';", true);  }  else  {  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('修改备注失败!');", true);  } }

以上就是asp.net微信开发已关注用户管理步骤详解的详细内容,更多请关注php中文网其它相关文章!

工具库

本文由欣才IT学院整理发布,未经许可,禁止转载。
支持16不支持0