ab
2024-11-05 bead00668eebce8d39d027515d564376de2f5978
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package com.nova.sankuai.domain.api.aliyunIP;
 
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nova.sankuai.domain.entity.City;
import com.nova.sankuai.infra.mapper.CityMapper;
import com.nova.sankuai.infra.utils.HttpUtils;
import com.nova.sankuai.infra.utils.IpUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
 
@Slf4j
@Component
public class AliyunUtil {
 
    //阿里云appkey,获取ip地理信息
//    private static final String appKey = "204061314";
//    private static final String appKey = "204574003";
 
    //阿里云appSecrets,获取ip地理信息
//    private static final String appSecrets = "JVkQzP2zyCBvU0frWtCG9NM8w3FbkoLm";
//    private static final String appSecrets = "CBvIivU5E7AnfF1YYOjGnsle3267BkuP";
 
    private static final String appcode = "40575e08cd5c424fb1939000f262d25f";
 
    private static final String host = "https://zjip.market.alicloudapi.com";
 
    private static final String path = "/lifeservice/QueryIpAddr/query";
    private static final String method = "GET";
 
    // AppKey:204574003 AppSecret:CBvIivU5E7AnfF1YYOjGnsle3267BkuP
    //AppCode:40575e08cd5c424fb1939000f262d25f
 
//    private static final String US_CODE = "US";
 
//    private static final Logger logger = LoggerFactory.getLogger("loginLogger");
 
    @Resource
    private CityMapper cityMapper;
 
 
//    static {
//        //初始化appKey和appSecrets
//        HttpClientBuilderParams httpParam = new HttpClientBuilderParams();
//        httpParam.setAppKey(appKey);
//        httpParam.setAppSecret(appSecrets);
//        httpParam.setConnectionTimeout(Constants.TIME_OUT);
//        HttpApiClientIP.getInstance().init();
//    }
 
 
    /**
     * 调用阿里云的查询ip地理信息
     *
     * @param request
     * @param phone
     * @return
     */
    public static ResponseInfo IPLocaltionHttpSyncTest(HttpServletRequest request, String phone) {
        String ipAddr = IpUtil.getIpAddr(request);
        /*ApiResponse response = HttpsApiClientIP.getInstance().IPLocaltionSyncMode(ipAddr);
        if (response == null){
            log.error(phone + "阿里云IP地址获取:获取失败:{}");
            return null;
        }
        AliyunResponse aliyunResponse = null;
        String result = null;
        try {
            result = new String(response.getBody(), SdkConstant.CLOUDAPI_ENCODING);
            aliyunResponse = JSON.parseObject(result, AliyunResponse.class);
        } catch (Exception ex) {
            ex.printStackTrace();
            log.error(phone + "阿里云IP地址获取:ip地址为" + ipAddr + "阿里云原始返回response:" + response + ",原始返回body转换字符串:" + result + ",转换ip地址信息失败:{}", ex);
            return null;
        }
        if (100 != aliyunResponse.getCode()) {
            log.error(phone + "阿里云IP地址获取:获取失败:{}", aliyunResponse.toString());
            return null;
        } else {
            return aliyunResponse.getResult();
        }*/
 
//        String host = "https://zjip.market.alicloudapi.com";
//        String path = "/lifeservice/QueryIpAddr/query";
//        String method = "GET";
//        String appcode = appCode;
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<>();
        querys.put("ip", ipAddr);
        AliyunResponse aliyunResponse = null;
        try {
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
            //获取response的body
            if (response == null){
                log.error(phone + "阿里云IP地址获取:获取失败,ip地址为:{}", ipAddr);
                return null;
            }
 
            String result = EntityUtils.toString(response.getEntity());
 
            aliyunResponse = JSON.parseObject(result, AliyunResponse.class);
        } catch (Exception ex) {
            ex.printStackTrace();
            log.error(phone + "阿里云IP地址获取:ip地址为" + ipAddr + "转换ip地址信息失败:{}", ex);
            return null;
        }
 
        if (aliyunResponse == null || 0 != aliyunResponse.getError_code()) {
            log.error(phone + "阿里云IP地址获取:获取失败:{}", aliyunResponse);
            return null;
        } else {
            return aliyunResponse.getResult();
        }
    }
 
    /**
     * 调用阿里云的查询ip地理信息
     *
     * @param request
     * @return
     */
    public static void test(HttpServletRequest request) {
        String ipAddr = IpUtil.getIpAddr(request);
 
        ipAddr = "111.29.72.92";
 
//        ApiResponse response = HttpsApiClientIP.getInstance().IPLocaltionSyncMode(ipAddr);
//        if (response == null){
//            log.error("阿里云IP地址获取:获取失败:{}");
//            return;
//        }
//        String result = new String(response.getBody(), SdkConstant.CLOUDAPI_ENCODING);
//        log.info("response:{}", result);
 
//        String host = "https://zjip.market.alicloudapi.com";
//        String path = "/lifeservice/QueryIpAddr/query";
//        String method = "GET";
//        String appcode = appCode;
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<>();
        querys.put("ip", ipAddr);
        AliyunResponse aliyunResponse = null;
        try {
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
            //获取response的body
            if (response == null){
                log.error("阿里云IP地址获取:获取失败:{}");
            }
 
            String result = EntityUtils.toString(response.getEntity());
 
            aliyunResponse = JSON.parseObject(result, AliyunResponse.class);
        } catch (Exception ex) {
            ex.printStackTrace();
            log.error("阿里云IP地址获取:ip地址为" + ipAddr + "转换ip地址信息失败:{}", ex);
        }
 
        if (aliyunResponse == null || 0 != aliyunResponse.getError_code()) {
            log.error("阿里云IP地址获取:获取失败:{}", aliyunResponse);
        } else {
            System.out.println("阿里云IP地址获取:获得城市信息为:" +  aliyunResponse.getResult());
        }
    }
 
 
    /**
     * 通过阿里云的ip地理信息,获取城市信息
     *
     * @param request
     * @param phone
     * @return
     */
    public City getCityInfo(HttpServletRequest request, String phone) {
        ResponseInfo responseInfo = AliyunUtil.IPLocaltionHttpSyncTest(request, phone);
        if (responseInfo == null) {
            log.info(phone + " 阿里云IP地址获取:获得城市信息失败");
            return null;
        } else {
            log.info(phone + " 阿里云IP地址获取:获得城市信息为:{}", responseInfo);
            City cityInfo = cityMapper.selectOne(new LambdaQueryWrapper<City>().
                    likeRight(City::getName, responseInfo.getCity()).last(" limit 1 "));
            if (cityInfo != null) {
                log.info(phone + " 阿里云IP地址获取:获得城市信息为:{}", cityInfo);
                return cityInfo;
            }
            log.info(phone + " 阿里云IP地址获取:未获取到城市信息为:{}", cityInfo);
            return null;
        }
    }
 
    /**
     * 获取用户的ip及地理信息
     *
     * @param request
     * @param phone
     * @return
     */
    public CustomerIpInfo getCustomerIpInfo(HttpServletRequest request, String phone) {
        String ipAddr = IpUtil.getIpAddr(request);
        ResponseInfo responseInfo = AliyunUtil.IPLocaltionHttpSyncTest(request, phone);
        CustomerIpInfo customerIpInfo = new CustomerIpInfo();
        if (ipAddr != null) {
            customerIpInfo.setIp(ipAddr);
        }
        if (responseInfo != null) {
//            customerIpInfo.setCityName(responseInfo.getCity());
            City cityInfo = cityMapper.selectOne(new LambdaQueryWrapper<City>().
                    likeRight(City::getName, responseInfo.getCity()).last(" limit 1 "));
            if (cityInfo != null) {
                log.info(phone + "阿里云IP地址获取:获得城市信息为:{}", cityInfo);
                customerIpInfo.setSysCityId(cityInfo.getCityId());
                customerIpInfo.setCityName(cityInfo.getName());
            } else {
                customerIpInfo.setCityName(responseInfo.getCity());
            }
        } else {
            log.error(phone + "阿里云IP地址获取:未获得城市信息");
        }
        return customerIpInfo;
    }
 
    /**
     * 判断ip所属地是否未US
     */
    public boolean checkCounty(String ip) {
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<>();
        querys.put("ip", ip);
        AliyunResponse aliyunResponse = null;
        try {
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
            //获取response的body
            if (response == null){
                log.error("阿里云IP地址获取:转换ip地址信息失败");
                return false;
            }
 
            String result = EntityUtils.toString(response.getEntity());
 
            aliyunResponse = JSON.parseObject(result, AliyunResponse.class);
        } catch (Exception ex) {
            ex.printStackTrace();
            log.error("阿里云IP地址获取:转换ip地址信息失败:{}", ex);
            return false;
        }
 
        if (0 != aliyunResponse.getError_code()) {
            log.error("阿里云IP地址获取:获取失败:{}", aliyunResponse.toString());
            return false;
        } else {
            ResponseInfo responseInfo = aliyunResponse.getResult();
            if (responseInfo == null) {
                log.error("阿里云IP地址获取:获取地理信息失败:{}", aliyunResponse);
                return false;
            } else {
                log.info("阿里云IP地址获取:获得信息为:{}", responseInfo);
                return true;
            }
        }
 
 
    }
 
 
}