feat(addon/aikefu): 新增AI智能客服插件

This commit is contained in:
2025-12-06 10:09:08 +08:00
parent 8da4563435
commit 8ceb252d79
17 changed files with 2043 additions and 0 deletions

View File

@@ -0,0 +1,222 @@
<?php
namespace addon\aikefu\shop\controller;
use addon\aikefu\model\Config as KefuConfigModel;
use addon\aikefu\model\Conversation as KefuConversationModel;
use addon\aikefu\model\Message as KefuMessageModel;
use app\shop\controller\BaseShop;
use think\facade\View;
class Kefu extends BaseShop
{
/**
* 智能客服配置页
* @return \think\response\View
*/
public function config()
{
$kefu_config_model = new KefuConfigModel();
$config_info = $kefu_config_model->getConfigInfo([['site_id', '=', $this->site_id]]);
View::assign('config_info', $config_info);
return View::fetch('kefu/config');
}
/**
* 保存智能客服配置
* @return \think\response\Json
*/
public function saveConfig()
{
$params = $this->request->post();
$kefu_config_model = new KefuConfigModel();
$data = [
'api_key' => $params['api_key'] ?? '',
'base_url' => $params['base_url'] ?? 'https://api.dify.ai/v1',
'chat_endpoint' => $params['chat_endpoint'] ?? '/chat-messages',
'status' => $params['status'] ?? 0,
];
$result = $kefu_config_model->setConfig($data, $this->site_id);
return $this->success($result);
}
/**
* 会话管理列表
* @return \think\response\View
*/
public function conversation()
{
return View::fetch('kefu/conversation');
}
/**
* 获取会话列表
* @return \think\response\Json
*/
public function getConversationList()
{
$params = $this->request->post();
$page = $params['page'] ?? 1;
$limit = $params['limit'] ?? 10;
$user_id = $params['user_id'] ?? '';
$status = $params['status'] ?? '';
$kefu_conversation_model = new KefuConversationModel();
$condition = [['site_id', '=', $this->site_id]];
if (!empty($user_id)) {
$condition[] = ['user_id', '=', $user_id];
}
if ($status !== '') {
$condition[] = ['status', '=', $status];
}
$conversation_list = $kefu_conversation_model->getConversationList($condition, '*', 'update_time desc', $page, $limit);
return $this->success($conversation_list);
}
/**
* 获取会话信息
* @return \think\response\Json
*/
public function getConversationInfo()
{
$params = $this->request->post();
$conversation_id = $params['conversation_id'] ?? '';
if (empty($conversation_id)) {
return $this->error('会话ID不能为空');
}
$kefu_conversation_model = new KefuConversationModel();
$conversation_info = $kefu_conversation_model->getConversationInfo([
['site_id', '=', $this->site_id],
['conversation_id', '=', $conversation_id]
]);
if (empty($conversation_info)) {
return $this->error('会话不存在');
}
return $this->success($conversation_info);
}
/**
* 结束会话
* @return \think\response\Json
*/
public function endConversation()
{
$params = $this->request->post();
$id = $params['id'] ?? '';
if (empty($id)) {
return $this->error('会话ID不能为空');
}
$kefu_conversation_model = new KefuConversationModel();
$result = $kefu_conversation_model->updateConversation(
['status' => 0],
[
['id', '=', $id],
['site_id', '=', $this->site_id]
]
);
return $this->success($result);
}
/**
* 删除会话
* @return \think\response\Json
*/
public function deleteConversation()
{
$params = $this->request->post();
$id = $params['id'] ?? '';
if (empty($id)) {
return $this->error('会话ID不能为空');
}
$kefu_conversation_model = new KefuConversationModel();
$kefu_message_model = new KefuMessageModel();
// 开启事务
$this->model->startTrans();
try {
// 删除会话关联的消息
$conversation_info = $kefu_conversation_model->getConversationInfo([
['id', '=', $id],
['site_id', '=', $this->site_id]
]);
if (!empty($conversation_info)) {
$kefu_message_model->deleteMessage([
['site_id', '=', $this->site_id],
['conversation_id', '=', $conversation_info['conversation_id']]
]);
}
// 删除会话
$result = $kefu_conversation_model->deleteConversation([
['id', '=', $id],
['site_id', '=', $this->site_id]
]);
// 提交事务
$this->model->commit();
return $this->success($result);
} catch (\Exception $e) {
// 回滚事务
$this->model->rollback();
return $this->error($e->getMessage());
}
}
/**
* 消息管理列表
* @return \think\response\View
*/
public function message()
{
$conversation_id = $this->request->param('conversation_id') ?? '';
View::assign('conversation_id', $conversation_id);
return View::fetch('kefu/message');
}
/**
* 获取消息列表
* @return \think\response\Json
*/
public function getMessageList()
{
$params = $this->request->post();
$page = $params['page'] ?? 1;
$limit = $params['limit'] ?? 50;
$conversation_id = $params['conversation_id'] ?? '';
if (empty($conversation_id)) {
return $this->error('会话ID不能为空');
}
$kefu_message_model = new KefuMessageModel();
$condition = [
['site_id', '=', $this->site_id],
['conversation_id', '=', $conversation_id]
];
$message_list = $kefu_message_model->getMessageList($condition, '*', 'create_time asc', $page, $limit);
return $this->success($message_list);
}
}