no message

main
HUOJIN\92525 2025-08-30 13:18:18 +08:00
parent d923f6e007
commit c56d4a3f80
5 changed files with 60 additions and 82 deletions

View File

@ -246,8 +246,7 @@ public class NioF3AppServiceImpl implements NioF3AppService {
}
//回传LES
String signe="";
lesService.lesCallBack(les, lesService.lesSigneJson(UrlApi.app_id, signe));
lesService.lesCallBack(les);
}
/**

View File

@ -91,13 +91,11 @@ public interface LesService {
*/
String lesSigneJson(String app_id, String signe);
/**
* LES
* @param les les
* @param json
*/
void lesCallBack(Les les,String json);
void lesCallBack(Les les);
/**
*

View File

@ -14,6 +14,8 @@ import com.youchain.businessdata.service.dto.LesQueryCriteria;
import com.youchain.businessdata.service.mapstruct.LesMapper;
import com.youchain.exception.BadRequestException;
import com.youchain.modules.system.domain.Dict;
import com.youchain.modules.system.domain.DictDetail;
import com.youchain.modules.system.repository.DictDetailRepository;
import com.youchain.modules.system.repository.DictRepository;
import com.youchain.utils.*;
import lombok.RequiredArgsConstructor;
@ -41,6 +43,8 @@ public class LesServiceImpl implements LesService {
private final DictRepository dictRepository;
private final DictDetailRepository dictDetailRepository;
private final PointService pointService;
private final LesMapper lesMapper;
@ -159,6 +163,7 @@ public class LesServiceImpl implements LesService {
return jsonObject.toString();
}
@Override
public String lesSigneJson(String app_id, String signe) {
JSONObject jsonObject = new JSONObject(true);
@ -166,17 +171,48 @@ public class LesServiceImpl implements LesService {
jsonObject.put("signe", signe);
jsonObject.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000));
jsonObject.put("hash_type", "sha256");
log.info("回传LES请求参数: {}", jsonObject);
return jsonObject.toString();
}
@Override
@Transactional(rollbackFor = Exception.class)
public void lesCallBack(Les les, String json) {
public void lesCallBack(Les les) {
//接口开关是否启动
Dict dict = dictRepository.findDictByName("OPEN");
String resultJson = "";
String json = "";
if (dict != null) {
String url="";
resultJson = HttpPostUtil.sendPostReq(UrlApi.lesCallBack(), json);
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");
//json参数
String jsonBody = this.lesCallBackJson(les.getDstPositionCode(), les.getTaskCode());
//生成签名;
String sign = PostRequestSigner.generateSign(host, path, jsonBody, appSecret);
//请求url
String url = UrlApi.lesCallBack();
log.info("回传LES请求路径: {}", url);
//请求参数
json = this.lesSigneJson(appId, sign);
//返回结果
resultJson = HttpPostUtil.sendPostReq(url, json);
if (StringUtils.isEmpty(resultJson)) {
throw new BadRequestException("LES返回信息:LES回传接口调用失败!");
}
@ -207,7 +243,7 @@ public class LesServiceImpl implements LesService {
throw new BadRequestException(currentPositionCode + "停靠点未识别到任务");
}
List<Long> lesIds = lesList.stream().map(Les::getId).collect(Collectors.toList());
lesRepository.batchUpdateStatus(BizStatus.ARRIVED,lesIds);
lesRepository.batchUpdateStatus(BizStatus.ARRIVED, lesIds);
}
/**

View File

@ -18,19 +18,24 @@ package com.youchain.modules.system.repository;
import com.youchain.modules.system.domain.DictDetail;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
/**
* @author Liu Xue
* @date 2019-04-10
*/
* @author Liu Xue
* @date 2019-04-10
*/
public interface DictDetailRepository extends JpaRepository<DictDetail, Long>, JpaSpecificationExecutor<DictDetail> {
/**
*
*
* @param name /
* @return /
*/
List<DictDetail> findByDictName(String name);
@Query("from DictDetail dictDetail where dictDetail.dict.id=:dictId and dictDetail.label=:label ")
DictDetail findDictDetailByLabel(Long dictId, String label);
}

View File

@ -1,5 +1,7 @@
package com.youchain.utils;
import lombok.extern.slf4j.Slf4j;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
@ -7,38 +9,24 @@ import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
@Slf4j
public class PostRequestSigner {
/**
* POST
*
* @param host ()
* @param path
* @param urlParams URLsigne
* @param jsonBody JSON
* @param appSecret
* @param accessToken 访null
* @param host ()
* @param path
* @param jsonBody JSON
* @param appSecret
* @return
*/
public static String generateSign(String host, String path, Map<String, String> urlParams,
String jsonBody, String appSecret, String accessToken) {
public static String generateSign(String host, String path,
String jsonBody, String appSecret) {
try {
// 1. 准备所有需要参与签名的参数
Map<String, String> allParams = new TreeMap<>(); // TreeMap会自动按key升序排序
// 添加URL参数排除signe
if (urlParams != null && !urlParams.isEmpty()) {
for (Map.Entry<String, String> entry : urlParams.entrySet()) {
String key = entry.getKey();
// 跳过signe参数
if ("signe".equals(key)) {
continue;
}
allParams.put(key, entry.getValue() != null ? entry.getValue() : "");
}
}
// 添加JSON Body参数
if (jsonBody != null && !jsonBody.isEmpty()) {
allParams.put("jsonBody", jsonBody);
@ -61,11 +49,8 @@ public class PostRequestSigner {
.append("?").append(paramsStr.toString()) // 参数部分
.append(appSecret); // appSecret
// 如果有access_token添加到签名字符串
if (accessToken != null && !accessToken.isEmpty()) {
signSource.append(accessToken);
}
log.info("签名生成: {}", signSource);
// 4. 计算SHA256哈希并转为小写字符串
return sha256(signSource.toString());
@ -96,49 +81,4 @@ public class PostRequestSigner {
}
return hexString.toString();
}
private static String SignerUrl() {
try {
// 示例参数
String host = "tsp-dev.nio.com";
String path = "/api/1/infotainment/weather/now";
String appId = "test_app_id";
String appSecret = "test_app_secret";
long timestamp = System.currentTimeMillis() / 1000; // Unix时间戳
// URL参数不包含signe
Map<String, String> urlParams = new HashMap<>();
urlParams.put("app_id", appId);
urlParams.put("timestamp", String.valueOf(timestamp));
urlParams.put("hash_type", "sha256");
// JSON请求体
String jsonBody = "{\"location\":\"beijing\",\"language\":\"zh-CN\"}";
// 生成签名
String sign = PostRequestSigner.generateSign(host, path, urlParams, jsonBody, appSecret, null);
System.out.println("生成的签名: " + sign);
// 可以将签名添加到URL参数中用于实际请求
urlParams.put("signe", sign);
// 构建完整的请求URL示例
StringBuilder urlBuilder = new StringBuilder();
urlBuilder.append("https://").append(host).append(path).append("?");
for (Map.Entry<String, String> entry : urlParams.entrySet()) {
urlBuilder.append(entry.getKey()).append("=")
.append(URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8.name())).append("&");
}
String requestUrl = urlBuilder.toString();
if (requestUrl.endsWith("&")) {
requestUrl = requestUrl.substring(0, requestUrl.length() - 1);
}
System.out.println("完整请求URL: " + requestUrl);
return requestUrl;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
}