請求微信接口,判斷用戶是否關注公衆號

          最近開發了一個投票活動,用戶可上傳一張照片,分享微信好友或朋友圈邀請好友點贊,點贊前幾名可得到相應的獎品。上傳照片的用戶必須關注某公衆號。php

  用戶點擊上傳按鈕,後臺判斷用戶是否關注公衆號,關注則前端可以使用相機或選擇本地照片進行上傳 ,未關注則彈出二維碼。前端

          對於判斷用戶是否關注某公衆號,微信官方提供的接口是:json

https://api.weixin.qq.com/cgi-bin/user/info?access_token="+accessToken+"&openid="+openid+"&lang=zh_CN
其中access_token是全局的access_token,openid就是當前微信關於當前服務號的openId
附上代碼
 1     public static Boolean judgeFollow(String openId){
 2         List<NameValuePair> params=new ArrayList<NameValuePair>();
 3         params.add(new BasicNameValuePair("xx","xx"));
 4         params.add(new BasicNameValuePair("xxx","xx"));
 5         params.add(new BasicNameValuePair("openid",openId));
 6         String s = new HttpUtil().get("http://xxx.xxx.cn/xx/xxx.php", params, "UTF-8", "UTF-8");
 7         String decode = DES.Decode(s);
 8         JSONObject jsonObject = JSONArray.parseObject(decode);
 9         String data = jsonObject.get("data").toString();
10         JSONObject jsonObject1 = JSONObject.parseObject(data);
11         Integer subscribe = jsonObject1.getInteger("subscribe");
12         if(subscribe==null){
13             return false;
14         }
15         return 1==subscribe?true:false;
16     }

       上面的這個接口地址是公司封裝好了的,沒有直接請求官方的接口,只用帶一個openID過去就好了,另外2個參數xx是公司接口需填的參數。json包是阿里的fastjson。經過這個接口得到subscribe這個字段,是否訂閱,爲1則表示當前微信關注了該公衆號,爲0則表示未關注。 完整的請求結果爲api

"subscribe":1,
    "openid":"oeQDZabsrfs12341***",
    "nickname":"會飛的貓",
    "sex":1,
    "language":"zh_CN",
    "city":"武漢",
    "province":"湖北",
    "country":"中國",
    "headimgurl":"http://wx.qlogo.cn/mmopen/kBwGJuwqK9**********************ibVUEpgFE90LH3b3uj7AYRjZP/0",
    "subscribe_time":1474964999,
    "unionid":"oGCG8t5**********jPQTPw",
    "remark":"",
    "groupid":0,
    "tagid_list":[

 只須要拿到subscribe這個字段的值就能夠了。
下面附上一段直接請求官方接口的代碼,也是網上搜的,試了一下沒有問題。原文地址:https://blog.csdn.net/qq_23842683/article/details/53888927
 1     public static boolean judgeIsFollow(String openid){
 2         logger.error("判斷是否關注:"+openid);
 3         Integer subscribe=null;
 4         String accessToken = getAccessToken();
 5         String url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="+accessToken+"&openid="+openid+"&lang=zh_CN";
 6         try {
 7             URL urlGet = new URL(url);
 8             logger.error("urlGet:"+urlGet);
 9             HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
10             http.setRequestMethod("GET"); // 必須是get方式請求
11             http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
12             http.setDoOutput(true);
13             http.setDoInput(true);
14             http.connect();
15             InputStream is = http.getInputStream();
16             int size = is.available();
17             byte[] jsonBytes = new byte[size];
18             is.read(jsonBytes);
19             String message = new String(jsonBytes, "UTF-8");
20             JSONObject demoJson = JSONObject.fromObject(message);
21             System.out.println("JSON字符串:"+demoJson);
22             subscribe = demoJson.getInt("subscribe");
23             is.close();
24             logger.error("當前subscribe:"+subscribe);
25         } catch (Exception e) {
26             e.printStackTrace();
27         }
28         return 1==subscribe?true:false;
29     }

      上面那個getAccessToken()方法是得到我當前服務號的全局accessToken。其他的基本和原文同樣。有不對的地方但願各位看官批評指正。(雖然好像沒什麼人看)微信

相關文章
相關標籤/搜索