From d8a0dd5d31420ce9abc9eab824dd9d7c2aafc4b4 Mon Sep 17 00:00:00 2001 From: ZF sun <34314687@qq.com> Date: Sat, 6 Dec 2025 13:25:15 +0800 Subject: [PATCH] =?UTF-8?q?chore(addon/aikefu):=20=E8=B0=83=E6=95=B4config?= =?UTF-8?q?=E7=9A=84=E9=85=8D=E7=BD=AE=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/addon/aikefu/model/Config.php | 11 +- src/addon/aikefu/shop/controller/Kefu.php | 123 +++++++++++----------- 2 files changed, 70 insertions(+), 64 deletions(-) diff --git a/src/addon/aikefu/model/Config.php b/src/addon/aikefu/model/Config.php index 4515fdabe..52ded479b 100644 --- a/src/addon/aikefu/model/Config.php +++ b/src/addon/aikefu/model/Config.php @@ -1,4 +1,9 @@ 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; } diff --git a/src/addon/aikefu/shop/controller/Kefu.php b/src/addon/aikefu/shop/controller/Kefu.php index 43bb14009..377c2d7cd 100644 --- a/src/addon/aikefu/shop/controller/Kefu.php +++ b/src/addon/aikefu/shop/controller/Kefu.php @@ -1,4 +1,7 @@ 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); } }