Files
fdesk/app/common/utils/Feishu.php
T
mzl92 3d8d9f841a Merge branch 'feature/mzl_dev' into develop
# Conflicts:
#	app/functions.php
#	config/middleware.php
2025-07-09 19:11:57 +08:00

228 lines
6.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace app\common\utils;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use support\Cache;
use Webman\Exception\BusinessException;
class Feishu
{
protected $appId = '';
protected $appSecret = '';
protected $baseUri = '';
protected $client = null;
protected $options = [];
protected $error = '';
protected $abbr = 'hesu';
/**
* 初始化
*/
public function __construct(string $appId = '', string $appSecret = '', string $abbr = 'hesu')
{
if ($abbr === 'mbaf') {
$this->baseUri = 'https://open.larksuite.com/open-apis/';
} else {
$this->baseUri = 'https://open.feishu.cn/open-apis/';
}
$this->appId = $appId;
$this->appSecret = $appSecret;
$this->abbr = $abbr;
$this->client = new Client([
'base_uri' => $this->baseUri
]);
$this->options = [
'headers' => [
'Content-Type' => 'application/json; charset=utf-8'
]
];
}
/**
* 异常
*/
public function error()
{
return $this->error;
}
/**
* 请求
*/
public function requestResult($url = "", $method = "POST")
{
try {
$res = $this->client->request($method, $this->baseUri.$url, $this->options);
if ($res->getBody()) {
$result = json_decode($res->getBody(), true);
// var_dump($result);
if ($result["code"] === 0) {
return $result;
} else {
$this->error = $result["msg"];
return false;
}
} else {
$this->error = "result error";
return false;
}
} catch (RequestException $e) {
if ($e->hasResponse()) {
var_dump('2 :'.$e->getResponse()->getBody());
throw new BusinessException($e->getResponse()->getBody());
} else {
var_dump('3 :'.$e->getMessage());
throw new BusinessException($e->getMessage());
}
} catch (\Throwable $th) {
var_dump('4 :'.$th->getFile()."|".$th->getLine()."|".$th->getMessage());
$this->error = $th->getFile()."|".$th->getLine()."|".$th->getMessage();
return false;
}
}
/**
* 授权
*/
public function authorize($certificate, $url = 'authen/v1/authorize')
{
$arr = [
'app_id' => $certificate['app_id'],
'redirect_uri' => $certificate['r_url'],
'scope' => $certificate['scope'],
'state' => $certificate['state']
];
$queryData = http_build_query($arr);
return $this->baseUri.$url.'?'.$queryData;
}
/**
* 自建应用获取 tenant_access_token
*/
public function tenantAccessToken()
{
$tenantAccessToken = Cache::get($this->abbr.'-tenantAccessToken');
if (!$tenantAccessToken) {
$this->options["json"] = [
"app_id" => $this->appId,
"app_secret" => $this->appSecret
];
$rst = $this->requestResult('auth/v3/tenant_access_token/internal', 'POST');
if ($rst) {
$tenantAccessToken = $rst['tenant_access_token'];
Cache::set($this->abbr.'-tenantAccessToken', $tenantAccessToken, $rst['expire']);
} else {
throw new BusinessException('tenant_access_token 获取失败');
}
}
return $tenantAccessToken;
}
/**
* 通过手机号或邮箱获取用户 ID
*/
public function getUserIdByPhoneOrEmail($phone = '', $userIdType = 'open_id')
{
$this->options["headers"] = [
"Authorization" => "Bearer " . $this->tenantAccessToken(),
"Content-Type" => "application/json; charset=utf-8"
];
$this->options["json"] = [
"emails" => [],
"mobiles" => [$phone],
"include_resigned" => false
];
$rst = $this->requestResult('contact/v3/users/batch_get_id?user_id_type='.$userIdType, 'POST');
return $rst;
}
/**
* 发送消息
*/
public function sendMessages($receiveId = '', $content = '', $msgType = 'text', $receiveIdtype = 'open_id')
{
$this->options["headers"] = [
"Authorization" => "Bearer " . $this->tenantAccessToken(),
"Content-Type" => "application/json; charset=utf-8"
];
$this->options["json"] = [
"receive_id" => $receiveId,
"msg_type" => $msgType,
"content" => $content
];
$rst = $this->requestResult('im/v1/messages?receive_id_type='.$receiveIdtype, 'POST');
return $rst;
}
/**
* 编辑消息
*/
public function updateSendMessages($messageId = '', $content = '', $msgType = 'text')
{
$this->options["headers"] = [
"Authorization" => "Bearer " . $this->tenantAccessToken(),
"Content-Type" => "application/json; charset=utf-8"
];
$this->options["json"] = [
"msg_type" => $msgType,
"content" => $content
];
$rst = $this->requestResult('im/v1/messages/'.$messageId, 'PUT');
return $rst;
}
/**
* 用户 user_access_token
*/
public function userAccessToken($code = "", $redirect_uri = "")
{
$this->options["headers"] = [
"Content-Type" => "application/json; charset=utf-8"
];
$this->options["json"] = [
"grant_type" => "authorization_code",
"client_id" => $this->appId,
"client_secret" => $this->appSecret,
"code" => $code,
"redirect_uri" => $redirect_uri
];
return $this->requestResult('authen/v2/oauth/token');
}
/**
* 用户信息
*/
public function userInfo($userAccessToken = "")
{
$this->options["headers"] = [
"Authorization" => "Bearer " . $userAccessToken,
"Content-Type" => "application/json; charset=utf-8"
];
return $this->requestResult('authen/v1/user_info', "GET");
}
// public function user_access_token($code = "")
// {
// $url = "";
// $this->options["headers"] = [
// "Authorization" => "Bearer ".$this->tenantAccessToken(),
// "Content-Type" => "application/json; charset=utf-8"
// ];
// $this->options["json"] = [
// "grant_type" => "authorization_code",
// "code" => $code
// ];
// $this->options["form_params"] = [
// "app_id" => $this->app_id,
// "app_secret" => $this->app_secret
// ];
// return $this->requestResult($url);
// }
}