From 0763c4288d2473b6b979dfbff79cea15e19103c9 Mon Sep 17 00:00:00 2001 From: wangyidong Date: Tue, 26 Nov 2019 09:35:27 +0800 Subject: [PATCH] * finish task #6567. --- config/zentaopms.php | 2 +- db/update11.6.5.sql | 11 +++++---- module/webhook/lang/zh-cn.php | 1 + module/webhook/model.php | 45 ++++++++++++++++++++--------------- 4 files changed, 35 insertions(+), 24 deletions(-) diff --git a/config/zentaopms.php b/config/zentaopms.php index bef43360ae..1f2afa89ae 100644 --- a/config/zentaopms.php +++ b/config/zentaopms.php @@ -168,7 +168,7 @@ define('TABLE_WEBHOOK', '`' . $config->db->prefix . 'webhook`'); define('TABLE_LOG', '`' . $config->db->prefix . 'log`'); define('TABLE_SCORE', '`' . $config->db->prefix . 'score`'); define('TABLE_NOTIFY', '`' . $config->db->prefix . 'notify`'); -define('TABLE_DINGUSERID', '`' . $config->db->prefix . 'dinguserid`'); +define('TABLE_OAUTH', '`' . $config->db->prefix . 'oauth`'); if(!defined('TABLE_LANG')) define('TABLE_LANG', '`' . $config->db->prefix . 'lang`'); $config->objectTables['product'] = TABLE_PRODUCT; diff --git a/db/update11.6.5.sql b/db/update11.6.5.sql index b07f84fe7d..87b18ed0ad 100644 --- a/db/update11.6.5.sql +++ b/db/update11.6.5.sql @@ -1,7 +1,10 @@ ALTER TABLE `zt_dept` CHANGE `order` `order` smallint(4) unsigned NOT NULL DEFAULT '0' AFTER `grade`; -CREATE TABLE `zt_dinguserid` ( - `webhook` mediumint(8) unsigned NOT NULL, +CREATE TABLE IF NOT EXISTS `zt_oauth` ( `account` varchar(30) NOT NULL, - `userid` varchar(255) NOT NULL, - UNIQUE KEY `webhook_account` (`webhook`,`account`) + `openID` varchar(255) NOT NULL, + `providerType` varchar(30) NOT NULL, + `providerID` mediumint(8) unsigned NOT NULL, + KEY `account` (`account`), + KEY `providerType` (`providerType`), + KEY `providerID` (`providerID`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; diff --git a/module/webhook/lang/zh-cn.php b/module/webhook/lang/zh-cn.php index efaadc34ef..8b15093593 100644 --- a/module/webhook/lang/zh-cn.php +++ b/module/webhook/lang/zh-cn.php @@ -71,6 +71,7 @@ $lang->webhook->note->async = '异步需要打开计划任务'; $lang->webhook->note->bind = '只有钉钉工作通知类型才需要绑定用户。'; $lang->webhook->note->product = "此项为空时所有{$lang->productCommon}的动作都会触发钩子,否则只有关联{$lang->productCommon}的动作才会触发。"; $lang->webhook->note->project = "此项为空时所有{$lang->projectCommon}的动作都会触发钩子,否则只有关联{$lang->projectCommon}的动作才会触发。"; +$lang->webhook->note->dingKey = ""; $lang->webhook->note->typeList['bearychat'] = '请在倍洽中添加一个禅道机器人,并将其webhook填写到此处。'; $lang->webhook->note->typeList['dingding'] = '请在钉钉中添加一个自定义机器人,并将其webhook填写到此处。'; diff --git a/module/webhook/model.php b/module/webhook/model.php index 9a3c80bcb5..179d575c8a 100644 --- a/module/webhook/model.php +++ b/module/webhook/model.php @@ -123,9 +123,10 @@ class webhookModel extends model */ public function getBindUsers($webhookID, $users = array()) { - return $this->dao->select('*')->from(TABLE_DINGUSERID)->where('webhook')->eq($webhookID) + return $this->dao->select('*')->from(TABLE_OAUTH)->where('providerType')->eq('webhook') + ->andWhere('providerID')->eq($webhookID) ->beginIF($users)->andWhere('account')->in($users)->fi() - ->fetchPairs('account', 'userid'); + ->fetchPairs('account', 'openID'); } /** @@ -144,7 +145,8 @@ class webhookModel extends model ->skipSpecial('url') ->remove('allParams, allActions') ->get(); - $webhook->params = $this->post->params ? implode(',', $this->post->params) . ',text' : 'text'; + $webhook->params = $this->post->params ? implode(',', $this->post->params) . ',text' : 'text'; + if($webhook->type == 'dingapi') { $webhook->secret = array(); @@ -212,7 +214,7 @@ class webhookModel extends model } /** - * Bind ding userid. + * Bind ding openID. * * @param int $webhookID * @access public @@ -222,16 +224,21 @@ class webhookModel extends model { $data = fixer::input('post')->get(); - $this->dao->delete()->from(TABLE_DINGUSERID)->where('account')->in(array_keys($data->userid))->exec(); + $this->dao->delete()->from(TABLE_OAUTH) + ->where('providerType')->eq('webhook') + ->andWhere('providerID')->eq($webhookID) + ->andWhere('account')->in(array_keys($data->userid)) + ->exec(); foreach($data->userid as $account => $userid) { if(empty($userid)) continue; - $dingUser = new stdclass(); - $dingUser->webhook = $webhookID; - $dingUser->account = $account; - $dingUser->userid = $userid; - $this->dao->insert(TABLE_DINGUSERID)->data($dingUser)->exec(); + $oauth = new stdclass(); + $oauth->account = $account; + $oauth->openID = $userid; + $oauth->providerType = 'webhook'; + $oauth->providerID = $webhookID; + $this->dao->insert(TABLE_OAUTH)->data($oauth)->exec(); } return !dao::isError(); } @@ -259,7 +266,7 @@ class webhookModel extends model if($webhook->sendType == 'async') { - if($webhook->type == 'dingapi' and empty($this->getUseridList($webhook->id, $actionID))) continue; + if($webhook->type == 'dingapi' and empty($this->getOpenIdList($webhook->id, $actionID))) continue; $this->saveData($id, $actionID, $postData); continue; } @@ -466,13 +473,13 @@ class webhookModel extends model } /** - * Get userid list. + * Get openID list. * * @param int $actionID * @access public * @return string */ - public function getUseridList($webhookID, $actionID) + public function getOpenIdList($webhookID, $actionID) { if(empty($actionID)) return false; @@ -485,9 +492,9 @@ class webhookModel extends model if(!empty($object->mailto)) $toList .= ',' . $object->mailto; if(empty($toList)) return false; - $useridList = $this->getBindUsers($webhookID, $toList); - $useridList = join(',', $useridList); - return $useridList; + $openIdList = $this->getBindUsers($webhookID, $toList); + $openIdList = join(',', $openIdList); + return $openIdList; } /** @@ -507,12 +514,12 @@ class webhookModel extends model { $webhook->secret = json_decode($webhook->secret); - $useridList = $this->getUseridList($webhook->id, $actionID); - if(empty($useridList)) return false; + $openIdList = $this->getOpenIdList($webhook->id, $actionID); + if(empty($openIdList)) return false; $this->app->loadClass('dingapi', true); $dingapi = new dingapi($webhook->secret->appKey, $webhook->secret->appSecret, $webhook->secret->agentId); - $result = $dingapi->send($useridList, $sendData); + $result = $dingapi->send($openIdList, $sendData); return json_encode($result); }