main.py 7.9 KB

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