Bootstrap

python小项目实例流程-公司内项目对接钉钉审批流程(python)

1 importrequests2 importjson3 importtime4 from dingtalk.crypto importDingTalkCrypto5

6 from django.conf importsettings7 #settings.BASE_DIR

8

9

10 classCrypto(object):11 def __init__(self, token):12 #随便填的字符串

13 self.token =token14 #自己生成的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_id24 )25

26 defencrypt_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 returnresult34

35

36 classDING(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 defget_token(self):47 '''

48 获取钉钉的token49 :return: 钉钉token50 '''

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 returnresponse.json()101

102 defget_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_id106 }107 response = requests.post(url, data=data)108 returnresponse.json()109

110 defregister_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 returnresponse.json()122

123 defget_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 returnreq128

129 defcreate_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 ifcc_list:143 data['cc_list'] =cc_list144 data['cc_position'] = 'FINISH'

145

146 response = requests.post(url, data=data)147 returnresponse.json()148

149 defcreate_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 returnresponse.json()178

179

180 if __name__ == "__main__":181

182 importdjango, os, sys183 sys.path.append('xxxxxx') #项目路径

184 os.environ['DJANGO_SETTINGS_MODULE'] = 'OMS.settings'

185 #print("settings.DINGDING", settings.DINGDING)

186 ding = DING("create_sendexplain")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())

;