chore(addon/kefu):优化代码
This commit is contained in:
@@ -20,22 +20,22 @@ class Kefu extends BaseApi
|
||||
if (!request()->isPost()) {
|
||||
return $this->response($this->error('请求方式错误,仅支持POST请求'));
|
||||
}
|
||||
|
||||
|
||||
$start_time = microtime(true);
|
||||
|
||||
// (必选) 获取站点ID和会员ID,可以通过事件数据传递
|
||||
$site_id = $this->params['uniacid'] ?? $this->site_id; // 使用 uniacid, 方便以后迁移,而且uniacid 是唯一的, site_id 不是,同时被params给过滤了
|
||||
|
||||
|
||||
// 检查site_id 如果 < 1, 则返回错误
|
||||
if ($site_id < 1) {
|
||||
return $this->response($this->error('缺少关键参数站点ID'));
|
||||
}
|
||||
|
||||
|
||||
// 验证事件是否存在
|
||||
if (!hasEvent('KefuHealthCheck')) {
|
||||
return $this->response($this->error('智能客服插件未成功注册事件'));
|
||||
}
|
||||
|
||||
|
||||
// 准备事件数据
|
||||
$event_data = [
|
||||
'check_id' => uniqid('health_', true),
|
||||
@@ -55,7 +55,7 @@ class Kefu extends BaseApi
|
||||
try {
|
||||
// 触发健康检查事件
|
||||
$result = event('KefuHealthCheck', $event_data, true);
|
||||
|
||||
|
||||
// 汇总检查结果
|
||||
$health_summary = [
|
||||
'status' => 'healthy',
|
||||
@@ -69,13 +69,13 @@ class Kefu extends BaseApi
|
||||
'warnings' => [],
|
||||
'errors' => []
|
||||
];
|
||||
|
||||
|
||||
if (is_array($result) && !empty($result)) {
|
||||
foreach ($result as $check_result) {
|
||||
if (isset($check_result['component'])) {
|
||||
$health_summary['components'][$check_result['component']] = $check_result;
|
||||
$health_summary['total_checks']++;
|
||||
|
||||
|
||||
if ($check_result['status'] === 'healthy') {
|
||||
$health_summary['passed_checks']++;
|
||||
} else {
|
||||
@@ -93,10 +93,10 @@ class Kefu extends BaseApi
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 计算总体响应时间
|
||||
$health_summary['response_time_ms'] = round((microtime(true) - $start_time) * 1000, 2);
|
||||
|
||||
|
||||
// 如果没有任何检查结果,进行基础检查
|
||||
if ($health_summary['total_checks'] === 0) {
|
||||
$health_summary['components']['basic'] = [
|
||||
@@ -112,7 +112,7 @@ class Kefu extends BaseApi
|
||||
$health_summary['total_checks'] = 1;
|
||||
$health_summary['passed_checks'] = 1;
|
||||
}
|
||||
|
||||
|
||||
// 根据检查结果确定HTTP状态码
|
||||
$http_code = 200;
|
||||
if ($health_summary['status'] === 'unhealthy') {
|
||||
@@ -120,13 +120,13 @@ class Kefu extends BaseApi
|
||||
} elseif ($health_summary['status'] === 'warning') {
|
||||
$http_code = 200; // Still OK but with warnings
|
||||
}
|
||||
|
||||
|
||||
return $this->response([
|
||||
'code' => 0,
|
||||
'message' => $health_summary['status'],
|
||||
'data' => $health_summary
|
||||
], $http_code);
|
||||
|
||||
|
||||
} catch (\Exception $e) {
|
||||
return $this->response([
|
||||
'code' => -1,
|
||||
@@ -152,11 +152,11 @@ class Kefu extends BaseApi
|
||||
if (!request()->isPost()) {
|
||||
return $this->response($this->error('请求方式错误,仅支持POST请求'));
|
||||
}
|
||||
|
||||
|
||||
// (可选)获取站点ID和会员ID,可以通过事件数据传递
|
||||
$site_id = $this->params['uniacid'] ?? $this->site_id;
|
||||
$member_id = $this->params['member_id'] ?? $this->member_id;
|
||||
$token = $this->params['token'] ?? $this->token;
|
||||
$token = $this->params['token'] ?? $this->token;
|
||||
|
||||
// 验证事件是否存在
|
||||
if (!hasEvent('KefuGetInfo')) {
|
||||
@@ -178,14 +178,14 @@ class Kefu extends BaseApi
|
||||
|
||||
// 触发获取配置信息事件
|
||||
$result = event('KefuGetInfo', $event_data, true);
|
||||
|
||||
|
||||
// 处理事件结果
|
||||
$response = [
|
||||
'code' => 0,
|
||||
'message' => 'success',
|
||||
'data' => []
|
||||
];
|
||||
|
||||
|
||||
if (is_array($result) && !empty($result)) {
|
||||
foreach ($result as $res) {
|
||||
if (isset($res['code']) && $res['code'] < 0) {
|
||||
@@ -210,7 +210,7 @@ class Kefu extends BaseApi
|
||||
private function getClientIp()
|
||||
{
|
||||
$ip = '';
|
||||
|
||||
|
||||
if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
|
||||
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
|
||||
} elseif (isset($_SERVER['HTTP_X_REAL_IP']) && !empty($_SERVER['HTTP_X_REAL_IP'])) {
|
||||
@@ -218,13 +218,13 @@ class Kefu extends BaseApi
|
||||
} elseif (isset($_SERVER['REMOTE_ADDR']) && !empty($_SERVER['REMOTE_ADDR'])) {
|
||||
$ip = $_SERVER['REMOTE_ADDR'];
|
||||
}
|
||||
|
||||
|
||||
// 处理多个IP的情况(X-Forwarded-For可能包含多个IP)
|
||||
if (strpos($ip, ',') !== false) {
|
||||
$ips = explode(',', $ip);
|
||||
$ip = trim($ips[0]);
|
||||
}
|
||||
|
||||
|
||||
return $ip;
|
||||
}
|
||||
|
||||
@@ -237,7 +237,7 @@ class Kefu extends BaseApi
|
||||
if (!request()->isPost()) {
|
||||
return $this->response($this->error('请求方式错误,仅支持POST请求'));
|
||||
}
|
||||
|
||||
|
||||
// 获取请求参数
|
||||
$conversation_id = $this->params['conversation_id'] ?? '';
|
||||
$user_id = $this->params['user_id'] ?? $this->member_id;
|
||||
@@ -245,7 +245,7 @@ class Kefu extends BaseApi
|
||||
// (可选)获取站点ID和会员ID,可以通过事件数据传递
|
||||
$site_id = $this->params['uniacid'] ?? $this->site_id; // 使用 uniacid, 方便以后迁移,而且uniacid 是唯一的, site_id 不是,同时被params给过滤了
|
||||
$member_id = $this->params['member_id'] ?? $this->member_id;
|
||||
$token = $this->params['token'] ?? $this->token;
|
||||
$token = $this->params['token'] ?? $this->token;
|
||||
|
||||
// 验证参数(conversation_id 和 user_id 至少需要一个)
|
||||
if (empty($conversation_id) && empty($user_id)) {
|
||||
@@ -262,21 +262,21 @@ class Kefu extends BaseApi
|
||||
$event_data = [
|
||||
'conversation_id' => $conversation_id,
|
||||
'user_id' => $user_id,
|
||||
'site_id' =>$site_id,
|
||||
'site_id' => $site_id,
|
||||
'member_id' => $member_id,
|
||||
'token' => $token,
|
||||
];
|
||||
|
||||
// 触发清除会话事件
|
||||
$result = event('KefuClearConversation', $event_data, true);
|
||||
|
||||
|
||||
// 处理事件结果
|
||||
$response = [
|
||||
'code' => 0,
|
||||
'message' => 'success',
|
||||
'data' => []
|
||||
];
|
||||
|
||||
|
||||
if (is_array($result) && !empty($result)) {
|
||||
foreach ($result as $res) {
|
||||
if (isset($res['code']) && $res['code'] < 0) {
|
||||
@@ -294,7 +294,7 @@ class Kefu extends BaseApi
|
||||
return $this->response($this->error('请求失败:' . $e->getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 智能客服聊天接口
|
||||
* @return \think\response\Json|array|bool|string|void
|
||||
@@ -305,11 +305,11 @@ class Kefu extends BaseApi
|
||||
$isPost = request()->isPost();
|
||||
$isGet = request()->isGet();
|
||||
$isEventSource = $isGet && request()->header('Accept') === 'text/event-stream';
|
||||
|
||||
|
||||
if (!$isPost && !$isEventSource) {
|
||||
return $this->response($this->error('请求方式错误,仅支持POST或EventSource请求'));
|
||||
}
|
||||
|
||||
|
||||
// 获取请求参数
|
||||
// 对于GET请求,需要单独获取参数,因为BaseApi的构造函数可能没有正确处理GET参数
|
||||
// response_mode 响应模式:streaming(流式)、blocking(阻塞),默认streaming
|
||||
@@ -326,7 +326,7 @@ class Kefu extends BaseApi
|
||||
$stream = $this->params['stream'] ?? false;
|
||||
$response_mode = $this->params['response_mode'] ?? 'streaming';
|
||||
}
|
||||
|
||||
|
||||
// 确保stream参数正确处理字符串'false'和'0'
|
||||
if (is_string($stream)) {
|
||||
$stream = filter_var($stream, FILTER_VALIDATE_BOOLEAN);
|
||||
@@ -335,7 +335,7 @@ class Kefu extends BaseApi
|
||||
// (可选)获取站点ID和会员ID,可以通过事件数据传递
|
||||
$site_id = $this->params['uniacid'] ?? $this->site_id; // 使用 uniacid, 方便以后迁移,而且uniacid 是唯一的, site_id 不是,同时被params给过滤了
|
||||
$member_id = $this->params['member_id'] ?? $this->member_id;
|
||||
$token = $this->params['token'] ?? $this->token;
|
||||
$token = $this->params['token'] ?? $this->token;
|
||||
|
||||
// 验证参数
|
||||
if (empty($query)) {
|
||||
@@ -357,7 +357,7 @@ class Kefu extends BaseApi
|
||||
'user_id' => $user_id,
|
||||
'conversation_id' => $conversation_id,
|
||||
'stream' => $enable_stream,
|
||||
'site_id' =>$site_id,
|
||||
'site_id' => $site_id,
|
||||
'member_id' => $member_id,
|
||||
'token' => $token,
|
||||
];
|
||||
@@ -368,35 +368,17 @@ class Kefu extends BaseApi
|
||||
header('Cache-Control: no-cache');
|
||||
header('Connection: keep-alive');
|
||||
header('X-Accel-Buffering: no'); // 禁用Nginx缓冲
|
||||
|
||||
|
||||
// 触发事件,让监听器处理流式响应
|
||||
event('KefuChat', $event_data);
|
||||
event('KefuChat', $event_data, true);
|
||||
// 流式响应需要终止脚本执行,避免输出额外内容导致EventSource错误
|
||||
exit;
|
||||
} else {
|
||||
// 触发智能客服聊天事件(非流式)
|
||||
$result = event('KefuChat', $event_data);
|
||||
|
||||
// 处理事件结果
|
||||
$response = [
|
||||
'code' => 0,
|
||||
'message' => 'success',
|
||||
'data' => []
|
||||
];
|
||||
|
||||
if (is_array($result) && !empty($result)) {
|
||||
foreach ($result as $res) {
|
||||
if (isset($res['code']) && $res['code'] < 0) {
|
||||
$response = $res;
|
||||
break;
|
||||
}
|
||||
if (isset($res['data'])) {
|
||||
$response['data'] = array_merge($response['data'], $res['data']);
|
||||
}
|
||||
}
|
||||
}
|
||||
$result = event('KefuChat', $event_data, true);
|
||||
|
||||
return $this->response($response);
|
||||
// 处理事件结果
|
||||
return $this->response($result);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
return $this->response($this->error('请求失败:' . $e->getMessage()));
|
||||
@@ -413,7 +395,7 @@ class Kefu extends BaseApi
|
||||
if (!request()->isPost()) {
|
||||
return $this->response($this->error('请求方式错误,仅支持POST请求'));
|
||||
}
|
||||
|
||||
|
||||
// 获取请求参数
|
||||
$conversation_id = $this->params['conversation_id'] ?? '';
|
||||
$user_id = $this->params['user_id'] ?? $this->member_id;
|
||||
@@ -423,11 +405,11 @@ class Kefu extends BaseApi
|
||||
// (可选)获取站点ID和会员ID,可以通过事件数据传递
|
||||
$site_id = $this->params['uniacid'] ?? $this->site_id; // 使用 uniacid, 方便以后迁移,而且uniacid 是唯一的, site_id 不是,同时被params给过滤了
|
||||
$member_id = $this->params['member_id'] ?? $this->member_id;
|
||||
$token = $this->params['token'] ?? $this->token;
|
||||
$token = $this->params['token'] ?? $this->token;
|
||||
|
||||
// 验证参数(conversation_id 和 user_id 至少需要一个)
|
||||
if (empty($conversation_id) && empty($user_id)) {
|
||||
return $this->response($this->error('会话ID或用户ID不能为空'));
|
||||
if (empty($conversation_id)) {
|
||||
return $this->response($this->error('会话ID不能为空'));
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -437,34 +419,16 @@ class Kefu extends BaseApi
|
||||
'user_id' => $user_id,
|
||||
'limit' => $limit,
|
||||
'offset' => $offset,
|
||||
'site_id' =>$site_id,
|
||||
'site_id' => $site_id,
|
||||
'member_id' => $member_id,
|
||||
'token' => $token,
|
||||
];
|
||||
|
||||
// 触发获取历史消息事件
|
||||
$result = event('KefuGetHistory', $event_data, true);
|
||||
|
||||
// 处理事件结果
|
||||
$response = [
|
||||
'code' => 0,
|
||||
'message' => 'success',
|
||||
'data' => []
|
||||
];
|
||||
|
||||
if (is_array($result) && !empty($result)) {
|
||||
foreach ($result as $res) {
|
||||
if (isset($res['code']) && $res['code'] < 0) {
|
||||
$response = $res;
|
||||
break;
|
||||
}
|
||||
if (isset($res['data'])) {
|
||||
$response['data'] = array_merge($response['data'], $res['data']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->response($response);
|
||||
// 处理事件结果
|
||||
return $this->response($result);
|
||||
} catch (\Exception $e) {
|
||||
return $this->response($this->error('请求失败:' . $e->getMessage()));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user