获取优惠卷
This commit is contained in:
@@ -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<String, Object> signParams = new LinkedHashMap<>();
|
||||
signParams.put("timestamp", timestamp);
|
||||
signParams.put("noncestr", noncestr);
|
||||
String sign = HxrGoodsSignUtil.computeSign(signParams, ctx.appStr().trim());
|
||||
List<JsonNode> 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<String> 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<String, Object> 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<String> 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 {
|
||||
|
||||
Reference in New Issue
Block a user