1. 程式人生 > >微信轉化介面(2018-06-14)

微信轉化介面(2018-06-14)

微信遷移主體 將關注使用者轉移到新公眾號

/**
 * 得到微信公眾號使用者OPENID
 * @param 原微信accessToken
 */

public static List getOpenIdList(String accessToken)
{
    String total;           //關注該公眾賬號的總使用者數
    String count;           //拉取的OPENID個數,最大值為10000
    JSONObject data;            //列表資料,OPENID的列表
    String next_openid;     //拉取列表的最後一個使用者的OPENID
    List openidList =null;
    //http請求方式: GET(請使用https協議)
    //https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID
    String url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token="+accessToken;
    try {
        URL getUrl=new URL(url);
        HttpURLConnection http=(HttpURLConnection)getUrl.openConnection();
        http.setRequestMethod("GET");
        http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        http.setDoOutput(true);
        http.setDoInput(true);
        http.connect();
        //請求成功200提示碼
       if( http.getResponseCode()==200 ) {
           InputStream is = http.getInputStream(); 
           int size = is.available(); 
            byte[] b = new byte[size];
            is.read(b);

            String message = new String(b, "UTF-8");

            JSONObject obj=new JSONObject(message);
            total=(String) obj.get("total");
            count=(String) obj.get("count");
            data = (JSONObject)obj.get("data");
            openidList = (List)data.get("openid");
            next_openid=(String) obj.get("next_openid");
       }


        } catch (MalformedURLException e) {
        e.printStackTrace();
        } catch (IOException e) {
        e.printStackTrace();
        }
        return openidList;
}
/**
 * 舊微信公眾號OPENID轉化為新微信公眾號OPENID----
 * @param openidList原微信的使用者openid集合  新微信accessToken 原微信appid
 */
public static LinkedHashMap<String,String>  oldOpenIdConvertToNewOpenId(List openidList,String accessToken,String appid)
{

    LinkedHashMap<String,String> map = new LinkedHashMap<String,String>();
    //http請求方式: post(請使用https協議)token為新帳號的token
    //http://api.weixin.qq.com/cgi-bin/changeopenid?access_token=xxxxx
    if( openidList != null && openidList.size() > 0){
        //計算需要轉化次數  因為每次微信轉化最多100個/次
        int amount = (int)Math.ceil(openidList.size()/100);
        //從下標為0開始擷取
        int count = 0;
        //擷取list
        List subList;
        for( int i=0;i<amount; i++ ){

            count +=1;
            subList = openidList.subList((count-1)*100, count*100);

            if( amount == count){
                subList = openidList.subList((count-1)*100, openidList.size()+1);
            }
            String url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token="+accessToken;
            try {
                URL getUrl=new URL(url);
                HttpURLConnection http=(HttpURLConnection)getUrl.openConnection();
                http.setDoOutput(true);
                http.setDoInput(true);
                http.setRequestMethod("POST");
                http.setUseCaches(false); 
                http.setInstanceFollowRedirects(true);
                http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
                http.setRequestProperty("Charset", "UTF-8"); 
                http.connect();
                DataOutputStream out = new DataOutputStream(http.getOutputStream());

                String params="{"
                                + "\"from_appid\":"+ appid+","
                                + "\"openid_list\":"+subList+"}";        
                System.out.println(params);
                out.write(params.getBytes("UTF-8"));
                out.flush();
                out.close(); 
                BufferedReader reader = new BufferedReader(new InputStreamReader(http.getInputStream(),"UTF-8"));
                String line;
                String responseStr="";
                while ((line = reader.readLine()) != null){
                    responseStr+=line;
                }
                System.out.println(responseStr);
                JSONObject obj=new JSONObject(responseStr);
                System.out.println(obj.get("errmsg"));

                JSONArray newOpenIdList = JSON.parseArray( obj.get("result_list")+"");
                //遍歷得到取到的新舊OPENID
                if( newOpenIdList!=null && newOpenIdList.size()>0){

                    for(int j=0;j<newOpenIdList.size();j++){

                        com.alibaba.fastjson.JSONObject jsonObject = newOpenIdList.getJSONObject(j);
                        String err_msg = jsonObject.get("err_msg")+"";

                        //取到目前還關注公眾號的使用者,即會返回"ok"
                        if( err_msg != null && err_msg.equals("ok") ){
                            String ori_openid = jsonObject.get("ori_openid")+"";
                            String new_openid = jsonObject.get("new_openid")+"";
                            System.out.println(ori_openid);
                            System.out.println(new_openid);
                            map.put(ori_openid, new_openid);
                        }

                    }
                }

                reader.close();
                http.disconnect();

            }catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (ProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return map;
}

}