update 对接lianlian平台

This commit is contained in:
2024-05-08 18:40:45 +08:00
parent 6a82341c8a
commit 376e14adeb
4 changed files with 222 additions and 22 deletions

View File

@@ -2,6 +2,7 @@ package com.jsowell.web.controller.thirdparty;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.Maps;
import com.jsowell.common.annotation.Anonymous;
import com.jsowell.common.core.controller.BaseController;
@@ -20,6 +21,9 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
/**
@@ -61,7 +65,7 @@ public class ThirdPartyBaseController extends BaseController {
* @return
* @param <T>
*/
protected <T> T parseParamsDTO(CommonParamsDTO dto, Class<T> targetClass) {
protected <T> T parseParamsDTO(CommonParamsDTO dto, Class<T> targetClass) throws NoSuchFieldException, IllegalAccessException {
// 解密
String operatorId = StringUtils.isNotBlank(dto.getOperatorID()) ? dto.getOperatorID() : dto.getPlatformID();
// 通过operatorId 查出 operatorSecret
@@ -76,7 +80,115 @@ public class ThirdPartyBaseController extends BaseController {
// 解密data 获取参数中的OperatorSecret
String decrypt = Cryptos.decrypt(dto.getData(), dataSecret, dataSecretIv);
return JSONObject.parseObject(decrypt, targetClass);
T t = JSONObject.parseObject(decrypt, targetClass);
// 校验是否有operatorId, 没有就set
verifyOperatorId(dto, t);
return t;
}
/**
* 获取OperatorId的值 verify
* @param t
* @return
* @throws NoSuchFieldException
* @throws IllegalAccessException
*/
private <T> void verifyOperatorId(CommonParamsDTO dto, T t) throws NoSuchFieldException, IllegalAccessException {
String targetFieldName = "operatorId";
String operatorId = (String) getFieldValueByObject(t, targetFieldName);
if (StringUtils.isBlank(operatorId)) {
setFieldValueByFieldName(t, targetFieldName, dto.getOperatorID());
}
}
private <T> void setFieldValueByFieldName(T t, String targetFieldName, Object value) {
// 获取该对象的class
Class<? extends Object> tClass = t.getClass();
// 获取所有的属性数组
Field[] fields = tClass.getDeclaredFields();
for (Field field : fields) {
// 属性名称
String currentFieldName = "";
try {
boolean has_JsonProperty = field.isAnnotationPresent(JsonProperty.class);
if (has_JsonProperty) {
currentFieldName = field.getAnnotation(JsonProperty.class).value();
} else {
currentFieldName = field.getName();
}
// 忽略大小写对比
if (currentFieldName.equalsIgnoreCase(targetFieldName)) {
// 取消语言访问检查
field.setAccessible(true);
// 给变量赋值
field.set(t, value);
return;
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
/**
* 使用反射获取字段值
*/
private <T> Object getFieldValueByObject(T t, String targetFieldName) throws NoSuchFieldException, IllegalAccessException {
// 获取该对象的class
Class<? extends Object> tClass = t.getClass();
// 获取所有的属性数组
Field[] fields = tClass.getDeclaredFields();
/**
* 这里只需要 id 这个属性,所以直接取 fields[0] 这
* 一个如果id不是排在第一位自己取相应的位置
* 如果有需要可以写成for循环遍历全部属性
*/
for (Field field : fields) {
// 属性名称
String currentFieldName = "";
// 获取属性上面的注解 import com.fasterxml.jackson.annotation.JsonProperty;
/**
* 举例: @JsonProperty("roleIds")
* private String roleIds;
*/
try {
boolean has_JsonProperty = field.isAnnotationPresent(JsonProperty.class);
if (has_JsonProperty) {
currentFieldName = field.getAnnotation(JsonProperty.class).value();
} else {
currentFieldName = field.getName();
}
// 忽略大小写对比
if (currentFieldName.equalsIgnoreCase(targetFieldName)) {
field.setAccessible(true);
currentFieldName = currentFieldName.replaceFirst(currentFieldName.substring(0, 1), currentFieldName.substring(0, 1).toUpperCase());
//整合出 getId() 属性这个方法
Method m = tClass.getMethod("get" + currentFieldName);
return m.invoke(t);
}
} catch (SecurityException e) {
// 安全性异常
e.printStackTrace();
} catch (IllegalArgumentException e) {
// 非法参数
e.printStackTrace();
} catch (IllegalAccessException | NoSuchMethodException e) {
// 无访问权限
e.printStackTrace();
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
}
return null;
}
/**

View File

@@ -3,7 +3,6 @@ package com.jsowell.web.controller.thirdparty.lianlian;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.jsowell.common.annotation.Anonymous;
import com.jsowell.common.core.controller.BaseController;
import com.jsowell.common.enums.thirdparty.ThirdPlatformTypeEnum;
import com.jsowell.common.enums.ykc.ReturnCodeEnum;
import com.jsowell.common.exception.BusinessException;
@@ -18,6 +17,7 @@ import com.jsowell.thirdparty.lianlian.common.CommonResult;
import com.jsowell.thirdparty.platform.ThirdPartyPlatformService;
import com.jsowell.thirdparty.platform.util.Cryptos;
import com.jsowell.thirdparty.platform.util.Encodes;
import com.jsowell.web.controller.thirdparty.ThirdPartyBaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
@@ -35,7 +35,7 @@ import java.util.Map;
@Anonymous
@RestController
@RequestMapping("/LianLian")
public class LianLianController extends BaseController {
public class LianLianController extends ThirdPartyBaseController {
// @Autowired
// private LianLianService lianLianService;
@@ -106,6 +106,9 @@ public class LianLianController extends BaseController {
QueryStationInfoDTO queryStationInfoDTO = JSONObject.parseObject(dataStr, QueryStationInfoDTO.class);
queryStationInfoDTO.setOperatorId(dto.getOperatorID());
// Map<String, String> map = lianLianService.query_stations_info(queryStationInfoDTO);
QueryOperatorInfoDTO paramDTO = parseParamsDTO(dto, QueryOperatorInfoDTO.class);
Map<String, String> map = lianLianService.queryStationsInfo(queryStationInfoDTO);
return CommonResult.success(0, "查询充电站信息成功!", map.get("Data"), map.get("Sig"));
@@ -415,10 +418,11 @@ public class LianLianController extends BaseController {
try {
// 校验令牌
String token = request.getHeader("Authorization");
if (!JWTUtils.checkThirdPartyToken(token)) {
// 校验失败
return CommonResult.failed("令牌校验错误");
}
// if (!JWTUtils.checkThirdPartyToken(token)) {
// // 校验失败
// return CommonResult.failed("令牌校验错误");
// }
// 校验签名
// Map<String, String> resultMap = lianLianService.checkoutSign(dto);
// if (resultMap == null) {
@@ -431,19 +435,23 @@ public class LianLianController extends BaseController {
// String dataSecretIV = resultMap.get("DataSecretIV");
// 查询配置信息
ThirdPartyPlatformConfig platformConfig = thirdPartyPlatformConfigService.getInfoByOperatorId(dto.getOperatorID());
String operatorSecret = platformConfig.getOperatorSecret();
String dataString = dto.getData();
String dataSecret = platformConfig.getDataSecret();
String dataSecretIV = platformConfig.getDataSecretIv();
// ThirdPartyPlatformConfig platformConfig = thirdPartyPlatformConfigService.getInfoByOperatorId(dto.getOperatorID());
// String operatorSecret = platformConfig.getOperatorSecret();
// String dataString = dto.getData();
// String dataSecret = platformConfig.getDataSecret();
// String dataSecretIV = platformConfig.getDataSecretIv();
// 解密data
byte[] plainText = Cryptos.aesDecrypt(Encodes.decodeBase64(dataString), dataSecret.getBytes(), dataSecretIV.getBytes());
String dataStr = new String(plainText, StandardCharsets.UTF_8);
// byte[] plainText = Cryptos.aesDecrypt(Encodes.decodeBase64(dataString), dataSecret.getBytes(), dataSecretIV.getBytes());
// String dataStr = new String(plainText, StandardCharsets.UTF_8);
// 转换成相应对象
QueryStartChargeDTO queryStartChargeDTO = JSONObject.parseObject(dataStr, QueryStartChargeDTO.class);
queryStartChargeDTO.setOperatorId(dto.getOperatorID());
// QueryStartChargeDTO queryStartChargeDTO = JSONObject.parseObject(dataStr, QueryStartChargeDTO.class);
// queryStartChargeDTO.setOperatorId(dto.getOperatorID());
// Map<String, String> map = lianLianService.query_start_charge(queryStartChargeDTO);
QueryStartChargeDTO queryStartChargeDTO = parseParamsDTO(dto, QueryStartChargeDTO.class);
queryStartChargeDTO.setOperatorId(dto.getOperatorID());
Map<String, String> map = lianLianService.queryStartCharge(queryStartChargeDTO);
return CommonResult.success(0, "请求启动充电成功!", map.get("Data"), map.get("Sig"));