diff --git a/app/routers/resell.py b/app/routers/resell.py index 8efe5df..8dba756 100644 --- a/app/routers/resell.py +++ b/app/routers/resell.py @@ -295,8 +295,11 @@ def create_resell_router( cat = dict(item_do.get("itemCatDTO") or {}) cat_name = _category_name(labels) - # 2. 类目名缺失时用 kgraph AI 识别兜底(正常详情都带,兜底很少触发) - if not cat.get("catId") or not cat_name: + # 2. 类目信息不全时用 kgraph AI 识别兜底。发布接口要求 itemCatDTO 的 + # channelCatId/tbCatId 是真实值——详情偶尔缺这两项,直接透传会变成 + # 字符串 "None",闲鱼解析报文失败(FAIL_BIZ_JSON_ERROR)。 + if not (cat.get("catId") and cat.get("channelCatId") + and cat.get("tbCatId") and cat_name): cat, cat_name = await _recommend_category( req.cookie_id, cookies_str, req.title.strip(), images, req.item_id, @@ -313,7 +316,14 @@ def create_resell_router( ) poi_data = poi_result.get("data") or {} addresses = poi_data.get("commonAddresses") or [] - selected = poi_data.get("selectedPoi") or (addresses[0] if addresses else None) + # 发布地址必须带 divisionId:缺了 itemAddrDTO 会退化成空字段,闲鱼 + # 解析报文失败,返回 FAIL_BIZ_JSON_ERROR(通用文案,看不出是地址问题)。 + candidates = [poi_data.get("selectedPoi") or {}] + candidates += [a for a in addresses if isinstance(a, dict)] + selected = next( + (a for a in candidates if a.get("divisionId")), + None, + ) if not selected: raise HTTPException(status_code=400, detail="账号未设置发布地址,请先在闲鱼 App 设置默认地址") @@ -359,7 +369,6 @@ def create_resell_router( delivery=req.delivery, quantity=req.quantity, images=images, - labels=labels, cat=cat, cat_name=cat_name, location=selected, @@ -370,7 +379,8 @@ def create_resell_router( logger.info( f"【转卖发布】{req.cookie_id} item={req.item_id} " f"title_len={len(req.title.strip())} desc_len={len(publish_desc)} " - f"images={len(images)} labels={len(labels)} cat={cat.get('catId')} " + f"images={len(images)} cat={cat.get('catId')}/{cat.get('channelCatId')} " + f"cat_name={cat_name} " f"price={req.price} delivery={req.delivery} qty={req.quantity}" ) @@ -635,7 +645,6 @@ def _build_publish_payload( delivery: str, quantity: int, images: List[Dict[str, Any]], - labels: List[Dict[str, Any]], cat: Dict[str, Any], cat_name: str, location: Dict[str, Any], @@ -696,7 +705,11 @@ def _build_publish_payload( "simpleItem": "true", "imageInfoDOList": image_do_list, "itemTextDTO": {"desc": desc, "title": title, "titleDescSeparate": True}, - "itemLabelExtList": labels or [], + # 必须是空数组:这里的 labels 是详情接口 itemDO.itemLabelExtList 的 + # 展示型标签(propertyText/valueText),与发布接口要的结构完全不同, + # 原样透传会让闲鱼反序列化失败并返回 FAIL_BIZ_JSON_ERROR。 + # 已核对真实发布实现(goofish-cli / PC 发布页):这一项恒为空。 + "itemLabelExtList": [], "itemPriceDTO": price_dto, "userRightsProtocols": [{"enable": False, "serviceCode": "SKILL_PLAY_NO_MIND"}], "itemPostFeeDTO": post_fee, diff --git a/app/routers/xyh_items.py b/app/routers/xyh_items.py index 1eaf501..a07c30e 100644 --- a/app/routers/xyh_items.py +++ b/app/routers/xyh_items.py @@ -21,6 +21,7 @@ from app.routers.resell import ( _detail_images, _money, _normalize_detail, + _recommend_category, ) logger = logging.getLogger(__name__) @@ -235,7 +236,14 @@ def create_xyh_items_router( labels = item_do.get("itemLabelExtList") or [] cat = dict(item_do.get("itemCatDTO") or {}) cat_name = _category_name(labels) - if not cat.get("catId") or not cat_name: + if not (cat.get("catId") and cat.get("channelCatId") + and cat.get("tbCatId") and cat_name): + # 类目字段不全时透传会变成 "None",闲鱼以 FAIL_BIZ_JSON_ERROR 拒绝 + cat, cat_name = await _recommend_category( + req.cookie_id, cookies_str, + req.title.strip(), images, req.item_id, + ) + if not cat.get("catId"): raise HTTPException(status_code=400, detail="详情里没有类目信息,无法保存编辑") # 2. 发布地址(与转卖一致,取账号默认) @@ -247,7 +255,9 @@ def create_xyh_items_router( ) poi_data = poi_result.get("data") or {} addresses = poi_data.get("commonAddresses") or [] - selected = poi_data.get("selectedPoi") or (addresses[0] if addresses else None) + candidates = [poi_data.get("selectedPoi") or {}] + candidates += [a for a in addresses if isinstance(a, dict)] + selected = next((a for a in candidates if a.get("divisionId")), None) if not selected: raise HTTPException(status_code=400, detail="账号未设置发布地址,请先在闲鱼 App 设置默认地址") @@ -261,7 +271,6 @@ def create_xyh_items_router( delivery=req.delivery, quantity=max(1, int(req.quantity or 1)), images=images, - labels=labels, cat=cat, cat_name=cat_name, location=selected,