|
|
|
@ -57,12 +57,14 @@ public class RedServiceImpl implements RedService { |
|
|
|
@Value("${red.urllogin}") |
|
|
|
private String BASE_URL_LOGIN; |
|
|
|
private static final String BASE_URLDev = "http://af9c6249.natappfree.cc"; |
|
|
|
private static final String BASE_URL_Margen = "http://pcc8496a.natappfree.cc"; |
|
|
|
private static final String PATH = "/api/coupon/IssueRechargeRedPacket"; |
|
|
|
private static final String PATHRED = "/api/coupon/getTotalDiscount"; |
|
|
|
private static final String BASE_URL = "http://localhost:8080"; |
|
|
|
private static final String PATHLIST = "/api/coupon/getRedPacket"; |
|
|
|
private static final String PATHLOGIN = "/api/user/toujiaoAppLogin"; |
|
|
|
private static final String PATH_REFUND = "/api/coupon/refundRedPacket"; |
|
|
|
private static final String PATH_CHECK = "/api/coupon/checkRefundState"; |
|
|
|
|
|
|
|
private static final String CONSUME_PATH = "/api/coupon/updateCostRedPacket"; |
|
|
|
private static final HttpClient CLIENT = HttpClient.newHttpClient(); |
|
|
|
@ -237,9 +239,7 @@ public class RedServiceImpl implements RedService { |
|
|
|
String body = "{\"order_id\":\"" + linkId + "\",\"amount\":" + price.divideToIntegralValue(BigDecimal.valueOf(100)).intValue() + "}"; |
|
|
|
|
|
|
|
// 构建 HttpRequest |
|
|
|
HttpRequest request = HttpRequest.newBuilder() |
|
|
|
// .uri(URI.create(BASE_URLDev + PATH_REFUND)) |
|
|
|
.uri(URI.create("http://a4f24573.natappfree.cc/api/coupon/refundRedPacket")) |
|
|
|
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(BASE_URL_Margen+PATH_CHECK)) |
|
|
|
.header("Content-Type", "application/json") |
|
|
|
// ⭐ 透传 token,避免 JWT 过期问题 |
|
|
|
// .header("Authorization", authHeader) |
|
|
|
@ -251,29 +251,31 @@ public class RedServiceImpl implements RedService { |
|
|
|
CLIENT.send(request, HttpResponse.BodyHandlers.ofString()); |
|
|
|
|
|
|
|
// HTTP 层校验 |
|
|
|
if (resp.statusCode() != 200) { |
|
|
|
if (resp.statusCode() != 200 ) { |
|
|
|
log.warn("红包接口 HTTP 异常,status:{},body:{}", |
|
|
|
resp.statusCode(), resp.body()); |
|
|
|
throw new RuntimeException("红包接口 HTTP 异常"); |
|
|
|
} |
|
|
|
|
|
|
|
// 解析返回 JSON |
|
|
|
JsonNode root = objectMapper.readTree(resp.body()); |
|
|
|
int code = root.path("code").asInt(); |
|
|
|
if (code == 400) { |
|
|
|
// 400 状态码表示参数错误 |
|
|
|
log.warn("该订单无法处理,status:{},body:{}", |
|
|
|
resp.statusCode(), resp.body()); |
|
|
|
throw new RuntimeException("该订单无法自动处理,请联系工作人员"); |
|
|
|
} |
|
|
|
|
|
|
|
boolean flag = root.path("data").path("flag").asBoolean(); |
|
|
|
int num = root.path("data").path("num").asInt(); // ✅ 获取 num |
|
|
|
int num = root.path("data").path("num").asInt(); // 获取 num |
|
|
|
// 业务判断 |
|
|
|
if (code == 200 && flag) { |
|
|
|
//updateOrderStatus(linkId); |
|
|
|
return "success"; |
|
|
|
} |
|
|
|
|
|
|
|
// 失败 同时抛出须补充的金币数 |
|
|
|
throw new RedCheckException( |
|
|
|
"红包校验失败,orderId=" + linkId, |
|
|
|
num |
|
|
|
); |
|
|
|
} else { // flag 为 false |
|
|
|
|
|
|
|
throw new RedCheckException( |
|
|
|
"红包校验失败,orderId=" + linkId, num); |
|
|
|
} |
|
|
|
} catch (IOException | InterruptedException e) { |
|
|
|
log.error("调用红包接口失败,orderId:{}", linkId, e); |
|
|
|
throw new RuntimeException("调用红包接口异常", e); |
|
|
|
@ -281,16 +283,36 @@ public class RedServiceImpl implements RedService { |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// 扣除金币抵扣红包 |
|
|
|
@Override |
|
|
|
public void updateOrderStatus(String linkId) throws Exception { |
|
|
|
public String consumeRed(String linkId) throws Exception { |
|
|
|
try { |
|
|
|
String body = JSON.toJSONString(java.util.Map.of("linkId", linkId)); |
|
|
|
} |
|
|
|
catch (Exception e) { |
|
|
|
log.error("修改状态失败", e); |
|
|
|
throw new Exception("系统异常"); |
|
|
|
// 构造参数 |
|
|
|
String body = "{\"order_id\":\"" + linkId + "}"; |
|
|
|
// 构建 HttpRequest |
|
|
|
HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(BASE_URL_Margen+PATH_REFUND)) |
|
|
|
.header("Content-Type", "application/json") |
|
|
|
.POST(HttpRequest.BodyPublishers.ofString(body)) |
|
|
|
.build(); |
|
|
|
// 发送请求 |
|
|
|
HttpResponse<String> resp = |
|
|
|
CLIENT.send(request, HttpResponse.BodyHandlers.ofString()); |
|
|
|
|
|
|
|
// HTTP 层校验 |
|
|
|
if (resp.statusCode() != 200 ) { |
|
|
|
log.warn("红包接口 HTTP 异常,status:{},body:{}", |
|
|
|
resp.statusCode(), resp.body()); |
|
|
|
throw new RuntimeException("红包接口 HTTP 异常"); |
|
|
|
} |
|
|
|
} catch (IOException | InterruptedException e) { |
|
|
|
log.error("调用红包接口失败,orderId:{}", linkId, e); |
|
|
|
throw new RuntimeException("调用红包接口异常", e); |
|
|
|
} |
|
|
|
return "success"; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
@Override |
|
|
|
public Result updateRed(ConsumeUser consumeUser) throws Exception { |
|
|
|
|