Files
cunkebao_v3/Server/application/chukebao/controller/KnowledgeBaseController.php
Manus AI 5517457929 sync: 以本地为准同步全量变更至 GitHub
含四端需求文档、前端/后端/超管/触客宝迭代及部署脚本更新;未拉取远程。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-30 04:24:56 +08:00

121 lines
4.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\chukebao\controller;
use library\ResponseHelper;
use think\Db;
/**
* 模块 D · 知识库(需求三 §3.2 · 需求六 §6.7
* 列表 = 存客宝已分配enabledForTouchkebao=true非全量默认库由存客宝项目配置。
* 配置真源在存客宝 H5 + Server触客宝只读已分配。
* 数据表ck_kb_touchkebao_config懒建存放存客宝下发的分配结果
*/
class KnowledgeBaseController extends BaseController
{
private function ensureTable()
{
Db::execute("CREATE TABLE IF NOT EXISTS `ck_kb_touchkebao_config` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`companyId` int(11) NOT NULL DEFAULT 0,
`kbId` int(11) NOT NULL DEFAULT 0,
`kbName` varchar(128) NOT NULL DEFAULT '',
`isDefault` tinyint(1) NOT NULL DEFAULT 0,
`kefuId` int(11) NOT NULL DEFAULT 0,
`currentKbId` int(11) NOT NULL DEFAULT 0,
`enabled` tinyint(1) NOT NULL DEFAULT 1,
`updateTime` int(11) NOT NULL DEFAULT 0,
PRIMARY KEY (`id`),
KEY `idx_company` (`companyId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4");
}
/**
* 仅返回已分配知识库
* GET /v1/kefu/ai/knowledge-bases
*/
public function knowledgeBases()
{
$this->ensureTable();
$companyId = $this->getUserInfo('companyId');
$kefuId = $this->getUserInfo('id');
// 仅返回 enabled 的库(存客宝下发);无分配 → 空列表,触客宝提示联系管理员
$rows = Db::name('kb_touchkebao_config')
->where('companyId', $companyId)
->where('enabled', 1)
->where('kefuId', 0) // 项目级开放
->select();
$list = [];
$defaultKbId = null;
foreach ($rows as $r) {
$list[] = [
'id' => (int)$r['kbId'],
'name' => $r['kbName'],
'isDefault' => (bool)$r['isDefault'],
];
if ($r['isDefault']) {
$defaultKbId = (int)$r['kbId'];
}
}
// 客服个人当前选择(覆盖默认,须在已开放范围内)
$currentKbId = $defaultKbId;
$myCurrent = Db::name('kb_touchkebao_config')
->where('companyId', $companyId)
->where('kefuId', $kefuId)
->value('currentKbId');
if ($myCurrent && in_array((int)$myCurrent, array_column($list, 'id'))) {
$currentKbId = (int)$myCurrent;
}
return ResponseHelper::success([
'defaultKbId' => $defaultKbId,
'currentKbId' => $currentKbId,
'list' => $list,
]);
}
/**
* 客服在已分配范围内切换默认库
* POST /v1/kefu/ai/knowledge-base/select
*/
public function selectKnowledgeBase()
{
$this->ensureTable();
$companyId = $this->getUserInfo('companyId');
$kefuId = $this->getUserInfo('id');
$kbId = (int)$this->request->param('kbId', 0);
if (!$kbId) {
return ResponseHelper::error('kbId 缺失', 400);
}
// 校验是否在已开放范围内
$allowed = Db::name('kb_touchkebao_config')
->where('companyId', $companyId)
->where('enabled', 1)
->where('kbId', $kbId)
->count();
if (!$allowed) {
return ResponseHelper::error('该知识库未对触客宝开放', 403);
}
$exist = Db::name('kb_touchkebao_config')
->where('companyId', $companyId)
->where('kefuId', $kefuId)
->find();
if ($exist) {
Db::name('kb_touchkebao_config')->where('id', $exist['id'])
->update(['currentKbId' => $kbId, 'updateTime' => time()]);
} else {
Db::name('kb_touchkebao_config')->insert([
'companyId' => $companyId,
'kefuId' => $kefuId,
'currentKbId' => $kbId,
'enabled' => 0,
'updateTime' => time(),
]);
}
return ResponseHelper::success(['currentKbId' => $kbId], '已切换');
}
}