ソースを参照

接口响应问题

mengqiankang 2 ヶ月 前
コミット
af2bdbf5cb
1 ファイル変更58 行追加47 行削除
  1. 58 47
      alien_store/api/router.py

+ 58 - 47
alien_store/api/router.py

@@ -122,57 +122,68 @@ async def list_contracts(
     templates_server: ContractServer = Depends(get_contract_service)
 ) -> Any:
     """根据 store_id 查询所有合同,支持根据 status 筛选和分页"""
-    # 1. 检查 store_info 中的审核状态
-    reason = await templates_server.get_store_reason(store_id)
-    if reason != "审核通过":
-        return {"code": 555, "msg": "先进行认证"}
-
-    # 2. 返回合同列表
-    rows = await templates_server.list_by_store(store_id)
-    
-    all_filtered_items = []
-    # 3. 解析并筛选所有符合条件的合同项
-    for row in rows:
-        contract_url_raw = row.get("contract_url")
-        if not contract_url_raw:
-            continue
-        try:
-            items = json.loads(contract_url_raw)
-            if not isinstance(items, list):
+    logger.info(
+        "list_contracts request store_id=%s status=%s page=%s page_size=%s",
+        store_id,
+        status,
+        page,
+        page_size,
+    )
+    try:
+        # 1. 检查 store_info 中的审核状态
+        reason = await templates_server.get_store_reason(store_id)
+        if reason != "审核通过":
+            return {"code": 555, "msg": "先进行认证", "reason": reason}
+
+        # 2. 返回合同列表
+        rows = await templates_server.list_by_store(store_id)
+        
+        all_filtered_items = []
+        # 3. 解析并筛选所有符合条件的合同项
+        for row in rows:
+            contract_url_raw = row.get("contract_url")
+            if not contract_url_raw:
                 continue
-            
-            for item in items:
-                # 如果传了 status,则进行筛选
-                if status is not None and item.get("status") != status:
+            try:
+                items = json.loads(contract_url_raw)
+                if not isinstance(items, list):
                     continue
                 
-                # 将店铺基础信息混入每个合同项中,方便前端展示
-                item_with_info = dict(item)
-                item_with_info["id"] = row.get("id")
-                item_with_info["store_id"] = row.get("store_id")
-                item_with_info["store_name"] = row.get("store_name")
-                item_with_info["merchant_name"] = row.get("merchant_name")
-                item_with_info["contact_phone"] = row.get("contact_phone")
-                all_filtered_items.append(item_with_info)
-        except Exception as e:
-            logger.error(f"Error processing contracts for store_id {store_id}: {e}")
-            continue
-
-    # 4. 手动分页
-    total = len(all_filtered_items)
-    start = (page - 1) * page_size
-    end = start + page_size
-    paged_items = all_filtered_items[start:end]
-    
-    total_pages = (total + page_size - 1) // page_size if total > 0 else 0
+                for item in items:
+                    # 如果传了 status,则进行筛选
+                    if status is not None and item.get("status") != status:
+                        continue
+                    
+                    # 将店铺基础信息混入每个合同项中,方便前端展示
+                    item_with_info = dict(item)
+                    item_with_info["id"] = row.get("id")
+                    item_with_info["store_id"] = row.get("store_id")
+                    item_with_info["store_name"] = row.get("store_name")
+                    item_with_info["merchant_name"] = row.get("merchant_name")
+                    item_with_info["contact_phone"] = row.get("contact_phone")
+                    all_filtered_items.append(item_with_info)
+            except Exception as e:
+                logger.error(f"Error processing contracts for store_id {store_id}: {e}", exc_info=True)
+                continue
 
-    return {
-        "items": paged_items,
-        "total": total,
-        "page": page,
-        "page_size": page_size,
-        "total_pages": total_pages
-    }
+        # 4. 手动分页
+        total = len(all_filtered_items)
+        start = (page - 1) * page_size
+        end = start + page_size
+        paged_items = all_filtered_items[start:end]
+        
+        total_pages = (total + page_size - 1) // page_size if total > 0 else 0
+
+        return {
+            "items": paged_items,
+            "total": total,
+            "page": page,
+            "page_size": page_size,
+            "total_pages": total_pages
+        }
+    except Exception as e:
+        logger.error(f"list_contracts failed store_id={store_id}: {e}", exc_info=True)
+        return {"code": 500, "msg": "查询合同失败", "error": str(e)}
 
 @router.get("/contracts/detail/{sign_flow_id}", response_model=Union[dict, ErrorResponse])
 async def get_contract_detail(