fix: 修复 Mall API 数据提取逻辑和添加配置字段
## 问题 1: Settings 缺少 Mall API 配置
**错误**: `'Settings' object has no attribute 'mall_api_url'`
**原因**: Settings 类只有 hyperf 配置,缺少 Mall API 相关字段
**解决方案**: 添加 Mall API 配置字段(第 20-25 行)
```python
mall_api_url: str
mall_tenant_id: str = "2"
mall_currency_code: str = "EUR"
mall_language_id: str = "1"
mall_source: str = "us.qa1.gaia888.com"
```
## 问题 2: Mall API 数据结构解析错误
**现象**: 商品搜索始终返回 0 个商品
**原因**: Mall API 返回的数据结构与预期不符
**Mall API 实际返回**:
```json
{
"total": 0,
"data": {
"data": [], // ← 商品列表在这里
"isClothesClassification": false,
"ad": {...}
}
}
```
**代码原来查找**: `result.get("list", [])` ❌
**修复后查找**: `result["data"]["data"]` ✅
**解决方案**: 修改数据提取逻辑(第 317-323 行)
```python
if "data" in result and isinstance(result["data"], dict):
products = result["data"].get("data", [])
else:
products = result.get("list", []) # 向后兼容
total = result.get("total", 0)
```
## 调试增强
添加 print 调试语句:
- 第 292 行:打印调用参数
- 第 315 行:打印 Mall API 返回结果
便于诊断 API 调用问题。
## 测试结果
修复前:
```
'Settings' object has no attribute 'mall_api_url'
```
修复后:
```json
{
"success": true,
"products": [],
"total": 0,
"keyword": "61607"
}
```
✅ 工具调用成功
⚠️ 返回 0 商品(可能是关键词无匹配)
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -17,6 +17,11 @@ class Settings(BaseSettings):
|
|||||||
"""Server configuration"""
|
"""Server configuration"""
|
||||||
hyperf_api_url: str
|
hyperf_api_url: str
|
||||||
hyperf_api_token: str
|
hyperf_api_token: str
|
||||||
|
mall_api_url: str
|
||||||
|
mall_tenant_id: str = "2"
|
||||||
|
mall_currency_code: str = "EUR"
|
||||||
|
mall_language_id: str = "1"
|
||||||
|
mall_source: str = "us.qa1.gaia888.com"
|
||||||
log_level: str = "INFO"
|
log_level: str = "INFO"
|
||||||
|
|
||||||
model_config = ConfigDict(env_file=".env")
|
model_config = ConfigDict(env_file=".env")
|
||||||
@@ -277,6 +282,14 @@ async def search_products(
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
from shared.mall_client import MallClient
|
from shared.mall_client import MallClient
|
||||||
|
import logging
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
f"search_products called with keyword={keyword}, "
|
||||||
|
f"user_token_prefix={user_token[:20] if user_token else None}..."
|
||||||
|
)
|
||||||
|
print(f"[DEBUG] search_products called: keyword={keyword}, user_token={user_token[:20] if user_token else None}...")
|
||||||
|
|
||||||
# 使用用户 token 创建 Mall 客户端
|
# 使用用户 token 创建 Mall 客户端
|
||||||
mall = MallClient(
|
mall = MallClient(
|
||||||
@@ -294,7 +307,18 @@ async def search_products(
|
|||||||
page=page
|
page=page
|
||||||
)
|
)
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
f"Mall API returned: result_type={type(result).__name__}, "
|
||||||
|
f"result_keys={list(result.keys()) if isinstance(result, dict) else 'not a dict'}, "
|
||||||
|
f"result={result}"
|
||||||
|
)
|
||||||
|
print(f"[DEBUG] Mall API returned: {result}")
|
||||||
|
|
||||||
# 解析返回结果
|
# 解析返回结果
|
||||||
|
# Mall API 返回结构: {"total": X, "data": {"data": [...], ...}}
|
||||||
|
if "data" in result and isinstance(result["data"], dict):
|
||||||
|
products = result["data"].get("data", [])
|
||||||
|
else:
|
||||||
products = result.get("list", [])
|
products = result.get("list", [])
|
||||||
total = result.get("total", 0)
|
total = result.get("total", 0)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user