chore(addon/aikefu): API的响应结构现在与Dify API的标准格式保持一致,提高了代码的一致性和可维护性
This commit is contained in:
@@ -195,14 +195,8 @@ class Kefu extends BaseApi
|
||||
]);
|
||||
}
|
||||
|
||||
// 返回成功响应
|
||||
return $this->response($this->success([
|
||||
'conversation_id' => $result['conversation_id'] ?? '',
|
||||
'reply' => $result['answer'] ?? '',
|
||||
'message_id' => $result['message_id'] ?? '',
|
||||
'finish_reason' => $result['finish_reason'] ?? '',
|
||||
'usage' => $result['usage'] ?? [],
|
||||
]));
|
||||
// 返回成功响应,保持与Dify API一致的响应结构
|
||||
return $this->response($this->success($result));
|
||||
} catch (\Exception $e) {
|
||||
return $this->response($this->error('请求失败:' . $e->getMessage()));
|
||||
}
|
||||
@@ -234,80 +228,19 @@ class Kefu extends BaseApi
|
||||
['conversation_id', '=', $conversation_id],
|
||||
], 'id, role, content, create_time', 'create_time asc', $limit, $offset);
|
||||
|
||||
// 返回成功响应
|
||||
// 返回成功响应,保持与Dify API风格一致
|
||||
return $this->response($this->success([
|
||||
'messages' => $message_list['data'] ?? [],
|
||||
'total' => $message_list['total'] ?? 0,
|
||||
'limit' => $limit,
|
||||
'offset' => $offset,
|
||||
'page_info' => [
|
||||
'limit' => $limit,
|
||||
'offset' => $offset
|
||||
]
|
||||
]));
|
||||
} catch (\Exception $e) {
|
||||
return $this->response($this->error('请求失败:' . $e->getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建新会话
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function createConversation()
|
||||
{
|
||||
// 获取请求参数
|
||||
$user_id = $this->params['user_id'] ?? $this->member_id;
|
||||
|
||||
try {
|
||||
// 获取智能客服配置
|
||||
$kefu_config_model = new KefuConfigModel();
|
||||
$config_info = $kefu_config_model->getConfig($this->site_id)['data']['value'] ?? [];
|
||||
|
||||
if (empty($config_info) || $config_info['status'] != 1) {
|
||||
return $this->response($this->error('智能客服暂未启用'));
|
||||
}
|
||||
|
||||
$config = $config_info;
|
||||
$apiKey = $config['api_key'];
|
||||
$baseUrl = $config['base_url'];
|
||||
|
||||
// 构建请求数据
|
||||
$requestData = [
|
||||
'name' => '智能客服会话',
|
||||
'user' => $user_id,
|
||||
];
|
||||
|
||||
// 构建请求头
|
||||
$headers = [
|
||||
'Authorization: Bearer ' . $apiKey,
|
||||
'Content-Type: application/json',
|
||||
];
|
||||
|
||||
// 发送请求到Dify API
|
||||
$url = $baseUrl . '/conversations';
|
||||
$response = $this->curlRequest($url, 'POST', $requestData, $headers);
|
||||
|
||||
// 解析响应
|
||||
$result = json_decode($response, true);
|
||||
|
||||
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||
return $this->response($this->error('解析响应失败'));
|
||||
}
|
||||
|
||||
// 保存会话记录
|
||||
$kefu_conversation_model = new KefuConversationModel();
|
||||
$kefu_conversation_model->addConversation([
|
||||
'site_id' => $this->site_id,
|
||||
'user_id' => $user_id,
|
||||
'conversation_id' => $result['id'] ?? '',
|
||||
'name' => $result['name'] ?? '智能客服会话',
|
||||
]);
|
||||
|
||||
// 返回成功响应
|
||||
return $this->response($this->success([
|
||||
'conversation_id' => $result['id'] ?? '',
|
||||
'name' => $result['name'] ?? '',
|
||||
'created_at' => $result['created_at'] ?? '',
|
||||
]));
|
||||
} catch (\Exception $e) {
|
||||
return $this->response($this->error('请求失败:' . $e->getMessage()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,9 +14,7 @@ return [
|
||||
'KefuChat' => [
|
||||
'addon\aikefu\event\KefuChat'
|
||||
],
|
||||
'KefuCreateConversation' => [
|
||||
'addon\aikefu\event\KefuCreateConversation'
|
||||
],
|
||||
|
||||
'KefuGetHistory' => [
|
||||
'addon\aikefu\event\KefuGetHistory'
|
||||
],
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace addon\aikefu\event;
|
||||
|
||||
use addon\aikefu\api\controller\Kefu as KefuApi;
|
||||
|
||||
/**
|
||||
* 处理智能客服创建会话事件
|
||||
*/
|
||||
class KefuCreateConversation
|
||||
{
|
||||
|
||||
/**
|
||||
* 处理智能客服创建会话事件
|
||||
* @param array $data 事件数据
|
||||
* @return array
|
||||
*/
|
||||
public function handle($data)
|
||||
{
|
||||
try {
|
||||
// 创建addon的KefuApi实例
|
||||
$kefu_api = new KefuApi();
|
||||
|
||||
// 调用初始化方法设置属性
|
||||
$kefu_api->initializeForEvent($data);
|
||||
|
||||
// 调用addon的createConversation方法
|
||||
$response = $kefu_api->createConversation();
|
||||
|
||||
// 返回响应数据
|
||||
return json_decode($response->getContent(), true);
|
||||
} catch (\Exception $e) {
|
||||
return [
|
||||
'code' => -1,
|
||||
'message' => '创建会话失败:' . $e->getMessage(),
|
||||
'data' => []
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -42,13 +42,12 @@ class KefuGetInfo
|
||||
]
|
||||
],
|
||||
'endpoints' => [
|
||||
'chat' => '/api/kefu/chat',
|
||||
'chat_stream' => '/api/kefu/chatStream',
|
||||
'create_conversation' => '/api/kefu/createConversation',
|
||||
'get_history' => '/api/kefu/getHistory',
|
||||
'clear_conversation' => '/api/kefu/clearConversation',
|
||||
'health' => '/api/kefu/health',
|
||||
'info' => '/api/kefu/info'
|
||||
'chat' => '/api/kefu/chat',
|
||||
'chat_stream' => '/api/kefu/chatStream',
|
||||
'get_history' => '/api/kefu/getHistory',
|
||||
'clear_conversation' => '/api/kefu/clearConversation',
|
||||
'health' => '/api/kefu/health',
|
||||
'info' => '/api/kefu/info'
|
||||
],
|
||||
'client_info' => $client_info,
|
||||
'server_info' => [
|
||||
|
||||
@@ -91,12 +91,18 @@ class Kefu extends BaseShop
|
||||
|
||||
$conversation_list = $kefu_conversation_model->getConversationList($condition, '*', 'update_time desc', $page, $limit);
|
||||
|
||||
// 适配layui table的返回格式
|
||||
// 适配layui table的返回格式,同时保持与Dify API风格一致
|
||||
$result = [
|
||||
'code' => 0, // layui table要求成功状态码为0
|
||||
'msg' => '获取会话列表成功',
|
||||
'count' => $conversation_list['total'], // 总记录数
|
||||
'data' => $conversation_list['data'] // 数据列表
|
||||
'data' => [
|
||||
'conversations' => $conversation_list['data'], // 会话列表
|
||||
'page_info' => [
|
||||
'limit' => $limit,
|
||||
'offset' => ($page - 1) * $limit
|
||||
]
|
||||
] // 数据列表
|
||||
];
|
||||
|
||||
return json($result);
|
||||
@@ -242,12 +248,18 @@ class Kefu extends BaseShop
|
||||
}
|
||||
|
||||
$message_list = $kefu_message_model->getMessageList($condition, '*', 'create_time asc', $page, $limit);
|
||||
// 适配layui table的返回格式
|
||||
// 适配layui table的返回格式,同时保持与Dify API风格一致
|
||||
$result = [
|
||||
'code' => 0, // layui table要求成功状态码为0
|
||||
'msg' => '获取消息列表成功',
|
||||
'count' => $message_list['total'], // 总记录数
|
||||
'data' => $message_list['data'] // 数据列表
|
||||
'data' => [
|
||||
'messages' => $message_list['data'], // 消息列表
|
||||
'page_info' => [
|
||||
'limit' => $limit,
|
||||
'offset' => ($page - 1) * $limit
|
||||
]
|
||||
] // 数据列表
|
||||
];
|
||||
|
||||
return json($result);
|
||||
|
||||
@@ -224,7 +224,7 @@
|
||||
},
|
||||
dataType: 'json',
|
||||
success: function(res) {
|
||||
var messageList = res.data.list || [];
|
||||
var messageList = res.data.messages || [];
|
||||
var html = '';
|
||||
|
||||
if (messageList.length > 0) {
|
||||
|
||||
@@ -304,8 +304,8 @@
|
||||
dataType: 'json',
|
||||
success: function(res) {
|
||||
if (res.code === 0) {
|
||||
var list = res.data.list;
|
||||
total = res.data.total;
|
||||
var list = res.data.messages;
|
||||
total = res.count;
|
||||
|
||||
var html = '';
|
||||
if (list.length > 0) {
|
||||
|
||||
@@ -496,57 +496,6 @@ class Kefu extends BaseApi
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建新会话
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function createConversation()
|
||||
{
|
||||
// 获取请求参数
|
||||
$user_id = $this->params['user_id'] ?? $this->member_id;
|
||||
|
||||
// (可选)获取站点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;
|
||||
|
||||
try {
|
||||
// 准备事件数据
|
||||
$event_data = [
|
||||
'user_id' => $user_id,
|
||||
'site_id' =>$site_id,
|
||||
'member_id' => $member_id,
|
||||
'token' => $token,
|
||||
];
|
||||
|
||||
// 触发创建会话事件
|
||||
$result = event('KefuCreateConversation', $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']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->response($response);
|
||||
} catch (\Exception $e) {
|
||||
return $this->response($this->error('请求失败:' . $e->getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会话历史
|
||||
* @return \think\response\Json
|
||||
|
||||
Reference in New Issue
Block a user