refactor: 重构飞书授权代码,提取公共工具类

1. 创建 app/util/HttpClient.php
   - 公共 HTTP 请求类
   - httpGet() 和 httpPost() 方法
   - 支持自定义 headers 和 timeout
   - 统一错误处理和日志

2. 创建 app/util/FeishuUtil.php
   - 飞书授权工具类,可被其他业务模块复用
   - getAppAccessToken() - 获取应用访问令牌(带缓存)
   - getUserAccessToken() - 获取用户访问令牌
   - getUserInfo() - 获取用户信息
   - getCompleteUserInfo() - 一步获取完整用户信息
   - clearTokenCache() - 清除 token 缓存

3. 重构 app/service/FeishuService.php
   - 使用新的工具类
   - 移除重复的 HTTP 请求方法
   - 保持向后兼容

飞书授权流程:
1. app_access_token (应用级令牌)
2. user_access_token (用户级令牌,需要 app_access_token)
3. 用户信息 (需要 user_access_token)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
root
2026-02-26 10:43:29 +08:00
parent b1d58ece61
commit 6899bad529
5 changed files with 398 additions and 145 deletions
+165
View File
@@ -0,0 +1,165 @@
<?php
namespace app\util;
use Exception;
/**
* HTTP 请求工具类
* 提供简单的 HTTP GET 和 POST 请求方法
*/
class HttpClient
{
private static int $defaultTimeout = 30;
/**
* 发送 HTTP GET 请求
*
* @param string $url 请求URL
* @param array $params 查询参数
* @param array $headers 请求头
* @param int $timeout 超时时间(秒)
* @return array 响应数据(JSON解码后的数组)
* @throws Exception
*/
public static function httpGet(
string $url,
array $params = [],
array $headers = [],
int $timeout = 0
): array {
if (!empty($params)) {
$url .= '?' . http_build_query($params);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout ?: self::$defaultTimeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if (!empty($headers)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
self::logRequest('GET', $url, [], $response, $httpCode);
if ($error) {
throw new Exception("HTTP GET 请求失败: {$error}");
}
if ($httpCode !== 200) {
throw new Exception("HTTP GET 请求失败: HTTP {$httpCode}, Response: {$response}");
}
$result = json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new Exception('JSON 解析失败: ' . $response);
}
return $result;
}
/**
* 发送 HTTP POST 请求
*
* @param string $url 请求URL
* @param array $data 请求数据
* @param array $headers 请求头
* @param int $timeout 超时时间(秒)
* @return array 响应数据(JSON解码后的数组)
* @throws Exception
*/
public static function httpPost(
string $url,
array $data = [],
array $headers = [],
int $timeout = 0
): array {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout ?: self::$defaultTimeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$defaultHeaders = [
'Content-Type: application/json',
];
curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge($defaultHeaders, $headers));
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
self::logRequest('POST', $url, $data, $response, $httpCode);
if ($error) {
throw new Exception("HTTP POST 请求失败: {$error}");
}
if ($httpCode !== 200) {
throw new Exception("HTTP POST 请求失败: HTTP {$httpCode}, Response: {$response}");
}
$result = json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new Exception('JSON 解析失败: ' . $response);
}
return $result;
}
/**
* 记录请求日志
*
* @param string $method 请求方法
* @param string $url 请求URL
* @param array $requestData 请求数据
* @param string $response 响应数据
* @param int $httpCode HTTP状态码
*/
private static function logRequest(
string $method,
string $url,
array $requestData,
string $response,
int $httpCode
): void {
$emoji = $method === 'GET' ? '🌐' : '📤';
$methodLower = strtolower($method);
$logMessage = "{$emoji} HTTP {$method} {$url} -> HTTP {$httpCode}";
error_log($logMessage);
if (!empty($requestData)) {
error_log("📤 Request: " . json_encode($requestData));
}
error_log("📥 Response: {$response}");
}
/**
* 设置默认超时时间
*
* @param int $timeout 超时时间(秒)
*/
public static function setDefaultTimeout(int $timeout): void
{
self::$defaultTimeout = $timeout;
}
}