router.py 1.3 KB

1234567891011121314151617181920212223242526272829303132
  1. from fastapi import APIRouter
  2. from fastapi.params import Depends
  3. from alien_store.api.deps import get_contract_service
  4. from alien_store.schemas.request.contract_store import TemplatesCreate
  5. from alien_store.services.contract_server import ContractServer
  6. from common.esigntool.main import fill_in_template
  7. import json
  8. router = APIRouter()
  9. @router.get("/")
  10. async def index():
  11. return {"module": "Contract", "status": "Ok"}
  12. @router.post("/get_esign_templates")
  13. async def create_esign_templates(templates_data: TemplatesCreate, templates_server: ContractServer = Depends(get_contract_service)):
  14. # 调用 e签宝生成文件
  15. res_text = fill_in_template(templates_data.merchant_name)
  16. try:
  17. res_data = json.loads(res_text)
  18. except json.JSONDecodeError:
  19. return {"success": False, "message": "e签宝返回非 JSON", "raw": res_text}
  20. # 从返回结构提取下载链接,需与实际返回字段匹配
  21. try:
  22. contract_url = res_data["data"]["fileDownloadUrl"]
  23. except Exception:
  24. return {"success": False, "message": "e签宝返回缺少 fileDownloadUrl", "raw": res_data}
  25. # pydantic v2 使用 model_copy 更新字段
  26. data_with_url = templates_data.model_copy(update={"contract_url": contract_url, "seal_url": None})
  27. return await templates_server.create_template(data_with_url)