main.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import requests
  2. import json
  3. from common.esigntool.esign_config import Config
  4. from common.esigntool.esign_algorithm import buildSignJsonHeader
  5. import time
  6. from datetime import datetime
  7. config = Config()
  8. def get_auth_flow_id():
  9. """获取机构认证&授权页面链接"""
  10. api_path = "/v3/org-auth-url"
  11. method = "POST"
  12. body = {
  13. "clientType": "ALL",
  14. "redirectConfig": {
  15. "redirectUrl": "https://www.baidu.com"
  16. },
  17. "orgAuthConfig": {
  18. "orgName": "爱丽恩严(大连)商务科技有限公司深圳分公司",
  19. "orgInfo": {
  20. "orgIDCardNum": "91440300MADDW7XC4C",
  21. "orgIDCardType": "CRED_ORG_USCC",
  22. "legalRepName": "彭少荣",
  23. "legalRepIDCardNum": "362204198807182420",
  24. "legalRepIDCardType": "CRED_PSN_CH_IDCARD"
  25. },
  26. "transactorInfo": {
  27. "psnAccount": "17337039317",
  28. "psnInfo": {
  29. "psnName": "孟骞康",
  30. "psnIDCardNum": "411426200308121212",
  31. "psnIDCardType": "CRED_PSN_CH_IDCARD",
  32. "psnMobile": "17337039317"
  33. }
  34. }
  35. },
  36. # "authorizeConfig": {
  37. # "authorizedScopes": [
  38. # "get_org_identity_info",
  39. # "get_psn_identity_info",
  40. # "org_initiate_sign",
  41. # "manage_org_resource"
  42. # ]
  43. # },
  44. "notifyUrl": "http://120.26.186.130:33333",
  45. "transactorUseSeal": True
  46. }
  47. json_headers = buildSignJsonHeader(config.appId, config.scert, method, api_path, body=body)
  48. body_json = json.dumps(body, separators=(",", ":"), ensure_ascii=False)
  49. resp = requests.request(method, config.host + api_path, data=body_json, headers=json_headers)
  50. print(resp.text)
  51. return resp.text
  52. get_auth_flow_id()
  53. def get_template_detail():
  54. """查询合同模板中控件详情"""
  55. api_path = f"/v3/doc-templates/{config.templates_id}"
  56. method = "GET"
  57. json_headers = buildSignJsonHeader(config.appId, config.scert, method, api_path)
  58. resp = requests.request(method, config.host + api_path, headers=json_headers)
  59. print(resp.text)
  60. def fill_in_template(name):
  61. """填写模板生成文件"""
  62. api_path = "/v3/files/create-by-doc-template"
  63. method = "POST"
  64. body = {
  65. "docTemplateId": config.templates_id,
  66. "fileName": "U店在这-商户入驻协议",
  67. "components": [
  68. {
  69. "componentKey": "alien_name",
  70. "componentValue": "爱丽恩严(大连)商务科技有限公司"
  71. },
  72. {
  73. "componentKey": "store_name",
  74. "componentValue": name
  75. },
  76. {
  77. "componentKey": "date",
  78. "componentValue": datetime.now().strftime("%Y年%m月%d日")
  79. }
  80. ]
  81. }
  82. json_headers = buildSignJsonHeader(config.appId, config.scert, method, api_path, body=body)
  83. body_json = json.dumps(body, separators=(",", ":"), ensure_ascii=False)
  84. resp = requests.request(method, config.host + api_path, data=body_json, headers=json_headers)
  85. print(resp.text)
  86. return resp.text
  87. def create_by_file(file_id, file_name, contact_phone, merchant_name):
  88. """基于文件发起签署"""
  89. api_path = "/v3/sign-flow/create-by-file"
  90. method = "POST"
  91. body = {
  92. "docs": [
  93. {
  94. "fileId": file_id,
  95. "fileName": f"{file_name}.pdf"
  96. }
  97. ],
  98. "signFlowConfig": {
  99. "signFlowTitle": "商家入驻U店的签署协议", # 请设置当前签署任务的主题
  100. "autoFinish": True,
  101. "noticeConfig": {
  102. "noticeTypes": "" #
  103. # """通知类型,通知发起方、签署方、抄送方,默认不通知(值为""空字符串),允许多种通知方式,请使用英文逗号分隔
  104. #
  105. # "" - 不通知(默认值)
  106. #
  107. # 1 - 短信通知(如果套餐内带“分项”字样,请确保开通【电子签名流量费(分项)认证】中的子项:【短信服务】,否则短信通知收不到)
  108. #
  109. # 2 - 邮件通知
  110. #
  111. # 3 - 钉钉工作通知(需使用e签宝钉签产品)
  112. #
  113. # 5 - 微信通知(用户需关注“e签宝电子签名”微信公众号且使用过e签宝微信小程序)
  114. #
  115. # 6 - 企业微信通知(需要使用e签宝企微版产品)
  116. #
  117. # 7 - 飞书通知(需要使用e签宝飞书版产品)
  118. #
  119. # 补充说明:
  120. #
  121. # 1、2:个人账号中需要绑定短信/邮件才有对应的通知方式;
  122. # 3、5、6、7:仅限e签宝正式环境调用才会有。"""
  123. },
  124. "notifyUrl": "http://120.26.186.130:33333", # 接收相关回调通知的Web地址,
  125. "redirectConfig": {
  126. "redirectUrl": "https://www.esign.cn/"
  127. }
  128. },
  129. "signers": [
  130. {
  131. "signConfig": {
  132. "signOrder": 1
  133. },
  134. "signerType": 1,
  135. "signFields": [
  136. {
  137. "customBizNum": "9527", # 开发者自定义业务编号
  138. "fileId": file_id, #签署区所在待签署文件ID 【注】这里的fileId需先添加在docs数组中,否则会报错“参数错误: 文件id不在签署流程中”。
  139. "normalSignFieldConfig": {
  140. "autoSign": True,
  141. "signFieldStyle": 1,
  142. "signFieldPosition": {
  143. "positionPage": "7",
  144. "positionX": 114, # 获取需要盖章的位置: https://open.esign.cn/tools/seal-position
  145. "positionY": 666
  146. }
  147. }
  148. }
  149. ]
  150. },
  151. {
  152. "psnSignerInfo": {
  153. "psnAccount": contact_phone,
  154. "psnInfo": {
  155. "psnName": merchant_name
  156. }
  157. },
  158. "signConfig": {
  159. "forcedReadingTime": 10,
  160. "signOrder": 2
  161. },
  162. "signerType": 0,
  163. "signFields": [
  164. {
  165. "customBizNum": "9527",
  166. "fileId": file_id,
  167. "normalSignFieldConfig": {
  168. "signFieldStyle": 1,
  169. "signFieldPosition": {
  170. "positionPage": "7",
  171. "positionX": 294,
  172. "positionY": 668
  173. }
  174. }
  175. }
  176. ]
  177. }
  178. ]
  179. }
  180. json_headers = buildSignJsonHeader(config.appId, config.scert, method, api_path, body=body)
  181. body_json = json.dumps(body, separators=(",", ":"), ensure_ascii=False)
  182. resp = requests.request(method, config.host + api_path, data=body_json, headers=json_headers)
  183. print(resp.text)
  184. return resp.text
  185. def sign_url(sign_flow_id, contact_phone):
  186. api_path = f"/v3/sign-flow/{sign_flow_id}/sign-url"
  187. method = "POST"
  188. body = {
  189. "signFlowId": sign_flow_id,
  190. "clientType": "ALL",
  191. "needLogin": False,
  192. "operator": {
  193. "psnAccount": contact_phone
  194. },
  195. "urlType": 2
  196. }
  197. json_headers = buildSignJsonHeader(config.appId, config.scert, method, api_path, body=body)
  198. body_json = json.dumps(body, separators=(",", ":"), ensure_ascii=False)
  199. resp = requests.request(method, config.host + api_path, data=body_json, headers=json_headers)
  200. print(resp.text)
  201. return resp.text
  202. def file_download_url(sign_flow_id):
  203. """下载已签署文件及附属材料"""
  204. api_path = f"/v3/sign-flow/{sign_flow_id}/file-download-url"
  205. method = "POST"
  206. body = {
  207. "urlAvailableDate": "3600"
  208. }
  209. json_headers = buildSignJsonHeader(config.appId, config.scert, method, api_path, body=body)
  210. body_json = json.dumps(body, separators=(",", ":"), ensure_ascii=False)
  211. resp = requests.request(method, config.host + api_path, data=body_json, headers=json_headers)
  212. print(resp.text)
  213. return resp.text
  214. # fill_in_template("我勒个去")
  215. # sing_data = create_by_file("41bd938c47394e6b9bf4a491949c161e", "U店在这-商户入驻协议", "13503301290", "孟骞康")
  216. # sign_json = json.loads(sing_data)
  217. # sing_id = sign_json["data"]["signFlowId"]
  218. # sign_url("", "13503301290")
  219. # file_download_url("56245b135f5546f39329cb2aea47a7d0")