246 lines
7.0 KiB
PHP
246 lines
7.0 KiB
PHP
<?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\Db as Db;
|
|
|
|
/**
|
|
* 智能客服 控制器
|
|
*/
|
|
class Kefu extends BaseShop
|
|
{
|
|
/**
|
|
* 智能客服默认页面
|
|
* @return \think\response\View
|
|
*/
|
|
public function index()
|
|
{
|
|
$kefu_config_model = new KefuConfigModel();
|
|
$config_info = $kefu_config_model->getConfig($this->site_id, $this->app_module)["data"]["value"] ?? [];
|
|
$this->assign("config_info", $config_info);
|
|
return $this->fetch("kefu/index");
|
|
}
|
|
|
|
/**
|
|
* 智能客服配置页
|
|
* @return \think\response\View|\think\response\Json
|
|
*/
|
|
public function config()
|
|
{
|
|
$kefu_config_model = new KefuConfigModel();
|
|
|
|
if (request()->isJson()) {
|
|
$api_key = input("api_key/s", "");//Dify API密钥
|
|
$base_url = input("base_url/s", "https://api.dify.ai/v1");//API基础地址
|
|
$chat_endpoint = input("chat_endpoint/s", "/chat-messages");//聊天接口端点
|
|
$status = input("status/d", 0);//状态
|
|
|
|
$data = array(
|
|
"api_key" => $api_key,
|
|
"base_url" => $base_url,
|
|
"chat_endpoint" => $chat_endpoint,
|
|
"status" => $status
|
|
);
|
|
$result = $kefu_config_model->setConfig($data, $this->site_id, $this->app_module);
|
|
return $result;
|
|
} else {
|
|
$config_info = $kefu_config_model->getConfig($this->site_id, $this->app_module)['data']['value'] ?? [];
|
|
$this->assign("config_info", $config_info);
|
|
return $this->fetch("kefu/config");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 会话管理列表
|
|
* @return \think\response\View
|
|
*/
|
|
public function conversation()
|
|
{
|
|
return $this->fetch("kefu/conversation");
|
|
}
|
|
|
|
/**
|
|
* 获取会话列表
|
|
* @return \think\response\Json
|
|
*/
|
|
public function getConversationList()
|
|
{
|
|
$page = input("page/d", 1);
|
|
$limit = input("limit/d", 10);
|
|
$user_id = input("user_id/s", "");
|
|
$status = input("status/s", "");
|
|
|
|
$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);
|
|
|
|
// 适配layui table的返回格式
|
|
$result = [
|
|
'code' => 0, // layui table要求成功状态码为0
|
|
'msg' => '获取会话列表成功',
|
|
'count' => $conversation_list['total'], // 总记录数
|
|
'data' => $conversation_list['data'] // 数据列表
|
|
];
|
|
|
|
return json($result);
|
|
}
|
|
|
|
/**
|
|
* 获取会话信息
|
|
* @return \think\response\Json
|
|
*/
|
|
public function getConversationInfo()
|
|
{
|
|
$conversation_id = input("conversation_id/s", "");
|
|
|
|
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('获取会话信息成功', null, $conversation_info);
|
|
}
|
|
|
|
/**
|
|
* 结束会话
|
|
* @return \think\response\Json
|
|
*/
|
|
public function endConversation()
|
|
{
|
|
$id = input("id/d", "");
|
|
|
|
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('会话已结束', null, $result);
|
|
}
|
|
|
|
/**
|
|
* 删除会话
|
|
* @return \think\response\Json
|
|
*/
|
|
public function deleteConversation()
|
|
{
|
|
$id = input("id/d", "");
|
|
|
|
if (empty($id)) {
|
|
return $this->error('会话ID不能为空');
|
|
}
|
|
|
|
$kefu_conversation_model = new KefuConversationModel();
|
|
$kefu_message_model = new KefuMessageModel();
|
|
|
|
// 开启事务
|
|
Db::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]
|
|
]);
|
|
|
|
// 提交事务
|
|
Db::commit();
|
|
|
|
return $this->success('会话已删除', null, $result);
|
|
} catch (\Exception $e) {
|
|
// 回滚事务
|
|
Db::rollback();
|
|
return $this->error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 消息管理列表
|
|
* @return \think\response\View
|
|
*/
|
|
public function message()
|
|
{
|
|
$conversation_id = input("conversation_id/s", "");
|
|
$this->assign("conversation_id", $conversation_id);
|
|
return $this->fetch("kefu/message");
|
|
}
|
|
|
|
/**
|
|
* 获取消息列表
|
|
* @return \think\response\Json
|
|
*/
|
|
public function getMessageList()
|
|
{
|
|
$page = input("page/d", 1);
|
|
$limit = input("limit/d", 50);
|
|
$conversation_id = input("conversation_id/s", "");
|
|
|
|
$kefu_message_model = new KefuMessageModel();
|
|
$condition = [
|
|
['site_id', '=', $this->site_id]
|
|
];
|
|
|
|
// 只有当会话ID不为空时才添加会话ID条件
|
|
if (!empty($conversation_id)) {
|
|
$condition[] = ['conversation_id', '=', $conversation_id];
|
|
}
|
|
|
|
$message_list = $kefu_message_model->getMessageList($condition, '*', 'create_time asc', $page, $limit);
|
|
// 适配layui table的返回格式
|
|
$result = [
|
|
'code' => 0, // layui table要求成功状态码为0
|
|
'msg' => '获取消息列表成功',
|
|
'count' => $message_list['total'], // 总记录数
|
|
'data' => $message_list['data'] // 数据列表
|
|
];
|
|
|
|
return json($result);
|
|
}
|
|
}
|