chore(addon/aikefu): 调整config的配置内容

This commit is contained in:
2025-12-06 13:25:15 +08:00
parent 0ff979917c
commit d8a0dd5d31
2 changed files with 70 additions and 64 deletions

View File

@@ -1,4 +1,9 @@
<?php <?php
/**
* 智能客服配置模型
* 用于存储和管理智能客服的配置信息
* 版本1.0.0
*/
namespace addon\aikefu\model; namespace addon\aikefu\model;
@@ -20,15 +25,15 @@ class Config extends BaseModel
public function setConfig($data, $site_id = 0, $app_module = 'shop') public function setConfig($data, $site_id = 0, $app_module = 'shop')
{ {
$config = new ConfigModel(); $config = new ConfigModel();
// 获取原始配置 // 获取原始配置
$original_config = $this->getConfig($site_id, $app_module)['data']['value'] ?? []; $original_config = $this->getConfig($site_id, $app_module)['data']['value'] ?? [];
// 如果 API Key 为空或保持不变,则使用原始值 // 如果 API Key 为空或保持不变,则使用原始值
if (isset($data['api_key']) && empty($data['api_key'])) { if (isset($data['api_key']) && empty($data['api_key'])) {
$data['api_key'] = $original_config['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']]); $res = $config->setConfig($data, '智能客服配置', 1, [['site_id', '=', $site_id], ['app_module', '=', $app_module], ['config_key', '=', 'AIKEFU_CONFIG']]);
return $res; return $res;
} }

View File

@@ -1,4 +1,7 @@
<?php <?php
/**
* 智能客服控制器
*/
namespace addon\aikefu\shop\controller; namespace addon\aikefu\shop\controller;
@@ -8,6 +11,9 @@ use addon\aikefu\model\Message as KefuMessageModel;
use app\shop\controller\BaseShop; use app\shop\controller\BaseShop;
use think\facade\View; use think\facade\View;
/**
* 智能客服 控制器
*/
class Kefu extends BaseShop class Kefu extends BaseShop
{ {
/** /**
@@ -17,25 +23,25 @@ class Kefu extends BaseShop
public function config() public function config()
{ {
$kefu_config_model = new KefuConfigModel(); $kefu_config_model = new KefuConfigModel();
if ($this->request->isJson()) { if (request()->isJson()) {
// 保存配置 $api_key = input("api_key", "");//Dify API密钥
$params = $this->request->post(); $base_url = input("base_url", "https://api.dify.ai/v1");//API基础地址
$chat_endpoint = input("chat_endpoint", "/chat-messages");//聊天接口端点
$data = [ $status = input("status", 0);//状态
'api_key' => $params['api_key'] ?? '',
'base_url' => $params['base_url'] ?? 'https://api.dify.ai/v1', $data = array(
'chat_endpoint' => $params['chat_endpoint'] ?? '/chat-messages', "api_key" => $api_key,
'status' => $params['status'] ?? 0, "base_url" => $base_url,
]; "chat_endpoint" => $chat_endpoint,
"status" => $status
$result = $kefu_config_model->setConfig($data, $this->site_id); );
$result = $kefu_config_model->setConfig($data, $this->site_id, $this->app_module);
return $result; return $result;
} else { } else {
// 获取配置
$config_info = $kefu_config_model->getConfig($this->site_id, $this->app_module)['data']['value'] ?? []; $config_info = $kefu_config_model->getConfig($this->site_id, $this->app_module)['data']['value'] ?? [];
$this->assign('config_info', $config_info); $this->assign("config_info", $config_info);
return $this->fetch('kefu/config'); return $this->fetch("kefu/config");
} }
} }
@@ -45,7 +51,7 @@ class Kefu extends BaseShop
*/ */
public function conversation() public function conversation()
{ {
return View::fetch('kefu/conversation'); return View::fetch("kefu/conversation");
} }
/** /**
@@ -54,25 +60,24 @@ class Kefu extends BaseShop
*/ */
public function getConversationList() public function getConversationList()
{ {
$params = $this->request->post(); $page = input("page", 1);
$page = $params['page'] ?? 1; $limit = input("limit", 10);
$limit = $params['limit'] ?? 10; $user_id = input("user_id", "");
$user_id = $params['user_id'] ?? ''; $status = input("status", "");
$status = $params['status'] ?? '';
$kefu_conversation_model = new KefuConversationModel(); $kefu_conversation_model = new KefuConversationModel();
$condition = [['site_id', '=', $this->site_id]]; $condition = [['site_id', '=', $this->site_id]];
if (!empty($user_id)) { if (!empty($user_id)) {
$condition[] = ['user_id', '=', $user_id]; $condition[] = ['user_id', '=', $user_id];
} }
if ($status !== '') { if ($status !== '') {
$condition[] = ['status', '=', $status]; $condition[] = ['status', '=', $status];
} }
$conversation_list = $kefu_conversation_model->getConversationList($condition, '*', 'update_time desc', $page, $limit); $conversation_list = $kefu_conversation_model->getConversationList($condition, '*', 'update_time desc', $page, $limit);
return $this->success($conversation_list); return $this->success($conversation_list);
} }
@@ -82,23 +87,22 @@ class Kefu extends BaseShop
*/ */
public function getConversationInfo() public function getConversationInfo()
{ {
$params = $this->request->post(); $conversation_id = input("conversation_id", "");
$conversation_id = $params['conversation_id'] ?? '';
if (empty($conversation_id)) { if (empty($conversation_id)) {
return $this->error('会话ID不能为空'); return $this->error('会话ID不能为空');
} }
$kefu_conversation_model = new KefuConversationModel(); $kefu_conversation_model = new KefuConversationModel();
$conversation_info = $kefu_conversation_model->getConversationInfo([ $conversation_info = $kefu_conversation_model->getConversationInfo([
['site_id', '=', $this->site_id], ['site_id', '=', $this->site_id],
['conversation_id', '=', $conversation_id] ['conversation_id', '=', $conversation_id]
]); ]);
if (empty($conversation_info)) { if (empty($conversation_info)) {
return $this->error('会话不存在'); return $this->error('会话不存在');
} }
return $this->success($conversation_info); return $this->success($conversation_info);
} }
@@ -108,13 +112,12 @@ class Kefu extends BaseShop
*/ */
public function endConversation() public function endConversation()
{ {
$params = $this->request->post(); $id = input("id", "");
$id = $params['id'] ?? '';
if (empty($id)) { if (empty($id)) {
return $this->error('会话ID不能为空'); return $this->error('会话ID不能为空');
} }
$kefu_conversation_model = new KefuConversationModel(); $kefu_conversation_model = new KefuConversationModel();
$result = $kefu_conversation_model->updateConversation( $result = $kefu_conversation_model->updateConversation(
['status' => 0], ['status' => 0],
@@ -123,7 +126,7 @@ class Kefu extends BaseShop
['site_id', '=', $this->site_id] ['site_id', '=', $this->site_id]
] ]
); );
return $this->success($result); return $this->success($result);
} }
@@ -133,42 +136,41 @@ class Kefu extends BaseShop
*/ */
public function deleteConversation() public function deleteConversation()
{ {
$params = $this->request->post(); $id = input("id", "");
$id = $params['id'] ?? '';
if (empty($id)) { if (empty($id)) {
return $this->error('会话ID不能为空'); return $this->error('会话ID不能为空');
} }
$kefu_conversation_model = new KefuConversationModel(); $kefu_conversation_model = new KefuConversationModel();
$kefu_message_model = new KefuMessageModel(); $kefu_message_model = new KefuMessageModel();
// 开启事务 // 开启事务
\think\facade\Db::startTrans(); \think\facade\Db::startTrans();
try { try {
// 删除会话关联的消息 // 删除会话关联的消息
$conversation_info = $kefu_conversation_model->getConversationInfo([ $conversation_info = $kefu_conversation_model->getConversationInfo([
['id', '=', $id], ['id', '=', $id],
['site_id', '=', $this->site_id] ['site_id', '=', $this->site_id]
]); ]);
if (!empty($conversation_info)) { if (!empty($conversation_info)) {
$kefu_message_model->deleteMessage([ $kefu_message_model->deleteMessage([
['site_id', '=', $this->site_id], ['site_id', '=', $this->site_id],
['conversation_id', '=', $conversation_info['conversation_id']] ['conversation_id', '=', $conversation_info['conversation_id']]
]); ]);
} }
// 删除会话 // 删除会话
$result = $kefu_conversation_model->deleteConversation([ $result = $kefu_conversation_model->deleteConversation([
['id', '=', $id], ['id', '=', $id],
['site_id', '=', $this->site_id] ['site_id', '=', $this->site_id]
]); ]);
// 提交事务 // 提交事务
\think\facade\Db::commit(); \think\facade\Db::commit();
return $this->success($result); return $this->success($result);
} catch (\Exception $e) { } catch (\Exception $e) {
// 回滚事务 // 回滚事务
@@ -183,9 +185,9 @@ class Kefu extends BaseShop
*/ */
public function message() public function message()
{ {
$conversation_id = $this->request->param('conversation_id') ?? ''; $conversation_id = input("conversation_id", "");
View::assign('conversation_id', $conversation_id); View::assign("conversation_id", $conversation_id);
return View::fetch('kefu/message'); return View::fetch("kefu/message");
} }
/** /**
@@ -194,23 +196,22 @@ class Kefu extends BaseShop
*/ */
public function getMessageList() public function getMessageList()
{ {
$params = $this->request->post(); $page = input("page", 1);
$page = $params['page'] ?? 1; $limit = input("limit", 50);
$limit = $params['limit'] ?? 50; $conversation_id = input("conversation_id", "");
$conversation_id = $params['conversation_id'] ?? '';
if (empty($conversation_id)) { if (empty($conversation_id)) {
return $this->error('会话ID不能为空'); return $this->error('会话ID不能为空');
} }
$kefu_message_model = new KefuMessageModel(); $kefu_message_model = new KefuMessageModel();
$condition = [ $condition = [
['site_id', '=', $this->site_id], ['site_id', '=', $this->site_id],
['conversation_id', '=', $conversation_id] ['conversation_id', '=', $conversation_id]
]; ];
$message_list = $kefu_message_model->getMessageList($condition, '*', 'create_time asc', $page, $limit); $message_list = $kefu_message_model->getMessageList($condition, '*', 'create_time asc', $page, $limit);
return $this->success($message_list); return $this->success($message_list);
} }
} }