2023-02-16 15:28:15 +08:00
|
|
|
|
package com.zhgd.xmgl.util;
|
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.http.HttpUtil;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
import org.joda.time.DateTime;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @program: wisdomSite
|
|
|
|
|
|
* @description: 昌通码
|
|
|
|
|
|
* @author: Mr.Peng
|
|
|
|
|
|
* @create: 2021-09-09 18:12
|
|
|
|
|
|
**/
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
|
public class ElecardUtil {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static String createHMACSHA256Sign(SortedMap<Object, Object> parameters, String appSecret) {
|
|
|
|
|
|
StringBuffer sb = new StringBuffer();
|
|
|
|
|
|
Set es = parameters.entrySet();//所有参与传参的参数按照accsii排序(升序)
|
|
|
|
|
|
Iterator it = es.iterator();
|
|
|
|
|
|
while (it.hasNext()) {
|
|
|
|
|
|
Map.Entry entry = (Map.Entry) it.next();
|
|
|
|
|
|
String k = (String) entry.getKey();
|
|
|
|
|
|
Object v = entry.getValue();
|
|
|
|
|
|
if (null != v && !"".equals(v)
|
|
|
|
|
|
&& !"sign".equals(k) && !"key".equals(k)) {
|
|
|
|
|
|
sb.append(k + "=" + v + "&");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
sb.append("key=" + appSecret);
|
|
|
|
|
|
log.info(sb.toString());
|
|
|
|
|
|
String sign = EncriptionHelper.sha256_HMAC(sb.toString(), appSecret).toUpperCase();
|
|
|
|
|
|
return sign;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static String getToken(String url, String appId, String appSecert, String publicKey, String privateKey) {
|
|
|
|
|
|
String token = null;
|
|
|
|
|
|
try {
|
|
|
|
|
|
JSONObject obj = new JSONObject();
|
|
|
|
|
|
SortedMap<Object, Object> body = new TreeMap<Object, Object>();
|
|
|
|
|
|
body.put("msgType", "GET_TOKEN");
|
|
|
|
|
|
Map<String, String> param = new HashMap<>();
|
|
|
|
|
|
param.put("appSecret", appSecert);
|
|
|
|
|
|
byte[] bytes = RsaTool.encrypt(JSON.toJSONString(param).getBytes(), publicKey);
|
|
|
|
|
|
String msgBody = Base64.encodeBase64(bytes);
|
|
|
|
|
|
body.put("msgBody", msgBody);
|
|
|
|
|
|
String sign = createHMACSHA256Sign(body, appSecert);
|
|
|
|
|
|
JSONObject head = new JSONObject();
|
|
|
|
|
|
String timeStamp = DateTime.now().toString("yyyyMMddHHmmss");
|
|
|
|
|
|
head.put("appId", appId);
|
|
|
|
|
|
head.put("timeStamp", timeStamp);
|
|
|
|
|
|
head.put("sequenceId", System.currentTimeMillis() + "");
|
|
|
|
|
|
head.put("signture", sign);
|
|
|
|
|
|
obj.put("head", head);
|
|
|
|
|
|
obj.put("body", body);
|
|
|
|
|
|
String result = HttpUtil.post(url + "/elecard-open-web/api/v1", obj.toJSONString());
|
|
|
|
|
|
if (result != null && result.length() > 0) {
|
|
|
|
|
|
JSONObject jsonObject = JSONObject.parseObject(result);
|
|
|
|
|
|
JSONObject bodyObj = jsonObject.getJSONObject("body");
|
|
|
|
|
|
if ("0".equals(bodyObj.getString("resultCode"))) {
|
|
|
|
|
|
String base64 = bodyObj.getString("msgBody");
|
|
|
|
|
|
byte[] resultBytes = RsaTool.decrypt(Base64.decodeBase64(base64.toCharArray()), privateKey);
|
|
|
|
|
|
String resultObj = new String(resultBytes, "utf-8");
|
|
|
|
|
|
log.info(resultObj);
|
|
|
|
|
|
token = JSONObject.parseObject(resultObj).getString("accessToken");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (Exception e) {
|
2024-04-14 21:05:01 +08:00
|
|
|
|
log.error("error:", e);
|
2023-02-16 15:28:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
return token;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static String getCardInfo(String url, String appId, String appSecert, String publicKey, String privateKey, String token, String cardId) {
|
|
|
|
|
|
String enterStatus = null;
|
|
|
|
|
|
try {
|
|
|
|
|
|
JSONObject obj = new JSONObject();
|
|
|
|
|
|
SortedMap<Object, Object> body = new TreeMap<Object, Object>();
|
|
|
|
|
|
body.put("msgType", "GET_CARDIDINFO");
|
|
|
|
|
|
Map<String, String> param = new HashMap<>();
|
|
|
|
|
|
param.put("accessToken", token);
|
|
|
|
|
|
param.put("cardId", cardId);
|
|
|
|
|
|
byte[] bytes = RsaTool.encrypt(JSON.toJSONString(param).getBytes(), publicKey);
|
|
|
|
|
|
String msgBody = Base64.encodeBase64(bytes);
|
|
|
|
|
|
body.put("msgBody", msgBody);
|
|
|
|
|
|
String sign = createHMACSHA256Sign(body, appSecert);
|
|
|
|
|
|
JSONObject head = new JSONObject();
|
|
|
|
|
|
String timeStamp = DateTime.now().toString("yyyyMMddHHmmss");
|
|
|
|
|
|
head.put("appId", appId);
|
|
|
|
|
|
head.put("timeStamp", timeStamp);
|
|
|
|
|
|
head.put("sequenceId", System.currentTimeMillis() + "");
|
|
|
|
|
|
head.put("signture", sign);
|
|
|
|
|
|
obj.put("head", head);
|
|
|
|
|
|
obj.put("body", body);
|
|
|
|
|
|
String result = HttpUtil.post(url + "/elecard-open-web/api/v1", obj.toJSONString());
|
|
|
|
|
|
log.info(result);
|
|
|
|
|
|
if (result != null && result.length() > 0) {
|
|
|
|
|
|
JSONObject jsonObject = JSONObject.parseObject(result);
|
|
|
|
|
|
JSONObject bodyObj = jsonObject.getJSONObject("body");
|
|
|
|
|
|
if ("0".equals(bodyObj.getString("resultCode"))) {
|
|
|
|
|
|
String base64 = bodyObj.getString("msgBody");
|
|
|
|
|
|
byte[] resultBytes = RsaTool.decrypt(Base64.decodeBase64(base64.toCharArray()), privateKey);
|
|
|
|
|
|
String resultObj = new String(resultBytes, "utf-8");
|
|
|
|
|
|
log.info(resultObj);
|
|
|
|
|
|
//0 绿色
|
|
|
|
|
|
//1 橙色
|
|
|
|
|
|
//2 红色
|
|
|
|
|
|
enterStatus = JSONObject.parseObject(resultObj).getString("enterStatus");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (Exception e) {
|
2024-04-14 21:05:01 +08:00
|
|
|
|
log.error("error:", e);
|
2023-02-16 15:28:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
return enterStatus;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
|
String appId = "NV3pyiK6VzxRzL1j";
|
|
|
|
|
|
String appSecert = "i3gpNM55O3Bf6mz";
|
|
|
|
|
|
String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCCammp3Fh8XZ50OA5Mk4q0WhvpRsjM5pZ47dsHx7Hiz2CiR1H4UurVt0K4AlnZk88PHe0lUxvno43UWHnh2SNcdQdoVg/sM9GLsHmOJ15dtrZQPaTEbwfUNgzkRFq883jzj0zRqrF/9wLMpxnJgVoEYvMWswYIs64DEn6BgQOipwIDAQAB";
|
|
|
|
|
|
String RSAPrivate = "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAITi2c5rJnKgYawcNzlQyZxd2qjLUJszlDu+2vHRvCR/14eLdHleyDLJwPcatQhe8PjmSL+fzEKzdnOJ65EbuZkp2Gncm00J5xkiV5OQ6Yg+zZkFLCE3UOeIuD/gvQVEqzFOdTvelix1mqW+SchR4XvxKkecnZtrvAw5dZoOp/oXAgMBAAECgYEAgAaemUSaUi06bLryaYHv+3pgw+a9zgkvczA5eGj8vp18ZNZybMxoTKYcBvXwHBR8TRBwXk3yWQDVyhLCpZb0g7HVVlyzk1vtt5dqPK8zvvgtYYrqMI5FcnkinbJ7SObgtxQi7h75RvJUxXf3TjxgzHc3+bjMeY2uaQdmsenx3IECQQDrSXRNGo+sMhdIEPe56kEfRDGL6u3zygY5f2yW21nXPSV/ct4G/WNl97Hi328RYqH/tWHOmOlotVxCYLFljxxHAkEAkJWj5SAtjCadozE7rYrbUquiHdGwG4Z85M/NgrSFfCYxWYS3lZ/uAdHrpdgvCbcboC/SWzgMSc0imjPlXZ6rsQJBAKj9ziGfBTg5lg012qfWv8VUNn33U8c5ADWF9xl3HNUCXw9mEmBU2HKhcEuyDVUgUZiWz1QS8fo54LTwPBnkqvsCQGv65fFr67hc6qQDU++S4aPHPQcKIH0eHs6AB5rTGVIT8A1MGhOr4oQHc/djEhYuY3pp3K91l2/SvUHu+iDGLLECQQCYkiRX4siTSR9t0qVZ1dEAKuG30memOFA0+Rt305n9o7fvonw89xrQrnvixn4/KtKEZTsgYYW6JfZ7Z4ANoI+7";
|
|
|
|
|
|
//String token="202cbbca4d2674f040c1d4e7a24865adf89b439c9ec29196f48acd4099dc3acafa9eb97fee40fd74700d8d9d56047f87";
|
|
|
|
|
|
String token = getToken("https://elemanager.hcctm.com/", appId, appSecert, publicKey, RSAPrivate);
|
|
|
|
|
|
log.info(getCardInfo("https://elemanager.hcctm.com/", appId, appSecert, publicKey, RSAPrivate, token, "37242919711222591X"));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|