mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-04-22 03:55:17 +08:00
新增 商家推送充电设备动态数据 接口
This commit is contained in:
@@ -1,6 +1,14 @@
|
||||
package com.jsowell.thirdparty.amap.util;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.io.*;
|
||||
import java.security.*;
|
||||
@@ -19,6 +27,9 @@ public class AMapUtils {
|
||||
@Value("${aMap.aMapPublicKey}")
|
||||
private static String AMAP_PUBLIC_KEY;
|
||||
|
||||
@Value("${aMap.appId}")
|
||||
private static String APP_ID;
|
||||
|
||||
/**
|
||||
* 使用商家私钥生成签名
|
||||
*
|
||||
@@ -32,6 +43,69 @@ public class AMapUtils {
|
||||
return getSign(signContent, MERCHANT_PRIVATE_KEY);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造参数map
|
||||
* @param bizContent 业务参数
|
||||
* @param SPI 高德文档中每个接口的SPI
|
||||
* @return 构造好可直接发送请求的map参数
|
||||
*/
|
||||
public static Map<String, String> generateParamMap(JSONObject bizContent, String SPI) throws Exception {
|
||||
Map<String, String> map = new LinkedHashMap<>();
|
||||
map.put("biz_content", JSONObject.toJSONString(bizContent));
|
||||
map.put("app_id", APP_ID);
|
||||
map.put("utc_timestamp", String.valueOf(System.currentTimeMillis()));
|
||||
map.put("version", "1.0");
|
||||
map.put("charset", "UTF-8");
|
||||
map.put("method", SPI);
|
||||
// 生成签名
|
||||
String sign = AMapUtils.generateSign(map);
|
||||
map.put("sign", sign);
|
||||
map.put("sign_type", "RSA2");
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* 向高德地图发送请求
|
||||
* @param paramMap 请求参数
|
||||
* @return 高德返回结果
|
||||
*/
|
||||
public static String sendPost(Map<String, String> paramMap) {
|
||||
// 定义Content-type、初始化HttpEntity
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
||||
MultiValueMap<String, Object> formBody = convertToMultiValueMap(paramMap);
|
||||
HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(formBody, headers);
|
||||
|
||||
// 调用高德接口
|
||||
// 线下联调环境:https://restapi.amap.com/rest/openmp/devgw?key=高德云店秘钥key
|
||||
// 线上环境:https://restapi.amap.com/rest/openmp/gw?key=高德云店秘钥key
|
||||
// 高德云店秘钥key请登录高德云店「接入准备及配置」页面查看
|
||||
String url = "https://restapi.amap.com/rest/openmp/devgw?key=77ff5350723d29901fa859515f553644";
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
ResponseEntity<String> resp = restTemplate.postForEntity(url, entity, String.class);
|
||||
if (resp.getBody() != null && !"".equals(resp.getBody())) {
|
||||
JSONObject respObj = JSONObject.parseObject(resp.getBody());
|
||||
System.out.println("高德返回结果:" + respObj);
|
||||
return JSONObject.toJSONString(respObj);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将Map转为MultiValueMap
|
||||
* (MultiValueMap 可一个 key 对应多个 value)
|
||||
* @param paramMap 业务参数map
|
||||
* @return MultiValueMap
|
||||
*/
|
||||
private static MultiValueMap<String, Object> convertToMultiValueMap(Map<String, String> paramMap) {
|
||||
MultiValueMap<String, Object> multiValueMap = new LinkedMultiValueMap<>();
|
||||
for (Map.Entry<String, String> entry : paramMap.entrySet()) {
|
||||
multiValueMap.add(entry.getKey(), entry.getValue());
|
||||
}
|
||||
return multiValueMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* 参数转换为待加签字符串
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user