Explorar el Código

更改过期合同

mengqiankang hace 2 meses
padre
commit
87300825df
Se han modificado 3 ficheros con 61 adiciones y 19 borrados
  1. 28 1
      alien_store/api/router.py
  2. 17 2
      alien_store/schemas/request/contract_store.py
  3. 16 16
      test.py

+ 28 - 1
alien_store/api/router.py

@@ -3,6 +3,7 @@ from fastapi import APIRouter, Depends, Query
 from typing import Any,  Union, Optional
 import os
 import logging
+from pydantic import ValidationError
 from alien_store.api.deps import get_contract_service
 from alien_store.schemas.request.contract_store import TemplatesCreate, SignUrl
 from alien_store.schemas.response.contract_store import (
@@ -44,16 +45,42 @@ logger = _init_logger()
 
 router = APIRouter()
 
+
+def _format_validation_errors(exc: ValidationError) -> list[dict[str, str]]:
+    errors = []
+    for err in exc.errors():
+        loc = err.get("loc", ())
+        field = ".".join(str(item) for item in loc if item != "body")
+        errors.append(
+            {
+                "field": field or "body",
+                "type": err.get("type", "validation_error"),
+                "message": err.get("msg", "参数校验失败"),
+            }
+        )
+    return errors
+
 @router.get("/", response_model=ModuleStatusResponse)
 async def index() -> ModuleStatusResponse:
     return ModuleStatusResponse(module="Contract", status="Ok")
 
 @router.post("/get_esign_templates", response_model=Union[TemplatesCreateResponse, ErrorResponse])
 async def create_esign_templates(
-    templates_data: TemplatesCreate, 
+    templates_data: dict[str, Any],
     templates_server: ContractServer = Depends(get_contract_service)
 ) -> Union[TemplatesCreateResponse, ErrorResponse]:
     """AI审核完调用 e签宝生成文件"""
+    try:
+        templates_data = TemplatesCreate.model_validate(templates_data)
+    except ValidationError as e:
+        detail = _format_validation_errors(e)
+        logger.error("get_esign_templates validation failed: %s", detail)
+        return ErrorResponse(
+            success=False,
+            message="请求参数校验失败",
+            raw={"errors": detail},
+        )
+
     logger.info(f"get_esign_templates request: {templates_data}")
     # res_text = fill_in_template(templates_data.merchant_name)
     res_text = fill_in_template(templates_data.store_name)

+ 17 - 2
alien_store/schemas/request/contract_store.py

@@ -1,10 +1,11 @@
-from pydantic import BaseModel, EmailStr, Field, field_validator
+import re
+from pydantic import BaseModel, Field, field_validator
 
 
 
 class TemplatesCreate(BaseModel):
     """模板创建请求模型"""
-    store_id: int = Field(description="入驻店铺ID")
+    store_id: int = Field(gt=0, description="入驻店铺ID")
     store_name: str = Field(description="商家店铺名称")
     business_segment: str = Field(description="入驻店铺经营板块")
     merchant_name: str = Field(description="商家姓名")
@@ -13,6 +14,20 @@ class TemplatesCreate(BaseModel):
     seal_url: str | None = Field(default=None, description="印章文件地址")
     ord_id: str = Field(description="企业用户的统一社会信用代码")
 
+    @field_validator("contact_phone")
+    @classmethod
+    def validate_contact_phone(cls, value: str) -> str:
+        if not re.fullmatch(r"^1\d{10}$", value):
+            raise ValueError("contact_phone 格式错误,应为11位手机号")
+        return value
+
+    @field_validator("ord_id")
+    @classmethod
+    def validate_ord_id(cls, value: str) -> str:
+        if not re.fullmatch(r"^[0-9A-Z]{18}$", value):
+            raise ValueError("ord_id 格式错误,应为18位大写字母或数字")
+        return value
+
 class SignUrl(BaseModel):
     """签署合同页面请求模型"""
     sign_flow_id: str = Field(description="合同相关的签署id")

+ 16 - 16
test.py

@@ -19,7 +19,7 @@ import requests
 
 # ----------------------------------------------------------------------------------------------------------------------
 # url = "http://127.0.0.1:8001/api/store/contracts/400"
-# url = "http://120.26.186.130:33333/api/store/contracts/420?status=1"
+# url = "http://120.26.186.130:33333/api/store/contracts/420?status=0"
 # """
 # 商家点击合同管理模块时,可以条件筛选查询 未签署合同和已签署合同
 # status:签署状态 0 未签署 1 已签署
@@ -31,7 +31,7 @@ import requests
 
 # ----------------------------------------------------------------------------------------------------------------------
 # url = "http://127.0.0.1:8001/api/store/contracts/detail/c658fe83ce404934971d6912aaa315e0"
-# url = "http://120.26.186.130:33333/api/store/contracts/detail/"
+# url = "http://120.26.186.130:33333/api/store/contracts/detail/030e3e0a1d094399adc0c5e98c7ad2fd"
 # """
 # 商家点击到某个具体的合同 向这个接口发送请求
 # 由于e签宝提供的合同模板URL会过期,所以会再次携带flow_id 向e签宝发起请求刷新新的URL
@@ -58,17 +58,17 @@ import requests
 
 #-----------------------------------------------------------------------------------------------------------------------
 # url = "http://127.0.0.1:8001/api/store/get_all_templates"
-# url = "http://120.26.186.130:33333/api/store/get_all_templates"
-# params = {
-#     "page": 1, # 页码 默认1
-#     "page_size": 10, # 每页条数 默认10
-#     "store": "", # 店铺名称(模糊查询)
-#     "merchant_name": "", # 商家名称(模糊查询)
-#     "signing_status": "", # 签署状态
-#     "business_segment": "", # 经营板块
-#     "store_status": "", # 店铺状态:正常/禁用
-#     "expiry_start": "", # 到期时间起
-#     "expiry_end": "" # 到期时间止
-# }
-# resp = requests.get(url)
-# print(resp.text)
+url = "http://120.26.186.130:33333/api/store/get_all_templates"
+params = {
+    "page": 1, # 页码 默认1
+    "page_size": 10, # 每页条数 默认10
+    "store": "", # 店铺名称(模糊查询)
+    "merchant_name": "", # 商家名称(模糊查询)
+    "signing_status": "", # 签署状态
+    "business_segment": "", # 经营板块
+    "store_status": "", # 店铺状态:正常/禁用
+    "expiry_start": "", # 到期时间起
+    "expiry_end": "" # 到期时间止
+}
+resp = requests.get(url)
+print(resp.text)