* Finish task#57901.

This commit is contained in:
leiyong
2022-06-21 09:54:50 +00:00
parent 93a6442a55
commit 0feed34baa
6 changed files with 299 additions and 42 deletions

View File

@@ -150,30 +150,24 @@ class feishuapi
* @access public
* @return array
*/
public function getDeptTree()
public function getChildDeptTree($departmentID)
{
$depts = array('result' => 'success', 'data' => array());
/* Gets the enterprise name. */
$response = $this->queryAPI($this->apiUrl . "tenant/v2/tenant/query", '', array(CURLOPT_CUSTOMREQUEST => "GET"));
$company = array('id' => '1', 'pId' => '0', 'name' => $response->data->tenant->name, 'open' => 1);
$data = array($company);
/* Get depts by parent dept. */
$depts = array();
$pageToken = '';
$index = 0;
while(true)
{
$response = $this->queryAPI($this->apiUrl . "contact/v3/departments?parent_department_id=0" . ($pageToken ? "&page_token={$pageToken}" : '') . "&fetch_child=true&page_size=50", '', array(CURLOPT_CUSTOMREQUEST => "GET"));
$response = $this->queryAPI($this->apiUrl . "contact/v3/departments?parent_department_id={$departmentID}" . ($pageToken ? "&page_token={$pageToken}" : '') . "&fetch_child=true&page_size=50", '', array(CURLOPT_CUSTOMREQUEST => "GET"));
if(isset($response->data->items))
{
foreach($response->data->items as $key => $dept)
{
$depts[$index]['id'] = $dept->open_department_id;
$depts[$index]['pId'] = empty($dept->parent_department_id) ? 1 : $dept->parent_department_id;
$depts[$index]['name'] = $dept->name;
$depts[$index]['open'] = 1;
$index++;
$data[$index]['id'] = $dept->open_department_id;
$data[$index]['pId'] = empty($dept->parent_department_id) ? 1 : $dept->parent_department_id;
$data[$index]['name'] = $dept->name;
$data[$index]['open'] = 1;
}
}
@@ -181,10 +175,129 @@ class feishuapi
$pageToken = $response->data->page_token;
}
$depts['data'] = $data;
return $depts;
}
/**
* Get the first tier department.
*
* @access public
* @return array
*/
public function getDeptTree()
{
$depts = array('data' => array());
/* Gets the enterprise name. */
$response = $this->queryAPI($this->apiUrl . "tenant/v2/tenant/query", '', array(CURLOPT_CUSTOMREQUEST => "GET"));
$company = array('id' => '1', 'pId' => '0', 'name' => $response->data->tenant->name, 'open' => 1);
$depts = array($company);
$departmentIdList = $this->getScopes();
$urls = array();
foreach($departmentIdList as $departmentID) $urls[] = $this->apiUrl . "contact/v3/departments/{$departmentID}";
$datas = $this->multiRequest($urls);
foreach($datas as $index => $dept)
{
$index += 1;
$dept = json_decode($dept);
$memberCount = $dept->data->department->member_count;
$status = $dept->data->department->status->is_deleted;
$depts[$index]['id'] = $dept->data->department->open_department_id;
$depts[$index]['pId'] = empty($dept->data->department->parent_department_id) ? 1 : $dept->data->department->parent_department_id;
$depts[$index]['name'] = $dept->data->department->name;
$depts[$index]['open'] = 1;
}
return $depts;
}
/**
* Get the visible range of the application.
*
* @access public
* @return array
*/
public function getScopes()
{
$pageToken = '';
$departmentIdList = array();
while(true)
{
$response = $this->queryAPI($this->apiUrl . "contact/v3/scopes" . "?user_id_type=open_id&department_id_type=open_department_id&page_token={$pageToken}&page_size=100", '', array(CURLOPT_CUSTOMREQUEST => "GET"));
$departmentIds = isset($response->data->department_ids) ? $response->data->department_ids : array();
foreach($departmentIds as $id) $departmentIdList[] = $id;
if(!isset($response->data->page_token)) break;
$pageToken = $response->data->page_token;
}
return $departmentIdList;
}
/**
* Handle the concurrency of requests.
*
* @access public
* @return array
*/
public function multiRequest($urls)
{
$curl = curl_multi_init();
$urlHandlers = array();
$urlData = array();
/* Set request header information. */
$headers = array();
$headers[] = "Content-Type: application/json";
if($this->token) $headers[] = "Authorization:Bearer {$this->token}";
/* 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);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$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 && $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;
}
/**
* Send message
*