:art: Support tencentcloud.ess

huangqimin001 9 months ago
parent
commit
7af701951b
3 changed files with 95 additions and 44 deletions
  1. 52 42
      api/lensman/contract_mp_views.py
  2. 16 2
      kodo/settings.py
  3. 27 0
      utils/tencentcloud/ess.py

+ 52 - 42
api/lensman/contract_mp_views.py

@@ -5,10 +5,12 @@ from django_logit import logit
5 5
 from django_response import response
6 6
 from TimeConvert import TimeConvert as tc
7 7
 
8
+from account.models import LensmanInfo
8 9
 from apps.contract.models import LensmanContributionContractInfo
9 10
 from apps.lensman.activity.models import LensmanContributionActivityIncomeExpensesInfo
10
-from account.models import LensmanInfo
11 11
 from member.models import MemberActivityContributionInfo
12
+from utils.tencentcloud.ess import (create_document, create_flow, create_scheme_url, start_flow,
13
+                                    test_upload_document_files, upload_document_files)
12 14
 
13 15
 
14 16
 @logit(res=True)
@@ -25,7 +27,7 @@ def get_contribtion_contract_api(request):
25 27
 
26 28
     file_ids = upload_contribution_images(contribtuon_id)
27 29
 
28
-    operator =  {
30
+    operator = {
29 31
         "UserId": settings.CONTRACT_LENSMAN_CONTRIBUTION_OPERATOR_ID
30 32
     }
31 33
 
@@ -38,8 +40,8 @@ def get_contribtion_contract_api(request):
38 40
     contract.document_id = document_id
39 41
     contract.save()
40 42
 
41
-    # TODO : 发起签署流程
42
-    start_contribution_contract_flow(flow_id, operator)
43
+    # 发起签署流程
44
+    flow_status = start_contribution_contract_flow(flow_id, operator)
43 45
 
44 46
     scheme_url = get_contribtion_contract_sign_mppath(operator, lensman, flow_id)
45 47
 
@@ -50,35 +52,48 @@ def get_contribtion_contract_api(request):
50 52
 
51 53
 
52 54
 def upload_contribution_images(contribtuon_id):
53
-    # TODO : 上传MemberActivityContributionInfo图片 https://qian.tencent.com/developers/companyApis/templatesAndFiles/UploadFiles
55
+    # 上传MemberActivityContributionInfo图片 https://qian.tencent.com/developers/companyApis/templatesAndFiles/UploadFiles
54 56
 
55 57
     contribtuon = MemberActivityContributionInfo.objects.get(contribtuon_id=contribtuon_id)
56 58
 
57
-    # 返回图片的 file_ids
58
-    return []
59
+    # TODO: 从 MemberActivityContributionInfo 生成 files 对象
60
+    files = [
61
+        {
62
+            "FileBody": "文件base64编码,不含逗号前字符,即data:image/png;base64,",
63
+            "FileName": "test.png"
64
+        }
65
+    ]
66
+    file_type = 'png'
67
+    # upload_files_result = upload_document_files(files, file_type=file_type)
68
+    upload_files_result = test_upload_document_files(files, file_type=file_type)
69
+    file_ids = upload_files_result.get('FileIds', [])
70
+
71
+    return file_ids
59 72
 
60 73
 
61 74
 def create_contribution_contract_flow(lensman, Operator):
62
-    # TODO : 创建签署流程 https://qian.tencent.com/developers/companyApis/startFlows/CreateFlow
75
+    # 创建签署流程 https://qian.tencent.com/developers/companyApis/startFlows/CreateFlow
63 76
 
64 77
     # 创建签署流程参数 Operator
65 78
     FlowName = lensman.identity_card_name + "的投稿合同" + tc.local_string(format='%Y%m%d')
66 79
     FlowType = '活动投稿授权书'
67 80
     Approvers = [{
68
-            "ApproverType": "1",
69
-            "Required": "true",
70
-            "NotifyType": "none",
71
-            "ApproverMobile": lensman.phone,
72
-            "ApproverName": lensman.identity_card_name,
73
-            "ApproverIdCardNumber": lensman.identity_card_number,
74
-        }]
81
+        "ApproverType": 1,
82
+        "Required": True,
83
+        "NotifyType": None,
84
+        "ApproverMobile": lensman.phone,
85
+        "ApproverName": lensman.identity_card_name,
86
+        "ApproverIdCardType": "ID_CARD",
87
+        "ApproverIdCardNumber": lensman.identity_card_number,
88
+    }]
89
+    create_flow_result = create_flow(flow_name=FlowName, flow_type=FlowType, approvers=Approvers)
90
+    flow_id = create_flow_result.get('FlowId')
75 91
 
76
-    # 创建签署流程返回值 FlowId
77
-    return ''
92
+    return flow_id
78 93
 
79 94
 
80 95
 def create_contribution_contract_document(lensman, contribtuon_id, file_ids, FlowId, Operator):
81
-    # TODO : 创建电子签文档 https://qian.tencent.com/developers/companyApis/startFlows/CreateDocument
96
+    # 创建电子签文档 https://qian.tencent.com/developers/companyApis/startFlows/CreateDocument
82 97
 
83 98
     income = LensmanContributionActivityIncomeExpensesInfo.objects.get(contribtuon_id=contribtuon_id, lensman_id=lensman.lensman_id)
84 99
 
@@ -86,51 +101,46 @@ def create_contribution_contract_document(lensman, contribtuon_id, file_ids, Flo
86 101
     TemplateId = settings.CONTRACT_LENSMAN_CONTRIBUTION_TEMPLATE_ID
87 102
 
88 103
     FormFields = [{
89
-        "ComponentName": "ComponentId_0",
104
+        "ComponentId": "ComponentId_0",
90 105
         "ComponentValue": lensman.identity_card_name
91 106
     }, {
92
-        "ComponentName": "ComponentId_1",
107
+        "ComponentId": "ComponentId_1",
93 108
         "ComponentValue": lensman.identity_card_number,
94 109
     }, {
95
-        "ComponentName": "ComponentId_2",
96
-        "ComponentValue": income.amount,
110
+        "ComponentId": "ComponentId_2",
111
+        "ComponentValue": str(income.amount),
97 112
     }, {
98
-        "ComponentName": "ComponentId_3",
113
+        "ComponentId": "ComponentId_3",
99 114
         "ComponentValue": "",
100 115
     }]
101 116
 
102 117
     for i, file_id in enumerate(file_ids):
103 118
         FormFields.append({
104
-            "ComponentName": "ComponentId_" + str(37 +  i),
119
+            "ComponentId": "ComponentId_" + str(37 + i),
105 120
             "ComponentValue": file_id,
106 121
         })
122
+    create_document_result = create_document(flow_id=FlowId, form_fields=FormFields)
123
+    document_id = create_document_result.get('DocumentId')
107 124
 
108
-    # 返回创建电子文档 DocumentId
109
-    return '', FormFields
125
+    return document_id, FormFields
110 126
 
111 127
 
112 128
 def start_contribution_contract_flow(FlowId, Operator):
113
-    # TODO : 发起签署流程 https://qian.tencent.com/developers/companyApis/startFlows/StartFlow
129
+    # 发起签署流程 https://qian.tencent.com/developers/companyApis/startFlows/StartFlow
114 130
 
115
-    # 发起签署流程参数 Operator FlowId
131
+    start_flow_result = start_flow(flow_id=FlowId)
132
+    flow_status = start_flow_result.get('Status')
116 133
 
117
-    #
118
-    return ''
134
+    return flow_status
119 135
 
120 136
 
121 137
 def get_contribtion_contract_sign_mppath(Operator, lensman, FlowId):
122
-    # TODO : 获取签署链接 https://qian.tencent.com/developers/companyApis/startFlows/CreateSchemeUrl
123
-
124
-    Name = lensman.identity_card_name
125
-    Mobile = lensman.phone
126
-    IdCardType = 'ID_CARD'
127
-    IdCardNumber = lensman.identity_card_number
128
-    EndPoint = 'APP'
129
-    PathType = '1' #腾讯电子签小程序流程合同的详情页
130
-    AutoJumpBack = 'true' #签署完成会自动跳转回来
131
-
132
-    # 返回 SchemeUrl
133
-    return ''
138
+    # 获取签署链接 https://qian.tencent.com/developers/companyApis/startFlows/CreateSchemeUrl
139
+
140
+    create_scheme_url_result = create_scheme_url(flow_id=FlowId, name=lensman.identity_card_name, mobile=lensman.phone, card_type='ID_CARD', card_number=lensman.identity_card_number)
141
+    scheme_url = create_scheme_url_result.get('SchemeUrl')
142
+
143
+    return scheme_url
134 144
 
135 145
 
136 146
 @logit(res=True)

+ 16 - 2
kodo/settings.py

@@ -237,12 +237,22 @@ MEMBER_CARD_ID_HANYUAN = ''
237 237
 
238 238
 # 腾讯云
239 239
 TENCENTCLOUD = {
240
-    'default': {
240
+    'faceid': {
241 241
         'appid': '',
242 242
         'secret_id': '',
243 243
         'secret_key': '',
244 244
         'merchant_id': '',
245 245
     },
246
+    'ess': {
247
+        'operator_id': '',
248
+        'template_id': '',
249
+        'secret_id': '',
250
+        'secret_key': '',
251
+        'callback_secret_id': '',
252
+        'callback_secret_key': '',
253
+        'endpoint': 'ess.tencentcloudapi.com',
254
+        'file_endpoint': 'file.ess.tencent.cn',
255
+    },
246 256
 }
247 257
 
248 258
 # 七牛设置
@@ -592,5 +602,9 @@ DEFAULT_START_DATE = '1970-01-02'
592 602
 DEFAULT_END_DATE = '9999-12-03'
593 603
 
594 604
 # 腾讯电子签配置
605
+CONTRACT_LENSMAN_CONTRIBUTION_OPERATOR_ID = 'yDCp3UU055m70lUx6jaTk1RVkiVkKenJ'
595 606
 CONTRACT_LENSMAN_CONTRIBUTION_TEMPLATE_ID = 'yDCp3UU05rgksnUXtZsEvdrK1tRGb7ax'
596
-CONTRACT_LENSMAN_CONTRIBUTION_OPERATOR_ID = 'yDCp3UU055m70lUx6jaTk1RVkiVkKenJ'
607
+CONTRACT_LENSMAN_CONTRIBUTION_SECRET_ID = 'AKIDAHh3WzlYvHqOoR69MI94329FIGEDA62x'
608
+CONTRACT_LENSMAN_CONTRIBUTION_SECRET_KEY = 'tieio5Rn7PqARmJzcAcAh4gNE3rowEc3'
609
+CONTRACT_LENSMAN_CONTRIBUTION_CALLBACK_SECRET_ID = 'F0C875BC8B9442AE8E4769155F812EC0'
610
+CONTRACT_LENSMAN_CONTRIBUTION_CALLBACK_SECRET_KEY = '645CD7B0067B4FCEB6A9A6D589EC9DD6'

+ 27 - 0
utils/tencentcloud/ess.py

@@ -0,0 +1,291 @@
1
+# -*- coding: utf-8 -*-
2
+
3
+import json
4
+
5
+from django.conf import settings
6
+from tencentcloud.common import credential
7
+from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
8
+from tencentcloud.common.profile.client_profile import ClientProfile
9
+from tencentcloud.common.profile.http_profile import HttpProfile
10
+from tencentcloud.ess.v20201111 import ess_client, models
11
+from TimeConvert import TimeConvert as tc
12
+
13
+
14
+tencentcloud_cfg = settings.TENCENTCLOUD.get('ess', {})
15
+operator_id = tencentcloud_cfg.get('operator_id')
16
+template_id = tencentcloud_cfg.get('template_id')
17
+secret_id = tencentcloud_cfg.get('secret_id')
18
+secret_key = tencentcloud_cfg.get('secret_key')
19
+callback_secret_id = tencentcloud_cfg.get('callback_secret_id')
20
+callback_secret_key = tencentcloud_cfg.get('callback_secret_key')
21
+endpoint = tencentcloud_cfg.get('endpoint')
22
+file_endpoint = tencentcloud_cfg.get('file_endpoint')
23
+
24
+
25
+def test_upload_document_files(files, file_type='png'):
26
+    files = [
27
+        {