From abb680ac36b19b9047bad099ec63eb1cd4c4c809 Mon Sep 17 00:00:00 2001 From: sunguangming Date: Thu, 21 Dec 2023 16:58:30 +0800 Subject: [PATCH] * webhook: refactor fetchHook. --- module/webhook/model.php | 90 ++++++++++++--------- module/webhook/test/model/fetchhook.php | 33 ++++---- module/webhook/test/model/getopenidlist.php | 22 +++-- module/webhook/test/webhook.class.php | 2 +- 4 files changed, 86 insertions(+), 61 deletions(-) diff --git a/module/webhook/model.php b/module/webhook/model.php index 027a9477bd..d72448e109 100644 --- a/module/webhook/model.php +++ b/module/webhook/model.php @@ -328,6 +328,7 @@ class webhookModel extends model if(!isset($this->lang->action->label->$actionType)) return false; if(empty($this->config->objectTables[$objectType])) return false; $action = $this->dao->select('*')->from(TABLE_ACTION)->where('id')->eq($actionID)->fetch(); + a($action);die; if(!$action) return false; if($webhook->products) @@ -566,15 +567,16 @@ class webhookModel extends model } /** + * 获取openID列表。 * Get openID list. * - * @param int $webhookID - * @param int $actionID - * @param string $toList + * @param int $webhookID + * @param int $actionID + * @param string|array $toList * @access public * @return string */ - public function getOpenIdList($webhookID, $actionID, $toList = '') + public function getOpenIdList(int $webhookID, int $actionID, string|array $toList = ''): string { if($toList) { @@ -609,47 +611,21 @@ class webhookModel extends model } /** + * 请求发送数据的接口。 * Post hook data. * - * @param object $webhook - * @param string $sendData - * @param int $actionID - * @param string $appendUser + * @param object $webhook + * @param string $sendData + * @param int $actionID + * @param string|array $appendUser * @access public * @return int */ - public function fetchHook($webhook, $sendData, $actionID = 0, $appendUser = '') + public function fetchHook(object $webhook, string $sendData, int $actionID = 0, string|array $appendUser = '') { if(!extension_loaded('curl')) return print(helper::jsonEncode($this->lang->webhook->error->curl)); - if($webhook->type == 'dinguser' || $webhook->type == 'wechatuser' || $webhook->type == 'feishuuser') - { - if(is_string($webhook->secret)) $webhook->secret = json_decode($webhook->secret); - - $openIdList = $this->getOpenIdList($webhook->id, $actionID, $appendUser); - if(empty($openIdList)) return false; - if($webhook->type == 'dinguser') - { - $this->app->loadClass('dingapi', true); - $dingapi = new dingapi($webhook->secret->appKey, $webhook->secret->appSecret, $webhook->secret->agentId); - $result = $dingapi->send($openIdList, $sendData); - return json_encode($result); - } - elseif($webhook->type == 'wechatuser') - { - $this->app->loadClass('wechatapi', true); - $wechatapi = new wechatapi($webhook->secret->appKey, $webhook->secret->appSecret, $webhook->secret->agentId); - $result = $wechatapi->send($openIdList, $sendData); - return json_encode($result); - } - elseif($webhook->type == 'feishuuser') - { - $this->app->loadClass('feishuapi', true); - $feishuapi = new feishuapi($webhook->secret->appId, $webhook->secret->appSecret); - $result = $feishuapi->send($openIdList, $sendData); - return json_encode($result); - } - } + if(in_array($webhook->type, array('dinguser', 'wechatuser', 'feishuuser'))) return $this->sendToUser($webhook, $sendData, $actionID, $appendUser); $contentType = "Content-Type: {$webhook->contentType};charset=utf-8"; if($webhook->type == 'dinggroup' or $webhook->type == 'wechatgroup' or $webhook->type == 'feishugroup') $contentType = "Content-Type: application/json"; @@ -696,6 +672,46 @@ class webhookModel extends model return $httpCode; } + /** + * 发送消息到钉钉、飞书、企业微信的用户。 + * Send to dingding, feishu, weixin users. + * + * @param object $webhook + * @param string $sendData + * @param int $actionID + * @param string|array $appendUser + * @access public + * @return string|false + */ + public function sendToUser(object $webhook, string $sendData, int $actionID, string|array $appendUser): string|false + { + if(is_string($webhook->secret)) $webhook->secret = json_decode($webhook->secret); + + $openIdList = $this->getOpenIdList($webhook->id, $actionID, $appendUser); + if(empty($openIdList)) return false; + if($webhook->type == 'dinguser') + { + $this->app->loadClass('dingapi', true); + $dingapi = new dingapi($webhook->secret->appKey, $webhook->secret->appSecret, $webhook->secret->agentId); + $result = $dingapi->send($openIdList, $sendData); + return json_encode($result); + } + elseif($webhook->type == 'wechatuser') + { + $this->app->loadClass('wechatapi', true); + $wechatapi = new wechatapi($webhook->secret->appKey, $webhook->secret->appSecret, $webhook->secret->agentId); + $result = $wechatapi->send($openIdList, $sendData); + return json_encode($result); + } + elseif($webhook->type == 'feishuuser') + { + $this->app->loadClass('feishuapi', true); + $feishuapi = new feishuapi($webhook->secret->appId, $webhook->secret->appSecret); + $result = $feishuapi->send($openIdList, $sendData); + return json_encode($result); + } + } + /** * Save datas. * diff --git a/module/webhook/test/model/fetchhook.php b/module/webhook/test/model/fetchhook.php index 99fc5ea951..8f16a102db 100755 --- a/module/webhook/test/model/fetchhook.php +++ b/module/webhook/test/model/fetchhook.php @@ -4,17 +4,20 @@ include dirname(__FILE__, 5) . '/test/lib/init.php'; include dirname(__FILE__, 2) . '/webhook.class.php'; su('admin'); +zdTable('webhook')->gen(10); +zdTable('action')->gen(10); + /** title=测试 webhookModel->fetchHook(); +timeout=0 cid=1 -pid=1 -传入正确参数 >> Could not resolve host: oapi.dinggroup.com; Unknown error -不传入objectType >> Could not resolve host: oapi.dinggroup.com; Unknown error -不传入objectID >> Could not resolve host: oapi.dinggroup.com; Unknown error -不传入actionType >> Could not resolve host: oapi.dinggroup.com; Unknown error -不传入actionID >> Could not resolve host: oapi.dinggroup.com; Unknown error +- 传入正确参数 @0 +- 不传入objectType @0 +- 不传入objectID @0 +- 不传入actionType @0 +- 不传入actionID @0 */ @@ -25,16 +28,16 @@ $objectType[0] = 'product'; $objectType[1] = ''; $objectID = array(); -$objectID[0] = '1'; -$objectID[1] = ''; +$objectID[0] = 1; +$objectID[1] = 0; $actionType = array(); -$actionType[0] = 'created'; +$actionType[0] = 'common'; $actionType[1] = ''; $actionID = array(); $actionID[0] = '4'; -$actionID[1] = ''; +$actionID[1] = 0; $result1 = $webhook->fetchHookTest($objectType[0], $objectID[0], $actionType[0], $actionID[0]); $result2 = $webhook->fetchHookTest($objectType[1], $objectID[0], $actionType[0], $actionID[0]); @@ -42,8 +45,8 @@ $result3 = $webhook->fetchHookTest($objectType[0], $objectID[1], $actionType[0], $result4 = $webhook->fetchHookTest($objectType[0], $objectID[0], $actionType[1], $actionID[0]); $result5 = $webhook->fetchHookTest($objectType[0], $objectID[0], $actionType[0], $actionID[1]); -r($webhook->fetchHookTest($result1)) && p() && e('Could not resolve host: oapi.dinggroup.com; Unknown error'); //传入正确参数 -r($webhook->fetchHookTest($result2)) && p() && e('Could not resolve host: oapi.dinggroup.com; Unknown error'); //不传入objectType -r($webhook->fetchHookTest($result3)) && p() && e('Could not resolve host: oapi.dinggroup.com; Unknown error'); //不传入objectID -r($webhook->fetchHookTest($result4)) && p() && e('Could not resolve host: oapi.dinggroup.com; Unknown error'); //不传入actionType -r($webhook->fetchHookTest($result5)) && p() && e('Could not resolve host: oapi.dinggroup.com; Unknown error'); //不传入actionID \ No newline at end of file +r($result1) && p() && e('0'); //传入正确参数 +r($result2) && p() && e('0'); //不传入objectType +r($result3) && p() && e('0'); //不传入objectID +r($result4) && p() && e('0'); //不传入actionType +r($result5) && p() && e('0'); //不传入actionID \ No newline at end of file diff --git a/module/webhook/test/model/getopenidlist.php b/module/webhook/test/model/getopenidlist.php index e0dd4f666a..fd6179e6db 100755 --- a/module/webhook/test/model/getopenidlist.php +++ b/module/webhook/test/model/getopenidlist.php @@ -4,17 +4,23 @@ include dirname(__FILE__, 5) . '/test/lib/init.php'; include dirname(__FILE__, 2) . '/webhook.class.php'; su('admin'); +zdTable('action')->gen(10); +zdTable('oauth')->gen(10); + /** title=测试 webhookModel->getOpenIdList(); +timeout=0 cid=1 -pid=1 - +- 测试传入正常数据的情况 @0 +- 测试传入空的情况 @0 +- 测试webhook传入空的情况 @0 +- 测试action传入空的情况 @0 */ -$webhook = new webhookTest(); +$webhookTest = new webhookTest(); $webhook = array(); $webhook[0] = 2; @@ -22,9 +28,9 @@ $webhook[1] = ''; $action = array(); $action[0] = 1; -$action[1] = ''; +$action[1] = 0; -r($webhook->getOpenIdListTest($webhook[0], $action[0])) && p() && e(''); //测试传入正常数据的情况 -r($webhook->getOpenIdListTest($webhook[1], $action[1])) && p() && e(''); //测试传入空的情况 -r($webhook->getOpenIdListTest($webhook[0], $action[1])) && p() && e(''); //测试webhook传入空的情况 -r($webhook->getOpenIdListTest($webhook[1], $action[0])) && p() && e(''); //测试action传入空的情况 \ No newline at end of file +r($webhookTest->getOpenIdListTest($webhook[0], $action[0])) && p() && e('0'); //测试传入正常数据的情况 +r($webhookTest->getOpenIdListTest($webhook[1], $action[1])) && p() && e('0'); //测试传入空的情况 +r($webhookTest->getOpenIdListTest($webhook[0], $action[1])) && p() && e('0'); //测试webhook传入空的情况 +r($webhookTest->getOpenIdListTest($webhook[1], $action[0])) && p() && e('0'); //测试action传入空的情况 \ No newline at end of file diff --git a/module/webhook/test/webhook.class.php b/module/webhook/test/webhook.class.php index 61ab446b85..15ba95283c 100644 --- a/module/webhook/test/webhook.class.php +++ b/module/webhook/test/webhook.class.php @@ -352,7 +352,7 @@ class webhookTest if(!$webhooks) $webhooks = $this->getListTest(); if(!$webhooks) return true; - foreach($webhooks as $id => $webhook) + foreach($webhooks as $webhook) { $postData = $this->objectModel->buildData($objectType, $objectID, $actionType, $actionID, $webhook); $objects = $this->objectModel->fetchHook($webhook, $postData, $actionID);