内容库优化

This commit is contained in:
wong
2025-11-19 15:02:10 +08:00
parent 273ea73b1b
commit 5faf83e437
4 changed files with 119 additions and 44 deletions

View File

@@ -40,8 +40,21 @@ class ContentLibraryController extends Controller
return json(['code' => 400, 'msg' => '内容库名称不能为空']);
}
// 检查内容库名称是否已存在
$exists = ContentLibrary::where(['name' => $param['name'], 'userId' => $this->request->userInfo['id'], 'isDel' => 0])->find();
$where = [
['name', '=', $param['name']],
['companyId', '=', $this->request->userInfo['companyId']],
['isDel', '=', 0]
];
if (empty($this->request->userInfo['isAdmin'])) {
$where[] = ['userId', '=', $this->request->userInfo['id']];
}
// 查询内容库是否存在
$exists = ContentLibrary::where($where)->find();
if ($exists) {
return json(['code' => 400, 'msg' => '内容库名称已存在']);
}
@@ -111,16 +124,14 @@ class ContentLibraryController extends Controller
$sourceType = $this->request->param('sourceType', ''); // 新增来源类型1=好友2=群
$where = [
['companyId' , '=', $this->request->userInfo['companyId']],
['companyId', '=', $this->request->userInfo['companyId']],
['isDel', '=', 0] // 只查询未删除的记录
];
if(empty($this->request->userInfo['isAdmin'])){
$where[] = ['userId', '=', $this->request->userInfo['id']];
if (empty($this->request->userInfo['isAdmin'])) {
$where[] = ['userId', '=', $this->request->userInfo['id']];
}
// 添加名称模糊搜索
if ($keyword !== '') {
$where[] = ['name', 'like', '%' . $keyword . '%'];
@@ -214,11 +225,17 @@ class ContentLibraryController extends Controller
return json(['code' => 400, 'msg' => '参数错误']);
}
$library = ContentLibrary::where([
['id', '=', $id],
['userId', '=', $this->request->userInfo['id']],
$where = [
['companyId', '=', $this->request->userInfo['companyId']],
['isDel', '=', 0] // 只查询未删除的记录
])
];
if (empty($this->request->userInfo['isAdmin'])) {
$where[] = ['userId', '=', $this->request->userInfo['id']];
}
$library = ContentLibrary::where($where)
->field('id,name,sourceType,sourceFriends,sourceGroups,keywordInclude,keywordExclude,aiEnabled,aiPrompt,timeEnabled,timeStart,timeEnd,status,userId,companyId,createTime,updateTime,groupMembers,catchType')
->find();
@@ -233,7 +250,7 @@ class ContentLibraryController extends Controller
$library['keywordExclude'] = json_decode($library['keywordExclude'] ?: '[]', true);
$library['groupMembers'] = json_decode($library['groupMembers'] ?: '[]', true);
$library['catchType'] = json_decode($library['catchType'] ?: '[]', true);
unset($library['sourceFriends'],$library['sourceGroups']);
unset($library['sourceFriends'], $library['sourceGroups']);
// 将时间戳转换为日期格式(精确到日)
if (!empty($library['timeStart'])) {
@@ -260,7 +277,7 @@ class ContentLibraryController extends Controller
// 将好友信息添加到返回数据中
$library['friendsGroupsOptions'] = $friendsInfo;
}else{
} else {
$library['friendsGroupsOptions'] = [];
}
@@ -280,7 +297,7 @@ class ContentLibraryController extends Controller
// 将群组信息添加到返回数据中
$library['wechatGroupsOptions'] = $groupsInfo;
}else{
} else {
$library['wechatGroupsOptions'] = [];
}
@@ -315,12 +332,12 @@ class ContentLibraryController extends Controller
$where = [
['companyId' , '=', $this->request->userInfo['companyId']],
['companyId', '=', $this->request->userInfo['companyId']],
['isDel', '=', 0] // 只查询未删除的记录
];
if(empty($this->request->userInfo['isAdmin'])){
$where[] = ['userId', '=', $this->request->userInfo['id']];
if (empty($this->request->userInfo['isAdmin'])) {
$where[] = ['userId', '=', $this->request->userInfo['id']];
}
// 查询内容库是否存在
@@ -375,11 +392,16 @@ class ContentLibraryController extends Controller
return json(['code' => 400, 'msg' => '参数错误']);
}
$library = ContentLibrary::where([
$where = [
['id', '=', $id],
['userId', '=', $this->request->userInfo['id']],
['isDel', '=', 0] // 只删除未删除的记录
])->find();
['companyId', '=', $this->request->userInfo['companyId']],
['isDel', '=', 0]
];
if (empty($this->request->userInfo['isAdmin'])) {
$where[] = ['userId', '=', $this->request->userInfo['id']];
}
$library = ContentLibrary::where($where)->find();
if (empty($library)) {
return json(['code' => 500, 'msg' => '内容库不存在']);
@@ -416,12 +438,17 @@ class ContentLibraryController extends Controller
return json(['code' => 400, 'msg' => '内容库ID不能为空']);
}
// 验证内容库权限
$library = ContentLibrary::where([
$where = [
['id', '=', $libraryId],
['userId', '=', $this->request->userInfo['id']],
['companyId', '=', $this->request->userInfo['companyId']],
['isDel', '=', 0]
])->find();
];
if (empty($this->request->userInfo['isAdmin'])) {
$where[] = ['userId', '=', $this->request->userInfo['id']];
}
// 验证内容库权限
$library = ContentLibrary::where($where)->find();
if (empty($library)) {
return json(['code' => 500, 'msg' => '内容库不存在或无权限访问']);
@@ -447,10 +474,13 @@ class ContentLibraryController extends Controller
// 处理数据
foreach ($list as &$item) {
$item['content'] = !empty($item['contentAi']) ? $item['contentAi'] : $item['content'];
// 处理资源URL
$item['resUrls'] = json_decode($item['resUrls'] ?: '[]', true);
$item['urls'] = json_decode($item['urls'] ?: '[]', true);
// 格式化时间
//$item['createTime'] = date('Y-m-d H:i:s', $item['createTime']);
if ($item['createMomentTime']) {
@@ -462,11 +492,10 @@ class ContentLibraryController extends Controller
// 获取发送者信息
if ($item['type'] == 'moment' && !empty($item['friendId'])) {
$friendInfo = Db::name('wechat_friendship')
->alias('wf')
->join('wechat_account wa', 'wf.wechatId = wa.wechatId')
->where('wf.id', $item['friendId'])
->field('wa.nickname, wa.avatar')
$friendInfo = Db::table('s2_wechat_friend')
->where('id', $item['friendId'])
->field('nickname, avatar')
->order('id desc')
->find();
$item['senderNickname'] = !empty($friendInfo['nickname']) ? $friendInfo['nickname'] : '';
$item['senderAvatar'] = !empty($friendInfo['avatar']) ? $friendInfo['avatar'] : '';
@@ -478,6 +507,8 @@ class ContentLibraryController extends Controller
$item['senderNickname'] = !empty($friendInfo['nickname']) ? $friendInfo['nickname'] : '';
$item['senderAvatar'] = !empty($friendInfo['avatar']) ? $friendInfo['avatar'] : '';
}
unset($item['contentAi']);
}
unset($item);
@@ -534,11 +565,17 @@ class ContentLibraryController extends Controller
}
}
// 查询内容库是否存在
$library = ContentLibrary::where([
$where = [
['id', '=', $param['libraryId']],
['userId', '=', $this->request->userInfo['id']]
])->find();
['companyId', '=', $this->request->userInfo['companyId']],
['isDel', '=', 0]
];
if (empty($this->request->userInfo['isAdmin'])) {
$where[] = ['userId', '=', $this->request->userInfo['id']];
}
// 查询内容库是否存在
$library = ContentLibrary::where($where)->find();
if (!$library) {
return json(['code' => 500, 'msg' => '内容库不存在']);
@@ -783,14 +820,14 @@ class ContentLibraryController extends Controller
return json(['code' => 400, 'msg' => '参数错误']);
}
if(!empty($id)) {
if (!empty($id)) {
// 查询内容项目是否存在并检查权限
$item = ContentItem::alias('ci')
->join('content_library cl', 'ci.libraryId = cl.id')
->where(['ci.id' => $id, 'ci.isDel' => 0, 'cl.isDel' => 0, 'cl.companyId' => $companyId])
->field('ci.*')
->find();
}else{
} else {
$item['content'] = $content;
}