diff --git a/service/src/main/java/cn/qihangerp/service/external/sph/ExternalSphPublishService.java b/service/src/main/java/cn/qihangerp/service/external/sph/ExternalSphPublishService.java index 84388b58..1ed284e8 100644 --- a/service/src/main/java/cn/qihangerp/service/external/sph/ExternalSphPublishService.java +++ b/service/src/main/java/cn/qihangerp/service/external/sph/ExternalSphPublishService.java @@ -293,6 +293,8 @@ public class ExternalSphPublishService { + "access_token=" + URLEncoder.encode(accessToken.trim(), StandardCharsets.UTF_8); String raw = httpClient.postJson(fullUrl, body.toJSONString(), Duration.ofSeconds(Math.max(5, props.getHttpReadTimeoutSeconds()))); + log.info("[SPH] classify response shopId={} outGoodsId={} snippet={}", + req.getShopId(), req.getOutGoodsId(), WeixinShopEcLogSupport.snippet(raw, 2000)); if (!WeixinShopEcHttpClient.isBizOk(raw)) { throw new IllegalStateException("类目推荐失败: " + WeixinShopEcHttpClient.formatWxError(raw)); } @@ -313,6 +315,14 @@ public class ExternalSphPublishService { private static JSONArray parseRecommendedCatsV2(String raw) { JSONObject root = JSON.parseObject(raw); + JSONObject data = root.getJSONObject("data"); + if (data != null) { + JSONArray classifyList = data.getJSONArray("classify_list"); + JSONArray fromClassify = parseFromClassifyTree(classifyList); + if (fromClassify != null && !fromClassify.isEmpty()) { + return fromClassify; + } + } JSONArray categories = root.getJSONArray("categories"); if (categories == null || categories.isEmpty()) { return null; @@ -356,6 +366,56 @@ public class ExternalSphPublishService { return null; } + private static JSONArray parseFromClassifyTree(JSONArray classifyList) { + if (classifyList == null || classifyList.isEmpty()) { + return null; + } + JSONObject node = pickBestClassifyNode(classifyList); + if (node == null) { + return null; + } + JSONArray out = new JSONArray(); + while (node != null) { + Long catIdNum = node.getLong("cat_id"); + String catId = catIdNum == null ? node.getString("cat_id") : String.valueOf(catIdNum); + if (StringUtils.hasText(catId)) { + JSONObject one = new JSONObject(); + one.put("cat_id", catId.trim()); + out.add(one); + } + JSONArray sub = node.getJSONArray("sub_classify"); + if (sub == null || sub.isEmpty()) { + break; + } + node = pickBestClassifyNode(sub); + } + return out.isEmpty() ? null : out; + } + + private static JSONObject pickBestClassifyNode(JSONArray arr) { + JSONObject bestQualified = null; + double bestQualifiedScore = -1D; + JSONObject bestAny = null; + double bestAnyScore = -1D; + for (Object o : arr) { + if (!(o instanceof JSONObject n)) { + continue; + } + double score = n.getDoubleValue("confidence"); + if (bestAny == null || score > bestAnyScore) { + bestAny = n; + bestAnyScore = score; + } + if (n.getBooleanValue("qualification")) { + if (bestQualified == null || score > bestQualifiedScore) { + bestQualified = n; + bestQualifiedScore = score; + } + } + } + return bestQualified != null ? bestQualified : bestAny; + } + private static boolean hasCategoryAuth(JSONObject o) { if (o == null) { return false;