同步接口增加 token参数
This commit is contained in:
@@ -117,10 +117,14 @@ public class LbGoodsController {
|
|||||||
summary = "从 hxrd 第三方同步货品",
|
summary = "从 hxrd 第三方同步货品",
|
||||||
description =
|
description =
|
||||||
"分页调用 GET /api/order/goods(每页 limit=20,自动计算 S/T/N 签名),"
|
"分页调用 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(
|
public ResponseEntity<Map<String, Object>> syncFromHxr(
|
||||||
@Parameter(description = "租户 id", required = true) @RequestParam String tenantId) {
|
@Parameter(description = "租户 id", required = true) @RequestParam String tenantId,
|
||||||
Map<String, Object> result = lbGoodsService.syncFromHxrGoods(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");
|
Boolean success = (Boolean) result.get("success");
|
||||||
if (success != null && success) {
|
if (success != null && success) {
|
||||||
return ResponseEntity.ok(result);
|
return ResponseEntity.ok(result);
|
||||||
|
|||||||
@@ -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 {
|
public Optional<HxrLbGoodsSelectResponse> fetchGoodsPage(int page, String token) throws Exception {
|
||||||
String token = properties.getGoodsApiToken();
|
String resolvedToken = resolveToken(token);
|
||||||
if (token == null || token.isBlank()) {
|
if (resolvedToken == null) {
|
||||||
log.warn("hxr.admin 未配置 goods-api-token,跳过 /api/order/goods");
|
log.warn("未提供 token 且 hxr.admin 未配置 goods-api-token,跳过 /api/order/goods");
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
String appStr = properties.getGoodsApiAppStr();
|
String appStr = properties.getGoodsApiAppStr();
|
||||||
@@ -95,7 +96,7 @@ public class HxrAdminGoodsService {
|
|||||||
.header("Origin", properties.getGoodsApiOrigin())
|
.header("Origin", properties.getGoodsApiOrigin())
|
||||||
.header("Referer", properties.getGoodsApiReferer())
|
.header("Referer", properties.getGoodsApiReferer())
|
||||||
.header("User-Agent", USER_AGENT)
|
.header("User-Agent", USER_AGENT)
|
||||||
.header("token", token.trim())
|
.header("token", resolvedToken)
|
||||||
.header("S", sign)
|
.header("S", sign)
|
||||||
.header("T", String.valueOf(timestamp))
|
.header("T", String.valueOf(timestamp))
|
||||||
.header("N", noncestr)
|
.header("N", noncestr)
|
||||||
@@ -135,6 +136,18 @@ public class HxrAdminGoodsService {
|
|||||||
return Optional.of(body);
|
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 位随机串。 */
|
/** 与前端 {@code Math.random().toString(36).slice(-5)} 接近的 5 位随机串。 */
|
||||||
private static String randomNoncestr() {
|
private static String randomNoncestr() {
|
||||||
String base36 = Long.toUnsignedString(Math.abs(RANDOM.nextLong()), 36);
|
String base36 = Long.toUnsignedString(Math.abs(RANDOM.nextLong()), 36);
|
||||||
|
|||||||
@@ -30,8 +30,10 @@ public interface ILbGoodsService extends IService<LbGoods> {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 分页调用第三方 {@code /api/order/goods},解析后 upsert 到 {@code lb_goods}。
|
* 分页调用第三方 {@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} 抢购。
|
* 按 {@code total_money} 从大到小选取 {@code lb_goods},调用 hxrd {@code /api/order/buy} 抢购。
|
||||||
|
|||||||
@@ -244,7 +244,7 @@ public class LbGoodsServiceImpl extends ServiceImpl<LbGoodsMapper, LbGoods> impl
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@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<>();
|
Map<String, Object> result = new HashMap<>();
|
||||||
try {
|
try {
|
||||||
if (tenantId == null || tenantId.trim().isEmpty()) {
|
if (tenantId == null || tenantId.trim().isEmpty()) {
|
||||||
@@ -261,7 +261,8 @@ public class LbGoodsServiceImpl extends ServiceImpl<LbGoodsMapper, LbGoods> impl
|
|||||||
int page = 1;
|
int page = 1;
|
||||||
int totalSynced = 0;
|
int totalSynced = 0;
|
||||||
while (true) {
|
while (true) {
|
||||||
Optional<HxrLbGoodsSelectResponse> opt = hxrAdminGoodsService.fetchGoodsPage(page);
|
Optional<HxrLbGoodsSelectResponse> opt =
|
||||||
|
hxrAdminGoodsService.fetchGoodsPage(page, token);
|
||||||
if (opt.isEmpty()) {
|
if (opt.isEmpty()) {
|
||||||
if (page == 1) {
|
if (page == 1) {
|
||||||
result.put("success", false);
|
result.put("success", false);
|
||||||
|
|||||||
Reference in New Issue
Block a user