远程连接接口公用

 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;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值