From 9b94911319508c1ffc2b6d5d52287522657a9c90 Mon Sep 17 00:00:00 2001 From: cst61 Date: Sat, 27 Jun 2026 20:12:11 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E4=BC=98=E6=83=A0=E5=8D=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/LbUserCouponServiceImpl.java | 110 +++++++++++++----- 1 file changed, 83 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/rj/service/impl/LbUserCouponServiceImpl.java b/src/main/java/com/rj/service/impl/LbUserCouponServiceImpl.java index 5253aaa..6c931aa 100644 --- a/src/main/java/com/rj/service/impl/LbUserCouponServiceImpl.java +++ b/src/main/java/com/rj/service/impl/LbUserCouponServiceImpl.java @@ -7,6 +7,8 @@ import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.PropertyNamingStrategies; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; import com.rj.dto.hxr.HxrMoneyCouponApiContext; import com.rj.dto.hxr.HxrUserLoginApiContext; import com.rj.entity.LbThirdIntegrationConfig; @@ -211,43 +213,97 @@ public class LbUserCouponServiceImpl return null; } - int pageLimit = recentDays > 0 ? recentDays : 90; - String uri = couponApiBaseUrl + (couponApiBaseUrl.contains("?") ? "&" : "?") + "limit=" + pageLimit; + // 每页条数使用配置值 + int pageSize = ctx.pageLimit() > 0 ? ctx.pageLimit() : 10; + // 总共需要获取的条数 + int totalNeeded = recentDays > 0 ? recentDays : 90; - long timestamp = System.currentTimeMillis() / 1000; - String noncestr = randomNoncestr(); - Map signParams = new LinkedHashMap<>(); - signParams.put("timestamp", timestamp); - signParams.put("noncestr", noncestr); - String sign = HxrGoodsSignUtil.computeSign(signParams, ctx.appStr().trim()); + List allItems = new ArrayList<>(); + int page = 1; + int fetched = 0; HttpClient client = HttpClient.newBuilder() .connectTimeout(Duration.ofSeconds(60)) .followRedirects(HttpClient.Redirect.NORMAL) .build(); - HttpRequest request = HttpRequest.newBuilder() - .uri(URI.create(uri)) - .timeout(Duration.ofSeconds(120)) - .header("Accept", "application/json,*/*") - .header("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8") - .header("Origin", ctx.origin()) - .header("Referer", ctx.referer()) - .header("User-Agent", USER_AGENT) - .header(HEADER_TOKEN, ctx.token()) - .header("S", sign) - .header("T", String.valueOf(timestamp)) - .header("N", noncestr) - .GET() - .build(); + while (fetched < totalNeeded) { + int limit = Math.min(pageSize, totalNeeded - fetched); + String uri = couponApiBaseUrl + (couponApiBaseUrl.contains("?") ? "&" : "?") + + "cate=2&type=1&page=" + page + "&limit=" + limit; - HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); - int status = response.statusCode(); - if (status < 200 || status >= 300) { - log.warn("优惠券API HTTP {} bodyPrefix={}", status, abbreviate(response.body(), 400)); + long timestamp = System.currentTimeMillis() / 1000; + String noncestr = randomNoncestr(); + Map signParams = new LinkedHashMap<>(); + signParams.put("timestamp", timestamp); + signParams.put("noncestr", noncestr); + String sign = HxrGoodsSignUtil.computeSign(signParams, ctx.appStr().trim()); + + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create(uri)) + .timeout(Duration.ofSeconds(120)) + .header("Accept", "application/json,*/*") + .header("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8") + .header("Origin", ctx.origin()) + .header("Referer", ctx.referer()) + .header("User-Agent", USER_AGENT) + .header(HEADER_TOKEN, ctx.token()) + .header("S", sign) + .header("T", String.valueOf(timestamp)) + .header("N", noncestr) + .GET() + .build(); + + HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); + int status = response.statusCode(); + if (status < 200 || status >= 300) { + log.warn("优惠券API HTTP {} bodyPrefix={}", status, abbreviate(response.body(), 400)); + break; + } + + JsonNode root = JSON.readTree(response.body()); + int code = root.path("code").asInt(-1); + if (code != 0) { + log.warn("优惠券API返回错误 code={} msg={}", code, root.path("msg").asText("")); + break; + } + + JsonNode listNode = root.path("data").path("list"); + if (listNode.isMissingNode() || !listNode.isArray() || listNode.size() == 0) { + log.info("优惠券API返回空列表 page={}", page); + break; + } + + for (JsonNode item : listNode) { + if (fetched >= totalNeeded) { + break; + } + allItems.add(item); + fetched++; + } + + // 如果返回的数据少于请求的limit,说明没有更多数据了 + if (listNode.size() < limit) { + break; + } + + page++; + } + + if (allItems.isEmpty()) { return null; } - return response.body(); + + // 构建合并后的JSON + ObjectNode resultNode = JSON.createObjectNode(); + resultNode.put("code", 0); + ObjectNode dataNode = resultNode.putObject("data"); + ArrayNode listArray = dataNode.putArray("list"); + for (JsonNode item : allItems) { + listArray.add(item); + } + + return JSON.writeValueAsString(resultNode); } private int parseAndSaveCoupons(String couponJson, LbUser user, String tenantId) throws Exception {