From 855d89df21fcb932fda0fb4605135cc7029236d3 Mon Sep 17 00:00:00 2001 From: duzhichao <1098974448@qq.com> Date: Thu, 5 Feb 2026 20:11:30 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=90=BA=E7=A8=8B=E5=90=8E=E5=8F=B0?= =?UTF-8?q?=E9=80=80=E6=AC=BE2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/CtripApiServiceImpl.java | 92 ++++++++----------- 1 file changed, 40 insertions(+), 52 deletions(-) diff --git a/zlt-business/coupon-center/src/main/java/com/central/coupon/service/impl/CtripApiServiceImpl.java b/zlt-business/coupon-center/src/main/java/com/central/coupon/service/impl/CtripApiServiceImpl.java index 1c8b2226..28a1d771 100644 --- a/zlt-business/coupon-center/src/main/java/com/central/coupon/service/impl/CtripApiServiceImpl.java +++ b/zlt-business/coupon-center/src/main/java/com/central/coupon/service/impl/CtripApiServiceImpl.java @@ -1,5 +1,6 @@ package com.central.coupon.service.impl; +import com.central.coupon.config.CtripApiConfig; import com.central.coupon.service.ICtripApiService; import com.central.coupon.util.CtripDecryptUtil; import com.central.coupon.mapper.CtripOrderItemMapper; @@ -9,7 +10,6 @@ import com.central.coupon.entity.CtripOrder; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; import org.springframework.http.*; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; @@ -35,27 +35,15 @@ public class CtripApiServiceImpl implements ICtripApiService { @Autowired private ObjectMapper objectMapper; - + @Autowired private CtripOrderItemMapper ctripOrderItemMapper; - + @Autowired private CtripOrderMapper ctripOrderMapper; - @Value("${ctrip.api.base-url:https://api.ctrip.com}") - private String baseUrl; - - @Value("${ctrip.api.timeout:30000}") - private int timeout; - - @Value("${ctrip.api.retry-times:3}") - private int retryTimes; - - @Value("${ctrip.account.id:your-account-id}") - private String accountId; - - @Value("${ctrip.account.secret:your-account-secret}") - private String accountSecret; + @Autowired + private CtripApiConfig ctripApiConfig; @Override public boolean sendOrderConsumedNotice(String ctripCouponId, String orderNo) { @@ -126,7 +114,7 @@ public class CtripApiServiceImpl implements ICtripApiService { // 构建header Map header = new HashMap<>(); - header.put("accountId", accountId); + header.put("accountId", ctripApiConfig.getAccount().getId()); header.put("serviceName", "OrderConsumedNotice"); header.put("requestTime", java.time.LocalDateTime.now().format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); header.put("version", "1.0"); @@ -139,7 +127,7 @@ public class CtripApiServiceImpl implements ICtripApiService { return false; } - String sign = ctripDecryptUtil.generateSign(accountId, "OrderConsumedNotice", + String sign = ctripDecryptUtil.generateSign(ctripApiConfig.getAccount().getId(), "OrderConsumedNotice", header.get("requestTime").toString(), encryptedBody, "1.0"); header.put("sign", sign); @@ -152,7 +140,7 @@ public class CtripApiServiceImpl implements ICtripApiService { try { String requestJson = objectMapper.writeValueAsString(request); log.info("=== Postman测试用请求体 ==="); - log.info("URL: {}", baseUrl + "/order/notice.do"); + log.info("URL: {}", ctripApiConfig.getApi().getBaseUrl() + "/order/notice.do"); log.info("Method: POST"); log.info("Headers: Content-Type: application/json"); log.info("Body: {}", requestJson); @@ -162,7 +150,7 @@ public class CtripApiServiceImpl implements ICtripApiService { } // 发送HTTP请求到携程API - String url = baseUrl + "/order/notice.do"; + String url = ctripApiConfig.getApi().getBaseUrl() + "/order/notice.do"; log.info("准备发送请求到URL: {}", url); HttpHeaders headers = new HttpHeaders(); @@ -174,7 +162,7 @@ public class CtripApiServiceImpl implements ICtripApiService { // 恢复实际网络调用 log.info("开始发送实际网络请求到携程API"); // 重试机制 - for (int i = 0; i < retryTimes; i++) { + for (int i = 0; i < ctripApiConfig.getApi().getRetryTimes(); i++) { try { log.info("第{}次尝试调用携程API,URL: {}", i + 1, url); ResponseEntity response = restTemplate.postForEntity(url, httpRequest, Map.class); @@ -203,7 +191,7 @@ public class CtripApiServiceImpl implements ICtripApiService { log.error("携程订单核销通知接口调用异常,第{}次重试,ctripCouponId: {}, orderNo: {}", i + 1, ctripCouponId, orderNo, e); // 如果是最后一次重试,记录详细错误信息 - if (i == retryTimes - 1) { + if (i == ctripApiConfig.getApi().getRetryTimes() - 1) { log.error("所有重试都失败了,最后一次错误详情: {}", e.getMessage()); throw e; } @@ -221,7 +209,7 @@ public class CtripApiServiceImpl implements ICtripApiService { } } - log.error("携程订单核销通知接口调用失败,已重试{}次,ctripCouponId: {}, orderNo: {}", retryTimes, ctripCouponId, orderNo); + log.error("携程订单核销通知接口调用失败,已重试{}次,ctripCouponId: {}, orderNo: {}", ctripApiConfig.getApi().getRetryTimes(), ctripCouponId, orderNo); return false; } catch (Exception e) { @@ -245,14 +233,14 @@ public class CtripApiServiceImpl implements ICtripApiService { log.info("携程查询请求参数: {}", requestParams); // 发送HTTP请求到携程API - String url = baseUrl + "/coupon/status"; + String url = ctripApiConfig.getApi().getBaseUrl() + "/coupon/status"; HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); - headers.set("X-CTRIP-ACCOUNT-ID", accountId); + headers.set("X-CTRIP-ACCOUNT-ID", ctripApiConfig.getAccount().getId()); headers.set("X-CTRIP-TIMESTAMP", String.valueOf(System.currentTimeMillis())); // 生成签名 - String sign = ctripDecryptUtil.generateSign(accountId, "GetCouponStatus", + String sign = ctripDecryptUtil.generateSign(ctripApiConfig.getAccount().getId(), "GetCouponStatus", java.time.LocalDateTime.now().format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")), requestParams.toString(), "1.0"); headers.set("X-CTRIP-SIGN", sign); @@ -260,7 +248,7 @@ public class CtripApiServiceImpl implements ICtripApiService { HttpEntity> request = new HttpEntity<>(requestParams, headers); // 重试机制 - for (int i = 0; i < retryTimes; i++) { + for (int i = 0; i < ctripApiConfig.getApi().getRetryTimes(); i++) { try { ResponseEntity response = restTemplate.getForEntity(url, Map.class); @@ -278,7 +266,7 @@ public class CtripApiServiceImpl implements ICtripApiService { } } catch (Exception e) { log.error("携程查询接口调用异常,第{}次重试,ctripCouponId: {}", i + 1, ctripCouponId, e); - if (i == retryTimes - 1) { + if (i == ctripApiConfig.getApi().getRetryTimes() - 1) { throw e; } // 等待后重试 @@ -291,7 +279,7 @@ public class CtripApiServiceImpl implements ICtripApiService { } } - log.error("携程查询接口调用失败,已重试{}次,ctripCouponId: {}", retryTimes, ctripCouponId); + log.error("携程查询接口调用失败,已重试{}次,ctripCouponId: {}", ctripApiConfig.getApi().getRetryTimes(), ctripCouponId); return "UNKNOWN"; } catch (Exception e) { @@ -326,14 +314,14 @@ public class CtripApiServiceImpl implements ICtripApiService { log.info("携程创建优惠券请求参数: {}", requestParams); // 发送HTTP请求到携程API - String url = baseUrl + "/coupon/create"; + String url = ctripApiConfig.getApi().getBaseUrl() + "/coupon/create"; HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); - headers.set("X-CTRIP-ACCOUNT-ID", accountId); + headers.set("X-CTRIP-ACCOUNT-ID", ctripApiConfig.getAccount().getId()); headers.set("X-CTRIP-TIMESTAMP", String.valueOf(System.currentTimeMillis())); // 生成签名 - String sign = ctripDecryptUtil.generateSign(accountId, "CreateCoupon", + String sign = ctripDecryptUtil.generateSign(ctripApiConfig.getAccount().getId(), "CreateCoupon", java.time.LocalDateTime.now().format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")), requestParams.toString(), "1.0"); headers.set("X-CTRIP-SIGN", sign); @@ -341,7 +329,7 @@ public class CtripApiServiceImpl implements ICtripApiService { HttpEntity> request = new HttpEntity<>(requestParams, headers); // 重试机制 - for (int i = 0; i < retryTimes; i++) { + for (int i = 0; i < ctripApiConfig.getApi().getRetryTimes(); i++) { try { ResponseEntity response = restTemplate.postForEntity(url, request, Map.class); @@ -358,7 +346,7 @@ public class CtripApiServiceImpl implements ICtripApiService { } } catch (Exception e) { log.error("携程创建优惠券接口调用异常,第{}次重试,ctripCouponId: {}, userPhone: {}", i + 1, ctripCouponId, userPhone, e); - if (i == retryTimes - 1) { + if (i == ctripApiConfig.getApi().getRetryTimes() - 1) { throw e; } // 等待后重试 @@ -371,7 +359,7 @@ public class CtripApiServiceImpl implements ICtripApiService { } } - log.error("携程创建优惠券接口调用失败,已重试{}次,ctripCouponId: {}, userPhone: {}", retryTimes, ctripCouponId, userPhone); + log.error("携程创建优惠券接口调用失败,已重试{}次,ctripCouponId: {}, userPhone: {}", ctripApiConfig.getApi().getRetryTimes(), ctripCouponId, userPhone); return false; } catch (Exception e) { @@ -398,14 +386,14 @@ public class CtripApiServiceImpl implements ICtripApiService { log.info("携程取消优惠券请求参数: {}", requestParams); // 发送HTTP请求到携程API - String url = baseUrl + "/coupon/cancel"; + String url = ctripApiConfig.getApi().getBaseUrl() + "/coupon/cancel"; HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); - headers.set("X-CTRIP-ACCOUNT-ID", accountId); + headers.set("X-CTRIP-ACCOUNT-ID", ctripApiConfig.getAccount().getId()); headers.set("X-CTRIP-TIMESTAMP", String.valueOf(System.currentTimeMillis())); // 生成签名 - String sign = ctripDecryptUtil.generateSign(accountId, "CancelCoupon", + String sign = ctripDecryptUtil.generateSign(ctripApiConfig.getAccount().getId(), "CancelCoupon", java.time.LocalDateTime.now().format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")), requestParams.toString(), "1.0"); headers.set("X-CTRIP-SIGN", sign); @@ -413,7 +401,7 @@ public class CtripApiServiceImpl implements ICtripApiService { HttpEntity> request = new HttpEntity<>(requestParams, headers); // 重试机制 - for (int i = 0; i < retryTimes; i++) { + for (int i = 0; i < ctripApiConfig.getApi().getRetryTimes(); i++) { try { ResponseEntity response = restTemplate.postForEntity(url, request, Map.class); @@ -430,7 +418,7 @@ public class CtripApiServiceImpl implements ICtripApiService { } } catch (Exception e) { log.error("携程取消优惠券接口调用异常,第{}次重试,ctripCouponId: {}, userPhone: {}", i + 1, ctripCouponId, userPhone, e); - if (i == retryTimes - 1) { + if (i == ctripApiConfig.getApi().getRetryTimes() - 1) { throw e; } // 等待后重试 @@ -443,7 +431,7 @@ public class CtripApiServiceImpl implements ICtripApiService { } } - log.error("携程取消优惠券接口调用失败,已重试{}次,ctripCouponId: {}, userPhone: {}", retryTimes, ctripCouponId, userPhone); + log.error("携程取消优惠券接口调用失败,已重试{}次,ctripCouponId: {}, userPhone: {}", ctripApiConfig.getApi().getRetryTimes(), ctripCouponId, userPhone); return false; } catch (Exception e) { @@ -566,10 +554,10 @@ public class CtripApiServiceImpl implements ICtripApiService { otaOrderId, supplierOrderId, totalAmount, currency); // 网络连接测试 - if (!testNetworkConnection()) { - log.error("网络连接测试失败,无法访问携程API"); - return 0; - } +// if (!testNetworkConnection()) { +// log.error("网络连接测试失败,无法访问携程API"); +// return 0; +// } try { // 构建请求体 @@ -595,7 +583,7 @@ public class CtripApiServiceImpl implements ICtripApiService { // 构建header Map header = new HashMap<>(); - header.put("accountId", accountId); + header.put("accountId", ctripApiConfig.getAccount().getId()); header.put("serviceName", "RefundOrder"); String requestTime = java.time.LocalDateTime.now().format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); header.put("requestTime", requestTime); @@ -609,7 +597,7 @@ public class CtripApiServiceImpl implements ICtripApiService { return 0; } - String sign = ctripDecryptUtil.generateSign(accountId, "RefundOrder", + String sign = ctripDecryptUtil.generateSign(ctripApiConfig.getAccount().getId(), "RefundOrder", requestTime, encryptedBody, "1.0"); header.put("sign", sign); @@ -619,7 +607,7 @@ public class CtripApiServiceImpl implements ICtripApiService { log.info("携程退款完整请求: {}", request); // 发送HTTP请求到携程API - String url = baseUrl + "/order/refund.do"; + String url = ctripApiConfig.getApi().getBaseUrl() + "/order/refund.do"; log.info("准备发送退款请求到URL: {}", url); HttpHeaders headers = new HttpHeaders(); @@ -629,7 +617,7 @@ public class CtripApiServiceImpl implements ICtripApiService { HttpEntity> httpRequest = new HttpEntity<>(request, headers); // 重试机制 - for (int i = 0; i < retryTimes; i++) { + for (int i = 0; i < ctripApiConfig.getApi().getRetryTimes(); i++) { try { log.info("第{}次尝试调用携程退款API,URL: {}", i + 1, url); ResponseEntity response = restTemplate.postForEntity(url, httpRequest, Map.class); @@ -668,7 +656,7 @@ public class CtripApiServiceImpl implements ICtripApiService { } catch (Exception e) { log.error("携程退款接口调用异常,第{}次重试,otaOrderId: {}", i + 1, otaOrderId, e); - if (i == retryTimes - 1) { + if (i == ctripApiConfig.getApi().getRetryTimes() - 1) { log.error("所有重试都失败了,最后一次错误详情: {}", e.getMessage()); throw e; } @@ -686,7 +674,7 @@ public class CtripApiServiceImpl implements ICtripApiService { } } - log.error("携程退款接口调用失败,已重试{}次,otaOrderId: {}", retryTimes, otaOrderId); + log.error("携程退款接口调用失败,已重试{}次,otaOrderId: {}", ctripApiConfig.getApi().getRetryTimes(), otaOrderId); return 0; } catch (Exception e) {