import requests
import json
import time
from dingtalk.crypto import DingTalkCrypto
from django.conf import settings
# settings.BASE_DIR
class Crypto(object):
def __init__(self, token):
# 隨便填的字符串
self.token = token
# 自己生成的43位隨機(jī)字符串
self.aes_key = settings.DINGDING.get("DINGTALK_AES_TOKEN")
# 釘釘企業(yè)ID
self.corp_id = settings.DINGDING.get("CorpId") #
print("corp_id:", self.corp_id)
self.nonce = settings.DINGDING.get("nonce")
self.crypto = DingTalkCrypto(
token=self.nonce,
encoding_aes_key=self.aes_key,
corpid_or_suitekey=self.corp_id
)
def encrypt_success(self):
# 返回加密success
result = self.crypto.encrypt_message(
msg="success",
nonce=self.nonce,
timestamp=int(time.time()*1000)
)
return result
class DING(object):
def __init__(self, approve_process):
self.AgentId = settings.DINGDING.get("AgentId")
self.AppKey = settings.DINGDING.get("AppKey")
self.AppSecret = settings.DINGDING.get("AppSecret")
self.dingding_url = settings.DINGDING.get("URL")
self.process_code = settings.DINGDING.get("APPROVE_PROCESS").get(approve_process)['process_code']
self.aes_key = settings.DINGDING.get("DINGTALK_AES_TOKEN")
self.nonce = settings.DINGDING.get("nonce")
def get_token(self):
'''
獲取釘釘?shù)膖oken
:return: 釘釘token
'''
url = self.dingding_url + '/gettoken?appkey={}appsecret={}'.format(self.AppKey, self.AppSecret)
req = requests.get(url)
req = json.loads(req.text)
return req['access_token']
# def createCallbackDd():
# '''
# 注冊(cè)釘釘回調(diào)函數(shù)
# :return:
# '''
# url = 'https://oapi.dingtalk.com/call_back/register_call_back?access_token=' + self.getToken()
# data = {
# "call_back_tag": ["bpms_task_change", "bpms_instance_change"], #這兩個(gè)回調(diào)種類是審批的
# "token": TOKEN, #自定義的字符串
# "aes_key": AES_KEY, #自定義的43位字符串,密鑰
# "url": URL #回調(diào)地址
# }
# requests.post(url, data=json.dumps(data))
# return ('OK')
def create_process(self, originator_user_id, dept_id, form_component_value_vo, approvers, cc_list, has_cc=0):
'''
創(chuàng)建釘釘審批
approvers為list 元素為釘釘userid cc_list同理
'''
url = self.dingding_url + '/topapi/processinstance/create?access_token=' + self.get_token()
print("form_component_value_vo:", form_component_value_vo)
if has_cc == 0:
data = {
'agent_id': self.AgentId,
'process_code': self.process_code, #工單id
'originator_user_id': originator_user_id,
'dept_id': dept_id, #創(chuàng)建人的釘釘部門id
'form_component_values': str(form_component_value_vo), #釘釘后臺(tái)配置的需要填寫(xiě)的字段,
'approvers': approvers,
'cc_list': cc_list,
'cc_position': 'START_FINISH' # 發(fā)起和完成時(shí)與抄送
}
else:
data = {
'agent_id': self.AgentId,
'process_code': self.process_code, #工單id
'originator_user_id': originator_user_id, #創(chuàng)建人的釘釘userid
'dept_id': dept_id, #創(chuàng)建人的釘釘部門id
'form_component_values': str(form_component_value_vo), #釘釘后臺(tái)配置的需要填寫(xiě)的字段,
'approvers': approvers,
}
print("dingding_utils:", data)
response = requests.post(url, data=data)
return response.json()
def get_status(self, process_instance_id):
url = self.dingding_url + '/topapi/processinstance/get?access_token=' + self.get_token()
data = {
"process_instance_id": process_instance_id
}
response = requests.post(url, data=data)
return response.json()
def register_callback(self, call_back_url):
# 注冊(cè)回調(diào)
url = self.dingding_url + '/call_back/register_call_back?access_token=' + self.get_token()
print("self.get_token():", self.get_token())
data = {
"call_back_tag": ['bpms_task_change', 'bpms_instance_change'],
"token": self.nonce,
"aes_key": self.aes_key,
"url": call_back_url,
}
response = requests.post(url, data=json.dumps(data))
return response.json()
def get_callback(self):
url = self.dingding_url + '/call_back/get_call_back?access_token=' + self.get_token()
req = requests.get(url)
req = json.loads(req.text)
return req
def create_process_approver_v2(self, originator_user_id, dept_id, form_component_value_vo, approvers, cc_list):
'''
創(chuàng)建釘釘審批
'''
url = self.dingding_url + '/topapi/processinstance/create?access_token=' + self.get_token()
data = {
'agent_id': self.AgentId,
'process_code': self.process_code,
'originator_user_id': originator_user_id,
'dept_id': dept_id,
'form_component_values': str(form_component_value_vo),
'approvers_v2': json.dumps(approvers)
}
if cc_list:
data['cc_list'] = cc_list
data['cc_position'] = 'FINISH'
response = requests.post(url, data=data)
return response.json()
def create_process_approver_v2_test(self, originator_user_id, dept_id, form_component_value_vo):
'''
創(chuàng)建釘釘審批
'''
url = self.dingding_url + '/topapi/processinstance/create?access_token=' + self.get_token()
data = {
'agent_id': self.AgentId,
'process_code': self.process_code,
'originator_user_id': originator_user_id,
'dept_id': dept_id,
'form_component_values': str(form_component_value_vo),
'approvers_v2': json.dumps([
{
"task_action_type": "NONE",
"user_ids": ["dingding_id"], # 單獨(dú)審批人
},
{
"task_action_type": "OR",
"user_ids": ["dingding_id1", "dingding_id2"], # 或簽
},
{
"task_action_type": "AND",
"user_ids": ["dingding_id1", "dingding_id2"], # 會(huì)簽
}
])
}
response = requests.post(url, data=data)
return response.json()
if __name__ == "__main__":
import django, os, sys
sys.path.append('xxxxxx') # 項(xiàng)目路徑
os.environ['DJANGO_SETTINGS_MODULE'] = 'xx.settings'
# print("settings.DINGDING", settings.DINGDING)
ding = DING("create_xx")
# print(ding.get_token())
# info = [{'name': '單行輸入框','value': 'testixxxxxxxx'}]
# # print(ding.create_process('11', 11, info))
a = [
{'name': "輸入框1", 'value': "value1"},
{'name': "輸入框2", 'value': "value2"},
]
# print(ding.create_process_test('11', 11, a))
# print(ding.create_process_approver_v2_test('11', 11, a))
# print(ding.create_process_test2())
# print(ding.get_status('xxx'))
print(ding.get_status('xx'))
# # 驗(yàn)證 回調(diào)
# a = ding.get_token()
# print(a)
# c = Crypto(a)
# print(c.encrypt_success())
# 注冊(cè)回調(diào)
# print(ding.register_callback("http://xxxx.vaiwan.com/xxx"))
# print(ding.get_callback())
1 Crypto類用于對(duì)接釘釘回調(diào)用的。一個(gè)公司只有一個(gè)corpId,并且一個(gè)corpid只能注冊(cè)一個(gè)回調(diào)地址。我司有公共組注冊(cè)好了回調(diào)。只要接入公司內(nèi)的回調(diào)即可。所以我實(shí)際沒(méi)有使用到Crypto。
2 在釘釘管理后臺(tái)中創(chuàng)建應(yīng)用后會(huì)有這三個(gè)東西:AgentId、AppKey,AppSecret 。在創(chuàng)建釘釘審批流程,可以從審批流程瀏覽器中獲取到APPROVE_PROCESS。別忘啦給這個(gè)流程審批接口權(quán)限。這些官方文檔有說(shuō)。
2 不能審批流程前后有相同的人,不能自動(dòng)顯示成 “自動(dòng)同意”(管理后臺(tái)設(shè)置成去重后,但是接口指定審批人場(chǎng)景,不支持)
到此這篇關(guān)于python公司內(nèi)項(xiàng)目對(duì)接釘釘審批流程的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)python對(duì)接釘釘審批內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!