diff --git a/lib/dingapi/dingapi.class.php b/lib/dingapi/dingapi.class.php index 238e1a084c..55747a7d65 100644 --- a/lib/dingapi/dingapi.class.php +++ b/lib/dingapi/dingapi.class.php @@ -193,6 +193,95 @@ class dingapi return false; } + /** + * 根据open_id列表,获取用户。 + * Batch get users by open_id list. + * + * @param array $userIdList + * @access public + * @return array + */ + public function batchGetUsers($userIdList) + { + $useridPairs = array(); + $userGroup = array_chunk($userIdList, 49); + foreach($userGroup as $userIdList) + { + $urls = array(); + foreach($userIdList as $userID) $urls[] = $this->apiUrl . "topapi/v2/user/get?access_token={$this->token}&userid={$userID}"; + $datas = $this->multiRequest($urls); + foreach($datas as $response) + { + $response = json_decode($response); + if(empty($response->result)) continue; + + $user = $response->result; + $useridPairs[$user->userid] = $user->name; + } + } + + return $useridPairs; + } + + /** + * Handle the concurrency of requests. + * + * @param array $urls + * @access public + * @return array + */ + public function multiRequest($urls) + { + $curl = curl_multi_init(); + $urlHandlers = array(); + $urlData = array(); + + /* Set request header information. */ + /* Initialize multiple request handles to one. */ + foreach($urls as $url) + { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + if(!isset($_SERVER['HTTPS']) or $_SERVER['HTTPS'] != 'on') + { + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); + } + + $urlHandlers[] = $ch; + curl_multi_add_handle($curl, $ch); + } + + $active = null; + do + { + $mrc = curl_multi_exec($curl, $active); + } + while($mrc == CURLM_CALL_MULTI_PERFORM); + + while($active and $mrc == CURLM_OK) + { + usleep(50000); + if(curl_multi_select($curl) != -1) + { + do + { + $mrc = curl_multi_exec($curl, $active); + } + while($mrc == CURLM_CALL_MULTI_PERFORM); + } + } + + foreach($urlHandlers as $index => $ch) + { + $urlData[$index] = curl_multi_getcontent($ch); + curl_multi_remove_handle($curl, $ch); + } + curl_multi_close($curl); + return $urlData; + } + /** * Check for errors. * diff --git a/lib/feishuapi/feishuapi.class.php b/lib/feishuapi/feishuapi.class.php index a04220fd4b..dea13fc487 100644 --- a/lib/feishuapi/feishuapi.class.php +++ b/lib/feishuapi/feishuapi.class.php @@ -441,6 +441,36 @@ class feishuapi return $depts; } + /** + * 根据open_id列表,获取用户。 + * Batch get users by open_id list. + * + * @param array $userIdList + * @access public + * @return array + */ + public function batchGetUsers($userIdList) + { + $openidPairs = array(); + $userGroup = array_chunk($userIdList, 49); + foreach($userGroup as $userIdList) + { + $urls = array(); + foreach($userIdList as $userID) $urls[] = $this->apiUrl . "contact/v3/users/{$userID}"; + $datas = $this->multiRequest($urls); + foreach($datas as $response) + { + $response = json_decode($response); + if(empty($response->data->user)) continue; + + $user = $response->data->user; + $openidPairs[$user->open_id] = $user->name; + } + } + + return $openidPairs; + } + /** * Check for errors. * diff --git a/module/webhook/control.php b/module/webhook/control.php index 0161fb81ee..f7073e169c 100644 --- a/module/webhook/control.php +++ b/module/webhook/control.php @@ -194,13 +194,17 @@ class webhook extends control $this->app->loadClass('pager', true); $pager = new pager($recTotal, $recPerPage, $pageID); + $users = $this->loadModel('user')->getByQuery('inside', '', $pager); + $bindedUsers = $this->webhook->getBoundUsers($id); + $useridPairs = $this->webhookZen->getUseridPairs($webhook, $users, $bindedUsers, $oauthUsers); + $this->view->title = $this->lang->webhook->bind; $this->view->webhook = $webhook; $this->view->oauthUsers = $oauthUsers; - $this->view->useridPairs = array_flip($oauthUsers); - $this->view->users = $this->loadModel('user')->getByQuery('inside', '', $pager); + $this->view->useridPairs = $useridPairs; + $this->view->users = $users; $this->view->pager = $pager; - $this->view->bindedUsers = $this->webhook->getBoundUsers($id); + $this->view->bindedUsers = $bindedUsers; $this->display(); } diff --git a/module/webhook/js/choosedept.ui.js b/module/webhook/js/choosedept.ui.js index 813ad2ac0e..f05fdcf3a5 100644 --- a/module/webhook/js/choosedept.ui.js +++ b/module/webhook/js/choosedept.ui.js @@ -1,36 +1,10 @@ -loadedDept = []; -window.loadChildDept = function(event, node) -{ - if(typeof(node.parentKey) == 'undefined') return; - - var tree = $('#deptList').zui('tree'); - var options = tree.options; - var departmentID = node.key; - if(loadedDept.includes(departmentID)) return; - - $.ajax( - { - type: "post", - url: feishuUrl, - data: {departmentID: departmentID}, - dataType: "json", - async: true, - success: function(jsonData) - { - options.items = buildTreeItems(jsonData, options.items); - tree.render(options); - } - }); - loadedDept.push(departmentID); -}; - window.buildTreeItems = function(deptTree, treeItems) { if(typeof(treeItems) == 'undefined') treeItems = []; for(i in deptTree) { let dept = deptTree[i]; - let treeItem = {key: dept.id, text: dept.name, onClick: loadChildDept}; + let treeItem = {key: dept.id, text: dept.name}; treeItems = appendItems(treeItems, treeItem, dept.pId); } return treeItems; diff --git a/module/webhook/zen.php b/module/webhook/zen.php index f8041fbe15..2e8d94c50c 100644 --- a/module/webhook/zen.php +++ b/module/webhook/zen.php @@ -45,4 +45,46 @@ class webhookZen extends webhook $this->view->selectedDepts = $selectedDepts; return $response; } + + /** + * 获取open_id键值对,并追加未查询出的open_id。 + * Get open_id and name pairs, and append no fetch oauth users. + * + * @param object $webhook + * @param array $users + * @param array $bindedUsers + * @param array $oauthUsers + * @access public + * @return array + */ + public function getUseridPairs(object $webhook, array $users, array $bindedUsers, array $oauthUsers): array + { + $useridPairs = array_flip($oauthUsers); + $noFetchOauth = array(); + foreach($users as $user) + { + if(isset($bindedUsers[$user->account])) $userid = $bindedUsers[$user->account]; + if(isset($oauthUsers[$user->realname])) $userid = $oauthUsers[$user->realname]; + if(!isset($userid)) continue; + if(!isset($useridPairs[$userid])) $noFetchOauth[$userid] = $userid; + } + + if($noFetchOauth) + { + if($webhook->type == 'dinguser') + { + $this->app->loadClass('dingapi', true); + $dingapi = new dingapi($webhook->secret->appKey, $webhook->secret->appSecret, $webhook->secret->agentId); + foreach($dingapi->batchGetUsers($noFetchOauth) as $userid => $name) $useridPairs[$userid] = $name; + } + elseif($webhook->type == 'feishuuser') + { + $this->app->loadClass('feishuapi', true); + $feishuApi = new feishuapi($webhook->secret->appId, $webhook->secret->appSecret); + foreach($feishuApi->batchGetUsers($noFetchOauth) as $openid => $name) $useridPairs[$openid] = $name; + } + } + + return $useridPairs; + } }