feat(external-api): 新增外部接口调用日志,增强请求追踪与审计

- 外部订单/号码查询接口接入调用日志服务,记录鉴权、请求、响应、耗时与异常
- 新增日志主表、明细表、上下文、Mapper 与 Service,支持异步采集与上下文传递
- 新增大报文 gzip 存放目录配置,区分 dev/prod 环境路径
cp_1.0.0
chenhao 2026-11-17 17:37:51 +08:00
parent c88e6ce5be
commit e6ce4865c8
10 changed files with 838 additions and 14 deletions

View File

@ -69,3 +69,8 @@ spring:
opportunity:
integration:
base-url: https://crm.unissense.top
# 外部接口调用日志
external-api-log:
# 大报文 gzip 存放目录;相对路径基于应用工作目录,也可配置绝对路径
# 例如 Windows: D:/ruoyi/external-api-log Linux: /home/oms/external-api-log
response-file-dir: /logs/external-api

View File

@ -196,4 +196,10 @@ unis:
businessRoleId: 103
flowable:
database-schema-update: false
database-schema-update: false
# 外部接口调用日志
external-api-log:
# 大报文 gzip 存放目录;相对路径基于应用工作目录,也可配置绝对路径
# 例如 Windows: D:/ruoyi/external-api-log Linux: /home/oms/external-api-log
response-file-dir: logs/external-api

View File

@ -12,9 +12,12 @@ import com.ruoyi.sip.dto.ApiDataQueryDto;
import com.ruoyi.sip.dto.ApiProjectAddDto;
import com.ruoyi.sip.dto.ApiUserAddDto;
import com.ruoyi.sip.dto.ApiUserInfo;
import com.ruoyi.sip.dto.ExternalApiLogContext;
import com.ruoyi.sip.service.*;
import com.ruoyi.framework.shiro.service.SysPasswordService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.sip.vo.DeliveryInfoVo;
import com.ruoyi.sip.vo.OrderInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.BeanUtils;
import org.springframework.util.ObjectUtils;
@ -58,32 +61,57 @@ public class ExternalController {
private ISysUserService userService;
@Autowired
private SysPasswordService passwordService;
@Autowired
private IExternalApiCallLogService externalApiCallLogService;
private final static String API_KEY = "c7f858d0-30b8-4b7f-9ea1-0ccf5ceb1c54";
@GetMapping("/v1/order/info")
@Anonymous
public AjaxResult getOrderInfo(@Validated ApiDataQueryDto dto, @RequestHeader(value = "apiKey",required = false) String apiKey) {
if (StringUtils.isEmpty(apiKey)) {
return AjaxResult.error("apiKey不能为空");
ExternalApiLogContext logContext = externalApiCallLogService.begin(
IExternalApiCallLogService.API_ORDER_INFO, dto, apiKey);
try {
if (StringUtils.isEmpty(apiKey)) {
return logAndReturn(logContext, AjaxResult.error("apiKey不能为空"), null);
}
if (!API_KEY.equals(apiKey)) {
return logAndReturn(logContext, AjaxResult.error("鉴权失败"), null);
}
List<OrderInfoVo> orderInfoList = projectOrderInfoService.getOrderInfo(dto);
return logAndReturn(logContext, AjaxResult.success(orderInfoList), orderInfoList);
} catch (RuntimeException e) {
externalApiCallLogService.finishException(logContext, e);
throw e;
}
if (!API_KEY.equals(apiKey)) {
return AjaxResult.error("鉴权失败");
}
return AjaxResult.success(projectOrderInfoService.getOrderInfo(dto));
}
@GetMapping("/v1/number/info")
@Anonymous
private AjaxResult getNumberInfo(@Validated ApiDataQueryDto dto,@RequestHeader(value = "apiKey",required = false) String apiKey) {
if (StringUtils.isEmpty(apiKey)) {
return AjaxResult.error("apiKey不能为空");
ExternalApiLogContext logContext = externalApiCallLogService.begin(
IExternalApiCallLogService.API_NUMBER_INFO, dto, apiKey);
try {
if (StringUtils.isEmpty(apiKey)) {
return logAndReturn(logContext, AjaxResult.error("apiKey不能为空"), null);
}
if (!API_KEY.equals(apiKey)) {
return logAndReturn(logContext, AjaxResult.error("鉴权失败"), null);
}
List<DeliveryInfoVo> deliveryInfoList = deliveryService.getNumberInfo(dto);
return logAndReturn(logContext, AjaxResult.success(deliveryInfoList), deliveryInfoList);
} catch (RuntimeException e) {
externalApiCallLogService.finishException(logContext, e);
throw e;
}
if (!API_KEY.equals(apiKey)){
return AjaxResult.error("鉴权失败");
}
return AjaxResult.success(deliveryService.getNumberInfo(dto));
}
/**
* 记录外部接口调用日志后按原结果返回,日志失败不影响接口返回。
*/
private AjaxResult logAndReturn(ExternalApiLogContext logContext, AjaxResult result, Object data) {
externalApiCallLogService.finish(logContext, result, data);
return result;
}
@GetMapping("/v1/order/recall/info")

View File

@ -0,0 +1,85 @@
package com.ruoyi.sip.domain;
import java.util.Date;
import com.ruoyi.common.core.domain.BaseEntity;
import lombok.Data;
/**
* 外部接口调用日志对象 oms_external_api_log
*
* @author ruoyi
* @date 2026-11-17
*/
@Data
public class ExternalApiCallLog extends BaseEntity {
private static final long serialVersionUID = 1L;
/** 主键 */
private Long id;
/** 调用追踪号(响应头 X-Request-Id) */
private String traceId;
/** 接口编码:ORDER_INFO / NUMBER_INFO */
private String apiCode;
/** 请求开始时间 */
private Date requestTime;
/** 响应完成时间 */
private Date responseTime;
/** 耗时(毫秒) */
private Long costMs;
/** 调用方传入的查询开始时间 */
private Date queryStartTime;
/** 调用方传入的查询结束时间 */
private Date queryEndTime;
/** 调用方 IP */
private String requestIp;
/** apiKey 掩码 */
private String apiKeyMasked;
/** User-Agent */
private String userAgent;
/** 请求参数 JSON */
private String requestParams;
/** HTTP 状态码 */
private Integer httpStatus;
/** 业务码,0 表示成功 */
private Integer businessCode;
/** 是否成功 */
private Boolean success;
/** 去重后的订单数 */
private Integer orderCount;
/** 去重后的 SN 数 */
private Integer snCount;
/** 返回报文字节数 */
private Long responseSize;
/** 返回报文 SHA-256 */
private String responseHash;
/** 返回报文原文(<= 1MB) */
private String responseBody;
/** 大报文 gzip 文件路径 */
private String responseFile;
/** 异常摘要 */
private String errorMsg;
/** 删除标志(0 存在 2 删除) */
private String delFlag;
}

View File

@ -0,0 +1,61 @@
package com.ruoyi.sip.domain;
import java.util.Date;
import com.ruoyi.common.core.domain.BaseEntity;
import lombok.Data;
/**
* 外部接口调用日志明细对象 oms_external_api_log_item
*
* @author ruoyi
* @date 2026-11-17
*/
@Data
public class ExternalApiCallLogItem extends BaseEntity {
private static final long serialVersionUID = 1L;
/** 主键 */
private Long id;
/** 主表 id */
private Long logId;
/** 接口编码:ORDER_INFO / NUMBER_INFO */
private String apiCode;
/** 合同号 */
private String orderCode;
/** 订单版本号 */
private String orderVersionCode;
/** 合同版本号(版本号 + 操作版本) */
private String versionCode;
/** SN 码 */
private String serialNumber;
/** 产品编码 */
private String productCode;
/** 产品描述 */
private String productDescription;
/** 服务级别 */
private String serviceLevel;
/** 服务描述 */
private String serviceDescribe;
/** 服务开始时间 */
private Date serviceStartTime;
/** 服务结束时间 */
private Date serviceEndTime;
/** 同一 SN 的服务段序号 */
private Integer segmentIndex;
/** 删除标志(0 存在 2 删除) */
private String delFlag;
}

View File

@ -0,0 +1,47 @@
package com.ruoyi.sip.dto;
import java.util.Date;
import lombok.Data;
/**
* 外部接口调用日志上下文。
*
* <p>在请求线程内采集,随异步任务一起传给日志服务,避免异步线程读取不到
* RequestContextHolder。</p>
*
* @author ruoyi
* @date 2026-11-17
*/
@Data
public class ExternalApiLogContext {
/** 调用追踪号 */
private String traceId;
/** 接口编码 */
private String apiCode;
/** 请求开始时间 */
private Date requestTime;
/** 请求开始的纳秒时间,用于计算耗时 */
private Long startNanos;
/** 查询开始时间 */
private Date queryStartTime;
/** 查询结束时间 */
private Date queryEndTime;
/** 调用方 IP */
private String requestIp;
/** apiKey 掩码 */
private String apiKeyMasked;
/** User-Agent */
private String userAgent;
/** 请求参数 JSON */
private String requestParams;
}

View File

@ -0,0 +1,30 @@
package com.ruoyi.sip.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.ruoyi.sip.domain.ExternalApiCallLogItem;
/**
* 外部接口调用日志明细 Mapper 接口
*
* @author ruoyi
* @date 2026-11-17
*/
public interface ExternalApiCallLogItemMapper {
/**
* 批量新增调用日志明细
*
* @param list 明细集合
* @return 结果
*/
int batchInsertExternalApiCallLogItem(@Param("list") List<ExternalApiCallLogItem> list);
/**
* 按主表 id 查询明细
*
* @param logId 主表 id
* @return 明细集合
*/
List<ExternalApiCallLogItem> selectExternalApiCallLogItemByLogId(Long logId);
}

View File

@ -0,0 +1,28 @@
package com.ruoyi.sip.mapper;
import com.ruoyi.sip.domain.ExternalApiCallLog;
/**
* 外部接口调用日志 Mapper 接口
*
* @author ruoyi
* @date 2026-11-17
*/
public interface ExternalApiCallLogMapper {
/**
* 新增调用日志
*
* @param externalApiCallLog 调用日志
* @return 结果
*/
int insertExternalApiCallLog(ExternalApiCallLog externalApiCallLog);
/**
* 按追踪号查询调用日志(排查单次调用时使用)
*
* @param traceId 追踪号
* @return 调用日志
*/
ExternalApiCallLog selectExternalApiCallLogByTraceId(String traceId);
}

View File

@ -0,0 +1,49 @@
package com.ruoyi.sip.service;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.sip.dto.ApiDataQueryDto;
import com.ruoyi.sip.dto.ExternalApiLogContext;
/**
* 外部接口调用日志 Service 接口。
*
* <p>日志只做旁路记录,任何写入失败都不能影响接口本身的返回。</p>
*
* @author ruoyi
* @date 2026-11-17
*/
public interface IExternalApiCallLogService {
/** 订单信息接口 */
String API_ORDER_INFO = "ORDER_INFO";
/** SN 条码信息接口 */
String API_NUMBER_INFO = "NUMBER_INFO";
/**
* 请求进入时创建日志上下文(必须在请求线程内调用)。
*
* @param apiCode 接口编码
* @param dto 查询参数
* @param apiKey 调用方 apiKey,落库前会脱敏
* @return 日志上下文
*/
ExternalApiLogContext begin(String apiCode, ApiDataQueryDto dto, String apiKey);
/**
* 请求正常返回时记录日志。
*
* @param context 日志上下文
* @param result 实际返回给调用方的 AjaxResult
* @param data 返回业务数据(订单列表或 SN 列表),用于拆分明细
*/
void finish(ExternalApiLogContext context, AjaxResult result, Object data);
/**
* 请求抛异常时记录日志。
*
* @param context 日志上下文
* @param throwable 异常
*/
void finishException(ExternalApiLogContext context, Throwable throwable);
}

View File

@ -0,0 +1,485 @@
package com.ruoyi.sip.service.impl;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.zip.GZIPOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.sip.domain.ExternalApiCallLog;
import com.ruoyi.sip.domain.ExternalApiCallLogItem;
import com.ruoyi.sip.dto.ApiDataQueryDto;
import com.ruoyi.sip.dto.ExternalApiLogContext;
import com.ruoyi.sip.mapper.ExternalApiCallLogItemMapper;
import com.ruoyi.sip.mapper.ExternalApiCallLogMapper;
import com.ruoyi.sip.service.IExternalApiCallLogService;
import com.ruoyi.sip.vo.DeliveryInfoVo;
import com.ruoyi.sip.vo.OrderInfoVo;
/**
* 外部接口调用日志 Service 业务层处理。
*
* @author ruoyi
* @date 2026-11-17
*/
@Service
public class ExternalApiCallLogServiceImpl implements IExternalApiCallLogService {
private static final Logger log = LoggerFactory.getLogger(ExternalApiCallLogServiceImpl.class);
/** 追踪号请求/响应头 */
private static final String TRACE_HEADER = "X-Request-Id";
/** 报文内联入库上限:1MB */
private static final int INLINE_BODY_LIMIT = 1024 * 1024;
/** 明细批量插入条数 */
private static final int ITEM_BATCH_SIZE = 500;
/** 请求参数字段长度 */
private static final int REQUEST_PARAMS_LIMIT = 2000;
/** 异常摘要字段长度 */
private static final int ERROR_MSG_LIMIT = 2000;
/** 追踪号最大长度 */
private static final int TRACE_ID_LIMIT = 64;
/** User-Agent 字段长度 */
private static final int USER_AGENT_LIMIT = 500;
/** IP 字段长度 */
private static final int IP_LIMIT = 128;
/** apiKey 掩码保留位数 */
private static final int API_KEY_KEEP = 4;
/** 大报文落盘默认目录(相对应用工作目录) */
private static final String DEFAULT_RESPONSE_FILE_DIR = "logs/external-api";
private static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
private static final String DATE_PATTERN = "yyyyMMdd";
private static final String DEL_FLAG_NORMAL = "0";
private static final String UNKNOWN = "unknown";
@Autowired
private ExternalApiCallLogMapper externalApiCallLogMapper;
@Autowired
private ExternalApiCallLogItemMapper externalApiCallLogItemMapper;
@Autowired
private ObjectMapper objectMapper;
@Autowired
@Qualifier("scheduledExecutorService")
private ScheduledExecutorService scheduledExecutorService;
/** 大报文 gzip 存放目录,见 application.yml 的 external-api-log.response-file-dir */
@Value("${external-api-log.response-file-dir:" + DEFAULT_RESPONSE_FILE_DIR + "}")
private String responseFileDir;
@Override
public ExternalApiLogContext begin(String apiCode, ApiDataQueryDto dto, String apiKey) {
try {
return doBegin(apiCode, dto, apiKey);
} catch (Exception e) {
log.warn("外部接口调用日志初始化失败, apiCode={}", apiCode, e);
ExternalApiLogContext fallback = new ExternalApiLogContext();
fallback.setApiCode(apiCode);
fallback.setRequestTime(new Date());
fallback.setStartNanos(System.nanoTime());
fallback.setTraceId(resolveTraceId(null));
return fallback;
}
}
private ExternalApiLogContext doBegin(String apiCode, ApiDataQueryDto dto, String apiKey) {
ExternalApiLogContext context = new ExternalApiLogContext();
context.setApiCode(apiCode);
context.setRequestTime(DateUtils.getNowDate());
context.setStartNanos(System.nanoTime());
if (dto != null) {
context.setQueryStartTime(dto.getQueryStartTime());
context.setQueryEndTime(dto.getQueryEndTime());
}
context.setApiKeyMasked(maskApiKey(apiKey));
context.setRequestParams(buildRequestParams(dto));
HttpServletRequest request = currentRequest();
if (request != null) {
context.setTraceId(resolveTraceId(request.getHeader(TRACE_HEADER)));
context.setRequestIp(resolveClientIp(request));
context.setUserAgent(truncate(request.getHeader("User-Agent"), USER_AGENT_LIMIT));
} else {
context.setTraceId(resolveTraceId(null));
}
writeTraceHeader(context.getTraceId());
return context;
}
@Override
public void finish(ExternalApiLogContext context, AjaxResult result, Object data) {
if (context == null) {
return;
}
boolean success = result != null
&& Integer.valueOf(AjaxResult.Type.SUCCESS.value()).equals(result.get(AjaxResult.CODE_TAG));
String errorMsg = null;
if (!success && result != null && result.get(AjaxResult.MSG_TAG) != null) {
errorMsg = String.valueOf(result.get(AjaxResult.MSG_TAG));
}
submit(context, result, data, success, errorMsg);
}
@Override
public void finishException(ExternalApiLogContext context, Throwable throwable) {
if (context == null) {
return;
}
submit(context, null, null, false, buildErrorMsg(throwable));
}
private void submit(final ExternalApiLogContext context, final AjaxResult result, final Object data,
final boolean success, final String errorMsg) {
try {
scheduledExecutorService.schedule(new Runnable() {
@Override
public void run() {
persist(context, result, data, success, errorMsg);
}
}, 10, TimeUnit.MILLISECONDS);
} catch (Exception e) {
log.warn("外部接口调用日志提交失败, traceId={}, apiCode={}", context.getTraceId(), context.getApiCode(), e);
}
}
private void persist(ExternalApiLogContext context, AjaxResult result, Object data,
boolean success, String errorMsg) {
try {
List<ExternalApiCallLogItem> items = buildItems(context.getApiCode(), data);
Set<String> orderCodes = new HashSet<>();
Set<String> serialNumbers = new HashSet<>();
for (ExternalApiCallLogItem item : items) {
if (StringUtils.isNotEmpty(item.getOrderCode())) {
orderCodes.add(item.getOrderCode());
}
if (StringUtils.isNotEmpty(item.getSerialNumber())) {
serialNumbers.add(item.getSerialNumber());
}
}
ExternalApiCallLog record = new ExternalApiCallLog();
record.setTraceId(context.getTraceId());
record.setApiCode(context.getApiCode());
record.setRequestTime(context.getRequestTime());
record.setResponseTime(new Date());
record.setCostMs(calculateCostMs(context.getStartNanos()));
record.setQueryStartTime(context.getQueryStartTime());
record.setQueryEndTime(context.getQueryEndTime());
record.setRequestIp(context.getRequestIp());
record.setApiKeyMasked(context.getApiKeyMasked());
record.setUserAgent(context.getUserAgent());
record.setRequestParams(context.getRequestParams());
record.setHttpStatus(result != null ? 200 : 500);
record.setBusinessCode(resolveBusinessCode(result, success));
record.setSuccess(success);
record.setOrderCount(orderCodes.size());
record.setSnCount(serialNumbers.size());
record.setErrorMsg(truncate(errorMsg, ERROR_MSG_LIMIT));
record.setDelFlag(DEL_FLAG_NORMAL);
record.setCreateTime(new Date());
fillResponseBody(record, context, result);
int rows = externalApiCallLogMapper.insertExternalApiCallLog(record);
if (rows > 0 && !items.isEmpty()) {
for (ExternalApiCallLogItem item : items) {
item.setLogId(record.getId());
}
insertItems(items);
}
} catch (Exception e) {
log.error("外部接口调用日志落库失败, traceId={}, apiCode={}",
context.getTraceId(), context.getApiCode(), e);
}
}
private void insertItems(List<ExternalApiCallLogItem> items) {
for (int fromIndex = 0; fromIndex < items.size(); fromIndex += ITEM_BATCH_SIZE) {
int toIndex = Math.min(fromIndex + ITEM_BATCH_SIZE, items.size());
externalApiCallLogItemMapper.batchInsertExternalApiCallLogItem(items.subList(fromIndex, toIndex));
}
}
private void fillResponseBody(ExternalApiCallLog record, ExternalApiLogContext context, AjaxResult result) {
if (result == null) {
record.setResponseSize(0L);
return;
}
byte[] body;
try {
body = objectMapper.writeValueAsBytes(result);
} catch (Exception e) {
record.setErrorMsg(appendError(record.getErrorMsg(), "响应报文序列化失败: " + e.getMessage()));
return;
}
record.setResponseSize((long) body.length);
record.setResponseHash(sha256Hex(body));
if (body.length <= INLINE_BODY_LIMIT) {
record.setResponseBody(new String(body, StandardCharsets.UTF_8));
return;
}
String filePath = writeGzipFile(context, body);
if (filePath != null) {
record.setResponseFile(filePath);
} else {
// 落盘失败时回退入库,避免报文彻底丢失
record.setResponseBody(new String(body, StandardCharsets.UTF_8));
}
}
private String writeGzipFile(ExternalApiLogContext context, byte[] body) {
try {
String datePart = DateUtils.parseDateToStr(DATE_PATTERN, context.getRequestTime());
String baseDir = StringUtils.isEmpty(responseFileDir) ? DEFAULT_RESPONSE_FILE_DIR : responseFileDir.trim();
Path directory = Paths.get(baseDir, datePart);
Files.createDirectories(directory);
String fileName = context.getTraceId() + "_" + context.getApiCode() + ".json.gz";
Path file = directory.resolve(fileName);
try (OutputStream outputStream = new GZIPOutputStream(Files.newOutputStream(file))) {
outputStream.write(body);
}
return file.toString();
} catch (IOException e) {
log.warn("外部接口大报文落盘失败, traceId={}, apiCode={}", context.getTraceId(), context.getApiCode(), e);
return null;
}
}
private List<ExternalApiCallLogItem> buildItems(String apiCode, Object data) {
List<ExternalApiCallLogItem> items = new ArrayList<>();
if (!(data instanceof List)) {
return items;
}
for (Object row : (List<?>) data) {
if (row instanceof OrderInfoVo) {
OrderInfoVo orderInfoVo = (OrderInfoVo) row;
ExternalApiCallLogItem item = newItem(apiCode);
item.setOrderCode(orderInfoVo.getOrderCode());
item.setVersionCode(orderInfoVo.getVersionCode());
item.setOrderVersionCode(orderInfoVo.getOrderVersionCode());
items.add(item);
} else if (row instanceof DeliveryInfoVo) {
DeliveryInfoVo deliveryInfoVo = (DeliveryInfoVo) row;
List<DeliveryInfoVo.ServiceInfo> services = deliveryInfoVo.getServiceInfo();
if (services == null || services.isEmpty()) {
items.add(newDeliveryItem(apiCode, deliveryInfoVo, null, null));
continue;
}
int segmentIndex = 1;
for (DeliveryInfoVo.ServiceInfo service : services) {
items.add(newDeliveryItem(apiCode, deliveryInfoVo, service, segmentIndex++));
}
}
}
return items;
}
private ExternalApiCallLogItem newItem(String apiCode) {
ExternalApiCallLogItem item = new ExternalApiCallLogItem();
item.setApiCode(apiCode);
item.setDelFlag(DEL_FLAG_NORMAL);
item.setCreateTime(new Date());
return item;
}
private ExternalApiCallLogItem newDeliveryItem(String apiCode, DeliveryInfoVo deliveryInfoVo,
DeliveryInfoVo.ServiceInfo service, Integer segmentIndex) {
ExternalApiCallLogItem item = newItem(apiCode);
item.setOrderCode(deliveryInfoVo.getOrderCode());
item.setVersionCode(deliveryInfoVo.getVersionCode());
item.setOrderVersionCode(deliveryInfoVo.getOrderVersionCode());
item.setSerialNumber(deliveryInfoVo.getSerialNumber());
item.setProductCode(deliveryInfoVo.getProductCode());
item.setProductDescription(deliveryInfoVo.getProductDescription());
if (service != null) {
item.setServiceLevel(service.getServiceLevel());
item.setServiceDescribe(service.getServiceDescribe());
item.setServiceStartTime(service.getServiceStartTime());
item.setServiceEndTime(service.getServiceEndTime());
item.setSegmentIndex(segmentIndex);
}
return item;
}
private Integer resolveBusinessCode(AjaxResult result, boolean success) {
if (result != null) {
Object code = result.get(AjaxResult.CODE_TAG);
if (code instanceof Number) {
return ((Number) code).intValue();
}
}
return success ? AjaxResult.Type.SUCCESS.value() : AjaxResult.Type.ERROR.value();
}
private Long calculateCostMs(Long startNanos) {
if (startNanos == null) {
return null;
}
return TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos);
}
private String buildRequestParams(ApiDataQueryDto dto) {
if (dto == null) {
return null;
}
Map<String, Object> params = new LinkedHashMap<>();
params.put("queryStartTime", formatDateTime(dto.getQueryStartTime()));
params.put("queryEndTime", formatDateTime(dto.getQueryEndTime()));
params.put("serialNumber", dto.getSerialNumber());
params.put("serialNumbers", dto.getSerialNumbers());
try {
return truncate(objectMapper.writeValueAsString(params), REQUEST_PARAMS_LIMIT);
} catch (Exception e) {
String fallback = "queryStartTime=" + formatDateTime(dto.getQueryStartTime())
+ "&queryEndTime=" + formatDateTime(dto.getQueryEndTime());
return truncate(fallback, REQUEST_PARAMS_LIMIT);
}
}
private String formatDateTime(Date date) {
return date == null ? null : DateUtils.parseDateToStr(DATE_TIME_PATTERN, date);
}
private String resolveTraceId(String headerValue) {
if (StringUtils.isNotEmpty(headerValue)) {
StringBuilder builder = new StringBuilder();
for (char current : headerValue.trim().toCharArray()) {
boolean allowed = (current >= 'a' && current <= 'z')
|| (current >= 'A' && current <= 'Z')
|| (current >= '0' && current <= '9')
|| current == '-' || current == '_';
if (allowed && builder.length() < TRACE_ID_LIMIT) {
builder.append(current);
}
}
if (builder.length() > 0) {
return builder.toString();
}
}
return UUID.randomUUID().toString().replace("-", "");
}
private String resolveClientIp(HttpServletRequest request) {
String ip = request.getHeader("X-Forwarded-For");
if (StringUtils.isNotEmpty(ip) && !UNKNOWN.equalsIgnoreCase(ip)) {
int commaIndex = ip.indexOf(',');
if (commaIndex > 0) {
ip = ip.substring(0, commaIndex);
}
}
if (StringUtils.isEmpty(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
ip = request.getHeader("X-Real-IP");
}
if (StringUtils.isEmpty(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return truncate(ip == null ? null : ip.trim(), IP_LIMIT);
}
private String maskApiKey(String apiKey) {
if (StringUtils.isEmpty(apiKey)) {
return null;
}
String value = apiKey.trim();
if (value.length() <= API_KEY_KEEP * 2) {
return "****";
}
return value.substring(0, API_KEY_KEEP) + "****" + value.substring(value.length() - API_KEY_KEEP);
}
private String sha256Hex(byte[] body) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(body);
StringBuilder builder = new StringBuilder(hash.length * 2);
for (byte current : hash) {
builder.append(Character.forDigit((current >> 4) & 0xF, 16));
builder.append(Character.forDigit(current & 0xF, 16));
}
return builder.toString();
} catch (Exception e) {
return null;
}
}
private String buildErrorMsg(Throwable throwable) {
if (throwable == null) {
return null;
}
String message = throwable.getClass().getSimpleName();
if (StringUtils.isNotEmpty(throwable.getMessage())) {
message = message + ": " + throwable.getMessage();
}
return truncate(message, ERROR_MSG_LIMIT);
}
private String appendError(String errorMsg, String append) {
String merged = StringUtils.isEmpty(errorMsg) ? append : errorMsg + "; " + append;
return truncate(merged, ERROR_MSG_LIMIT);
}
private String truncate(String value, int maxLength) {
if (value == null || value.length() <= maxLength) {
return value;
}
return value.substring(0, maxLength);
}
private HttpServletRequest currentRequest() {
try {
return ServletUtils.getRequest();
} catch (Exception e) {
return null;
}
}
private void writeTraceHeader(String traceId) {
try {
HttpServletResponse response = ServletUtils.getResponse();
if (response != null && StringUtils.isNotEmpty(traceId)) {
response.setHeader(TRACE_HEADER, traceId);
}
} catch (Exception e) {
log.debug("写入 {} 响应头失败", TRACE_HEADER, e);
}
}
}