main.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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(org_name, org_id, legal_rep_name, legal_rep_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. "orgName": org_name,
  20. "orgInfo": {
  21. # "orgIDCardNum": "91440300MADDW7XC4C",
  22. "orgIDCardNum": org_id,
  23. "orgIDCardType": "CRED_ORG_USCC",
  24. # "legalRepName": "彭少荣",
  25. "legalRepName": legal_rep_name,
  26. # "legalRepIDCardNum": "362204198807182420",
  27. "legalRepIDCardNum": legal_rep_id,
  28. "legalRepIDCardType": "CRED_PSN_CH_IDCARD"
  29. },
  30. "transactorInfo": {
  31. "psnAccount": "17337039317",
  32. "psnInfo": {
  33. "psnName": "孟骞康",
  34. "psnIDCardNum": "411426200308121212",
  35. "psnIDCardType": "CRED_PSN_CH_IDCARD",
  36. "psnMobile": "17337039317"
  37. }
  38. }
  39. },
  40. # "authorizeConfig": {
  41. # "authorizedScopes": [
  42. # "get_org_identity_info",
  43. # "get_psn_identity_info",
  44. # "org_initiate_sign",
  45. # "manage_org_resource"
  46. # ]
  47. # },
  48. "notifyUrl": "http://120.26.186.130:33333/api/store/esign/callback_auth",
  49. "transactorUseSeal": True
  50. }
  51. json_headers = buildSignJsonHeader(config.appId, config.scert, method, api_path, body=body)
  52. body_json = json.dumps(body, separators=(",", ":"), ensure_ascii=False)
  53. resp = requests.request(method, config.host + api_path, data=body_json, headers=json_headers)
  54. print(resp.text)
  55. return resp.text
  56. # get_auth_flow_id()
  57. def get_template_detail():
  58. """查询合同模板中控件详情"""
  59. api_path = f"/v3/doc-templates/{config.templates_id}"
  60. method = "GET"
  61. json_headers = buildSignJsonHeader(config.appId, config.scert, method, api_path)
  62. resp = requests.request(method, config.host + api_path, headers=json_headers)
  63. print(resp.text)
  64. def fill_in_template(name):
  65. """填写模板生成文件"""
  66. api_path = "/v3/files/create-by-doc-template"
  67. method = "POST"
  68. body = {
  69. "docTemplateId": config.templates_id,
  70. "fileName": "U店在这-商户入驻协议",
  71. "components": [
  72. {
  73. "componentKey": "alien_name",
  74. "componentValue": "爱丽恩严(大连)商务科技有限公司"
  75. },
  76. {
  77. "componentKey": "store_name",
  78. "componentValue": name
  79. },
  80. {
  81. "componentKey": "date",
  82. "componentValue": datetime.now().strftime("%Y年%m月%d日")
  83. },
  84. {
  85. "componentKey": "one_name",
  86. "componentValue": name
  87. },
  88. ]
  89. }
  90. json_headers = buildSignJsonHeader(config.appId, config.scert, method, api_path, body=body)
  91. body_json = json.dumps(body, separators=(",", ":"), ensure_ascii=False)
  92. resp = requests.request(method, config.host + api_path, data=body_json, headers=json_headers)
  93. print(resp.text)
  94. return resp.text
  95. def get_contract_detail(file_id):
  96. """查询PDF模板填写后文件"""
  97. api_path = f"/v3/files/{file_id}"
  98. method = "GET"
  99. json_headers = buildSignJsonHeader(config.appId, config.scert, method, api_path)
  100. resp = requests.request(method, config.host + api_path, headers=json_headers)
  101. print(resp.text)
  102. return resp.text
  103. # get_contract_detail("f0371b4ae7c64c8ca16be3bf031d1d6e")
  104. # def create_by_file(file_id, file_name, contact_phone, merchant_name):
  105. # """基于文件发起签署"""
  106. # api_path = "/v3/sign-flow/create-by-file"
  107. # method = "POST"
  108. # body = {
  109. # "docs": [
  110. # {
  111. # "fileId": file_id,
  112. # "fileName": f"{file_name}.pdf"
  113. # }
  114. # ],
  115. # "signFlowConfig": {
  116. # "signFlowTitle": "商家入驻U店的签署协议", # 请设置当前签署任务的主题
  117. # "autoFinish": True,
  118. # "noticeConfig": {
  119. # "noticeTypes": "" #
  120. # # """通知类型,通知发起方、签署方、抄送方,默认不通知(值为""空字符串),允许多种通知方式,请使用英文逗号分隔
  121. # #
  122. # # "" - 不通知(默认值)
  123. # #
  124. # # 1 - 短信通知(如果套餐内带“分项”字样,请确保开通【电子签名流量费(分项)认证】中的子项:【短信服务】,否则短信通知收不到)
  125. # #
  126. # # 2 - 邮件通知
  127. # #
  128. # # 3 - 钉钉工作通知(需使用e签宝钉签产品)
  129. # #
  130. # # 5 - 微信通知(用户需关注“e签宝电子签名”微信公众号且使用过e签宝微信小程序)
  131. # #
  132. # # 6 - 企业微信通知(需要使用e签宝企微版产品)
  133. # #
  134. # # 7 - 飞书通知(需要使用e签宝飞书版产品)
  135. # #
  136. # # 补充说明:
  137. # #
  138. # # 1、2:个人账号中需要绑定短信/邮件才有对应的通知方式;
  139. # # 3、5、6、7:仅限e签宝正式环境调用才会有。"""
  140. # },
  141. # "notifyUrl": "http://120.26.186.130:33333:/api/store/esign/callback", # 接收相关回调通知的Web地址,
  142. # "redirectConfig": {
  143. # "redirectUrl": "https://www.esign.cn/"
  144. # }
  145. # },
  146. # "signers": [
  147. # {
  148. # "signConfig": {
  149. # "signOrder": 1
  150. # },
  151. # "signerType": 1,
  152. # "signFields": [
  153. # {
  154. # "customBizNum": "9527", # 开发者自定义业务编号
  155. # "fileId": file_id, #签署区所在待签署文件ID 【注】这里的fileId需先添加在docs数组中,否则会报错“参数错误: 文件id不在签署流程中”。
  156. # "normalSignFieldConfig": {
  157. # "autoSign": True,
  158. # "signFieldStyle": 1,
  159. # "signFieldPosition": {
  160. # "positionPage": "7",
  161. # "positionX": 294, # 获取需要盖章的位置: https://open.esign.cn/tools/seal-position
  162. # "positionY": 668
  163. # }
  164. # }
  165. # }
  166. # ]
  167. # },
  168. # {
  169. # "psnSignerInfo": {
  170. # "psnAccount": contact_phone,
  171. # "psnInfo": {
  172. # "psnName": merchant_name
  173. # }
  174. # },
  175. # "signConfig": {
  176. # "forcedReadingTime": 10,
  177. # "signOrder": 2
  178. # },
  179. # "signerType": 0,
  180. # "signFields": [
  181. # {
  182. # "customBizNum": "9527",
  183. # "fileId": file_id,
  184. # "normalSignFieldConfig": {
  185. # "signFieldStyle": 1,
  186. # "signFieldPosition": {
  187. # "positionPage": "7",
  188. # "positionX": 114, # 获取需要盖章的位置: https://open.esign.cn/tools/seal-position
  189. # "positionY": 666
  190. # }
  191. # }
  192. # }
  193. # ]
  194. # }
  195. # ]
  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 create_by_file(file_id, file_name, contact_phone, store_name, merchant_name, ord_id):
  203. """基于文件发起签署"""
  204. api_path = "/v3/sign-flow/create-by-file"
  205. method = "POST"
  206. body = {
  207. "docs": [
  208. {
  209. "fileId": file_id,
  210. "fileName": f"{file_name}.pdf"
  211. }
  212. ],
  213. "signFlowConfig": {
  214. "signFlowTitle": "商家入驻U店平台协议签署",
  215. "autoFinish": True,
  216. "noticeConfig": {
  217. "noticeTypes": "1,2"
  218. },
  219. "notifyUrl": "http://120.26.186.130:33333:/api/store/esign/callback",
  220. "redirectConfig": {
  221. "redirectUrl": "https://www.esign.cn/"
  222. }
  223. },
  224. "signers": [
  225. {
  226. "signConfig": {
  227. "signOrder": 1
  228. },
  229. "signerType": 1,
  230. "signFields": [
  231. {
  232. "customBizNum": "9527",
  233. "fileId": file_id,
  234. "normalSignFieldConfig": {
  235. "autoSign": True,
  236. "signFieldStyle": 1,
  237. "signFieldPosition": {
  238. "positionPage": "7",
  239. "positionX": 294,
  240. "positionY": 668
  241. }
  242. }
  243. }
  244. ]
  245. },
  246. {
  247. "orgSignerInfo": {
  248. "orgName": store_name,
  249. "orgInfo": {
  250. "orgIDCardNum": ord_id, # "91440300MADDW7XC4C"
  251. "orgIDCardType": "CRED_ORG_USCC"
  252. },
  253. "transactorInfo": {
  254. "psnAccount": contact_phone,
  255. "psnInfo": {
  256. "psnName": merchant_name
  257. }
  258. }
  259. },
  260. "signConfig": {
  261. "forcedReadingTime": 10,
  262. "signOrder": 2
  263. },
  264. "signerType": 1,
  265. "signFields": [
  266. {
  267. "customBizNum": "自定义编码001",
  268. "fileId": file_id,
  269. "normalSignFieldConfig": {
  270. "signFieldStyle": 1,
  271. "signFieldPosition": {
  272. "positionPage": "7",
  273. "positionX": 114,
  274. "positionY": 666
  275. }
  276. }
  277. }
  278. ]
  279. }
  280. ]
  281. }
  282. json_headers = buildSignJsonHeader(config.appId, config.scert, method, api_path, body=body)
  283. body_json = json.dumps(body, separators=(",", ":"), ensure_ascii=False)
  284. resp = requests.request(method, config.host + api_path, data=body_json, headers=json_headers)
  285. print(resp.text)
  286. return resp.text
  287. def sign_url(sign_flow_id, contact_phone):
  288. """获取签署页面链接"""
  289. api_path = f"/v3/sign-flow/{sign_flow_id}/sign-url"
  290. method = "POST"
  291. body = {
  292. "signFlowId": sign_flow_id,
  293. "clientType": "ALL",
  294. "needLogin": False,
  295. "operator": {
  296. "psnAccount": contact_phone
  297. },
  298. "urlType": 2
  299. }
  300. json_headers = buildSignJsonHeader(config.appId, config.scert, method, api_path, body=body)
  301. body_json = json.dumps(body, separators=(",", ":"), ensure_ascii=False)
  302. resp = requests.request(method, config.host + api_path, data=body_json, headers=json_headers)
  303. print(resp.text)
  304. return resp.text
  305. def file_download_url(sign_flow_id):
  306. """下载已签署文件及附属材料"""
  307. api_path = f"/v3/sign-flow/{sign_flow_id}/file-download-url"
  308. method = "POST"
  309. body = {
  310. "urlAvailableDate": "3600"
  311. }
  312. json_headers = buildSignJsonHeader(config.appId, config.scert, method, api_path, body=body)
  313. body_json = json.dumps(body, separators=(",", ":"), ensure_ascii=False)
  314. resp = requests.request(method, config.host + api_path, data=body_json, headers=json_headers)
  315. print(resp.text)
  316. return resp.text
  317. # fill_in_template("我勒个去")
  318. # sing_data = create_by_file("41bd938c47394e6b9bf4a491949c161e", "U店在这-商户入驻协议", "13503301290", "孟骞康")
  319. # sign_json = json.loads(sing_data)
  320. # sing_id = sign_json["data"]["signFlowId"]
  321. # sign_url("", "13503301290")
  322. # file_download_url("15156afb603e4145b112ad6eab0815d5")