部署钉钉提交审批

mobile-first
cyc 6 years ago
parent be37220b15
commit 55a9a3666d

@ -0,0 +1,119 @@
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class index extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper('dingtools');
$this->AppKey = 'dingjcbxgidah9uspeuc';
$this->AppSecret = 'C4-8rUDK1u5Twpsw7U3yo42s_bbYxFIqzLMp2j7uI80Sa8D-OPbtSoCMgZxHxo2d';
}
public function index(){
echo json_encode(array('https://data.chinahighlights.com/pic/logo/logo-132x104.png','https://data.chinahighlights.com/image/homepage/holding-panda.jpg'));
exit('前方高能!!!');
}
public function sendApproval(){
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:POST, GET');
header('Access-Control-Max-Age:0');
header('Access-Control-Allow-Headers:x-requested-with, Content-Type');
header('Access-Control-Allow-Credentials:true');
//接收数据
$img = $this->input->post('img');
$money = $this->input->post('money');
$postunionid = $this->input->post('postunionid');
$approvers = $this->input->post('approvers');
if(empty($postunionid)){
exit('参数错误');
}
//获取access_token
$access_url = 'https://oapi.dingtalk.com/gettoken?appkey='.$this->AppKey.'&appsecret='.$this->AppSecret;
$access_token = GetPost_http($access_url,'','');
$access_token = json_decode($access_token)->access_token;
$url = 'https://oapi.dingtalk.com/topapi/processinstance/create?access_token='.$access_token;
$originator_user_id = $this->getuserid($postunionid);
$dept_id = $this->getdeptid($originator_user_id);
$approversid = $this->getuserid($approvers);
$postData = array();
//表单code
$postData['process_code'] = 'PROC-9D90693F-DE34-423C-9B92-A19FFFC87577';
//发起人id
$postData['originator_user_id'] = $originator_user_id;
//发起人部门id
$postData['dept_id'] = $dept_id;
//审批人id
$postData['approvers'] = $approversid;
$postData['form_component_values'] = array();
$postData['form_component_values']['0']['name'] = '报账金额';
$postData['form_component_values']['0']['value'] = $money;
$postData['form_component_values']['1']['name'] = '附件';
$postData['form_component_values']['1']['value'] = json_decode($img);
//print_r(json_encode($postData));die();
$approoval = GetPost_http($url,json_encode($postData),'json');
print_r($approoval);
}
public function sendRefund(){
//获取access_token
$access_url = 'https://oapi.dingtalk.com/gettoken?appkey='.$this->AppKey.'&appsecret='.$this->AppSecret;
$access_token = GetPost_http($access_url,'','');
$access_token = json_decode($access_token)->access_token;
$url = 'https://oapi.dingtalk.com/topapi/processinstance/create?access_token='.$access_token;
$postData = array();
$postData['process_code'] = 'PROC-95F150AE-A2A9-4B99-B2D6-C26F62EC1E06';
//发起人id
$postData['originator_user_id'] = '103218225237808971';
//发起人部门id
$postData['dept_id'] = '111848239';
//审批人id
$postData['approvers'] = '011702444537734182';
$postData['form_component_values'] = array();
$postData['form_component_values']['0']['name'] = '退款金额';
$postData['form_component_values']['0']['value'] = 1000;
$approoval = GetPost_http($url,json_encode($postData),'json');
print_r($approoval);
}
//获取用户id
function getuserid($unionid){
if(empty($unionid)){
exit('参数为空');
}else{
$access_url = 'https://oapi.dingtalk.com/gettoken?appkey='.$this->AppKey.'&appsecret='.$this->AppSecret;
$access_token = GetPost_http($access_url,'','');
$access_token = json_decode($access_token)->access_token;
$listurl = 'https://oapi.dingtalk.com/user/getUseridByUnionid?access_token='.$access_token.'&unionid='.$unionid;
$userlist = GetPost_http($listurl,'','');
$userid = json_decode($userlist)->userid;
return $userid;
}
}
//获取部门列表
function getdeptid($userid){
if(empty($userid)){
exit('参数错误');
}else{
$access_url = 'https://oapi.dingtalk.com/gettoken?appkey='.$this->AppKey.'&appsecret='.$this->AppSecret;
$access_token = GetPost_http($access_url,'','');
$access_token = json_decode($access_token)->access_token;
$detailurl = 'https://oapi.dingtalk.com/user/get?access_token='.$access_token.'&userid='.$userid;
$userdetail = GetPost_http($detailurl,'','');
$departid = json_decode($userdetail)->department;
return $departid['0'];
}
}
}

@ -0,0 +1,49 @@
<?php
//获取毫秒
function get_microtime (){
list($s1, $s2) = explode(' ', microtime());
return (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
}
//获取钉钉登录sign
function get_loginsign($microtime,$appsecret){
$microtime = $microtime;
$sign = hash_hmac('sha256',$microtime,$appsecret,true);
$signature = base64_encode($sign);
return $signature;
}
//发送请求函数
function GetPost_http($url, $data = '',$format='') {
if(!isset($_SERVER['HTTP_USER_AGENT'])){
$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.2372.400 QQBrowser/9.5.10548.400';
}
$curl = curl_init(); // 启动一个CURL会话
curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
if (!empty($data)) {
curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
if($format == 'json'){
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json; charset=utf-8"));
}
}
curl_setopt($curl, CURLOPT_TIMEOUT, 40); // 设置超时限制防止死循环
curl_setopt($curl, CURLOPT_TIMEOUT_MS, 40000); // 设置超时限制防止死循环
curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
$tmpInfo = curl_exec($curl); // 执行操作
$errno = curl_errno($curl);
if ($errno !== 0) {
log_message('error', 'dingding'.$errno.curl_error($curl));
}
curl_close($curl); //关闭CURL会话
return $tmpInfo; //返回数据
}
?>
Loading…
Cancel
Save