no message
parent
094efb7c43
commit
2190d3f353
|
|
@ -12,6 +12,7 @@ import com.youchain.businessdata.service.LesService;
|
|||
import com.youchain.businessdata.service.dto.LesDto;
|
||||
import com.youchain.businessdata.service.dto.LesQueryCriteria;
|
||||
import com.youchain.businessdata.service.mapstruct.LesMapper;
|
||||
import com.youchain.config.SignConfig;
|
||||
import com.youchain.exception.BadRequestException;
|
||||
import com.youchain.modules.system.domain.Dict;
|
||||
import com.youchain.modules.system.domain.DictDetail;
|
||||
|
|
@ -174,69 +175,130 @@ public class LesServiceImpl implements LesService {
|
|||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void lesCallBack(Les les) {
|
||||
//接口开关是否启动
|
||||
Dict dict = dictRepository.findDictByName("OPEN");
|
||||
String resultJson = "";
|
||||
String json = "";
|
||||
if (dict != null) {
|
||||
Dict signeDict = dictRepository.findDictByName("signe");
|
||||
if (signeDict == null) {
|
||||
throw new BadRequestException("signe签名未配置");
|
||||
}
|
||||
DictDetail dictDetail = dictDetailRepository.findDictDetailByLabel(signeDict.getId(), signeDict.getDescription());
|
||||
String value = dictDetail.getValue();
|
||||
JSONObject jsonObject = JSONObject.parseObject(value);
|
||||
if (jsonObject == null) {
|
||||
throw new BadRequestException("signe签名格式错误");
|
||||
}
|
||||
String host = jsonObject.getString("host");
|
||||
String path = jsonObject.getString("path");
|
||||
String appId = jsonObject.getString("appId");
|
||||
String appSecret = jsonObject.getString("appSecret");
|
||||
String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
|
||||
|
||||
//jsonBody参数
|
||||
json = this.lesCallBackJson(les.getDstPositionCode(), les.getTaskCode());
|
||||
try {
|
||||
// 生成请求参数
|
||||
String json = this.lesCallBackJson(les.getDstPositionCode(), les.getTaskCode());
|
||||
log.info("LES回传参数: {}", json);
|
||||
|
||||
//生成签名;
|
||||
String sign = SignerUtils.generateSign(host, path, appId, timestamp, json, appSecret);
|
||||
log.info("生成的签名: {}", sign);
|
||||
|
||||
//请求url
|
||||
String url = SignerUtils.buildFullUrl(host, path, appId, timestamp, sign);
|
||||
log.info("回传LES请求路径: {}", url);
|
||||
|
||||
//返回结果
|
||||
resultJson = HttpPostUtil.sendPostReq(url, json);
|
||||
log.info("LES返回报文:{}", resultJson);
|
||||
Map<String, String> result = parseResponse(resultJson);
|
||||
if (result == null) {
|
||||
if (StringUtils.isEmpty(resultJson)) {
|
||||
throw new BadRequestException("LES返回信息:LES回传接口调用失败!");
|
||||
}
|
||||
|
||||
JSONObject resulObject = JSON.parseObject(resultJson);
|
||||
if (resulObject == null) {
|
||||
throw new BadRequestException("LES返回信息:LES回传接口返回为空!");
|
||||
}
|
||||
|
||||
String resultCode = resulObject.getString("resultCode");
|
||||
String displayMsg = resulObject.getString("displayMsg");
|
||||
if (!"success".equals(resultCode)) {
|
||||
throw new BadRequestException("LES返回消息:" + displayMsg);
|
||||
}
|
||||
} else {
|
||||
String resultCode = result.get("resultCode");
|
||||
String displayMsg = result.get("displayMsg");
|
||||
if (!"success".equals(resultCode)) {
|
||||
throw new BadRequestException("LES返回消息:" + displayMsg);
|
||||
}
|
||||
// 检查接口开关
|
||||
if (!isInterfaceEnabled()) {
|
||||
// 更新LES状态
|
||||
updateLesStatus(les, null, json);
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取签名配置
|
||||
SignConfig signConfig = getSignConfig();
|
||||
|
||||
// 发送请求并处理响应
|
||||
String result = sendLesRequest(signConfig, json);
|
||||
validateLesResponse(result);
|
||||
|
||||
// 更新LES状态
|
||||
updateLesStatus(les, result, json);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("LES回传失败", e);
|
||||
throw new BadRequestException("LES回传失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查接口开关是否启用
|
||||
*/
|
||||
private boolean isInterfaceEnabled() {
|
||||
Dict dict = dictRepository.findDictByName("OPEN");
|
||||
return dict != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取签名配置
|
||||
*/
|
||||
private SignConfig getSignConfig() {
|
||||
Dict signeDict = dictRepository.findDictByName("signe");
|
||||
if (signeDict == null) {
|
||||
throw new BadRequestException("signe签名未配置");
|
||||
}
|
||||
|
||||
DictDetail dictDetail = dictDetailRepository.findDictDetailByLabel(
|
||||
signeDict.getId(), signeDict.getDescription());
|
||||
String value = dictDetail.getValue();
|
||||
|
||||
JSONObject jsonObject = JSONObject.parseObject(value);
|
||||
if (jsonObject == null) {
|
||||
throw new BadRequestException("signe签名格式错误");
|
||||
}
|
||||
|
||||
return SignConfig.builder()
|
||||
.host(jsonObject.getString("host"))
|
||||
.path(jsonObject.getString("path"))
|
||||
.appId(jsonObject.getString("appId"))
|
||||
.appSecret(jsonObject.getString("appSecret"))
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送LES请求
|
||||
*/
|
||||
private String sendLesRequest(SignConfig config, String json) {
|
||||
String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
|
||||
|
||||
// 生成签名
|
||||
String sign = SignerUtils.generateSign(
|
||||
config.getHost(), config.getPath(), config.getAppId(),
|
||||
timestamp, json, config.getAppSecret());
|
||||
log.info("生成的签名: {}", sign);
|
||||
|
||||
// 构建请求URL
|
||||
String url = SignerUtils.buildFullUrl(
|
||||
config.getHost(), config.getPath(), config.getAppId(), timestamp, sign);
|
||||
log.info("LES请求路径: {}", url);
|
||||
|
||||
// 发送请求
|
||||
String result = HttpPostUtil.sendPostReq(url, json);
|
||||
log.info("LES返回报文:{}", result);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证LES响应
|
||||
*/
|
||||
private void validateLesResponse(String result) {
|
||||
if (StringUtils.isEmpty(result)) {
|
||||
throw new BadRequestException("LES返回信息:LES回传接口调用失败!");
|
||||
}
|
||||
|
||||
//解析XML格式
|
||||
Map<String, String> resultMap = parseResponse(result);
|
||||
if (resultMap == null) {
|
||||
//解析JSON格式
|
||||
JSONObject resulObject = JSON.parseObject(result);
|
||||
if (resulObject == null) {
|
||||
throw new BadRequestException("LES返回信息:LES回传接口返回为空!");
|
||||
}
|
||||
|
||||
String resultCode = resulObject.getString("resultCode");
|
||||
String displayMsg = resulObject.getString("displayMsg");
|
||||
if (!"success".equals(resultCode)) {
|
||||
throw new BadRequestException("LES返回消息:" + displayMsg);
|
||||
}
|
||||
} else {
|
||||
String resultCode = resultMap.get("resultCode");
|
||||
String displayMsg = resultMap.get("displayMsg");
|
||||
if (!"success".equals(resultCode)) {
|
||||
throw new BadRequestException("LES返回消息:" + displayMsg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新LES状态(成功)
|
||||
*/
|
||||
private void updateLesStatus(Les les, String result, String json) {
|
||||
les.setStatus(BizStatus.CLOSE);
|
||||
les.setRepMessage(resultJson);
|
||||
les.setRepMessage(result);
|
||||
les.setReqMessage(json);
|
||||
les.setReturnTime(new Timestamp(System.currentTimeMillis()));
|
||||
lesRepository.save(les);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,16 @@
|
|||
package com.youchain.config;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 签名配置类
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
public class SignConfig {
|
||||
private String host;
|
||||
private String path;
|
||||
private String appId;
|
||||
private String appSecret;
|
||||
}
|
||||
Loading…
Reference in New Issue