public String getToken(Integer organizationId) {
String redisKey = "parkToken-" + organizationId;
String token = RedisUtil.getString(0, redisKey);
if (StringUtils.isEmpty(token)) {
Map<String, String> loginMap = new HashMap<>();
//获取不同商户的账号和密码
ParkSecret parkSecret = iParkSecretService.getOne(new LambdaQueryWrapper<ParkSecret>().eq(ParkSecret::getOrganizationId,organizationId));
if(parkSecret == null || org.apache.commons.lang3.StringUtils.isBlank(parkSecret.getUserName())){
return "";
}
loginMap.put("client_id", parkSecret.getUserName());
loginMap.put("client_secret", parkSecret.getPassword());
//表单形式提交发送http请求
String respon = HttpClientUtil.doPost(ParkContsant.PARK_LOGIN, loginMap);
Map<String, Object> parkToken = JsonUtils.convertJSONStringTOMap(respon);
String accessToken = (String) parkToken.get("access_token");
if (StringUtils.isEmpty(accessToken)) {
log.error("停车场token为空,停车场token请求失败");
token = "";
} else {
Integer expiresIn = (Integer) parkToken.get("expires_in");
//返回的停车场总token存入redis中
token = accessToken;
RedisUtil.setString(0, "accessToken", token);
//设置停车场总token过期时间
RedisUtil.expire(0, "accessToken", expiresIn);
}
}
return token;
}
JSONObject jsonObject = new JSONObject();
jsonObject.put("parkCode", parkCode);
jsonObject.put("unid", unid);
String jsonString = JSONObject.toJSONString(jsonObject);
Map<String, Object> responMap = parkUtils.doPostTokenJson(ParkContsant.PARK_PICTURE, jsonString,organizationId);
String status = (String) responMap.get("code");
if (!status.equals("200")) {
return "";
} else {
List<String> listPicture = (List<String>) responMap.get("data");
if(listPicture == null || listPicture.isEmpty()){
return "";
}
return listPicture.get(listPicture.size()-1);
}
public Map<String, Object> doPostTokenJson(String url, String json,Integer organizationId){
String respon = HttpClientUtil.doPostTokenJson(url, json, getToken(organizationId));
System.out.println("parkJson:"+respon);
Map<String, Object> stringObjectMap = JsonUtils.convertJSONStringTOMap(respon);
return stringObjectMap;
}
public static String doPostTokenJson(String url, String json,String token) {
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
// 创建请求内容
if(!Objects.isNull(token)){
httpPost.addHeader("access_token",token);
}
if(!Objects.isNull(json)){
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
}
// 执行http请求
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
public static String doPost(String url, Map<String, String> param) {
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
// 创建参数列表
if (param != null) {
List<NameValuePair> paramList = new ArrayList<>();
for (String key : param.keySet()) {
paramList.add(new BasicNameValuePair(key, param.get(key)));
}
// 模拟表单
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList, "UTF-8");
httpPost.setEntity(entity);
}
// 执行http请求
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
private static final ObjectMapper MAPPER = new ObjectMapper();
public static Map<String, Object> convertJSONStringTOMap(String jsonData) {
if (jsonData == null) {
return null;
}
try {
Map<String, Object> map = (Map<String, Object>) MAPPER.readValue(jsonData, Map.class);
return map;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
远程连接接口公用
最新推荐文章于 2024-11-15 18:38:47 发布