同步接口增加 token参数

This commit is contained in:
2026-05-28 21:00:51 +08:00
parent 608e5a7adc
commit 246c5a7080
4 changed files with 32 additions and 12 deletions

View File

@@ -51,12 +51,13 @@ public class HxrAdminGoodsService {
/**
* 分页拉取货品列表。
*
* @param page 页码,从 1 开始
* @param page 页码,从 1 开始
* @param token 可选;非空时作为请求头 token否则使用 {@code hxr.admin.goods-api-token}
*/
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");
public Optional<HxrLbGoodsSelectResponse> fetchGoodsPage(int page, String token) throws Exception {
String resolvedToken = resolveToken(token);
if (resolvedToken == null) {
log.warn("未提供 token 且 hxr.admin 未配置 goods-api-token跳过 /api/order/goods");
return Optional.empty();
}
String appStr = properties.getGoodsApiAppStr();
@@ -95,7 +96,7 @@ public class HxrAdminGoodsService {
.header("Origin", properties.getGoodsApiOrigin())
.header("Referer", properties.getGoodsApiReferer())
.header("User-Agent", USER_AGENT)
.header("token", token.trim())
.header("token", resolvedToken)
.header("S", sign)
.header("T", String.valueOf(timestamp))
.header("N", noncestr)
@@ -135,6 +136,18 @@ public class HxrAdminGoodsService {
return Optional.of(body);
}
/** 优先使用入参 token否则回退到配置 {@code goods-api-token}。 */
private String resolveToken(String token) {
if (token != null && !token.isBlank()) {
return token.trim();
}
String configured = properties.getGoodsApiToken();
if (configured != null && !configured.isBlank()) {
return configured.trim();
}
return null;
}
/** 与前端 {@code Math.random().toString(36).slice(-5)} 接近的 5 位随机串。 */
private static String randomNoncestr() {
String base36 = Long.toUnsignedString(Math.abs(RANDOM.nextLong()), 36);