chore(addon/aikefu): 调整config的配置内容
This commit is contained in:
@@ -1,4 +1,9 @@
|
||||
<?php
|
||||
/**
|
||||
* 智能客服配置模型
|
||||
* 用于存储和管理智能客服的配置信息
|
||||
* 版本:1.0.0
|
||||
*/
|
||||
|
||||
namespace addon\aikefu\model;
|
||||
|
||||
@@ -20,15 +25,15 @@ class Config extends BaseModel
|
||||
public function setConfig($data, $site_id = 0, $app_module = 'shop')
|
||||
{
|
||||
$config = new ConfigModel();
|
||||
|
||||
|
||||
// 获取原始配置
|
||||
$original_config = $this->getConfig($site_id, $app_module)['data']['value'] ?? [];
|
||||
|
||||
|
||||
// 如果 API Key 为空或保持不变,则使用原始值
|
||||
if (isset($data['api_key']) && empty($data['api_key'])) {
|
||||
$data['api_key'] = $original_config['api_key'] ?? '';
|
||||
}
|
||||
|
||||
|
||||
$res = $config->setConfig($data, '智能客服配置', 1, [['site_id', '=', $site_id], ['app_module', '=', $app_module], ['config_key', '=', 'AIKEFU_CONFIG']]);
|
||||
return $res;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
<?php
|
||||
/**
|
||||
* 智能客服控制器
|
||||
*/
|
||||
|
||||
namespace addon\aikefu\shop\controller;
|
||||
|
||||
@@ -8,6 +11,9 @@ use addon\aikefu\model\Message as KefuMessageModel;
|
||||
use app\shop\controller\BaseShop;
|
||||
use think\facade\View;
|
||||
|
||||
/**
|
||||
* 智能客服 控制器
|
||||
*/
|
||||
class Kefu extends BaseShop
|
||||
{
|
||||
/**
|
||||
@@ -17,25 +23,25 @@ class Kefu extends BaseShop
|
||||
public function config()
|
||||
{
|
||||
$kefu_config_model = new KefuConfigModel();
|
||||
|
||||
if ($this->request->isJson()) {
|
||||
// 保存配置
|
||||
$params = $this->request->post();
|
||||
|
||||
$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);
|
||||
|
||||
if (request()->isJson()) {
|
||||
$api_key = input("api_key", "");//Dify API密钥
|
||||
$base_url = input("base_url", "https://api.dify.ai/v1");//API基础地址
|
||||
$chat_endpoint = input("chat_endpoint", "/chat-messages");//聊天接口端点
|
||||
$status = input("status", 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');
|
||||
$this->assign("config_info", $config_info);
|
||||
return $this->fetch("kefu/config");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,7 +51,7 @@ class Kefu extends BaseShop
|
||||
*/
|
||||
public function conversation()
|
||||
{
|
||||
return View::fetch('kefu/conversation');
|
||||
return View::fetch("kefu/conversation");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -54,25 +60,24 @@ class Kefu extends BaseShop
|
||||
*/
|
||||
public function getConversationList()
|
||||
{
|
||||
$params = $this->request->post();
|
||||
$page = $params['page'] ?? 1;
|
||||
$limit = $params['limit'] ?? 10;
|
||||
$user_id = $params['user_id'] ?? '';
|
||||
$status = $params['status'] ?? '';
|
||||
|
||||
$page = input("page", 1);
|
||||
$limit = input("limit", 10);
|
||||
$user_id = input("user_id", "");
|
||||
$status = input("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);
|
||||
}
|
||||
|
||||
@@ -82,23 +87,22 @@ class Kefu extends BaseShop
|
||||
*/
|
||||
public function getConversationInfo()
|
||||
{
|
||||
$params = $this->request->post();
|
||||
$conversation_id = $params['conversation_id'] ?? '';
|
||||
|
||||
$conversation_id = input("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);
|
||||
}
|
||||
|
||||
@@ -108,13 +112,12 @@ class Kefu extends BaseShop
|
||||
*/
|
||||
public function endConversation()
|
||||
{
|
||||
$params = $this->request->post();
|
||||
$id = $params['id'] ?? '';
|
||||
|
||||
$id = input("id", "");
|
||||
|
||||
if (empty($id)) {
|
||||
return $this->error('会话ID不能为空');
|
||||
}
|
||||
|
||||
|
||||
$kefu_conversation_model = new KefuConversationModel();
|
||||
$result = $kefu_conversation_model->updateConversation(
|
||||
['status' => 0],
|
||||
@@ -123,7 +126,7 @@ class Kefu extends BaseShop
|
||||
['site_id', '=', $this->site_id]
|
||||
]
|
||||
);
|
||||
|
||||
|
||||
return $this->success($result);
|
||||
}
|
||||
|
||||
@@ -133,42 +136,41 @@ class Kefu extends BaseShop
|
||||
*/
|
||||
public function deleteConversation()
|
||||
{
|
||||
$params = $this->request->post();
|
||||
$id = $params['id'] ?? '';
|
||||
|
||||
$id = input("id", "");
|
||||
|
||||
if (empty($id)) {
|
||||
return $this->error('会话ID不能为空');
|
||||
}
|
||||
|
||||
|
||||
$kefu_conversation_model = new KefuConversationModel();
|
||||
$kefu_message_model = new KefuMessageModel();
|
||||
|
||||
// 开启事务
|
||||
\think\facade\Db::startTrans();
|
||||
|
||||
|
||||
// 开启事务
|
||||
\think\facade\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]
|
||||
]);
|
||||
|
||||
|
||||
// 提交事务
|
||||
\think\facade\Db::commit();
|
||||
|
||||
|
||||
return $this->success($result);
|
||||
} catch (\Exception $e) {
|
||||
// 回滚事务
|
||||
@@ -183,9 +185,9 @@ class Kefu extends BaseShop
|
||||
*/
|
||||
public function message()
|
||||
{
|
||||
$conversation_id = $this->request->param('conversation_id') ?? '';
|
||||
View::assign('conversation_id', $conversation_id);
|
||||
return View::fetch('kefu/message');
|
||||
$conversation_id = input("conversation_id", "");
|
||||
View::assign("conversation_id", $conversation_id);
|
||||
return View::fetch("kefu/message");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -194,23 +196,22 @@ class Kefu extends BaseShop
|
||||
*/
|
||||
public function getMessageList()
|
||||
{
|
||||
$params = $this->request->post();
|
||||
$page = $params['page'] ?? 1;
|
||||
$limit = $params['limit'] ?? 50;
|
||||
$conversation_id = $params['conversation_id'] ?? '';
|
||||
|
||||
$page = input("page", 1);
|
||||
$limit = input("limit", 50);
|
||||
$conversation_id = input("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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user