• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

asp.net微信开发第四篇----已关注用户管理

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

如果本章不明白打标签是什么意思,可以先看用户分组管理,再看本章, 本项目所用框架基于:layUI,官网:https://www.layui.com/

 

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

接口调用请求说明

http请求方式: GET(请使用https协议)
https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID
参数 是否必须 说明
access_token 调用接口凭证
next_openid 第一个拉取的OPENID,不填默认从头开始拉取

返回说明

正确时返回JSON数据包:

{"total":2,"count":2,"data":{"openid":["","OPENID1","OPENID2"]},"next_openid":"NEXT_OPENID"}
参数 说明
total 关注该公众账号的总用户数
count 拉取的OPENID个数,最大值为10000
data 列表数据,OPENID的列表
next_openid 拉取列表的最后一个用户的OPENID

错误时返回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"
}

本篇更新于2017年5月11日,微信官方网站后台的接口权限表里(以服务号为例)每天调用的获取用户列表能获取500次,获取用户基本信息可以获取500000次,所以说接下来,我在获取用户列表的时候,会用到缓存,别看500次不少了,
可是真正的用起来快得不得了,先上效果图如下:

 

 

 

打标签:

 

 

批量打标签:

 

 

 

 

修改用户备注名:

 

 

 

同步官方数据:具体实现思路如下:

 

1.新建微信用户实体类,openID实体类,用于封装微信用户的每个属性信息,

2.从微信服务器获取到数据后(如果获取数据较多,容易出现卡死现象,我做的是ajax异步获取,加了一个加载数据的动画显示),序列化后转存到本地数据库(转存前删除本地数据库的原有用户数据),页面加载数据的时候是从本地数据库加载的,不和微信打交道,

 

 

 

 


先来看看用户列表,官网说获取用户的列表返回的是一组组openID,针对这个特性,我是这么做的,
创建一个用于存储openId的类,用于存储openID列表

 /// <summary>
    /// 微信用户的OpenID实体类,此类不用来参与数据库数据存储,只是临时储存用户的openID列表
    /// </summary>
    public class WeChat_UserOpenIdInfo
    {
        /// <summary>
        /// 微信用户的openID
        /// </summary>
        public String WeChatUserOpenId { get; set; }
    }

 

/// <summary>
    /// 微信已关注用户实体类
    /// </summary>
    public class WeChat_UserInfo
    {
        public int ID { get; set; }//编号,自增列

        public string 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 country { get; set; }//国家

        public string province { get; set; }//省份

        public string language { get; set; }//语言

        public string headimgurl { get; set; }//头像图片地址

        public string subscribe_time { get; set; }//用户关注时间

        public string remark { get; set; }//备注

        public string groupid { get; set; }//分组ID

        /// <summary>
        /// 用户被打上的标签ID列表
        /// </summary>
        public string tagid_list { get; set; }


    }

 


用户列表前台代码
<%@ 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="css/style.css" rel="Stylesheet" 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="../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 id="form1" runat="server">
    <div class="place">
                <span>位置:</span>
                <ul class="placeul">
                    <li><a href="WelCome.aspx" target="rightFrame">首页</a></li>
                    <li>微信管理</li>
                    <li>德桥员工服务中心--关注者列表管理</li>
                </ul>
      </div>
            <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                   <div id="shownewgroup">
                    <div class="closeLogin"><a class="closeloginpage"><span style="float:left; color:#08a5e0; font-size:18px; text-indent:5px;">新建分组</span>关闭</a>&nbsp;&nbsp;</div>
             <div style="font-size:12px; height:40px; color:red; line-height:40px;">&nbsp;&nbsp;30字符以内</div>
             <input  type="text" id="txtgroupsName" name="txtgroupsName"  class="inputstyle" maxlength="30"  runat="server" value="分组名称" onfocus="if(value==defaultValue){value='';this.style.color='#000'}" onblur="if(!value){value=defaultValue;this.style.color='#999'}" style="color:#999"/>
            <asp:LinkButton ID="LinkBtnCreateGroup" runat="server" OnClick="LinkBtnCreateGroup_Click"><div style="background-image:url('images/buttonbg.png'); width:111px; height:35px; line-height:35px; font-weight:bold;float:left; margin-top:20px; margin-left:5px; text-align:center;color:#fff;">&nbsp;确定创建</div></asp:LinkButton>
                  </div>
                 <div id="shownewgroupzhezhaoceng"></div>
            <div style=" border-bottom:2px solid #31bb34; height:30px; margin-top:10px; text-indent:10px; font-size:22px; line-height:30px; width:100%;"><span style="float:left; font-size:16px;">已关注人数</span><span style="color:red;"><asp:Label ID="lbsubscribeCount" CssClass="lbsubscribeCount" runat="server" Text="Label"></asp:Label> </span></div>
            <div id="title">
            <asp:CheckBox ID="CheckAll" runat="server" CssClass="checkstyle" OnCheckedChanged="CheckAll_CheckedChanged" /><span style="float:left;">全选&nbsp;</span>
            
            <asp:DropDownList ID="DDlAddgroups" CssClass="DDlAddgroups" runat="server" OnSelectedIndexChanged="DDlAddgroups_SelectedIndexChanged">
            </asp:DropDownList>
             <asp:DropDownList ID="DDLgroups" runat="server">
            </asp:DropDownList>
            <a class="newGroups"><div style="background-image:url('images/buttonbg.png'); width:111px; height:35px; line-height:35px; margin:auto 20px auto 10px; font-weight:bold;float:left; text-align:center;color:#fff;">&nbsp;+&nbsp;新建分组</div></a>
             <a href="WxGroupManageList.aspx"><div style="background-image:url('images/buttonbg.png'); width:111px; height:35px; line-height:35px; margin:auto 20px auto 10px; font-weight:bold;float:left; text-align:center;color:#fff;">分组管理</div></a>
                <a href="WeiXinUserList.aspx"><div style="background-image:url('images/buttonbg.png'); width:111px; height:35px; line-height:35px; margin:auto 20px auto 10px; font-weight:bold;float:left; text-align:center;color:#fff;">刷&nbsp;&nbsp;新</div></a>

<%--            <input  type="text" id="txtName" name="txtName"  class="inputstyle"  runat="server" value="用户昵称" onfocus="if(value==defaultValue){value='';this.style.color='#000'}" onblur="if(!value){value=defaultValue;this.style.color='#999'}" style="color:#999"/>

             <asp:LinkButton ID="LinkButton1" runat="server"><div style="background-image:url('images/buttonbg.png'); width:111px; height:35px; line-height:35px; font-weight:bold;float:left; text-align:center;color:#fff;">&nbsp;查询</div></asp:LinkButton>--%>
        </div>
         <table class="wxusertab">
        <asp:Repeater ID="RepeaterWxUserList" 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 style="width:100%; line-height:50px;">
                            <td style="width:30px;">&nbsp;&nbsp;<asp:CheckBox ID="CheckIn" runat="server" CssClass="checkstyle" /> <%--OnCheckedChanged="CheckIn_CheckedChanged"--%></td>
                            <td style="width:150px;"><asp:Label ID="lbwxopenID" runat="server" Text=""></asp:Label>
                            </td>
                            <td style="width:80px;"><asp:Image ID="ImgHeadUrl" runat="server" CssClass="imgheadstyle" /> </td>
                            <td style="width:150px;"><asp:Label ID="lbNickName" runat="server" CssClass="checkstyle" Text=""></asp:Label>
                                <asp:Label ID="lbRemark" runat="server" Text=""></asp:Label>
                            </td>
                            <td style="width:130px;">
                                <asp:Label ID="lbSubscrine_time" runat="server" Text=""></asp:Label>
                            </td>
                            <td style="width:100px;"> 
                                <asp:Label ID="lbgroupId" runat="server" CssClass="checkstyle" Visible="false" Text=""></asp:Label>
                                <asp:DropDownList ID="DDlAddgroupss" Enabled="false" CssClass="DDlAddgroups" runat="server">
                                <asp:ListItem Value="0">分组名称</asp:ListItem>
                            </asp:DropDownList>
                            </td>
                            <td style="width:110px;">
                                <a href='UpdateRemarkName.aspx?id=<%# Eval("WxopenId") %>'><div style=" border:1px solid #d9d9d9; border-radius:5px; width:111px; height:35px; line-height:35px; font-weight:bold;float:left; text-align:center;">&nbsp;修改备注名称</div></a>
                            </td>
                        </tr>
            </ItemTemplate>
        </asp:Repeater>
             </table>
         <div id="FenPage">
               <asp:LinkButton ID="LinkBtnToPage" CssClass="gotoPagebtn" runat="server" OnClick="LinkBtnToPage_Click">确定</asp:LinkButton>
               <asp:TextBox ID="txtPageIndex" CssClass="toPageIndex" runat="server"></asp:TextBox>&nbsp;
               <asp:HyperLink ID="lnkLast" runat="server"><span class="fenyebtn2">>>|</span></asp:HyperLink>&nbsp;
                <asp:HyperLink ID="lnkNext" runat="server"><span class="fenyebtn2">></span></asp:HyperLink>&nbsp;
                 <asp:HyperLink ID="lnkTop" runat="server"><span class="fenyebtn2"><</span></asp:HyperLink>&nbsp;
                <asp:HyperLink ID="lnkFist" runat="server"><span class="fenyebtn">|<<</span></asp:HyperLink>&nbsp;
              <span style="float:left;">当前第</span>
               <span style="float:left; color:red;"><asp:Label ID="lbPageIndex" runat="server" Text=""></asp:Label></span>
                <span style="float:left;margin-left:5px;">页/</span>
                <span style="float:left;margin-left:5px;">共</span>
                <span style="float:left;color:red;"><asp:Label ID="lbCountPage" runat="server" Text=""></asp:Label></span>
                <span style="float:left;margin-left:5px;">页</span>
                <span style="float:left;margin-left:10px;"><asp:Label ID="lbPageSize" runat="server" Text=""></asp:Label></span>
                <span style="float:left;margin-left:10px;">共搜索到&nbsp;</span>
                <span style="float:left;margin-left:5px; color:red;"><asp:Label ID="lbCountData" runat="server" Text=""></asp:Label></span>
                <span style="float:left;margin-left:5px;">条记录.</span>
           </div>
           </ContentTemplate>
       </asp:UpdatePanel>
    </form>
</body>
</html>

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

 

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

上代码:

 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;
            
        }
        /// <summary>
        /// 获取所有用户的openId列表
        /// </summary>
        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<WxOpenIdInfo> openidlist = new List<WxOpenIdInfo>();


            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
                      

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap