Php实现钉钉OA一级审批,二级审批
一级审批
public function oaPush($user_id,$person,$data){//测试数据,上线需要删除$user_id = '154502333155';//发起人$person = ['154502665555'];//审批人$len = count($person);$result = null;if($len>0){$approve_config = DingApproveConfig::where("type", 8)->first();$url = $approve_config->url;$rep['originatorUserId'] = $user_id;$rep['processCode'] = $approve_config->processCode;$type = $len>1?'AND':'NONE';$rep['approvers'][] = ['actionType'=>$type,'userIds'=>$person];$rep['microappAgentId'] = env("DD_AGENT_ID");$rep['formComponentValues'] = $data;$result = $this->oaCurl($url,$rep);}return $result;}
一级审批
public function oaPush2l($user_id,$person,$data){//测试数据,上线需要删除$user_id = '154502333155';//发起人$person = ['154502333155','013359400664'];//审批人$len = count($person);$result = null;if($len>0){// 审批人,按照顺序审批$arr = [];foreach ($person as $key => $value) {$level2 = [];$level2['actionType'] = "NONE";$level2['userIds'] = [$value];array_push($arr, $level2);}$approve_config = DingApproveConfig::where("type", 9)->first();$url = $approve_config->url;$rep['originatorUserId'] = $user_id;$rep['processCode'] = $approve_config->processCode;$rep['approvers'] = $arr;$rep['microappAgentId'] = env("DD_AGENT_ID");$rep['formComponentValues'] = $data;$result = $this->oaCurl($url,$rep);}return $result;}
区别在于参数: approvers
一级审批传入的是:
$type = $len>1?'AND':'NONE';
$rep['approvers'][] = ['actionType'=>$type,'userIds'=>$person];
'AND' 是两个人以审批,同时收到,然后同时通过才算通过。
二级审批传入的是
array:2 [0 => array:2 ["actionType" => "NONE""userIds" => array:1 [0 => "154502335555"]]1 => array:2 ["actionType" => "NONE""userIds" => array:1 [0 => "01335926400664"]]
]
$rep['approvers'] = $arr;
public function oaCurl($url,$data){$ding = new Dingd();$access_token = $ding->getDingdAccessToken(env('DD_APPKEY_COM'),env('DD_APPSECRET_COM'));$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));curl_setopt($ch, CURLOPT_REFERER, '');curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt( $ch,CURLOPT_HTTPHEADER, array('Content-Type: application/json','x-acs-dingtalk-access-token:' . $access_token));curl_setopt($ch, CURLOPT_TIMEOUT, 30);$result = curl_exec($ch);curl_close($ch);$result = mb_convert_encoding($result, 'UTF-8','UTF-8 ,GBK, GB2312, BIG5');return json_decode($result,true);}
钉钉回调处理
app\Jobs
public function handle(){// 钉钉审批事件$config = DingApproveConfig::where("type", 4)->first();$code= $config->processCode;if ($this->type == "bpms_task_change") {if (isset($this->res['result'])) {// 有result才有返回结果if($this->res['processCode']==$code){//对数据库进行处理}}}if ($this->type == 'bpms_instance_change') {if (isset($this->res['result'])) {// 有result才有返回结果if ($this->res['processCode'] == $code) {//对数据库进行处理}}if ($this->res['type'] === 'terminate') {// 审批撤回if ($this->res['processCode'] == $code) {//对数据库进行处理}}}}