公司內項目對接釘釘審批流程(python)


最近把組內的一個項目對接釘釘審批接口,通過python3.6。

釘釘官方文檔 https://ding-doc.dingtalk.com/doc#/serverapi2/ca8r99

廢話不多說了,上代碼:

  1 import requests
  2 import json
  3 import time
  4 from dingtalk.crypto import DingTalkCrypto
  5 
  6 from django.conf import settings
  7 # settings.BASE_DIR
  8 
  9 
 10 class Crypto(object):
 11     def __init__(self, token):
 12         # 隨便填的字符串
 13         self.token = token
 14         # 自己生成的43位隨機字符串
 15         self.aes_key = settings.DINGDING.get("DINGTALK_AES_TOKEN")
 16         # 釘釘企業ID
 17         self.corp_id = settings.DINGDING.get("CorpId") #
 18         print("corp_id:", self.corp_id)
 19         self.nonce = settings.DINGDING.get("nonce")
 20         self.crypto = DingTalkCrypto(
 21             token=self.nonce,
 22             encoding_aes_key=self.aes_key,
 23             corpid_or_suitekey=self.corp_id
 24         )
 25 
 26     def encrypt_success(self):
 27         # 返回加密success
 28         result = self.crypto.encrypt_message(
 29             msg="success",
 30             nonce=self.nonce,
 31             timestamp=int(time.time()*1000)
 32         )
 33         return result
 34 
 35 
 36 class DING(object):
 37     def __init__(self, approve_process):
 38         self.AgentId = settings.DINGDING.get("AgentId")
 39         self.AppKey = settings.DINGDING.get("AppKey")
 40         self.AppSecret = settings.DINGDING.get("AppSecret")
 41         self.dingding_url = settings.DINGDING.get("URL")
 42         self.process_code = settings.DINGDING.get("APPROVE_PROCESS").get(approve_process)['process_code']
 43         self.aes_key = settings.DINGDING.get("DINGTALK_AES_TOKEN")
 44         self.nonce = settings.DINGDING.get("nonce")
 45 
 46     def get_token(self):
 47         '''
 48         獲取釘釘的token
 49         :return: 釘釘token
 50         '''
 51         url = self.dingding_url + '/gettoken?appkey={}&appsecret={}'.format(self.AppKey, self.AppSecret)
 52         req = requests.get(url)
 53         req = json.loads(req.text)
 54         return req['access_token']
 55 
 56 # def createCallbackDd():
 57 #     '''
 58 #     注冊釘釘回調函數
 59 #     :return:
 60 #     '''
 61 #     url = 'https://oapi.dingtalk.com/call_back/register_call_back?access_token=' + self.getToken()
 62 #     data = {
 63 #         "call_back_tag": ["bpms_task_change", "bpms_instance_change"],  #這兩個回調種類是審批的
 64 #         "token": TOKEN,  #自定義的字符串
 65 #         "aes_key": AES_KEY, #自定義的43位字符串,密鑰
 66 #         "url": URL  #回調地址
 67 #     }
 68 #     requests.post(url, data=json.dumps(data))
 69 #     return ('OK')
 70 
 71     def create_process(self, originator_user_id, dept_id, form_component_value_vo, approvers, cc_list, has_cc=0):
 72         '''
 73         創建釘釘審批
 74         approvers為list 元素為釘釘userid   cc_list同理
 75         '''
 76         url = self.dingding_url + '/topapi/processinstance/create?access_token=' + self.get_token()
 77         print("form_component_value_vo:", form_component_value_vo)
 78         if has_cc == 0:
 79             data = {
 80                 'agent_id': self.AgentId,
 81                 'process_code': self.process_code,  #工單id
 82                 'originator_user_id': originator_user_id, 
 83                 'dept_id': dept_id,  #創建人的釘釘部門id  
 84                 'form_component_values': str(form_component_value_vo), #釘釘后台配置的需要填寫的字段,
 85                 'approvers': approvers,
 86                 'cc_list': cc_list,
 87                 'cc_position': 'START_FINISH'  # 發起和完成時與抄送
 88             }
 89         else:
 90             data = {
 91                 'agent_id': self.AgentId,
 92                 'process_code': self.process_code,  #工單id
 93                 'originator_user_id': originator_user_id, #創建人的釘釘userid 
 94                 'dept_id': dept_id,  #創建人的釘釘部門id 
 95                 'form_component_values': str(form_component_value_vo), #釘釘后台配置的需要填寫的字段,
 96                 'approvers': approvers,
 97             }
 98         print("dingding_utils:", data)
 99         response = requests.post(url, data=data)
100         return response.json()
101 
102     def get_status(self, process_instance_id):
103         url = self.dingding_url + '/topapi/processinstance/get?access_token=' + self.get_token()
104         data = {
105             "process_instance_id": process_instance_id
106         }
107         response = requests.post(url, data=data)
108         return response.json()
109 
110     def register_callback(self, call_back_url):
111         # 注冊回調
112         url = self.dingding_url + '/call_back/register_call_back?access_token=' + self.get_token()
113         print("self.get_token():", self.get_token())
114         data = {
115             "call_back_tag": ['bpms_task_change', 'bpms_instance_change'],
116             "token": self.nonce,
117             "aes_key": self.aes_key,
118             "url": call_back_url,
119         }
120         response = requests.post(url, data=json.dumps(data))
121         return response.json()
122 
123     def get_callback(self):
124         url = self.dingding_url + '/call_back/get_call_back?access_token=' + self.get_token()
125         req = requests.get(url)
126         req = json.loads(req.text)
127         return req
128 
129     def create_process_approver_v2(self, originator_user_id, dept_id, form_component_value_vo, approvers, cc_list):
130         '''
131         創建釘釘審批
132         '''
133         url = self.dingding_url + '/topapi/processinstance/create?access_token=' + self.get_token()
134         data = {
135             'agent_id': self.AgentId,
136             'process_code': self.process_code,
137             'originator_user_id': originator_user_id,
138             'dept_id': dept_id,
139             'form_component_values': str(form_component_value_vo),
140             'approvers_v2': json.dumps(approvers)
141         }
142         if cc_list:
143             data['cc_list'] = cc_list
144             data['cc_position'] = 'FINISH'
145 
146         response = requests.post(url, data=data)
147         return response.json()
148 
149     def create_process_approver_v2_test(self, originator_user_id, dept_id, form_component_value_vo):
150         '''
151         創建釘釘審批
152         '''
153         url = self.dingding_url + '/topapi/processinstance/create?access_token=' + self.get_token()
154         data = {
155             'agent_id': self.AgentId,
156             'process_code': self.process_code,
157             'originator_user_id': originator_user_id,
158             'dept_id': dept_id,
159             'form_component_values': str(form_component_value_vo),
160             'approvers_v2': json.dumps([
161                 {
162                     "task_action_type": "NONE",
163                     "user_ids": ["dingding_id"],   # 單獨審批人
164                 },
165                 {
166                     "task_action_type": "OR",
167                     "user_ids": ["dingding_id1", "dingding_id2"],   # 或簽
168                 },
169                 {
170                     "task_action_type": "AND",
171                     "user_ids": ["dingding_id1", "dingding_id2"],  # 會簽
172                 }
173             ])
174         }
175 
176         response = requests.post(url, data=data)
177         return response.json()
178 
179 
180 if __name__ == "__main__":
181 
182     import django, os, sys
183     sys.path.append('xxxxxx')   # 項目路徑
184     os.environ['DJANGO_SETTINGS_MODULE'] = 'xx.settings'
185     # print("settings.DINGDING", settings.DINGDING)
186     ding = DING("create_xx")
187     # print(ding.get_token())
188     # info = [{'name': '單行輸入框','value': 'testixxxxxxxx'}]
189     # # print(ding.create_process('11', 11, info))
190     a = [
191         {'name': "輸入框1", 'value': "value1"},
192         {'name': "輸入框2", 'value': "value2"},
193     ]
194     # print(ding.create_process_test('11', 11, a))
195     # print(ding.create_process_approver_v2_test('11', 11, a))
196     # print(ding.create_process_test2())  
197     # print(ding.get_status('xxx'))
198     print(ding.get_status('xx'))
199 
200     # # 驗證  回調
201     # a = ding.get_token()
202     # print(a)
203     # c = Crypto(a)
204     # print(c.encrypt_success())
205 
206     # 注冊回調
207     # print(ding.register_callback("http://xxxx.vaiwan.com/xxx"))
208     # print(ding.get_callback())

說明:

  1 Crypto類用於對接釘釘回調用的。一個公司只有一個corpId,並且一個corpid只能注冊一個回調地址。我司有公共組注冊好了回調。只要接入公司內的回調即可。所以我實際沒有使用到Crypto。

  2  在釘釘管理后台中創建應用后會有這三個東西:AgentId、AppKey,AppSecret  。在創建釘釘審批流程,可以從審批流程瀏覽器中獲取到APPROVE_PROCESS。別忘啦給這個流程審批接口權限。這些官方文檔有說。

  3  配置setting變量:

DINGDING = {
    "AgentId": 123,
    "AppKey": "xx",
    "AppSecret": "xx",
    "URL": "https://oapi.dingtalk.com",
    "APPROVE_PROCESS": { # process_code
        "create_xx": {
            "process_code": "abc", # 審批流程的id
    },
    "DINGTALK_AES_TOKEN": "abc",
    "nonce": "abc",
    "CorpId": "abc",
}

  4 接口形式創建的審批流程,與釘釘管理后台創建的流程有一些不同:

    1 不能在不同的審批環節設置不同的抄送人

    2 不能審批流程前后有相同的人,不能自動顯示成 “自動同意”(管理后台設置成去重后,但是接口指定審批人場景,不支持)

  5 其他如:審批內容、或簽,會簽代碼里都有示例。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM