从微信公众号获取关注名单

							个人记录使用
//查询处理出 所有关注的人 
    public String getWxListHandle(String access_token, String next_openid, StringBuffer stringBuffer) {
        String unionid = "";
        List<Map> user_list = new ArrayList<Map>();
        String wxcUserList = getWXCUserList(access_token, next_openid);
        List list = new ArrayList<String>();
        Map map = new HashMap();
        if (wxcUserList !=null && !"".equals(wxcUserList)) {
            Map mapB = JSONObject.parseObject(wxcUserList, Map.class);
            int errcode = mapB.containsKey("errcode") ? Integer.parseInt(mapB.get("errcode").toString()) : 0;
            if(errcode == 40001){
                access_token = 获取access_token 
                getWxListHandle(access_token, next_openid, stringBuffer);
            }
            int total = mapB.containsKey("total") ? Integer.parseInt(mapB.get("total").toString()) : 0;
            if (total == 0) {
                return "0";
            }
            next_openid = mapB.containsKey("next_openid") ? mapB.get("next_openid").toString() : "";
            int count = mapB.containsKey("count") ? Integer.parseInt(mapB.get("count").toString()) : 0;
            map = mapB.containsKey("data") ? JSONObject.parseObject(mapB.get("data").toString(), Map.class) : null;
            if (map == null || map.size() == 0) {
                return "0";
            }
            if(map.containsKey("openid")){
                list = JSONObject.parseObject(map.get("openid").toString(), List.class);
            }

            for (int y = 0; y < list.size(); y++) {
                Map<String, String> user_map = new HashMap<String, String>();
                user_map.put("openid", list.get(y).toString());
                user_map.put("lang", "zh_CN");
                user_list.add(user_map);
                if(y > 0 && (y%99 == 0 || y == list.size()-1)){
                    String user_info_query = getFollow(access_token, user_list);
                    if("".equals(user_info_query)){
                        continue;
                    }
                    Map user_info_map = new HashMap();
                    if(user_info_query.contains("user_info_list")){
                        user_info_map = JSONObject.parseObject(user_info_query, Map.class);
                    }
                    if(user_info_map ==null || !user_info_map.containsKey("user_info_list")){
                        continue;
                    }
                    List<Map> user_info_list = JSONObject.parseObject(user_info_map.get("user_info_list").toString(), List.class);
                    for (int x = 0; x < user_info_list.size(); x++) {
                        Object object = user_info_list.get(x).get("unionid");
                        if(object ==null ||"".equals(object)){
                            continue;
                        }
                        unionid = object.toString();
                        if ("".equals(unionid)) {
                            continue;
                        }
                        String sql = "select unionid,openid from 用户表where deleted = 0  and unionid = ?";
                        List<Map> list_integral = jdbcDao.queryForList(sql, new String[]{unionid});
                        if (list_integral != null && list_integral.size() > 0) {//没有生成过就生成一条关注得分
                            String openid = list_integral.get(0).get("openid").toString();
                            stringBuffer.append("'" + openid + "',");
                            
                        }
                    }
                    user_list.clear();
                }
            }
            if (count == 10000) {
                getWxListHandle(access_token, next_openid, stringBuffer);
            } else {
                return "1";
            }
        } else {
            return "0";
        }
        return "0";
    }
    //直接查询公众号用户列表
    public String getWXCUserList(String access_token, String next_openid) {
        List<NameValuePair> reqParamUser = new ArrayList<NameValuePair>();
        reqParamUser.add(new BasicNameValuePair("access_token", access_token));
        reqParamUser.add(new BasicNameValuePair("next_openid", next_openid));
        String resultUn = common.get("https://api.weixin.qq.com/cgi-bin/user/get", reqParamUser);
        return resultUn;
    }
    //查询用户是否关注以及unionid
    public String getFollow(String access_token, List lists) {
        String url = "https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token="+access_token;
        Map map =new HashMap();
        map.put("user_list",lists);
        String resultUn = common.post(url,JSON.toJSONString(map));
        return resultUn;
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值