package com.ruoyi.jianguan.util; import com.alibaba.fastjson.JSONObject; import com.ruoyi.common.core.utils.HttpUtils; import com.ruoyi.common.redis.service.RedisService; import com.ruoyi.jianguan.constant.ChargePileInterface; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.math.BigDecimal; import java.util.List; import java.util.concurrent.TimeUnit; /** * 通用http发送方法 * * @author ruoyi */ @Component @Slf4j public class ChargePileHttpUtil { private static final String OperatorID = "MA01H3BQ3"; private static final String OperatorSecret = "f1331ef0b37c2d2"; private static final String SigSecret = "a6fedf0e1b27d6f8"; private static final String DataSecret = "50a61b93919c9606"; private static final String DataSecretIV = "7c8ac6861661d586"; // @Autowired // private RedisService redisService; /** * 验签 * @return */ public static boolean checkSig(){ return true; } /** * 获取token * @return */ public String getToken(){ String token =""; // String token = redisService.getCacheObject(ChargePileInterface.TOKEN_KEY); // if(StringUtils.hasLength(token)){ // return token; // } JSONObject jsonObject = new JSONObject(); jsonObject.put("OperatorID", OperatorID); jsonObject.put("OperatorSecret", OperatorSecret); String params = jsonObject.toJSONString(); // 参数加密 String data = AesEncryption.encrypt(DataSecret, DataSecretIV, params); // 获取签名 String timeStamp = System.currentTimeMillis() + ""; SequenceGenerator generator = new SequenceGenerator(); String nextSequence = generator.getNextSequence(); String hmacMD5 = HMacMD5Util.getHMacMD5(OperatorID,timeStamp, data,nextSequence,SigSecret); jsonObject = new JSONObject(); jsonObject.put("OperatorID", OperatorID); jsonObject.put("Data", data); jsonObject.put("TimeStamp", timeStamp); jsonObject.put("Seq", nextSequence); jsonObject.put("Sig", hmacMD5); String result = HttpUtils.sendPost(ChargePileInterface.QUERY_TOKEN, jsonObject.toJSONString()); log.info("获取token请求结果:{}",result); String string = JSONObject.parseObject(result).getString("Data"); String decrypt = AesEncryption.decrypt(DataSecret, DataSecretIV, string); JSONObject tokenResult = JSONObject.parseObject(decrypt); token = tokenResult.getString("AccessToken"); // Integer TokenAvailableTime = tokenResult.getInteger("TokenAvailableTime"); // redisService.setCacheObject(ChargePileInterface.TOKEN_KEY,token,Long.valueOf(TokenAvailableTime)-60L, TimeUnit.SECONDS); return token; } /** * 获取充电桩 * @return */ public String queryStationsInfo(Integer PageNo, Integer PageSize){ String token = getToken(); JSONObject jsonObject = new JSONObject(); jsonObject.put("PageNo", PageNo); jsonObject.put("PageSize", PageSize); String params = jsonObject.toJSONString(); // 参数加密 String data = AesEncryption.encrypt(DataSecret, DataSecretIV, params); // 获取签名 String timeStamp = System.currentTimeMillis() + ""; SequenceGenerator generator = new SequenceGenerator(); String nextSequence = generator.getNextSequence(); String hmacMD5 = HMacMD5Util.getHMacMD5(OperatorID,timeStamp, data,nextSequence,SigSecret); jsonObject = new JSONObject(); jsonObject.put("OperatorID", OperatorID); jsonObject.put("Data", data); jsonObject.put("TimeStamp", timeStamp); jsonObject.put("Seq", nextSequence); jsonObject.put("Sig", hmacMD5); String result = HttpUtils.sendPost(ChargePileInterface.QUERY_STATIONS_INFO, jsonObject.toJSONString(),token); String string = JSONObject.parseObject(result).getString("Data"); String decrypt = AesEncryption.decrypt(DataSecret, DataSecretIV, string); System.err.println(decrypt); return decrypt; } /** * 获取充电桩 * @return */ public String getOrderList(Integer PageNo, Integer PageSize){ String token = getToken(); JSONObject jsonObject = new JSONObject(); jsonObject.put("PageNo", PageNo); jsonObject.put("PageSize", PageSize); String params = jsonObject.toJSONString(); // 参数加密 String data = AesEncryption.encrypt(DataSecret, DataSecretIV, params); // 获取签名 String timeStamp = System.currentTimeMillis() + ""; SequenceGenerator generator = new SequenceGenerator(); String nextSequence = generator.getNextSequence(); String hmacMD5 = HMacMD5Util.getHMacMD5(OperatorID,timeStamp, data,nextSequence,SigSecret); jsonObject = new JSONObject(); jsonObject.put("OperatorID", OperatorID); jsonObject.put("Data", data); jsonObject.put("TimeStamp", timeStamp); jsonObject.put("Seq", nextSequence); jsonObject.put("Sig", hmacMD5); String result = HttpUtils.sendPost(ChargePileInterface.QUERY_ORDER_LIST, jsonObject.toJSONString(),token); String string = JSONObject.parseObject(result).getString("Data"); String decrypt = AesEncryption.decrypt(DataSecret, DataSecretIV, string); System.err.println(decrypt); return decrypt; } /** * 获取充电桩 * @return */ public String queryStationStatus(List StationIDs){ String token = getToken(); JSONObject jsonObject = new JSONObject(); jsonObject.put("StationIDs", StationIDs); String params = jsonObject.toJSONString(); // 参数加密 String data = AesEncryption.encrypt(DataSecret, DataSecretIV, params); // 获取签名 String timeStamp = System.currentTimeMillis() + ""; SequenceGenerator generator = new SequenceGenerator(); String nextSequence = generator.getNextSequence(); String hmacMD5 = HMacMD5Util.getHMacMD5(OperatorID,timeStamp, data,nextSequence,SigSecret); jsonObject = new JSONObject(); jsonObject.put("OperatorID", OperatorID); jsonObject.put("Data", data); jsonObject.put("TimeStamp", timeStamp); jsonObject.put("Seq", nextSequence); jsonObject.put("Sig", hmacMD5); String result = HttpUtils.sendPost(ChargePileInterface.QUERY_STATION_STATUS, jsonObject.toJSONString(),token); String string = JSONObject.parseObject(result).getString("Data"); String decrypt = AesEncryption.decrypt(DataSecret, DataSecretIV, string); System.err.println(decrypt); return decrypt; } /** * 请求设备认证 * @param EquipAuthSeq 设备认证流水号 格式“运营商ID+唯一编号”,27字符 * @param ConnectorID 充电设备接口编码 格式“6位行政区编码,4位电站编码,3位终端编码”,13位字符 * @return */ public String queryEquipAuth(String EquipAuthSeq,String ConnectorID){ String token = getToken(); JSONObject jsonObject = new JSONObject(); jsonObject.put("EquipAuthSeq", EquipAuthSeq); jsonObject.put("ConnectorID", ConnectorID); String params = jsonObject.toJSONString(); // 参数加密 String data = AesEncryption.encrypt(DataSecret, DataSecretIV, params); // 获取签名 String timeStamp = System.currentTimeMillis() + ""; SequenceGenerator generator = new SequenceGenerator(); String nextSequence = generator.getNextSequence(); String hmacMD5 = HMacMD5Util.getHMacMD5(OperatorID,timeStamp, data,nextSequence,SigSecret); jsonObject = new JSONObject(); jsonObject.put("OperatorID", OperatorID); jsonObject.put("Data", data); jsonObject.put("TimeStamp", timeStamp); jsonObject.put("Seq", nextSequence); jsonObject.put("Sig", hmacMD5); String result = HttpUtils.sendPost(ChargePileInterface.QUERY_EQUIP_AUTH, jsonObject.toJSONString(),token); String string = JSONObject.parseObject(result).getString("Data"); String decrypt = AesEncryption.decrypt(DataSecret, DataSecretIV, string); System.err.println(decrypt); return decrypt; } /** * 请求充电 * @param StartChargeSeq 充电订单号 格式“运营商ID+唯一编号”,27字符 * @param ConnectorID 充电设备接口编码 格式“6位行政区编码,4位电站编码,3位终端编码”,13位字符 * @param QRCode 二维码其他信息 二维码中含有自定义部分的,需要将二维码自定义部分的原文传输。 * @param PhoneNum 手机号 个人支付必传,格式确保正确 * @param PlateNum 车牌号 停车减免必传,格式确保正确 * @return */ public String queryStartCharge(String StartChargeSeq,String ConnectorID,String QRCode,String PhoneNum,String PlateNum,BigDecimal money){ String token = getToken(); JSONObject jsonObject = new JSONObject(); jsonObject.put("StartChargeSeq", StartChargeSeq); jsonObject.put("ConnectorID", ConnectorID); jsonObject.put("QRCode", QRCode); jsonObject.put("PhoneNum", PhoneNum); jsonObject.put("PlateNum", PlateNum); jsonObject.put("chargingAmt", money); String params = jsonObject.toJSONString(); String data = AesEncryption.encrypt(DataSecret, DataSecretIV, params); // 获取签名 String timeStamp = System.currentTimeMillis() + ""; SequenceGenerator generator = new SequenceGenerator(); String nextSequence = generator.getNextSequence(); String hmacMD5 = HMacMD5Util.getHMacMD5(OperatorID,timeStamp, data,nextSequence,SigSecret); jsonObject = new JSONObject(); jsonObject.put("OperatorID", OperatorID); jsonObject.put("Data", data); jsonObject.put("TimeStamp", timeStamp); jsonObject.put("Seq", nextSequence); jsonObject.put("Sig", hmacMD5); String result = HttpUtils.sendPost(ChargePileInterface.QUERY_START_CHARGE, jsonObject.toJSONString(),token); String string = JSONObject.parseObject(result).getString("Data"); String decrypt = AesEncryption.decrypt(DataSecret, DataSecretIV, string); System.err.println(decrypt); return decrypt; } /** * 请求停止充电 * @param StartChargeSeq 充电订单号 格式“运营商ID+唯一编号”,27字符 * @param ConnectorID 充电设备接口编码 格式“6位行政区编码,4位电站编码,3位终端编码”,13位字符 * @return */ public String queryStopCharge(String StartChargeSeq,String ConnectorID){ String token = getToken(); JSONObject jsonObject = new JSONObject(); jsonObject.put("StartChargeSeq", StartChargeSeq); jsonObject.put("ConnectorID", ConnectorID); String params = jsonObject.toJSONString(); // 参数加密 String data = AesEncryption.encrypt(DataSecret, DataSecretIV, params); // 获取签名 String timeStamp = System.currentTimeMillis() + ""; SequenceGenerator generator = new SequenceGenerator(); String nextSequence = generator.getNextSequence(); String hmacMD5 = HMacMD5Util.getHMacMD5(OperatorID,timeStamp, data,nextSequence,SigSecret); jsonObject = new JSONObject(); jsonObject.put("OperatorID", OperatorID); jsonObject.put("Data", data); jsonObject.put("TimeStamp", timeStamp); jsonObject.put("Seq", nextSequence); jsonObject.put("Sig", hmacMD5); String result = HttpUtils.sendPost(ChargePileInterface.QUERY_STOP_CHARGE, jsonObject.toJSONString(),token); String string = JSONObject.parseObject(result).getString("Data"); String decrypt = AesEncryption.decrypt(DataSecret, DataSecretIV, string); System.err.println(decrypt); return decrypt; } /** * 查询订单状态 */ public String queryEquipChargeStatus(String StartChargeSeq){ String token = getToken(); JSONObject jsonObject = new JSONObject(); jsonObject.put("StartChargeSeq", StartChargeSeq); String params = jsonObject.toJSONString(); // 参数加密 String data = AesEncryption.encrypt(DataSecret, DataSecretIV, params); // 获取签名 String timeStamp = System.currentTimeMillis() + ""; SequenceGenerator generator = new SequenceGenerator(); String nextSequence = generator.getNextSequence(); String hmacMD5 = HMacMD5Util.getHMacMD5(OperatorID,timeStamp, data,nextSequence,SigSecret); jsonObject = new JSONObject(); jsonObject.put("OperatorID", OperatorID); jsonObject.put("Data", data); jsonObject.put("TimeStamp", timeStamp); jsonObject.put("Seq", nextSequence); jsonObject.put("Sig", hmacMD5); String result = HttpUtils.sendPost(ChargePileInterface.QUERY_EQUIP_CHARGE_STATUS, jsonObject.toJSONString(),token); String string = JSONObject.parseObject(result).getString("Data"); String decrypt = AesEncryption.decrypt(DataSecret, DataSecretIV, string); System.err.println(decrypt); return decrypt; } }