mitao
2024-06-06 3d2b51ea4520533de5e78f88dddf5b5c7dce4247
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package com.sinata.core.util.juhe;
 
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.internal.util.file.IOUtils;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;
 
/**三网手机实名认证
 * @author mitao
 * @date 2024/4/1
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class TelecomUtil {
    private final JuHeProperties juHeProperties;
    //设置超时时间为5秒
    public static RequestConfig config = RequestConfig.custom().setConnectTimeout(5000).setSocketTimeout(5000).build();
    //明文查询地址
    public static String query_url = "https://v.juhe.cn/telecom/query?key=";
    //加密查询地址
    public static String queryEncry_url = "https://v.juhe.cn/telecom/queryEncry?key=";
 
    public boolean verify(String realname, String idcard, String mobile) {
        // ----------------------三网手机实名制认证-----------------------------------------------------------------------
        // int queryType = 1;// 1:普通查询 2:加密查询
        // String realname = "";// 姓名
        // String idcard = "";// 身份证
        //String mobile="" // 手机号
        // int type = 1;// 是否显示手机运营商 1:显示 0:不显示(默认)
        //int showid = 1 //是否显示聚合订单账号 1:显示 0:不显示(默认)
        //int province = 1 //是否显示号码归属地 1:显示 0:不显示(默认)
        //int detail = 1 //是否买显示匹配详情码 1:显示 0:不显示(默认)
        // Map<String, Object> params = new HashMap<>();
        // params.put("realname", realname);
        // params.put("idcard", idcard);
        // params.put("mobile", mobile);
 
        // ----------------------调用三网手机实名认证(加密版)-----------------------------------------------------------------------
        String key = MD5(juHeProperties.getOpenid()).substring(0, 16);//取前16位作为加密密钥
        int queryType = 2;// 加密版本
        realname = SecurityAESTool.encrypt(realname, key);//加密姓名
        idcard = SecurityAESTool.encrypt(idcard, key);//加密身份证
        mobile = SecurityAESTool.encrypt(mobile, key);//
        Map<String, Object> params = new HashMap<>();//组合参数
        params.put("realname", realname);
        params.put("idcard", idcard);
        params.put("mobile", mobile);
        //请求接口
        JSONObject result = null;
        try {
            result = JSONObject.parseObject(queryResult(params, queryType));
        } catch (Exception e) {
            log.error("调用三网手机实名认证失败", e);
        }
        //打印结果
        System.out.println(result);
        JSONObject innerResult = result.getJSONObject("result");
        if (result != null && innerResult != null && innerResult.getInteger("res") == 1) {
            return true;
        }
        return false;
    }
 
    /**
     * 请求接口查询数据
     *
     * @param params    参数
     * @param queryType 类型,1明文查询(默认),2加密版
     * @return 结果
     * @throws Exception
     */
 
    private String queryResult(Map<String, Object> params, int queryType) throws Exception {
 
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String result = null;
        String url = query_url;
        switch (queryType) {
            case 2:
                url = queryEncry_url + juHeProperties.getAppkey();
                break;
        }
        try {
            url = new StringBuffer(url).append("&").append(urlencode(params)).toString();
            HttpGet httpget = new HttpGet(url);
            httpget.setConfig(config);
            response = httpClient.execute(httpget);
            HttpEntity resEntity = response.getEntity();
            if (resEntity != null) {
                result = IOUtils.toString(resEntity.getContent(), "UTF-8");
            }
            EntityUtils.consume(resEntity);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            response.close();
            httpClient.close();
        }
        return result;
    }
 
 
    // 将map型转为请求参数型
    public static String urlencode(Map<String, ?> data) {
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, ?> i : data.entrySet()) {
            try {
                sb.append(i.getKey()).append("=").append(URLEncoder.encode(i.getValue() + "", "UTF-8")).append("&");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        String result = sb.toString();
        result = result.substring(0, result.lastIndexOf("&"));
        return result;
    }
 
 
    /**
     * md5加密
     *
     * @param data
     * @return 加密结果
     */
    public static String MD5(String data) {
        StringBuffer md5str = new StringBuffer();
        byte[] input = data.getBytes();
        try {
            // 创建一个提供信息摘要算法的对象,初始化为md5算法对象
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 计算后获得字节数组
            byte[] buff = md.digest(input);
            // 把数组每一字节换成16进制连成md5字符串
            int digital;
            for (int i = 0; i < buff.length; i++) {
                digital = buff[i];
                if (digital < 0) {
                    digital += 256;
                }
                if (digital < 16) {
                    md5str.append("0");
                }
                md5str.append(Integer.toHexString(digital));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return md5str.toString();
    }
}