抢购货品同步
This commit is contained in:
101
src/main/java/com/rj/service/HxrAdminGoodsService.java
Normal file
101
src/main/java/com/rj/service/HxrAdminGoodsService.java
Normal file
@@ -0,0 +1,101 @@
|
||||
package com.rj.service;
|
||||
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
import com.rj.config.HxrAdminProperties;
|
||||
import com.rj.dto.hxr.HxrLbGoodsSelectResponse;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.time.Duration;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* 调用 hxrd {@code /api/order/goods} 货品列表(请求头 {@code token} 鉴权)。
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class HxrAdminGoodsService {
|
||||
|
||||
public static final int GOODS_PAGE_SIZE = 20;
|
||||
|
||||
private static final ObjectMapper JSON = new ObjectMapper()
|
||||
.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE)
|
||||
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
|
||||
.registerModule(new JavaTimeModule())
|
||||
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
|
||||
|
||||
private final HxrAdminProperties properties;
|
||||
|
||||
/**
|
||||
* 分页拉取货品列表。
|
||||
*
|
||||
* @param page 页码,从 1 开始
|
||||
*/
|
||||
public Optional<HxrLbGoodsSelectResponse> fetchGoodsPage(int page) throws Exception {
|
||||
String token = properties.getGoodsApiToken();
|
||||
if (token == null || token.isBlank()) {
|
||||
log.warn("hxr.admin 未配置 goods-api-token,跳过 /api/order/goods");
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
String uri = UriComponentsBuilder.fromUriString(properties.getGoodsApiUrl())
|
||||
.replaceQueryParam("page", Math.max(1, page))
|
||||
.replaceQueryParam("limit", GOODS_PAGE_SIZE)
|
||||
.build()
|
||||
.encode()
|
||||
.toUriString();
|
||||
|
||||
HttpClient client = HttpClient.newBuilder()
|
||||
.connectTimeout(Duration.ofSeconds(35))
|
||||
.followRedirects(HttpClient.Redirect.NORMAL)
|
||||
.build();
|
||||
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create(uri))
|
||||
.timeout(Duration.ofSeconds(120))
|
||||
.header("Accept", "application/json, text/plain, */*")
|
||||
.header("token", token.trim())
|
||||
.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36")
|
||||
.GET()
|
||||
.build();
|
||||
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
int status = response.statusCode();
|
||||
if (status < 200 || status >= 300) {
|
||||
log.warn("hxr /api/order/goods HTTP {} page={} bodyPrefix={}",
|
||||
status, page, abbreviate(response.body(), 400));
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
String bodyText = response.body();
|
||||
if (bodyText == null || bodyText.isBlank()) {
|
||||
log.warn("hxr /api/order/goods empty body page={}", page);
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
HxrLbGoodsSelectResponse body = JSON.readValue(bodyText, HxrLbGoodsSelectResponse.class);
|
||||
if (body.code() != 0) {
|
||||
log.warn("hxr /api/order/goods api code={} msg={} page={}", body.code(), body.msg(), page);
|
||||
return Optional.empty();
|
||||
}
|
||||
return Optional.of(body);
|
||||
}
|
||||
|
||||
private static String abbreviate(String s, int maxLen) {
|
||||
if (s == null) {
|
||||
return "";
|
||||
}
|
||||
return s.length() <= maxLen ? s : s.substring(0, maxLen) + "...";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user