公众号可通过本接口来获取帐号的关注者列表,关注者列表由一串OpenID(加密后的微信号,每个用户对每个公众号的OpenID是唯一的)组成。一次拉取调用最多拉取10000个关注者的OpenID,可以通过多次拉取的方式来满足需求。
接口调用请求说明
http请求方式: GET(请使用https协议)
返回说明
正确时返回JSON数据包:
错误时返回JSON数据包(示例为无效AppID错误):
{"errcode":40013,"errmsg":"invalid appid"}
附:关注者数量超过10000时
当公众号关注者数量超过10000时,可通过填写next_openid的值,从而多次拉取列表的方式来满足需求。
具体而言,就是在调用接口时,将上一次调用得到的返回中的next_openid值,作为下一次调用中的next_openid值。
示例如下:
公众账号A拥有23000个关注的人,想通过拉取关注接口获取所有关注的人,那么分别请求url如下:
https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN
返回结果:
{ "total":23000, "count":10000, "data":{" openid":[ "OPENID1", "OPENID2", ..., "OPENID10000" ] }, "next_openid":"OPENID10000" }
https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID1
返回结果:
{ "total":23000, "count":10000, "data":{ "openid":[ "OPENID10001", "OPENID10002", ..., "OPENID20000" ] }, "next_openid":"OPENID20000" }
https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID2
返回结果(关注者列表已返回完时,返回next_openid为空):
{ "total":23000, "count":3000, "data":{" "openid":[ "OPENID20001", "OPENID20002", ..., "OPENID23000" ] }, "next_openid":"OPENID23000" }
微信官方网站后台的接口权限表里(以服务号为例)每天调用的获取用户列表能获取500次,获取用户基本信息可以获取500000次,所以说接下来,我在获取用户列表的时候,会用到缓存,别看500次不少了,
可是真正的用起来快得不得了,先上效果图如下:
先来看看用户列表,官网说获取用户的列表返回的是一组组openID,针对这个特性,我是这么做的,
创建一个用于存储openId的类
public class WxOpenIdInfo { public string WxopenId { get; set; }//用户存放微信用户的openId }
然后再创建用户信息的基本类
/// <summary> /// 微信用户基本信息类 /// </summary> public class WxUserInfo { public int subscribe { get; set; }//关注状态 public string openid { get; set; }//OpenID public string nickname { get; set; }//昵称 public string sex { get; set; }//性别 public string city { get; set; }//城市 public string province { get; set; }//省份 public string headimgurl { get; set; }//头像图片地址 public string subscribe_time { get; set; }//关注时间 public string remark { get; set; }//备注 public string groupid { get; set; }//分组ID }
用户列表前台代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WeiXinUserList.aspx.cs" Inherits="DQWebSite.Administrator.WeiXinUserList" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <link href="https://www.jb51.net/css/style.css" type="text/css" /> <style type="text/css"> #title {width:100%; height:40px;margin-top:10px; text-indent:5px; line-height:40px;} .checkstyle { float:left;} #DDlAddgroups { text-align:center; width:161px; border:1px solid #d9d9d9; border-radius:5px; height:35px; line-height:35px; font-weight:bold; text-align:center; float:left; margin:auto 5px auto 5px;} .DDlAddgroups{ text-align:center; width:161px; border:1px solid #d9d9d9; border-radius:5px; height:35px; line-height:35px; font-weight:bold; text-align:center; float:left; margin:auto 5px auto 5px;} #DDLgroups { text-align:center; width:111px; border:1px solid #d9d9d9; border-radius:5px; height:35px; line-height:35px; font-weight:bold; text-align:center; float:left; margin:auto 5px auto 5px;} .newGroups { margin:auto 5px auto 5px;} .inputstyle { height:35px; line-height:35px; text-indent:5px; width:280px;background-image:url('images/inputbg.gif'); background-repeat:repeat-x;border-top:solid 1px #a7b5bc; border-left:solid 1px #a7b5bc; border-right:solid 1px #ced9df; border-bottom:solid 1px #ced9df; float:left; margin:auto 5px auto 5px; } .wxusertab { border:1px solid #d9d9d9; width:100%; text-align:left; text-indent:5px; } th { height:35px;background-image:url('images/th.gif'); background-repeat:repeat-x; } td { border-bottom:1px solid #d9d9d9; } .trcolor { background-color:#ecd9df; } tr:hover { cursor:pointer; } #FenPage { width:1124px; height:25px; line-height:25px; text-align:center; margin:20px auto 20px auto; } .linka { color:#0094ff; cursor:pointer; } .fenyebtn {width:60px; height:25px; border:1px solid #ced9df; border-radius:5px; text-align:center; line-height:25px; float:right; } .fenyebtn2 { width:60px; height:25px; border:1px solid #ced9df; border-radius:5px; text-align:center; line-height:25px;margin-left:10px;float:right; } .toPageIndex { width:60px;height:25px; background-image:url('images/inputbg.gif'); margin-left:10px; background-repeat:repeat-x;border-top:solid 1px #a7b5bc; border-left:solid 1px #a7b5bc; border-right:solid 1px #ced9df; border-bottom:solid 1px #ced9df; text-align:center; float:right; } .gotoPagebtn { width:60px; height:25px; border:1px solid #ced9df; border-radius:5px; text-align:center; line-height:25px;margin-left:10px;float:right; background-color:#ced9df; } .deletebtn {float:left;width:100px; color:#000; height:25px; background-color:#ced9df; border:1px solid #ced9df; border-radius:5px; text-align:center; } a { color:#08a5e0; } .droplist { background-image:url('images/inputbg.gif'); background-repeat:repeat-x; width:120px; height:25px; border:1px solid #ced9df; } .checkstyle { float:left; } .imgheadstyle { width:50px; height:50px; margin-top:10px; } .lbsubscribeCount { font-size:26px; } #shownewgroup { width:300px; height:200px; background-color:white;z-index:9999; border:2px solid #DDD; top:40%; left:40%; background-color:#fff; position:fixed;margin:-100px auto auto -100px; display:none; } #shownewgroupzhezhaoceng { height:200%; width:200%; left:0px; top:0px;position:fixed; z-index:9998; background:rgb(50,50,50);background:rgba(0,0,0,0.5); display:none; } .closeLogin { height:30px; border-bottom:2px solid #31bb34; text-align:right; line-height:30px; font-size:14px; font-weight:bold; } a:hover { cursor:pointer; } #updateremark { width:300px; height:200px; background-color:white;z-index:9999; border:2px solid #DDD; top:40%; left:40%; background-color:#fff; position:fixed;margin:-100px auto auto -100px; display:none; } #updateremark_zhezhaoceng { height:100%; width:100%; left:0px; top:0px;position:fixed; z-index:9998; background:rgb(50,50,50);background:rgba(0,0,0,0.5); display:none; } </style> <script src="https://www.jb51.net/js/jquery-1.7.1.min.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function () { $(".newGroups").click(function () { $("#shownewgroupzhezhaoceng").show(); $("#shownewgroup").show(); }), $('.closeloginpage').click(function () { $("#shownewgroupzhezhaoceng").hide(); $("#shownewgroup").hide(); }) }) </script> </head> <body> <form runat="server"> <div> <span>位置:</span> <ul> <li><a href="https://www.jb51.net/WelCome.aspx" target="rightFrame">首页</a></li> <li>微信管理</li> <li>德桥员工服务中心--关注者列表管理</li> </ul> </div> <asp:ScriptManager runat="server"></asp:ScriptManager> <asp:UpdatePanel runat="server"> <ContentTemplate> <div> <div><a><span>新建分组</span>关闭</a> </div> <div> 30字符以内</div> <input type="text" maxlength="30" runat="server" value="分组名称" onfocus="if(value==defaultValue){value='';this.style.color='#000'}" onblur="if(!value){value=defaultValue;this.style.color='#999'}"/> <asp:LinkButton runat="server"><div> 确定创建</div></asp:LinkButton> </div> <div></div> <div><span>已关注人数</span><span><asp:Label CssClass="lbsubscribeCount" runat="server" Text="Label"></asp:Label> </span></div> <div> <asp:CheckBox runat="server" CssClass="checkstyle" OnCheckedChanged="CheckAll_CheckedChanged" /><span>全选 </span> <asp:DropDownList CssClass="DDlAddgroups" runat="server" OnSelectedIndexChanged="DDlAddgroups_SelectedIndexChanged"> </asp:DropDownList> <asp:DropDownList runat="server"> </asp:DropDownList> <a><div> + 新建分组</div></a> <a href="https://www.jb51.net/WxGroupManageList.aspx"><div>分组管理</div></a> <a href="https://www.jb51.net/WeiXinUserList.aspx"><div>刷 新</div></a> <%-- <input type="text" runat="server" value="用户昵称" onfocus="if(value==defaultValue){value='';this.style.color='#000'}" onblur="if(!value){value=defaultValue;this.style.color='#999'}"/> <asp:LinkButton runat="server"><div> 查询</div></asp:LinkButton>--%> </div> <table> <asp:Repeater runat="server" OnItemDataBound="RepeaterWxUserList_ItemDataBound"> <HeaderTemplate> <tr> <th></th> <th>OpenID</th> <th>头像</th> <th>昵称(备注名)</th> <th>关注时间</th> <th>所属分组</th> <th>操作</th> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td> <asp:CheckBox runat="server" CssClass="checkstyle" /> <%--OnCheckedChanged="CheckIn_CheckedChanged"--%></td> <td><asp:Label runat="server" Text=""></asp:Label> </td> <td><asp:Image runat="server" CssClass="imgheadstyle" /> </td> <td><asp:Label runat="server" CssClass="checkstyle" Text=""></asp:Label> <asp:Label runat="server" Text=""></asp:Label> </td> <td> <asp:Label runat="server" Text=""></asp:Label> </td> <td> <asp:Label runat="server" CssClass="checkstyle" Visible="false" Text=""></asp:Label> <asp:DropDownList Enabled="false" CssClass="DDlAddgroups" runat="server"> <asp:ListItem Value="0">分组名称</asp:ListItem> </asp:DropDownList> </td> <td> <a href='UpdateRemarkName.aspx?id=<%# Eval("WxopenId") %>'><div> 修改备注名称</div></a> </td> </tr> </ItemTemplate> </asp:Repeater> </table> <div> <asp:LinkButton CssClass="gotoPagebtn" runat="server">确定</asp:LinkButton> <asp:TextBox CssClass="toPageIndex" runat="server"></asp:TextBox> <asp:HyperLink runat="server"><span>>>|</span></asp:HyperLink> <asp:HyperLink runat="server"><span>></span></asp:HyperLink> <asp:HyperLink runat="server"><span><</span></asp:HyperLink> <asp:HyperLink runat="server"><span>|<<</span></asp:HyperLink> <span>当前第</span> <span><asp:Label runat="server" Text=""></asp:Label></span> <span>页/</span> <span>共</span> <span><asp:Label runat="server" Text=""></asp:Label></span> <span>页</span> <span><asp:Label runat="server" Text=""></asp:Label></span> <span>共搜索到 </span> <span><asp:Label runat="server" Text=""></asp:Label></span> <span>条记录.</span> </div> </ContentTemplate> </asp:UpdatePanel> </form> </body> </html>
获取用户列表绑定用户信息的后台代码,已包括,修改备注名,将用户移动到分组,新建分组代码
分组统计,用于显示每个分组的已存在人数,无其他作用
上代码: