java 获取服务号所有粉丝

在数据库中获取当前服务号的所有粉丝数据

新建粉丝类,定时任务全局定时刷新 access_token (这是接口调用凭证推荐定时任务刷新入库,官网链接:https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_access_token.html)

类里面定义 boolean,正在更新不允许再次进入

static  boolean isPullWxFsing = false;//正在更新粉丝列表
					if (isPullWxFsing) {
    
    //在更新粉丝列表,请结束后再点击更新
                        message = "正在更新粉丝列表,请结束后再点击更新";
                        return;
                    }
                    isPullWxFsing = true;
                    Map<String, String> map = new HashMap<>();
                    String next_openid = "";// 服务号粉丝量大接口会返回一个next_openid 下次调接口要用这个 每次接口返回1万openid
                    
                    for (int i = 0; i < Integer.MAX_VALUE; i++) {
    
    
                        System.out.println("进来了" + (i + 1) + "次");
                        map = updateFans(h, next_openid, i);
                        String isok = map.get("isok") + "";
                        next_openid = map.get("next_openid") + "";
                        if (isok.equals("1")) {
    
    
                            isPullWxFsing = false;
                            message = "接口未知错误,请稍后重试";
                            out.print("<script>mt.show('" + message + "',1,'" + nexturl + "');</script>");
                            return;
                        }
                        if (next_openid.length() == 0) {
    
    
                            isPullWxFsing = false;
                            break;
                        }
                    }
  /**
     * 更新微信服务号粉丝列表
     */

    private Map updateFans(Http h, String next_opennid, int isdelete) throws IOException {
    
    

        int returnnum = 0;
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        String next_openid = "";
        try {
    
    
            httpClient = HttpClients.createDefault();
            
            //获取全局的access_token
            ArrayList<WeiXinTooken> weiXinTookens = WeiXinTooken.find("", 0, Integer.MAX_VALUE);
            String access_token = weiXinTookens.get(0).tooken;
            
            
            HttpGet httpGet = new HttpGet("https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + access_token + "&next_openid=" + next_opennid);
            response = httpClient.execute(httpGet);
            HttpEntity entity = response.getEntity();
            //返回值转string
            String string = EntityUtils.toString(entity, "utf-8");
            
            //string转json
            JSONObject jsonObject = new JSONObject(string);
            //接口中返回的count  是粉丝数量  
            String count = jsonObject.get("count") + "";

            //如果是1万就获取下一个openid这个值  下次循环要用
            if (count.equals("10000")) {
    
    //本次是顶值 获取next_openid
                next_openid = jsonObject.get("next_openid") + "";
            }
            //data 数据包含所有的openid
            String data = jsonObject.get("data") + "";
            //去除前后多余项
            data = data.substring(11, data.length() - 2);
            //截取所有的openid
            String[] split = data.split(",");
            //循环第一次执行  清除目前的所有粉丝信息删除操作  
            if (split.length > 0 && isdelete == 0) {
    
    
                //实体类里面的删除方法
                WeiXinFans.delete();
            }
            StringBuilder stringBuilder = new StringBuilder();//使用的是sql拼接,减少insert次数,多个数据进行一次insert执行
            for (int i = 1; i <= split.length; i++) {
    
    
                stringBuilder.append(" ( " + Seq.get() + ",");
                String opneid = split[i - 1].substring(1, split[i - 1].length() - 1);
                
                weiXinTookens = WeiXinTooken.find("", 0, Integer.MAX_VALUE);
                access_token = weiXinTookens.get(0).tooken;
                
                //获取粉丝的接口 
                httpGet = new HttpGet("https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + access_token + "&openid=" + opneid + "&lang=zh_CN");
                response = httpClient.execute(httpGet);
                entity = response.getEntity();
                string = EntityUtils.toString(entity, "utf-8");
                jsonObject = new JSONObject(string);
                //昵称比较特殊  使用base64加密存储    页面解密展示   因为有的昵称是表情  入库会报错
                if (!jsonObject.isNull("nickname")) {
    
    
                    stringBuilder.append(Database.cite(Base64.encodeBase64String((jsonObject.get("nickname") + "").getBytes("utf-8"))) + ",");
                    
                }
                if (!jsonObject.isNull("openid")) {
    
    
                    stringBuilder.append(Database.cite(jsonObject.get("openid") + "") + ",");
                }

                //unionid   绑定开放平台才会有
                if (!jsonObject.isNull("unionid")) {
    
    
                          stringBuilder.append(Database.cite(jsonObject.get("unionid") + "") + ",");
                }
                //省份
                if (!jsonObject.isNull("province")) {
    
    
                    //weiXinFans.setProvince(MT.f(jsonObject.get("province") + ""));
                    stringBuilder.append(Database.cite(jsonObject.get("province") + "") + ",");

                }
                //城市
                if (!jsonObject.isNull("city")) {
    
    
                    //weiXinFans.setCity(MT.f(jsonObject.get("city") + ""));
                    stringBuilder.append(Database.cite(jsonObject.get("city") + "") + ",");
                }
                //分组
                if (!jsonObject.isNull("groupid")) {
    
    
                    //weiXinFans.setGroupid(MT.f(jsonObject.get("groupid") + ""));
                    stringBuilder.append(Database.cite(jsonObject.get("groupid") + "") + ",");
                }
          
                if (!jsonObject.isNull("tagid_list")) {
    
    
                    //weiXinFans.setTagid_list(MT.f(jsonObject.get("tagid_list") + ""));
                    stringBuilder.append(Database.cite(jsonObject.get("tagid_list") + "") + "," + Database.cite(new Date()) + ",");
                }

                if (!jsonObject.isNull("subscribe_time")) {
    
    //秒  *1000 转换毫秒  转换string  转换date
                    String subscribeTimeStr = jsonObject.get("subscribe_time") + "000";
                    Date date = new Date(Long.parseLong(subscribeTimeStr));
                    //weiXinFans.setSubscribe_time(date);
                    stringBuilder.append(Database.cite(date) + ")");
                }
                //weiXinFans.set();


                if (i == split.length) {
    
    
                    WeiXinFans.set(stringBuilder.toString());
                    stringBuilder = new StringBuilder();
                } else if (i % 100 == 0) {
    
    
                    WeiXinFans.set(stringBuilder.toString());
                    stringBuilder = new StringBuilder();
                } else {
    
    
                    stringBuilder.append(",");
                }
            }

        } catch (Exception e) {
    
    
            returnnum = 1;
            e.printStackTrace();
        } finally {
    
    
            //5.关闭资源
            response.close();
            httpClient.close();
        }
        Map<String, String> map = new HashMap<>();
        map.put("next_openid", next_openid);
        map.put("isok", returnnum + "");
        return map;
    }

猜你喜欢

转载自blog.csdn.net/lzq_20120119/article/details/115034736