|
|
@@ -5,14 +5,13 @@ 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.request.contract_store import TemplatesCreate
|
|
|
from alien_store.schemas.response.contract_store import (
|
|
|
ModuleStatusResponse,
|
|
|
TemplatesCreateResponse,
|
|
|
ErrorResponse,
|
|
|
ContractStoreResponse,
|
|
|
PaginatedResponse,
|
|
|
- SignUrlResponse,
|
|
|
SuccessResponse
|
|
|
)
|
|
|
from alien_store.services.contract_server import ContractServer
|
|
|
@@ -257,10 +256,31 @@ async def get_contract_detail(
|
|
|
break
|
|
|
await templates_server.update_contract_items(row["id"], items)
|
|
|
|
|
|
+ # 融合原 /esign/signurl 逻辑:调用 e签宝 获取签署链接并落库
|
|
|
+ contact_phone = item.get("contact_phone") or (row.get("contact_phone") if isinstance(row, dict) else None)
|
|
|
+ if not contact_phone:
|
|
|
+ return ErrorResponse(success=False, message="缺少 contact_phone,无法获取签署链接")
|
|
|
+ try:
|
|
|
+ sign_resp = sign_url(sign_flow_id, contact_phone)
|
|
|
+ sign_json = json.loads(sign_resp)
|
|
|
+ except json.JSONDecodeError:
|
|
|
+ logger.error(f"sign_url non-json resp: {sign_resp}")
|
|
|
+ return ErrorResponse(success=False, message="e签宝返回非JSON", raw=sign_resp)
|
|
|
+ except Exception as e:
|
|
|
+ logger.error(f"sign_url failed sign_flow_id={sign_flow_id}, contact_phone={contact_phone}: {e}")
|
|
|
+ return ErrorResponse(success=False, message="获取签署链接失败", raw=str(e))
|
|
|
+
|
|
|
+ sign_data = sign_json.get("data") if isinstance(sign_json, dict) else None
|
|
|
+ result_sign_url = sign_data.get("url") if isinstance(sign_data, dict) else None
|
|
|
+ if not result_sign_url:
|
|
|
+ logger.error(f"sign_url missing url: {sign_json}")
|
|
|
+ return ErrorResponse(success=False, message="e签宝返回缺少签署链接", raw=sign_json)
|
|
|
+ await templates_server.update_sign_url(contact_phone, sign_flow_id, result_sign_url)
|
|
|
+
|
|
|
return {
|
|
|
"status": 0,
|
|
|
"contract_url": contract_url,
|
|
|
- "sign_url": item.get("sign_url", ""),
|
|
|
+ "sign_url": result_sign_url,
|
|
|
"sign_flow_id": sign_flow_id
|
|
|
}
|
|
|
|
|
|
@@ -325,36 +345,6 @@ async def get_all_templates(
|
|
|
total_pages=total_pages
|
|
|
)
|
|
|
|
|
|
-@router.post("/esign/signurl", response_model=Union[SignUrlResponse, ErrorResponse])
|
|
|
-async def get_esign_sign_url(
|
|
|
- body: SignUrl,
|
|
|
- templates_server: ContractServer = Depends(get_contract_service)
|
|
|
-) -> Union[SignUrlResponse, ErrorResponse]:
|
|
|
- """
|
|
|
- 当商家点击签署按钮时
|
|
|
- 携带合同相关的签署id和联系方式向e签宝发起请求
|
|
|
- 获取到签署的页面链接
|
|
|
- 并将签署url存入该合同对应的sign_url中
|
|
|
- """
|
|
|
-
|
|
|
- sing_flow_id = body.sign_flow_id
|
|
|
- contact_phone = body.contact_phone
|
|
|
- logger.info(f"esign/signurl request contact_phone={contact_phone}, sign_flow_id={sing_flow_id}")
|
|
|
- result = sign_url(sing_flow_id, contact_phone)
|
|
|
- try:
|
|
|
- result_json = json.loads(result)
|
|
|
- except json.JSONDecodeError:
|
|
|
- logger.error(f"sign_url non-json resp: {result}")
|
|
|
- return ErrorResponse(success=False, message="e签宝返回非JSON", raw=result)
|
|
|
- data = result_json.get("data") if isinstance(result_json, dict) else None
|
|
|
- if not data or not data.get("url"):
|
|
|
- logger.error(f"sign_url missing url: {result_json}")
|
|
|
- return ErrorResponse(success=False, message="e签宝返回缺少签署链接", raw=result_json)
|
|
|
- result_sign_url = data.get("url")
|
|
|
- await templates_server.update_sign_url(contact_phone, sing_flow_id, result_sign_url)
|
|
|
- logger.info(f"sign_url success contact_phone={contact_phone}, sign_flow_id={sing_flow_id}")
|
|
|
- return SignUrlResponse(success=True, data={"url": result_sign_url})
|
|
|
-
|
|
|
@router.post("/esign/callback", response_model=Union[SuccessResponse, ErrorResponse])
|
|
|
async def esign_callback(
|
|
|
payload: dict,
|