From aa471fff1b4c782b7c335f2021ec8354f315c611 Mon Sep 17 00:00:00 2001 From: wangyidong Date: Mon, 18 Nov 2019 16:07:46 +0800 Subject: [PATCH] * finish task #6540. --- lib/dingapi/dingapi.class.php | 2 +- module/webhook/control.php | 2 +- module/webhook/model.php | 51 +++++++++++++++++++++++++++++++---- 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/lib/dingapi/dingapi.class.php b/lib/dingapi/dingapi.class.php index 7c548a16e3..b6d65323cc 100644 --- a/lib/dingapi/dingapi.class.php +++ b/lib/dingapi/dingapi.class.php @@ -83,7 +83,7 @@ class dingapi $response = curl_exec($curl); curl_close($curl); - $response = json_encode($response); + $response = json_decode($response); if(isset($response->errcode) and $response->errcode == 0) return array('result' => 'success'); $this->errors[$response->errcode] = "Errcode:{$response->errcode}, Errmsg:{$response->errmsg}"; diff --git a/module/webhook/control.php b/module/webhook/control.php index 22d66491f0..d452c980ad 100644 --- a/module/webhook/control.php +++ b/module/webhook/control.php @@ -237,7 +237,7 @@ class webhook extends control if($diff < 29) { $time = time(); - $result = $this->webhook->fetchHook($webhook, $data->data); + $result = $this->webhook->fetchHook($webhook, $data->data, $data->action); $diff = time() - $time; } $this->webhook->saveLog($webhook, $data->action, $data->data, $result); diff --git a/module/webhook/model.php b/module/webhook/model.php index dfb686c4af..05f7951fbb 100644 --- a/module/webhook/model.php +++ b/module/webhook/model.php @@ -116,13 +116,15 @@ class webhookModel extends model /** * Get bind users. * - * @param int $webhookID + * @param int $webhookID + * @param array $users * @access public * @return array */ - public function getBindUsers($webhookID) + public function getBindUsers($webhookID, $users = array()) { return $this->dao->select('*')->from(TABLE_DINGUSERID)->where('webhook')->eq($webhookID) + ->beginIF($users)->andWhere('account')->in($users)->fi() ->fetchPairs('account', 'userid'); } @@ -259,7 +261,7 @@ class webhookModel extends model continue; } - $result = $this->fetchHook($webhook, $postData); + $result = $this->fetchHook($webhook, $postData, $actionID); $this->saveLog($webhook, $actionID, $postData, $result); } return !dao::isError(); @@ -322,7 +324,7 @@ class webhookModel extends model } $action->text = $text; - if($webhook->type == 'dingding') + if($webhook->type == 'dingding' or $webhook->type == 'dingapi') { $data = $this->getDingdingData($title, $text, $mobile); } @@ -432,18 +434,57 @@ class webhookModel extends model return $data; } + + /** + * Get userid list. + * + * @param int $actionID + * @access public + * @return string + */ + public function getUseridList($actionID) + { + if(empty($actionID)) return false; + + $action = $this->dao->select('*')->from(TABLE_ACTION)->where('id')->eq($actionID)->fetch(); + $table = zget($this->config->objectTables, $action->objectType, ''); + if(empty($table)) return false; + + $object = $this->dao->select('*')->from($table)->where('id')->eq($action->objectID)->fetch(); + $toList = $this->loadModel('message')->getToList($object, $action->objectType); + if(!empty($object->mailto)) $toList .= ',' . $object->mailto; + if(empty($toList)) return false; + + $useridList = $this->getBindUsers($webhook->id, $toList); + $useridList = join(',', $useridList); + return $useridList; + } + /** * Post hook data. * * @param object $webhook * @param string $sendData + * @param int $actionID * @access public * @return int */ - public function fetchHook($webhook, $sendData) + public function fetchHook($webhook, $sendData, $actionID = 0) { if(!extension_loaded('curl')) die(helper::jsonEncode($this->lang->webhook->error->curl)); + if($webhook->type == 'dingapi') + { + $webhook->secret = json_decode($webhook->secret); + + $useridList = $this->getUseridList($actionID); + if(empty($useridList)) return false; + + $this->app->loadClass('dingapi', true); + $dingapi = new dingapi($webhook->secret->appKey, $webhook->secret->appSecret, $webhook->secret->agentId); + return $dingapi->send($useridList, $sendData); + } + $contentType = "Content-Type: {$webhook->contentType};charset=utf-8"; if($webhook->type == 'dingding') $contentType = "Content-Type: application/json"; $header[] = $contentType;