|
|
@@ -4,7 +4,7 @@ from typing import Any
|
|
|
import json
|
|
|
from datetime import datetime
|
|
|
from alien_store.api.deps import get_contract_service
|
|
|
-from alien_store.schemas.request.contract_store import TemplatesCreate
|
|
|
+from alien_store.schemas.request.contract_store import TemplatesCreate, SignUrl
|
|
|
from alien_store.services.contract_server import ContractServer
|
|
|
from common.esigntool.main import *
|
|
|
import re, urllib.parse
|
|
|
@@ -17,7 +17,7 @@ async def index():
|
|
|
|
|
|
@router.post("/get_esign_templates")
|
|
|
async def create_esign_templates(templates_data: TemplatesCreate, templates_server: ContractServer = Depends(get_contract_service)):
|
|
|
- # 调用 e签宝生成文件
|
|
|
+ # AI审核完调用 e签宝生成文件
|
|
|
res_text = fill_in_template(templates_data.merchant_name)
|
|
|
try:
|
|
|
res_data = json.loads(res_text)
|
|
|
@@ -43,14 +43,10 @@ async def create_esign_templates(templates_data: TemplatesCreate, templates_serv
|
|
|
"file_id": file_id,
|
|
|
"status": 0,
|
|
|
"sign_flow_id": sing_id,
|
|
|
+ "sign_url": ""
|
|
|
}
|
|
|
- list_contract = [result_contract]
|
|
|
- # contract_url 字段为字符串,存储 JSON 字符串避免类型错误
|
|
|
- result_data = templates_data.model_copy(
|
|
|
- update={"contract_url": json.dumps(list_contract, ensure_ascii=False), "seal_url": None}
|
|
|
- )
|
|
|
- return await templates_server.create_template(result_data)
|
|
|
-
|
|
|
+ updated = await templates_server.append_contract_url(templates_data, result_contract)
|
|
|
+ return {"success": True, "message": "合同模板已追加/创建", "sign_flow_id": sing_id, "file_id": file_id, "contract_url": contract_url}
|
|
|
|
|
|
@router.get("/contracts/{store_id}")
|
|
|
async def list_contracts(store_id: int, templates_server: ContractServer = Depends(get_contract_service)) -> Any:
|
|
|
@@ -67,6 +63,22 @@ async def get_all_templates(
|
|
|
"""分页查询所有合同"""
|
|
|
return await templates_server.list_all_paged(page, page_size)
|
|
|
|
|
|
+@router.post("/esign/signurl")
|
|
|
+async def get_esign_sign_url(body: SignUrl, templates_server: ContractServer = Depends(get_contract_service)):
|
|
|
+ sing_flow_id = body.sign_flow_id
|
|
|
+ contact_phone = body.contact_phone
|
|
|
+ result = sign_url(sing_flow_id, contact_phone)
|
|
|
+ try:
|
|
|
+ result_json = json.loads(result)
|
|
|
+ except json.JSONDecodeError:
|
|
|
+ return {"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"):
|
|
|
+ return {"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)
|
|
|
+ return {"success": True, "data": {"url": result_sign_url}}
|
|
|
+
|
|
|
@router.post("/esign/callback")
|
|
|
async def esign_callback(payload: dict, templates_server: ContractServer = Depends(get_contract_service)) -> Any:
|
|
|
"""
|
|
|
@@ -75,6 +87,7 @@ async def esign_callback(payload: dict, templates_server: ContractServer = Depen
|
|
|
"""
|
|
|
sign_result = payload.get("signResult")
|
|
|
operator = payload.get("operator") or {}
|
|
|
+ sign_flow_id = payload.get("signFlowId")
|
|
|
psn_account = operator.get("psnAccount") or {}
|
|
|
contact_phone = psn_account.get("accountMobile")
|
|
|
# 取回调中的毫秒时间戳,优先 operateTime,其次 timestamp
|
|
|
@@ -86,7 +99,13 @@ async def esign_callback(payload: dict, templates_server: ContractServer = Depen
|
|
|
except Exception:
|
|
|
signing_dt = None
|
|
|
|
|
|
- if sign_result == 2 and contact_phone:
|
|
|
- updated = await templates_server.mark_signed_by_phone(contact_phone, signing_dt)
|
|
|
- return {"success": True, "updated": updated}
|
|
|
- return {"success": False, "message": "未处理: signResult!=2 或手机号缺失"}
|
|
|
+ if sign_result == 2 and contact_phone and sign_flow_id:
|
|
|
+ updated = await templates_server.mark_signed_by_phone(contact_phone, sign_flow_id, signing_dt)
|
|
|
+ return {"code":"200","msg":"success"}
|
|
|
+ return {"success": False, "message": "未处理: signResult!=2 或手机号/签署流程缺失"}
|
|
|
+
|
|
|
+@router.post("/esign/callback_auth")
|
|
|
+async def esign_callback_auth(payload: dict, templates_server: ContractServer = Depends(get_contract_service)):
|
|
|
+ print(payload)
|
|
|
+ return {"code":"200","msg":"success"}
|
|
|
+
|