test(addon/aikefu): 还原原先的kefu.app 方便测试
This commit is contained in:
187
src/app/api/controller/Kefu.php
Normal file
187
src/app/api/controller/Kefu.php
Normal file
@@ -0,0 +1,187 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
use app\api\controller\BaseApi;
|
||||
use think\facade\Event;
|
||||
|
||||
/**
|
||||
* 智能客服API控制器
|
||||
*/
|
||||
class Kefu extends BaseApi
|
||||
{
|
||||
/**
|
||||
* 智能客服聊天接口
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function chat()
|
||||
{
|
||||
// 获取请求参数
|
||||
$message = $this->params['message'] ?? '';
|
||||
$user_id = $this->params['user_id'] ?? $this->member_id;
|
||||
$conversation_id = $this->params['conversation_id'] ?? '';
|
||||
$stream = $this->params['stream'] ?? false;
|
||||
|
||||
// (可选)获取站点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;
|
||||
|
||||
// 验证参数
|
||||
if (empty($message)) {
|
||||
return $this->response($this->error('请输入消息内容'));
|
||||
}
|
||||
|
||||
try {
|
||||
// 准备事件数据
|
||||
$event_data = [
|
||||
'message' => $message,
|
||||
'user_id' => $user_id,
|
||||
'conversation_id' => $conversation_id,
|
||||
'stream' => $stream,
|
||||
'site_id' =>$site_id,
|
||||
'member_id' => $member_id,
|
||||
'token' => $token,
|
||||
];
|
||||
|
||||
// 触发智能客服聊天事件
|
||||
$result = Event::trigger('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']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->response($response);
|
||||
} 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;
|
||||
|
||||
// (可选)获取站点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::trigger('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
|
||||
*/
|
||||
public function getHistory()
|
||||
{
|
||||
// 获取请求参数
|
||||
$conversation_id = $this->params['conversation_id'] ?? '';
|
||||
$user_id = $this->params['user_id'] ?? $this->member_id;
|
||||
$limit = $this->params['limit'] ?? 20;
|
||||
$offset = $this->params['offset'] ?? 0;
|
||||
|
||||
// (可选)获取站点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;
|
||||
|
||||
// 验证参数
|
||||
if (empty($conversation_id)) {
|
||||
return $this->response($this->error('会话ID不能为空'));
|
||||
}
|
||||
|
||||
try {
|
||||
// 准备事件数据
|
||||
$event_data = [
|
||||
'conversation_id' => $conversation_id,
|
||||
'user_id' => $user_id,
|
||||
'limit' => $limit,
|
||||
'offset' => $offset,
|
||||
'site_id' =>$site_id,
|
||||
'member_id' => $member_id,
|
||||
'token' => $token,
|
||||
];
|
||||
|
||||
// 触发获取历史消息事件
|
||||
$result = Event::trigger('KefuGetHistory', $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()));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user