router.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. import datetime
  2. from fastapi import APIRouter, Depends, Query
  3. from typing import Any, List, Union, Optional
  4. import json
  5. import os
  6. import logging
  7. from datetime import datetime
  8. from alien_store.api.deps import get_contract_service
  9. from alien_store.schemas.request.contract_store import TemplatesCreate, SignUrl
  10. from alien_store.schemas.response.contract_store import (
  11. ModuleStatusResponse,
  12. TemplatesCreateResponse,
  13. ErrorResponse,
  14. ContractStoreResponse,
  15. PaginatedResponse,
  16. SignUrlResponse,
  17. SuccessResponse
  18. )
  19. from alien_store.services.contract_server import ContractServer
  20. from common.esigntool.main import *
  21. import re, urllib.parse
  22. # ------------------- 日志配置 -------------------
  23. LOG_DIR = os.path.join("common", "logs", "alien_store")
  24. os.makedirs(LOG_DIR, exist_ok=True)
  25. def _init_logger():
  26. logger = logging.getLogger("alien_store")
  27. if logger.handlers:
  28. return logger
  29. logger.setLevel(logging.INFO)
  30. fmt = logging.Formatter("%(asctime)s [%(levelname)s] %(name)s %(message)s")
  31. info_handler = logging.FileHandler(os.path.join(LOG_DIR, "info.log"), encoding="utf-8")
  32. info_handler.setLevel(logging.INFO)
  33. info_handler.setFormatter(fmt)
  34. error_handler = logging.FileHandler(os.path.join(LOG_DIR, "error.log"), encoding="utf-8")
  35. error_handler.setLevel(logging.ERROR)
  36. error_handler.setFormatter(fmt)
  37. logger.addHandler(info_handler)
  38. logger.addHandler(error_handler)
  39. # 控制台可选: logger.addHandler(logging.StreamHandler())
  40. return logger
  41. logger = _init_logger()
  42. router = APIRouter()
  43. @router.get("/", response_model=ModuleStatusResponse)
  44. async def index() -> ModuleStatusResponse:
  45. return ModuleStatusResponse(module="Contract", status="Ok")
  46. @router.post("/get_esign_templates", response_model=Union[TemplatesCreateResponse, ErrorResponse])
  47. async def create_esign_templates(
  48. templates_data: TemplatesCreate,
  49. templates_server: ContractServer = Depends(get_contract_service)
  50. ) -> Union[TemplatesCreateResponse, ErrorResponse]:
  51. """AI审核完调用 e签宝生成文件"""
  52. logger.info(f"get_esign_templates request: {templates_data}")
  53. # res_text = fill_in_template(templates_data.merchant_name)
  54. res_text = fill_in_template(templates_data.store_name)
  55. try:
  56. res_data = json.loads(res_text)
  57. except json.JSONDecodeError:
  58. logger.error(f"fill_in_template non-json resp: {res_text}")
  59. return ErrorResponse(success=False, message="e签宝返回非 JSON", raw=res_text)
  60. # 从返回结构提取下载链接,需与实际返回字段匹配
  61. try:
  62. contract_url = res_data["data"]["fileDownloadUrl"]
  63. file_id = res_data["data"]["fileId"]
  64. m = re.search(r'/([^/]+)\.pdf', contract_url)
  65. if m:
  66. encoded_name = m.group(1)
  67. file_name = urllib.parse.unquote(encoded_name)
  68. except Exception:
  69. logger.error(f"fill_in_template missing fileDownloadUrl: {res_data}")
  70. return ErrorResponse(success=False, message="e签宝返回缺少 fileDownloadUrl", raw=res_data)
  71. # sign_data = create_by_file(file_id, file_name, templates_data.contact_phone, templates_data.merchant_name)
  72. sign_data = create_by_file(file_id, file_name, templates_data.contact_phone, templates_data.store_name, templates_data.merchant_name, templates_data.ord_id)
  73. try:
  74. sign_json = json.loads(sign_data)
  75. except json.JSONDecodeError:
  76. logger.error(f"create_by_file non-json resp: {sign_data}")
  77. return ErrorResponse(success=False, message="e签宝 create_by_file 返回非 JSON", raw=sign_data)
  78. if not sign_json.get("data"):
  79. logger.error(f"create_by_file failed or missing data: {sign_json}")
  80. return ErrorResponse(success=False, message="e签宝创建签署流程失败", raw=sign_json)
  81. sing_id = sign_json["data"].get("signFlowId")
  82. if not sing_id:
  83. logger.error(f"create_by_file missing signFlowId: {sign_json}")
  84. return ErrorResponse(success=False, message="e签宝返回缺少 signFlowId", raw=sign_json)
  85. result_contract = {
  86. "contract_url": contract_url, # 合同模版链接
  87. "file_name": file_name, # 签署的合同的文件名称
  88. "file_id": file_id, # 生成的文件ID
  89. "status": 0, # 签署状态 0 未签署 1 已签署
  90. "sign_flow_id": sing_id, # 从
  91. "sign_url": "", # e签宝生成的签署页面
  92. "signing_time": "", # 签署合同的时间
  93. "effective_time": "", # 合同生效的时间
  94. "expiry_time": "", # 合同失效的时间
  95. "contract_download_url": "", # 合同签署完成后 下载文件的链接
  96. "is_master": 1 # 是否是入驻店铺的协议合同 是 1 否 0
  97. }
  98. updated = await templates_server.append_contract_url(templates_data, result_contract)
  99. logger.info(f"get_esign_templates success contact_phone={templates_data.contact_phone}, sign_flow_id={sing_id}")
  100. return TemplatesCreateResponse(
  101. success=True,
  102. message="合同模板已追加/创建",
  103. sign_flow_id=sing_id,
  104. file_id=file_id,
  105. contract_url=contract_url
  106. )
  107. @router.get("/contracts/{store_id}", response_model=Union[dict, Any])
  108. async def list_contracts(
  109. store_id: int,
  110. status: Optional[int] = Query(None, description="筛选合同状态:0 未签署,1 已签署"),
  111. page: int = Query(1, ge=1, description="页码,从1开始"),
  112. page_size: int = Query(10, ge=1, le=100, description="每页条数,默认10"),
  113. templates_server: ContractServer = Depends(get_contract_service)
  114. ) -> Any:
  115. """根据 store_id 查询所有合同,支持根据 status 筛选和分页"""
  116. # 1. 检查 store_info 中的审核状态
  117. reason = await templates_server.get_store_reason(store_id)
  118. if reason != "审核通过":
  119. return {"code": 555, "msg": "先进行认证"}
  120. # 2. 返回合同列表
  121. rows = await templates_server.list_by_store(store_id)
  122. all_filtered_items = []
  123. # 3. 解析并筛选所有符合条件的合同项
  124. for row in rows:
  125. contract_url_raw = row.get("contract_url")
  126. if not contract_url_raw:
  127. continue
  128. try:
  129. items = json.loads(contract_url_raw)
  130. if not isinstance(items, list):
  131. continue
  132. for item in items:
  133. # 如果传了 status,则进行筛选
  134. if status is not None and item.get("status") != status:
  135. continue
  136. # 将店铺基础信息混入每个合同项中,方便前端展示
  137. item_with_info = dict(item)
  138. item_with_info["id"] = row.get("id")
  139. item_with_info["store_id"] = row.get("store_id")
  140. item_with_info["store_name"] = row.get("store_name")
  141. item_with_info["merchant_name"] = row.get("merchant_name")
  142. item_with_info["contact_phone"] = row.get("contact_phone")
  143. all_filtered_items.append(item_with_info)
  144. except Exception as e:
  145. logger.error(f"Error processing contracts for store_id {store_id}: {e}")
  146. continue
  147. # 4. 手动分页
  148. total = len(all_filtered_items)
  149. start = (page - 1) * page_size
  150. end = start + page_size
  151. paged_items = all_filtered_items[start:end]
  152. total_pages = (total + page_size - 1) // page_size if total > 0 else 0
  153. return {
  154. "items": paged_items,
  155. "total": total,
  156. "page": page,
  157. "page_size": page_size,
  158. "total_pages": total_pages
  159. }
  160. @router.get("/contracts/detail/{sign_flow_id}", response_model=Union[dict, ErrorResponse])
  161. async def get_contract_detail(
  162. sign_flow_id: str,
  163. templates_server: ContractServer = Depends(get_contract_service)
  164. ) -> Union[dict, ErrorResponse]:
  165. """
  166. 根据 sign_flow_id 获取合同详情
  167. - status=0: 返回合同PDF链接(contract_url)和签署链接(sign_url)
  168. - status=1: 拉取最新下载链接并更新数据库,返回 contract_download_url
  169. """
  170. row, item, items = await templates_server.get_contract_item_by_sign_flow_id(sign_flow_id)
  171. if not item:
  172. return ErrorResponse(success=False, message="未找到合同")
  173. status = item.get("status")
  174. if status == 0:
  175. return {
  176. "status": 0,
  177. "contract_url": item.get("contract_url", ""),
  178. "sign_url": item.get("sign_url", ""),
  179. "sign_flow_id": sign_flow_id
  180. }
  181. if status == 1:
  182. try:
  183. download_resp = file_download_url(sign_flow_id)
  184. download_json = json.loads(download_resp)
  185. contract_download_url = download_json["data"]["files"][0]["downloadUrl"]
  186. except Exception as e:
  187. logger.error(f"file_download_url failed sign_flow_id={sign_flow_id}: {e}")
  188. return ErrorResponse(success=False, message="获取合同下载链接失败", raw=str(e))
  189. if row and isinstance(items, list):
  190. for it in items:
  191. if it.get("sign_flow_id") == sign_flow_id:
  192. it["contract_download_url"] = contract_download_url
  193. break
  194. await templates_server.update_contract_items(row["id"], items)
  195. return {
  196. "status": 1,
  197. "contract_download_url": contract_download_url,
  198. "sign_flow_id": sign_flow_id
  199. }
  200. return ErrorResponse(success=False, message="未知合同状态", raw={"status": status})
  201. @router.get("/get_all_templates", response_model=PaginatedResponse)
  202. async def get_all_templates(
  203. page: int = Query(1, ge=1, description="页码,从1开始"),
  204. page_size: int = Query(10, ge=1, le=100, description="每页条数,默认10"),
  205. templates_server: ContractServer = Depends(get_contract_service)
  206. ) -> PaginatedResponse:
  207. """分页查询所有合同"""
  208. rows, total = await templates_server.list_all_paged(page, page_size)
  209. total_pages = (total + page_size - 1) // page_size if total > 0 else 0
  210. items = [ContractStoreResponse(**row) for row in rows]
  211. return PaginatedResponse(
  212. items=items,
  213. total=total,
  214. page=page,
  215. page_size=page_size,
  216. total_pages=total_pages
  217. )
  218. @router.post("/esign/signurl", response_model=Union[SignUrlResponse, ErrorResponse])
  219. async def get_esign_sign_url(
  220. body: SignUrl,
  221. templates_server: ContractServer = Depends(get_contract_service)
  222. ) -> Union[SignUrlResponse, ErrorResponse]:
  223. """
  224. 当商家点击签署按钮时
  225. 携带合同相关的签署id和联系方式向e签宝发起请求
  226. 获取到签署的页面链接
  227. 并将签署url存入该合同对应的sign_url中
  228. """
  229. sing_flow_id = body.sign_flow_id
  230. contact_phone = body.contact_phone
  231. logger.info(f"esign/signurl request contact_phone={contact_phone}, sign_flow_id={sing_flow_id}")
  232. result = sign_url(sing_flow_id, contact_phone)
  233. try:
  234. result_json = json.loads(result)
  235. except json.JSONDecodeError:
  236. logger.error(f"sign_url non-json resp: {result}")
  237. return ErrorResponse(success=False, message="e签宝返回非JSON", raw=result)
  238. data = result_json.get("data") if isinstance(result_json, dict) else None
  239. if not data or not data.get("url"):
  240. logger.error(f"sign_url missing url: {result_json}")
  241. return ErrorResponse(success=False, message="e签宝返回缺少签署链接", raw=result_json)
  242. result_sign_url = data.get("url")
  243. await templates_server.update_sign_url(contact_phone, sing_flow_id, result_sign_url)
  244. logger.info(f"sign_url success contact_phone={contact_phone}, sign_flow_id={sing_flow_id}")
  245. return SignUrlResponse(success=True, data={"url": result_sign_url})
  246. @router.post("/esign/callback", response_model=Union[SuccessResponse, ErrorResponse])
  247. async def esign_callback(
  248. payload: dict,
  249. templates_server: ContractServer = Depends(get_contract_service)
  250. ) -> Union[SuccessResponse, ErrorResponse]:
  251. """
  252. e签宝签署结果回调
  253. 需求:签署完成 -> 更新 signing_status=已签署,contract_url 中 status=1
  254. """
  255. sign_result = payload.get("signResult")
  256. operator = payload.get("operator") or {}
  257. sign_flow_id = payload.get("signFlowId")
  258. psn_account = operator.get("psnAccount") or {}
  259. contact_phone = psn_account.get("accountMobile")
  260. # 取回调中的毫秒时间戳,优先 operateTime,其次 timestamp
  261. ts_ms = payload.get("operateTime") or payload.get("timestamp")
  262. signing_dt = None
  263. if ts_ms:
  264. try:
  265. signing_dt = datetime.fromtimestamp(ts_ms / 1000)
  266. except Exception:
  267. signing_dt = None
  268. if sign_result == 2:
  269. # 获取合同下载链接
  270. contract_download_url = None
  271. try:
  272. download_resp = file_download_url(sign_flow_id)
  273. download_json = json.loads(download_resp)
  274. contract_download_url = download_json["data"]["files"][0]["downloadUrl"]
  275. except Exception as e:
  276. logger.error(f"file_download_url failed for sign_flow_id={sign_flow_id}: {e}")
  277. print(contract_download_url,666666666666666666666666666666)
  278. updated = await templates_server.mark_signed_by_phone(contact_phone, sign_flow_id, signing_dt, contract_download_url)
  279. logger.info(f"esign_callback success phone={contact_phone}, sign_flow_id={sign_flow_id}, updated={updated}")
  280. return SuccessResponse(code="200", msg="success")
  281. logger.error(f"esign_callback ignored payload: {payload}")
  282. return ErrorResponse(success=False, message="未处理: signResult!=2 或手机号/签署流程缺失")
  283. # @router.post("/esign/callback_auth", response_model=SuccessResponse)
  284. # async def esign_callback_auth(
  285. # payload: dict,
  286. # templates_server: ContractServer = Depends(get_contract_service)
  287. # ) -> SuccessResponse:
  288. # logger.info(f"esign_callback_auth payload: {payload}")
  289. # return SuccessResponse(code="200", msg="success")