|
|
|
@ -11,6 +11,7 @@ import com.example.demo.mapper.Temporary.RedMapper; |
|
|
|
import com.example.demo.service.Temporary.RedService; |
|
|
|
import com.fasterxml.jackson.databind.JsonNode; |
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper; |
|
|
|
import jakarta.servlet.http.HttpServletRequest; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
@ -23,6 +24,9 @@ import org.springframework.http.*; |
|
|
|
import org.springframework.util.LinkedMultiValueMap; |
|
|
|
import org.springframework.util.MultiValueMap; |
|
|
|
import org.springframework.web.client.RestTemplate; |
|
|
|
import org.springframework.web.context.request.RequestContextHolder; |
|
|
|
import org.springframework.web.context.request.ServletRequestAttributes; |
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.math.BigDecimal; |
|
|
|
import java.net.ConnectException; |
|
|
|
@ -227,35 +231,57 @@ public class RedServiceImpl implements RedService { |
|
|
|
|
|
|
|
@Override |
|
|
|
public String checkRed(String linkId)throws Exception { |
|
|
|
// 组装 form 表单参数 |
|
|
|
MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); |
|
|
|
params.add("order_id", linkId); |
|
|
|
|
|
|
|
HttpHeaders headers = new HttpHeaders(); |
|
|
|
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); |
|
|
|
|
|
|
|
HttpEntity<MultiValueMap<String, String>> request = |
|
|
|
new HttpEntity<>(params, headers); |
|
|
|
|
|
|
|
// 发送 POST 请求 |
|
|
|
ResponseEntity<String> response = |
|
|
|
restTemplate.postForEntity(BASE_URLDev+PATH_REFUND, request, String.class); |
|
|
|
|
|
|
|
// 解析返回 JSON |
|
|
|
JsonNode root = objectMapper.readTree(response.getBody()); |
|
|
|
|
|
|
|
int code = root.path("code").asInt(); |
|
|
|
boolean flag = root.path("data").path("flag").asBoolean(); |
|
|
|
try { |
|
|
|
// 构造 参数(order_id=L125) |
|
|
|
String body = "{\"order_id\":\"" + linkId + "\"}"; |
|
|
|
|
|
|
|
/* // 从当前请求中获取 Authorization(关键!) |
|
|
|
ServletRequestAttributes attrs = |
|
|
|
(ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); |
|
|
|
HttpServletRequest currentRequest = attrs.getRequest(); |
|
|
|
String authHeader = currentRequest.getHeader("Authorization"); |
|
|
|
*/ |
|
|
|
// 构建 HttpRequest |
|
|
|
HttpRequest request = HttpRequest.newBuilder() |
|
|
|
// .uri(URI.create(BASE_URLDev + PATH_REFUND)) |
|
|
|
.uri(URI.create("http://a4f24573.natappfree.cc/api/coupon/refundRedPacket")) |
|
|
|
.header("Content-Type", "application/json") |
|
|
|
// ⭐ 透传 token,避免 JWT 过期问题 |
|
|
|
// .header("Authorization", authHeader) |
|
|
|
.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 异常"); |
|
|
|
} |
|
|
|
|
|
|
|
// 解析返回 JSON |
|
|
|
JsonNode root = objectMapper.readTree(resp.body()); |
|
|
|
int code = root.path("code").asInt(); |
|
|
|
boolean flag = root.path("data").path("flag").asBoolean(); |
|
|
|
int num = root.path("data").path("num").asInt(); // ✅ 获取 num |
|
|
|
// 业务判断 |
|
|
|
if (code == 200 && flag) { |
|
|
|
//updateOrderStatus(linkId); |
|
|
|
return "success"; |
|
|
|
} |
|
|
|
|
|
|
|
// 失败 |
|
|
|
throw new RuntimeException("红包校验失败,orderId=" + linkId +"还需金币数"+num); |
|
|
|
|
|
|
|
} catch (IOException | InterruptedException e) { |
|
|
|
log.error("调用红包接口失败,orderId:{}", linkId, e); |
|
|
|
throw new RuntimeException("调用红包接口异常", e); |
|
|
|
} |
|
|
|
|
|
|
|
// 业务判断 |
|
|
|
if (code == 200 && flag) { |
|
|
|
// 正常流程 |
|
|
|
updateOrderStatus(linkId); |
|
|
|
return "success"; |
|
|
|
} |
|
|
|
|
|
|
|
// false 或接口异常 |
|
|
|
throw new RuntimeException("红包校验失败,orderId=" + linkId); |
|
|
|
} |
|
|
|
@Override |
|
|
|
public void updateOrderStatus(String linkId) throws Exception { |
|
|
|
|