同步接口增加 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

@@ -117,10 +117,14 @@ public class LbGoodsController {
summary = "从 hxrd 第三方同步货品",
description =
"分页调用 GET /api/order/goods每页 limit=20自动计算 S/T/N 签名),"
+ "解析 data.list 后 upsert 到 lb_goods需配置 hxr.admin.goods-api-token、goods-api-app-str")
+ "解析 data.list 后 upsert 到 lb_goods需配置 hxr.admin.goods-api-token、goods-api-app-str"
+ "token 可选,传入时替代配置中的 goods-api-token")
public ResponseEntity<Map<String, Object>> syncFromHxr(
@Parameter(description = "租户 id", required = true) @RequestParam String tenantId) {
Map<String, Object> result = lbGoodsService.syncFromHxrGoods(tenantId);
@Parameter(description = "租户 id", required = true) @RequestParam String tenantId,
@Parameter(description = "hxrd 接口请求头 token未传时使用 hxr.admin.goods-api-token")
@RequestParam(required = false)
String token) {
Map<String, Object> result = lbGoodsService.syncFromHxrGoods(tenantId, token);
Boolean success = (Boolean) result.get("success");
if (success != null && success) {
return ResponseEntity.ok(result);

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);

View File

@@ -30,8 +30,10 @@ public interface ILbGoodsService extends IService<LbGoods> {
/**
* 分页调用第三方 {@code /api/order/goods},解析后 upsert 到 {@code lb_goods}。
*
* @param token 可选;非空时作为请求头 token否则使用 {@code hxr.admin.goods-api-token}
*/
Map<String, Object> syncFromHxrGoods(String tenantId);
Map<String, Object> syncFromHxrGoods(String tenantId, String token);
/**
* 按 {@code total_money} 从大到小选取 {@code lb_goods},调用 hxrd {@code /api/order/buy} 抢购。

View File

@@ -244,7 +244,7 @@ public class LbGoodsServiceImpl extends ServiceImpl<LbGoodsMapper, LbGoods> impl
@Override
@Transactional(rollbackFor = Exception.class)
public Map<String, Object> syncFromHxrGoods(String tenantId) {
public Map<String, Object> syncFromHxrGoods(String tenantId, String token) {
Map<String, Object> result = new HashMap<>();
try {
if (tenantId == null || tenantId.trim().isEmpty()) {
@@ -261,7 +261,8 @@ public class LbGoodsServiceImpl extends ServiceImpl<LbGoodsMapper, LbGoods> impl
int page = 1;
int totalSynced = 0;
while (true) {
Optional<HxrLbGoodsSelectResponse> opt = hxrAdminGoodsService.fetchGoodsPage(page);
Optional<HxrLbGoodsSelectResponse> opt =
hxrAdminGoodsService.fetchGoodsPage(page, token);
if (opt.isEmpty()) {
if (page == 1) {
result.put("success", false);