BatchGetUserIds.java 10.9 KB
package cn.com.glsx.wechatbuild.util;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class BatchGetUserIds {

    // 定义请求的 URL
    private static final String URL = "http://192.168.5.4:8856/wechat-build-server/wechat/oa/getuserid";
    private static final int MERCHANT_ID = 44211276; // 商户 ID

    public static void main(String[] args) {
        // 输入参数:由逗号分隔的手机号字符串
        String mobileNumbers = "18676666516,\n"
            + "13802229598,\n"
            + "13919272907,\n"
            + "18566760303,\n"
            + "13526701628,\n"
            + "18682180325,\n"
            + "13728711329,\n"
            + "19902905889,\n"
            + "13652364839,\n"
            + "13510950533,\n"
            + "18581101077,\n"
            + "13724315942,\n"
            + "13760137906,\n"
            + "13632791593,\n"
            + "15816882305,\n"
            + "18118758708,\n"
            + "18680331654,\n"
            + "18682474862,\n"
            + "18665871042,\n"
            + "18908415199,\n"
            + "15018517717,\n"
            + "13632614859,\n"
            + "13480792980,\n"
            + "18823398223,\n"
            + "13682565704,\n"
            + "13553354585,\n"
            + "18824231191,\n"
            + "13510709443,\n"
            + "15989449484,\n"
            + "18996176130,\n"
            + "15234057707,\n"
            + "13631653672,\n"
            + "18919005480,\n"
            + "15818722524,\n"
            + "17603040301,\n"
            + "13510769158,\n"
            + "13999188852,\n"
            + "18129809927,\n"
            + "13714501965,\n"
            + "13418902274,\n"
            + "15207557473,\n"
            + "13410512690,\n"
            + "18873336682,\n"
            + "13410260372,\n"
            + "13825236036,\n"
            + "13570899179,\n"
            + "13714966184,\n"
            + "13084995463,\n"
            + "13888061059,\n"
            + "15920068599,\n"
            + "13510892076,\n"
            + "15889357022,\n"
            + "15398879285,\n"
            + "15718122929,\n"
            + "13480758420,\n"
            + "18906352768,\n"
            + "13750094813,\n"
            + "13387572660,\n"
            + "18529534556,\n"
            + "18565743895,\n"
            + "13229655547,\n"
            + "13826510577,\n"
            + "18207493352,\n"
            + "17665387186,\n"
            + "15017915884,\n"
            + "13823254902,\n"
            + "17620387810,\n"
            + "15626599735,\n"
            + "13476670746,\n"
            + "13027947509,\n"
            + "18565882751,\n"
            + "13544162147,\n"
            + "18723016068,\n"
            + "13428582370,\n"
            + "13530356814,\n"
            + "18211271651,\n"
            + "18520855727,\n"
            + "13560142275,\n"
            + "13692232975,\n"
            + "18978849947,\n"
            + "13686451207,\n"
            + "13725594447,\n"
            + "18126301457,\n"
            + "18925261040,\n"
            + "18588459791,\n"
            + "15521063171,\n"
            + "15816539334,\n"
            + "13602576682,\n"
            + "13640414684,\n"
            + "13164067126,\n"
            + "13510080191,\n"
            + "15814019200,\n"
            + "15986611271,\n"
            + "15002093718,\n"
            + "18098972608,\n"
            + "13480670943,\n"
            + "13729407587,\n"
            + "13113615137,\n"
            + "13590474377,\n"
            + "13132112126,\n"
            + "17718004057,\n"
            + "15029223187,\n"
            + "13825207633,\n"
            + "15091195572,\n"
            + "13923832622,\n"
            + "13650810420,\n"
            + "13560435936,\n"
            + "15914132780,\n"
            + "15218375798,\n"
            + "15130200650,\n"
            + "13632814608,\n"
            + "13428794434,\n"
            + "18750768529,\n"
            + "15811800411,\n"
            + "19842779228,\n"
            + "17717113018,\n"
            + "15019903579,\n"
            + "13714250690,\n"
            + "13367011268,\n"
            + "18608638562,\n"
            + "15814684652,\n"
            + "13145829752,\n"
            + "15728669308,\n"
            + "15765100646,\n"
            + "13971818453,\n"
            + "15121903206,\n"
            + "18778661361,\n"
            + "17363923696,\n"
            + "13333970369,\n"
            + "15348433022,\n"
            + "13545099802,\n"
            + "13924651037,\n"
            + "18845036713,\n"
            + "17875511834,\n"
            + "13811039666,\n"
            + "13437112703,\n"
            + "15007612682,\n"
            + "13919196474,\n"
            + "13641460319,\n"
            + "14776593605,\n"
            + "17723810309,\n"
            + "18312008906,\n"
            + "13603070903,\n"
            + "15599117475,\n"
            + "15974485939,\n"
            + "18372203315,\n"
            + "18777537522,\n"
            + "15926409442,\n"
            + "18501960271,\n"
            + "18991371609,\n"
            + "17320505265,\n"
            + "15060463550,\n"
            + "15619023691,\n"
            + "13114808777,\n"
            + "17279285915,\n"
            + "18918071512,\n"
            + "15243552261,\n"
            + "17730566070,\n"
            + "13699753087,\n"
            + "13823275167,\n"
            + "13477802110,\n"
            + "15118075090,\n"
            + "13336542373,\n"
            + "17388809098,\n"
            + "17608426680,\n"
            + "13410574821,\n"
            + "13798241069,\n"
            + "18476546702,\n"
            + "18923029556,\n"
            + "13662232548,\n"
            + "15018500224,\n"
            + "15012971497,\n"
            + "15213642943,\n"
            + "16620140401,\n"
            + "15897150260,\n"
            + "19144475436,\n"
            + "19146473576,\n"
            + "13502026987,\n"
            + "13750327261,\n"
            + "15302793173,\n"
            + "18055827902,\n"
            + "15288110710,\n"
            + "15218742455,\n"
            + "13872331184,\n"
            + "18937619155,\n"
            + "15549042039,\n"
            + "18172379350,\n"
            + "15189464150,\n"
            + "15989792150,\n"
            + "13631278800,\n"
            + "15056333678,\n"
            + "17820774214,\n"
            + "13794566101,\n"
            + "18088803701,\n"
            + "13192910803,\n"
            + "13754841667,\n"
            + "18589053739,\n"
            + "1360201245,\n"
            + "18829958191,\n"
            + "13692759494,\n"
            + "13472209000,\n"
            + "13553723351,\n"
            + "15768618575,\n"
            + "13824226277,\n"
            + "13928455491,\n"
            + "15035677790,\n"
            + "15766901106,\n"
            + "18048516825,\n"
            + "18993509794,\n"
            + "13829913982,\n"
            + "18337408707,\n"
            + "18218808652,\n"
            + "17640559222,\n"
            + "13530954435,\n"
            + "15221101760,\n"
            + "17754026554,\n"
            + "18502669991,\n"
            + "18670052366,\n"
            + "15907124293,\n"
            + "19575361116,\n"
            + "15609207271,\n"
            + "18651630115,\n"
            + "18503817906,\n"
            + "15537622080,\n"
            + "18963987451,\n"
            + "19353601481,\n"
            + "13210038756,\n"
            + "18671398843,\n"
            + "13039586474,\n"
            + "18553927659,\n"
            + "15038286098,\n"
            + "13848877213,\n"
            + "18570062288,\n"
            + "18638692652,\n"
            + "17388657712,\n"
            + "13852289577,\n"
            + "1392273926,\n"
            + "15148078508,\n"
            + "13080667702,\n"
            + "13560114686,\n"
            + "13215017737,\n"
            + "18061660551";

        // 分割手机号字符串
        String[] mobileArray = mobileNumbers.split(",");
        List<String> list= new ArrayList<>();
        // 遍历每个手机号并发送请求
        for (String mobile : mobileArray) {
            try {
                String response = sendPostRequest(mobile.trim(), MERCHANT_ID);
                String s = parseAndPrintResponse(response, mobile.trim());
                list.add(s);
            } catch (Exception e) {
                System.err.println("请求失败,手机号:" + mobile + ",错误信息:" + e.getMessage());
            }
        }
        for (String s : list){
            System.out.println(s);
        }
    }

    /**
     * 发送 HTTP POST 请求
     *
     * @param mobile 手机号
     * @param merchantId 商户 ID
     * @return 响应内容字符串
     * @throws Exception 如果请求失败或出现异常
     */
    private static String sendPostRequest(String mobile, int merchantId) throws Exception {
        // 创建 HttpClient 实例
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            // 创建 POST 请求
            HttpPost httpPost = new HttpPost(URL);

            // 设置请求体
            JsonObject jsonBody = new JsonObject();
            jsonBody.addProperty("mobile", mobile);
            jsonBody.addProperty("merchantId", merchantId);

            StringEntity entity = new StringEntity(jsonBody.toString(), "UTF-8");
            entity.setContentType("application/json");
            httpPost.setEntity(entity);

            // 执行请求并获取响应
            try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
                return EntityUtils.toString(response.getEntity(), "UTF-8");
            }
        }
    }

    /**
     * 解析响应并打印结果
     *
     * @param jsonResponse 响应内容字符串
     * @param mobile 手机号
     */
    private static String parseAndPrintResponse(String jsonResponse, String mobile) {
        try {
            // 解析 JSON 响应
            JSONObject jsonObject = JSONObject.parseObject(jsonResponse);

            // 获取 errCode 和 errMsg
            int errCode = (int) jsonObject.get("errCode");
            String errMsg = (String) jsonObject.get("errMsg");

            if (errCode == 0) { // 请求成功
                String userId = (String) jsonObject.get("userid");
                return mobile + " " + userId;
            } else {
                System.err.println("请求失败,手机号:" + mobile + ",错误信息:" + errMsg);
                return mobile;
            }
        } catch (Exception e) {
            System.err.println("解析响应失败,手机号:" + mobile + ",错误信息:" + e.getMessage());
            return mobile;
        }
    }
}