| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- import datetime
- from fastapi import APIRouter, Depends, Query
- 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.services.contract_server import ContractServer
- from common.esigntool.main import *
- import re, urllib.parse
- router = APIRouter()
- @router.get("/")
- async def index():
- return {"module": "Contract", "status": "Ok"}
- @router.post("/get_esign_templates")
- async def create_esign_templates(templates_data: TemplatesCreate, templates_server: ContractServer = Depends(get_contract_service)):
- # 调用 e签宝生成文件
- res_text = fill_in_template(templates_data.merchant_name)
- try:
- res_data = json.loads(res_text)
- except json.JSONDecodeError:
- return {"success": False, "message": "e签宝返回非 JSON", "raw": res_text}
- # 从返回结构提取下载链接,需与实际返回字段匹配
- try:
- contract_url = res_data["data"]["fileDownloadUrl"]
- file_id = res_data["data"]["fileId"]
- m = re.search(r'/([^/]+)\.pdf', contract_url)
- if m:
- encoded_name = m.group(1)
- file_name = urllib.parse.unquote(encoded_name)
- except Exception:
- return {"success": False, "message": "e签宝返回缺少 fileDownloadUrl", "raw": res_data}
- sign_data = create_by_file(file_id, file_name, templates_data.contact_phone, templates_data.merchant_name)
- sign_json = json.loads(sign_data)
- sing_id = sign_json["data"]["signFlowId"]
- result_contract = {
- "contract_url": contract_url,
- "file_name": file_name,
- "file_id": file_id,
- "status": 0,
- "sign_flow_id": sing_id,
- }
- 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)
- @router.get("/contracts/{store_id}")
- async def list_contracts(store_id: int, templates_server: ContractServer = Depends(get_contract_service)) -> Any:
- """根据 store_id 查询所有合同"""
- rows = await templates_server.list_by_store(store_id)
- return rows
- @router.get("/get_all_templates")
- async def get_all_templates(
- page: int = Query(1, ge=1, description="页码,从1开始"),
- page_size: int = Query(10, ge=1, le=100, description="每页条数,默认10"),
- templates_server: ContractServer = Depends(get_contract_service)
- ) -> Any:
- """分页查询所有合同"""
- return await templates_server.list_all_paged(page, page_size)
- @router.post("/esign/callback")
- async def esign_callback(payload: dict, templates_server: ContractServer = Depends(get_contract_service)) -> Any:
- """
- e签宝签署结果回调
- 需求:签署完成 -> 更新 signing_status=已签署,contract_url 中 status=1
- """
- sign_result = payload.get("signResult")
- operator = payload.get("operator") or {}
- psn_account = operator.get("psnAccount") or {}
- contact_phone = psn_account.get("accountMobile")
- # 取回调中的毫秒时间戳,优先 operateTime,其次 timestamp
- ts_ms = payload.get("operateTime") or payload.get("timestamp")
- signing_dt = None
- if ts_ms:
- try:
- signing_dt = datetime.fromtimestamp(ts_ms / 1000)
- 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 或手机号缺失"}
|