chore(addon/kefu):优化代码
This commit is contained in:
@@ -370,33 +370,15 @@ class Kefu extends BaseApi
|
|||||||
header('X-Accel-Buffering: no'); // 禁用Nginx缓冲
|
header('X-Accel-Buffering: no'); // 禁用Nginx缓冲
|
||||||
|
|
||||||
// 触发事件,让监听器处理流式响应
|
// 触发事件,让监听器处理流式响应
|
||||||
event('KefuChat', $event_data);
|
event('KefuChat', $event_data, true);
|
||||||
// 流式响应需要终止脚本执行,避免输出额外内容导致EventSource错误
|
// 流式响应需要终止脚本执行,避免输出额外内容导致EventSource错误
|
||||||
exit;
|
exit;
|
||||||
} else {
|
} else {
|
||||||
// 触发智能客服聊天事件(非流式)
|
// 触发智能客服聊天事件(非流式)
|
||||||
$result = event('KefuChat', $event_data);
|
$result = event('KefuChat', $event_data, true);
|
||||||
|
|
||||||
// 处理事件结果
|
// 处理事件结果
|
||||||
$response = [
|
return $this->response($result);
|
||||||
'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);
|
|
||||||
}
|
}
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
return $this->response($this->error('请求失败:' . $e->getMessage()));
|
return $this->response($this->error('请求失败:' . $e->getMessage()));
|
||||||
@@ -426,8 +408,8 @@ class Kefu extends BaseApi
|
|||||||
$token = $this->params['token'] ?? $this->token;
|
$token = $this->params['token'] ?? $this->token;
|
||||||
|
|
||||||
// 验证参数(conversation_id 和 user_id 至少需要一个)
|
// 验证参数(conversation_id 和 user_id 至少需要一个)
|
||||||
if (empty($conversation_id) && empty($user_id)) {
|
if (empty($conversation_id)) {
|
||||||
return $this->response($this->error('会话ID或用户ID不能为空'));
|
return $this->response($this->error('会话ID不能为空'));
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -446,25 +428,7 @@ class Kefu extends BaseApi
|
|||||||
$result = event('KefuGetHistory', $event_data, true);
|
$result = event('KefuGetHistory', $event_data, true);
|
||||||
|
|
||||||
// 处理事件结果
|
// 处理事件结果
|
||||||
$response = [
|
return $this->response($result);
|
||||||
'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);
|
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
return $this->response($this->error('请求失败:' . $e->getMessage()));
|
return $this->response($this->error('请求失败:' . $e->getMessage()));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user