From fc5adc765c6732aa59bfdaf92e2029dbfa26e010 Mon Sep 17 00:00:00 2001 From: wangyidong Date: Fri, 29 Jan 2021 13:15:50 +0800 Subject: [PATCH] * adjust for feishu webhook. --- lib/feishuapi/feishuapi.class.php | 49 ++++++++++++++++++++++++++--- module/webhook/control.php | 4 +-- module/webhook/js/bind.js | 11 ++++--- module/webhook/js/create.js | 10 +++--- module/webhook/lang/zh-cn.php | 3 +- module/webhook/model.php | 30 ++++++++++++++---- module/webhook/view/bind.html.php | 4 +-- module/webhook/view/browse.html.php | 2 +- module/webhook/view/create.html.php | 1 - module/webhook/view/edit.html.php | 6 ++-- 10 files changed, 91 insertions(+), 29 deletions(-) diff --git a/lib/feishuapi/feishuapi.class.php b/lib/feishuapi/feishuapi.class.php index 263fd359c8..49fe1dee92 100644 --- a/lib/feishuapi/feishuapi.class.php +++ b/lib/feishuapi/feishuapi.class.php @@ -55,17 +55,58 @@ class feishuapi public function getAllUsers() { set_time_limit(0); - $users = array(); - $response = $this->queryAPI($this->apiUrl . "contact/v3/users", json_encode(array('department_id' => 0)), array(CURLOPT_CUSTOMREQUEST => "GET")); - if(isset($response->data->items)) + $users = array(); + $depts = $this->getDepts(); + + foreach($depts as $deptID => $count) { - foreach($response->data->items as $user) $users[$user->name] = $user->open_id; + if($deptID and empty($count)) continue; + + $pageToken = ''; + while(true) + { + $response = $this->queryAPI($this->apiUrl . "contact/v3/users?department_id={$deptID}" . ($pageToken ? "&page_token={$pageToken}" : ''), '', array(CURLOPT_CUSTOMREQUEST => "GET")); + if(isset($response->data->items)) + { + foreach($response->data->items as $user) $users[$user->name] = $user->open_id; + } + + if(!isset($response->data->page_token)) break; + $pageToken = $response->data->page_token; + } } return array('result' => 'success', 'data' => $users); } + /** + * Get depts. + * + * @access public + * @return array + */ + public function getDepts() + { + set_time_limit(0); + + $depts = array('0' => '0'); + $pageToken = ''; + while(true) + { + $response = $this->queryAPI($this->apiUrl . "contact/v3/departments?parent_department_id=0" . ($pageToken ? "&page_token={$pageToken}" : '') . "&fetch_child=true", '', array(CURLOPT_CUSTOMREQUEST => "GET")); + if(isset($response->data->items)) + { + foreach($response->data->items as $dept) $depts[$dept->open_department_id] = $dept->member_count; + } + + if(!isset($response->data->page_token)) break; + $pageToken = $response->data->page_token; + } + + return $depts; + } + /** * Send message * diff --git a/module/webhook/control.php b/module/webhook/control.php index a63cf68e9a..d9040fbba5 100644 --- a/module/webhook/control.php +++ b/module/webhook/control.php @@ -185,7 +185,7 @@ class webhook extends control } $webhook = $this->webhook->getById($id); - if($webhook->type != 'dinguser' && $webhook->type != 'wechatuser' && $webhook->type != 'feishu') + if($webhook->type != 'dinguser' && $webhook->type != 'wechatuser' && $webhook->type != 'feishuuser') { echo js::alert($this->lang->webhook->note->bind); die(js::locate($this->createLink('webhook', 'browse'))); @@ -212,7 +212,7 @@ class webhook extends control $wechatApi = new wechatapi($webhook->secret->appKey, $webhook->secret->appSecret, $webhook->secret->agentId); $response = $wechatApi->getAllUsers(); } - elseif($webhook->type == 'feishu') + elseif($webhook->type == 'feishuuser') { $this->app->loadClass('feishuapi', true); $feishuApi = new feishuapi($webhook->secret->appId, $webhook->secret->appSecret); diff --git a/module/webhook/js/bind.js b/module/webhook/js/bind.js index aaa062393e..74a184920f 100644 --- a/module/webhook/js/bind.js +++ b/module/webhook/js/bind.js @@ -21,8 +21,11 @@ $(function () $(".bind").on("click",function() { - var inputName = this.getAttribute("data-value"); - $("#saveInput").html(); + var $obj = $(this); + var inputName = $obj.attr("data-value"); $("#saveInput").html(""); - myModalTrigger.show(); -}); \ No newline at end of file + myModalTrigger.show({shown:function() + { + $('#triggerModal #userSelect').val($obj.closest('tr').find("[id^=userid]").val()).trigger('chosen:updated'); + }}); +}); diff --git a/module/webhook/js/create.js b/module/webhook/js/create.js index bb43a91171..0084948e72 100644 --- a/module/webhook/js/create.js +++ b/module/webhook/js/create.js @@ -3,13 +3,13 @@ $(function() $('#type').change(function() { var type = $(this).val(); - $('#sendTypeTR').toggle(type != 'dinggroup' && type != 'dinguser' && type != 'wechatuser' && type != 'wechatgroup' && type != 'feishu'); - $('#secretTR').toggle(type == 'dinggroup'); - $('#urlTR').toggle(type != 'dinguser' && type != 'wechatuser' && type != 'feishu'); + $('#sendTypeTR').toggle(type != 'dinggroup' && type != 'dinguser' && type != 'wechatuser' && type != 'wechatgroup' && type != 'feishuuser' && type != 'feishugroup'); + $('#secretTR').toggle(type == 'dinggroup' || type == 'feishugroup'); + $('#urlTR').toggle(type != 'dinguser' && type != 'wechatuser' && type != 'feishuuser'); $('.dinguserTR').toggle(type == 'dinguser'); $('.wechatTR').toggle(type == 'wechatuser'); - $('.feishuTR').toggle(type == 'feishu'); - $('#paramsTR').toggle(type != 'bearychat' && type != 'dinggroup' && type != 'dinguser' && type != 'wechatuser' && type != 'wechatgroup' && type != 'feishu'); + $('.feishuTR').toggle(type == 'feishuuser'); + $('#paramsTR').toggle(type != 'bearychat' && type != 'dinggroup' && type != 'dinguser' && type != 'wechatuser' && type != 'wechatgroup' && type != 'feishuuser' && type != 'feishugroup'); $('#urlNote').html(urlNote[type]); }); diff --git a/module/webhook/lang/zh-cn.php b/module/webhook/lang/zh-cn.php index 657f780834..1a5df0e0e5 100644 --- a/module/webhook/lang/zh-cn.php +++ b/module/webhook/lang/zh-cn.php @@ -40,7 +40,8 @@ $lang->webhook->typeList['dinggroup'] = '钉钉群通知机器人'; $lang->webhook->typeList['dinguser'] = '钉钉工作消息通知'; $lang->webhook->typeList['wechatgroup'] = '企业微信群机器人'; $lang->webhook->typeList['wechatuser'] = '企业微信应用消息'; -$lang->webhook->typeList['feishu'] = '飞书工作消息通知'; +$lang->webhook->typeList['feishugroup'] = '飞书群通知机器人'; +$lang->webhook->typeList['feishuuser'] = '飞书工作消息通知'; $lang->webhook->typeList['default'] = '其他'; $lang->webhook->sendTypeList['sync'] = '同步'; diff --git a/module/webhook/model.php b/module/webhook/model.php index 604902fe28..ecc6186077 100644 --- a/module/webhook/model.php +++ b/module/webhook/model.php @@ -96,6 +96,11 @@ class webhookModel extends model { $text = substr($data->markdown->content, 0, strpos($data->markdown->content, '(http')); } + elseif(isset($data->content)) + { + $text = $data->content->text; + $text = substr($text, 0, strpos($text, '(http')) ? substr($text, 0, strpos($text, '(http')) : zget($users, $data->user, $this->app->user->realname) . $this->lang->action->label->{$action->action} . $this->lang->action->objectTypes[$action->objectType] . "[#{$action->objectID}::{$object->$field}]"; + } else { $text = substr($data->text, 0, strpos($data->text, '(http')) ? substr($data->text, 0, strpos($data->text, '(http')) : zget($users, $data->user, $this->app->user->realname) . $this->lang->action->label->{$action->action} . $this->lang->action->objectTypes[$action->objectType] . "[#{$action->objectID}::{$object->$field}]"; @@ -188,7 +193,7 @@ class webhookModel extends model $webhook->secret = json_encode($webhook->secret); $webhook->url = $this->config->webhook->wechatApiUrl; } - elseif($webhook->type == 'feishu') + elseif($webhook->type == 'feishuuser') { $webhook->secret = array(); $webhook->secret['appId'] = $webhook->feishuAppId; @@ -259,7 +264,7 @@ class webhookModel extends model $webhook->secret = json_encode($webhook->secret); } - elseif($webhook->type == 'feishu') + elseif($webhook->type == 'feishuuser') { $webhook->secret = array(); $webhook->secret['appId'] = $webhook->feishuAppId; @@ -418,7 +423,7 @@ class webhookModel extends model { $data = $this->getWeixinData($title, $text, $mobile); } - elseif($webhook->type == 'feishu') + elseif($webhook->type == 'feishuuser') { $data = $this->getFeishuData($title, $text); } @@ -595,7 +600,7 @@ class webhookModel extends model { if(!extension_loaded('curl')) die(helper::jsonEncode($this->lang->webhook->error->curl)); - if($webhook->type == 'dinguser' || $webhook->type == 'wechatuser' || $webhook->type == 'feishu') + if($webhook->type == 'dinguser' || $webhook->type == 'wechatuser' || $webhook->type == 'feishuuser') { if(is_string($webhook->secret)) $webhook->secret = json_decode($webhook->secret); @@ -615,7 +620,7 @@ class webhookModel extends model $result = $wechatapi->send($openIdList, $sendData); return json_encode($result); } - elseif($webhook->type == 'feishu') + elseif($webhook->type == 'feishuuser') { $this->app->loadClass('feishuapi', true); $feishuapi = new feishuapi($webhook->secret->appId, $webhook->secret->appSecret); @@ -625,7 +630,7 @@ class webhookModel extends model } $contentType = "Content-Type: {$webhook->contentType};charset=utf-8"; - if($webhook->type == 'dinggroup' or $webhook->type == 'wechatgroup') $contentType = "Content-Type: application/json"; + if($webhook->type == 'dinggroup' or $webhook->type == 'wechatgroup' or $webhook->type == 'feishugroup') $contentType = "Content-Type: application/json"; $header[] = $contentType; $url = $webhook->url; @@ -636,6 +641,19 @@ class webhookModel extends model $sign = urlencode(base64_encode(hash_hmac('sha256', $sign, $webhook->secret, true))); $url .= "×tamp={$timestamp}&sign={$sign}"; } + if($webhook->type == 'feishugroup' and $webhook->secret) + { + $timestamp = time(); + $sign = $timestamp . "\n" . $webhook->secret; + $sign = base64_encode(hash_hmac('sha256', '', $sign, true)); + + $content = array(); + $content['timestamp'] = $timestamp; + $content['sign'] = $sign; + $content['msg_type'] = 'text'; + $content['content'] = json_decode($sendData); + $sendData = json_encode($content); + } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); diff --git a/module/webhook/view/bind.html.php b/module/webhook/view/bind.html.php index 06dd4f076a..527294ed5d 100644 --- a/module/webhook/view/bind.html.php +++ b/module/webhook/view/bind.html.php @@ -13,7 +13,7 @@ type == 'dinguser') echo $lang->webhook->dingUserid; if($webhook->type == 'wechatuser') echo $lang->webhook->wechatUserid; - if($webhook->type == 'feishu') echo $lang->webhook->feishuUserid; + if($webhook->type == 'feishuuser') echo $lang->webhook->feishuUserid; ?> actions;?> @@ -21,7 +21,7 @@ type == 'dinguser') echo $lang->webhook->dingBindStatus; if($webhook->type == 'wechatuser') echo $lang->webhook->wechatBindStatus; - if($webhook->type == 'feishu') echo $lang->webhook->feishuBindStatus; + if($webhook->type == 'feishuuser') echo $lang->webhook->feishuBindStatus; ?> diff --git a/module/webhook/view/browse.html.php b/module/webhook/view/browse.html.php index c2cf7d00a9..e6056a9bfe 100644 --- a/module/webhook/view/browse.html.php +++ b/module/webhook/view/browse.html.php @@ -35,7 +35,7 @@ type == 'dinguser') common::printIcon('webhook', 'chooseDept', "webhookID=$id", '', 'list', 'link'); - if($webhook->type == 'wechatuser' or $webhook->type == 'feishu') common::printIcon('webhook', 'bind', "webhookID=$id", '', 'list', 'link'); + if($webhook->type == 'wechatuser' or $webhook->type == 'feishuuser') common::printIcon('webhook', 'bind', "webhookID=$id", '', 'list', 'link'); common::printIcon('webhook', 'log', "webhookID=$id", '', 'list', 'file-text'); common::printIcon('webhook', 'edit', "webhookID=$id", '', 'list'); if(common::hasPriv('webhook', 'delete')) diff --git a/module/webhook/view/create.html.php b/module/webhook/view/create.html.php index c950cfa845..65bfb23dff 100644 --- a/module/webhook/view/create.html.php +++ b/module/webhook/view/create.html.php @@ -70,7 +70,6 @@ webhook->feishuAppId;?> - webhook->note->dingHelp;?> webhook->feishuAppSecret;?> diff --git a/module/webhook/view/edit.html.php b/module/webhook/view/edit.html.php index 497fc79b66..efba582f64 100644 --- a/module/webhook/view/edit.html.php +++ b/module/webhook/view/edit.html.php @@ -29,7 +29,7 @@ name, "class='form-control'");?> - '> + '> webhook->url;?> url, "class='form-control'");?> webhook->note->typeList, $webhook->type, '');?> @@ -74,7 +74,7 @@ - type == 'feishu'):?> + type == 'feishuuser'):?> secret);?> webhook->feishuAppId;?> @@ -91,7 +91,7 @@ domain, "class='form-control'");?> - type) === false):?> + type) === false):?> webhook->sendType;?> webhook->sendTypeList, $webhook->sendType, "class='form-control'");?>