Няма описание

contract_mp_views.py 9.3KB

    # -*- coding: utf-8 -*- import base64 import json import requests from django_logit import logit from django_response import response from TimeConvert import TimeConvert as tc from account.models import LensmanInfo from apps.contract.models import LensmanContributionContractInfo from apps.lensman.activity.models import LensmanContributionActivityIncomeExpensesInfo from member.models import MemberActivityContributionInfo from utils import cn2an from utils.error.errno_utils import ContractStatusCode from utils.redis.rimage import get_images_data from utils.tencentcloud.ess import (callback_decode, create_document, create_flow, create_scheme_url, start_flow, test_upload_document_files, upload_document_files) @logit(res=True) def get_contribtion_contract_status_api(request): user_id = request.POST.get('user_id', '') activity_id = request.POST.get('activity_id', '') contribution_id = request.POST.get('contribution_id', '') try: contract = LensmanContributionContractInfo.objects.get(user_id=user_id, activity_id=activity_id, contribution_id=contribution_id) except LensmanContributionContractInfo.DoesNotExist: return response(ContractStatusCode.CONTRACT_NOT_FOUND) return response(200, data={ 'contract_status': contract.contract_status, }) @logit(res=True) def get_contribtion_contract_api(request): user_id = request.POST.get('user_id', '') lensman_id = request.POST.get('lensman_id', '') activity_id = request.POST.get('activity_id', '') contribution_id = request.POST.get('contribution_id', '') lensman = LensmanInfo.objects.get(lensman_id=lensman_id) contract, _ = LensmanContributionContractInfo.objects.update_or_create( user_id=user_id, lensman_id=lensman_id, activity_id=activity_id, contribution_id=contribution_id, ) file_ids = upload_contribution_images(contribution_id) flow_id = create_contribution_contract_flow(lensman) contract.flow_id = flow_id contract.save() document_id, fields = create_contribution_contract_document(lensman, contribution_id, file_ids, flow_id) contract.contract_content_fields = fields contract.document_id = document_id contract.save() # 发起签署流程 flow_status = start_contribution_contract_flow(flow_id) scheme_url = get_contribtion_contract_sign_mppath(lensman, flow_id) return response(200, data={ 'scheme_url': scheme_url }) def get_contribtion_contract_sign_mppath_api(request): user_id = request.POST.get('user_id', '') flow_id = request.POST.get('flow_id', '') lensman_id = request.POST.get('lensman_id', '') contribution_id = request.POST.get('contribution_id', '') lensman = LensmanInfo.objects.get(lensman_id=lensman_id) try: contract = LensmanContributionContractInfo.objects.get( user_id=user_id, contribution_id=contribution_id, lensman_id=lensman_id, ) except LensmanContributionContractInfo.DoesNotExist: return response(ContractStatusCode.CONTRACT_NOT_FOUND) scheme_url = get_contribtion_contract_sign_mppath(lensman, contract.flow_id) return response(200, data={ 'contract': contract.mpdata, 'scheme_url': scheme_url }) def generate_file_from_qiniu(file_url): try: data = requests.get(file_url).content data = base64.b64encode(data).decode('utf-8') except Exception: data = None return data def generate_files_from_qiniu(file_urls): files = [] for file_url in file_urls: file_b64str = generate_file_from_qiniu(file_url) if not file_b64str: continue files.append({ 'FileBody': file_b64str, 'FileName': file_url.split('/')[-1] }) return files def upload_contribution_images(contribution_id): # 上传MemberActivityContributionInfo图片 https://qian.tencent.com/developers/companyApis/templatesAndFiles/UploadFiles contribtuon = MemberActivityContributionInfo.objects.get(contribution_id=contribution_id) file_urls = [image['image_url'] for image in contribtuon.images] file_names = [file_url.split('/')[-1] for file_url in file_urls] file_type = file_names[0].split('.')[-1] files = get_images_data(file_names) # Redis 已无缓存的数据 if len(files) != len(file_names): files = generate_files_from_qiniu(file_urls) # files = [ # { # "FileBody": "文件base64编码,不含逗号前字符,即data:image/png;base64,", # "FileName": "test.png" # } # ] # file_type = 'png' return upload_document_files(files, file_type=file_type) # upload_files_result = test_upload_document_files(files, file_type=file_type) def create_contribution_contract_flow(lensman): # 创建签署流程 https://qian.tencent.com/developers/companyApis/startFlows/CreateFlow # 创建签署流程参数 Operator FlowName = lensman.identity_card_name + u"的投稿合同" + tc.local_string(format='%Y%m%d') FlowType = u"活动投稿授权书" Approvers = [{ "ApproverType": 1, "Required": True, "NotifyType": 'SMS', "ApproverMobile": lensman.phone, "ApproverName": lensman.identity_card_name, "ApproverIdCardType": "ID_CARD", "ApproverIdCardNumber": lensman.identity_card_number, }] create_flow_result = create_flow(flow_name=FlowName, flow_type=FlowType, approvers=Approvers) if not create_flow_result: return '' return create_flow_result.FlowId def create_contribution_contract_document(lensman, contribution_id, file_ids, FlowId): # 创建电子签文档 https://qian.tencent.com/developers/companyApis/startFlows/CreateDocument IMAGE_COMPONENTIDS = ['ComponentId_37', 'ComponentId_38','ComponentId_39','ComponentId_40','ComponentId_41','ComponentId_42', 'ComponentId_43','ComponentId_44','ComponentId_45','ComponentId_46','ComponentId_47','ComponentId_48','ComponentId_49','ComponentId_50','ComponentId_51','ComponentId_52','ComponentId_53','ComponentId_54','ComponentId_55', 'ComponentId_55', 'ComponentId_56', 'ComponentId_57', 'ComponentId_58', 'ComponentId_59', 'ComponentId_60', 'ComponentId_9', 'ComponentId_10', 'ComponentId_11', 'ComponentId_12', 'ComponentId_13', 'ComponentId_14', 'ComponentId_15', 'ComponentId_16', 'ComponentId_17', 'ComponentId_18', 'ComponentId_19', 'ComponentId_20', 'ComponentId_21', 'ComponentId_22', 'ComponentId_23', 'ComponentId_24', 'ComponentId_25', 'ComponentId_26', 'ComponentId_27', 'ComponentId_28','ComponentId_29', 'ComponentId_30', 'ComponentId_31', 'ComponentId_32'] try: amount = LensmanContributionActivityIncomeExpensesInfo.objects.get(contribution_id=contribution_id, lensman_id=lensman.lensman_id).amount except LensmanContributionActivityIncomeExpensesInfo.DoesNotExist: amount = 0 FormFields = [{ "ComponentId": "ComponentId_0", "ComponentValue": lensman.identity_card_name }, { "ComponentId": "ComponentId_1", "ComponentValue": lensman.identity_card_number, }, { "ComponentId": "ComponentId_2", "ComponentValue": str(amount), }, { "ComponentId": "ComponentId_3", "ComponentValue": cn2an.an2cn(amount, mode='rmb'), }] for idx, file_id in enumerate(file_ids): FormFields.append({ "ComponentId": IMAGE_COMPONENTIDS[idx], "ComponentValue": file_id, }) create_document_result = create_document(flow_id=FlowId, form_fields=FormFields) if not create_document_result: return '', FormFields return create_document_result.DocumentId, FormFields def start_contribution_contract_flow(FlowId): # 发起签署流程 https://qian.tencent.com/developers/companyApis/startFlows/StartFlow start_flow_result = start_flow(flow_id=FlowId) if not start_flow_result: return '' return start_flow_result.Status def get_contribtion_contract_sign_mppath(lensman, FlowId): # 获取签署链接 https://qian.tencent.com/developers/companyApis/startFlows/CreateSchemeUrl 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) if not create_scheme_url_result: return '' return create_scheme_url_result.SchemeUrl @logit(body=True, res=True) def ess_callback(request): # curl http://127.0.0.1:8888/api/mp/ess/callback -H 'Content-type: application/json' -X POST -d '{"encrypt":"62KE4r5Wz0yHzEpMOwVRbM1KV0"}' data = json.loads(request.body) data = callback_decode(data['encrypt']) # https://qian.tencent.com/developers/company/callback_types_contracts_sign MsgType = data.get('MsgType') if MsgType == 'FlowStatusChange': MsgData = data.get('MsgData', {}) FlowId = MsgData.get('FlowId') # DocumentId = MsgData.get('DocumentId') FlowCallbackStatus = MsgData.get('FlowCallbackStatus', -1) Approvers = MsgData.get('Approvers') or [{}] ApproveCallbackStatus = Approvers[-1].get('ApproveCallbackStatus', -1) LensmanContributionContractInfo.objects.filter(flow_id=FlowId).update(tencent_contract_status=FlowCallbackStatus, tencent_approve_status=ApproveCallbackStatus) return response()