package com.oying.modules.rider.utils;
|
|
import cn.hutool.core.io.IoUtil;
|
import cn.hutool.http.Header;
|
import cn.hutool.http.HttpRequest;
|
import cn.hutool.http.HttpResponse;
|
import com.alibaba.fastjson2.JSON;
|
import com.alibaba.fastjson2.JSONObject;
|
import com.oying.modules.rider.domain.RiderSourceInfo;
|
import com.oying.modules.rider.domain.RiderSourceInfoHttp;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
|
/**
|
* TODO
|
* 骑手信息查询
|
*
|
* @author pxb
|
* @version 1.0
|
*/
|
public class RiderSourceHttpUtils {
|
|
private static final Logger log = LoggerFactory.getLogger(RiderSourceHttpUtils.class);
|
private static final String URL = "https://1.95.3.255:443/lyhd/api/contract/syncRiderContract";
|
// private static final String URL = "http://192.168.18.111:5000/";
|
|
|
/**
|
* 功能描述: <HTTp请求>
|
*
|
* @param: paramMap 请求参数
|
* @return: java.lang.Object
|
* @author: pxb
|
*/
|
public static String httpPostRequest(HashMap<String, Object> paramMap, String url) {
|
String jsonParam = JSON.toJSONString(paramMap);
|
HttpResponse response = null;
|
try {
|
response = HttpRequest.post(url)
|
// 头信息,多个头信息多次调用此方法即可
|
.header(Header.USER_AGENT, "*")
|
.header(Header.CONTENT_TYPE, "application/json;charset=utf-8")
|
// .form(paramMap)//表单内容
|
// 超时,毫秒
|
.timeout(30000)
|
.body(jsonParam)
|
.execute();
|
return response.body();
|
} catch (Exception e) {
|
RiderSourceInfoHttp riderSourceInfoHttp = new RiderSourceInfoHttp();
|
riderSourceInfoHttp.setCode("500");
|
riderSourceInfoHttp.setMessage(paramMap.get("phone") + "手机号请求异常:" + url);
|
return JSON.toJSONString(riderSourceInfoHttp);
|
} finally {
|
IoUtil.close(response);
|
}
|
}
|
|
/**
|
* 功能描述: <获取第三方骑手数据信息>
|
*
|
* @param: [phone, sourcePlatform] 手机号,来源平台
|
* @return: riderSourceInfoHttp
|
* @author: pxb
|
*/
|
public static RiderSourceInfoHttp getRiderSourceInfoHttp(List<String> phones, String sourcePlatform) {
|
HashMap<String, Object> param = new HashMap<>(2);
|
param.put("phones", phones);
|
param.put("sourcePlatform", sourcePlatform);
|
String url = URL;
|
// 转化成对象
|
RiderSourceInfoHttp riderSourceInfoHttp = new RiderSourceInfoHttp();
|
try {
|
String result = httpPostRequest(param, url);
|
log.info("第三方骑手数据信息:{}", result);
|
// 转换数据
|
riderSourceInfoHttp = riderConvertUtils(result);
|
} catch (Exception e) {
|
riderSourceInfoHttp.setCode("500");
|
riderSourceInfoHttp.setMessage("获取数据失败");
|
riderSourceInfoHttp.setData(null);
|
return riderSourceInfoHttp;
|
}
|
// riderSourceInfoHttp.setCode(Constants.HTTP_CODE_SUCCESS);
|
// riderSourceInfoHttp.setMessage("请求成功");
|
// RiderSourceInfo riderSourceInfo = new RiderSourceInfo();
|
// riderSourceInfo.setSourcePlatform("LY");
|
// riderSourceInfo.setCardNum("123456789012345678");
|
// riderSourceInfo.setPhone("18706999997");
|
// riderSourceInfo.setCardName("张三");
|
// riderSourceInfo.setEnabled(Constants.SOURCE_ENABLED_ON);
|
// riderSourceInfoHttp.getData().add(riderSourceInfo);
|
return riderSourceInfoHttp;
|
}
|
|
/**
|
* 功能描述: <转换第三方骑手数据信息>
|
*
|
* @param: []
|
* @return: java.lang.Object
|
* @author: pxb
|
*/
|
public static RiderSourceInfoHttp riderConvertUtils(String result) {
|
RiderSourceInfoHttp riderSourceInfoHttp = new RiderSourceInfoHttp();
|
try {
|
riderSourceInfoHttp = JSONObject.parseObject(result, RiderSourceInfoHttp.class);
|
return riderSourceInfoHttp;
|
} catch (Exception e) {
|
riderSourceInfoHttp.setCode("500");
|
riderSourceInfoHttp.setMessage("转化数据失败");
|
return riderSourceInfoHttp;
|
}
|
}
|
|
public static void main(String[] args) {
|
List<String> phones = new ArrayList<>();
|
phones.add("13954532585");
|
RiderSourceHttpUtils.getRiderSourceInfoHttp(phones, "LY");
|
}
|
}
|