群列表+群成员接口外加对接

This commit is contained in:
wong
2025-04-22 19:17:14 +08:00
parent 793ee5cb4f
commit 46ba136255
18 changed files with 1137 additions and 123 deletions

View File

@@ -70,4 +70,12 @@ Route::group('v1/', function () {
Route::group('friend', function () {
Route::get('', 'app\\cunkebao\\controller\\friend\\GetFriendListV1Controller@index'); // 获取好友列表
});
//群相关
Route::group('chatroom', function () {
Route::get('', 'app\\cunkebao\\controller\\chatroom\\GetChatroomListV1Controller@index'); // 获取群列表
Route::get('getMemberList', 'app\\cunkebao\\controller\\chatroom\\GetChatroomListV1Controller@getMemberList'); // 获取群详情
});
})->middleware(['jwt']);

View File

@@ -57,6 +57,43 @@ class ContentLibraryController extends Controller
$item['keywordExclude'] = json_decode($item['keywordExclude'] ?: '[]', true);
// 添加创建人名称
$item['creatorName'] = $item['user']['username'] ?? '';
// 获取好友详细信息
if (!empty($item['sourceFriends'] && $item['sourceType'] == 1)) {
$friendIds = $item['sourceFriends'];
$friendsInfo = [];
if (!empty($friendIds)) {
// 查询好友信息使用wechat_friend表
$friendsInfo = Db::name('wechat_friend')->alias('wf')
->field('wf.id,wf.wechatId, wa.nickname, wa.avatar')
->join('wechat_account wa', 'wf.wechatId = wa.wechatId')
->whereIn('wf.id', $friendIds)
->select();
}
// 将好友信息添加到返回数据中
$item['selectedFriends'] = $friendsInfo;
}
// 获取群组详细信息
if (!empty($item['sourceGroups']) && $item['sourceType'] == 2) {
$groupIds = $item['sourceGroups'];
$groupsInfo = [];
if (!empty($groupIds)) {
// 查询群组信息
$groupsInfo = Db::name('wechat_group')->alias('g')
->field('g.id, g.chatroomId, g.name, g.avatar, g.ownerWechatId')
->whereIn('g.id', $groupIds)
->select();
}
// 将群组信息添加到返回数据中
$item['selectedGroups'] = $groupsInfo;
}
unset($item['user']); // 移除关联数据
}
unset($item);
@@ -117,17 +154,36 @@ class ContentLibraryController extends Controller
$friendsInfo = [];
if (!empty($friendIds)) {
// 查询好友信息使用wechat_account
$friendsInfo = Db::name('wechat_account')
->field('wechatId, nickname, avatar')
->whereIn('wechatId', $friendIds)
->select();
// 查询好友信息使用wechat_friend
$friendsInfo = Db::name('wechat_friend')->alias('wf')
->field('wf.id,wf.wechatId, wa.nickname, wa.avatar')
->join('wechat_account wa', 'wf.wechatId = wa.wechatId')
->whereIn('wf.id', $friendIds)
->select();
}
// 将好友信息添加到返回数据中
$library['selectedFriends'] = $friendsInfo;
}
// 获取群组详细信息
if (!empty($library['sourceGroups'])) {
$groupIds = $library['sourceGroups'];
$groupsInfo = [];
if (!empty($groupIds)) {
// 查询群组信息
$groupsInfo = Db::name('wechat_group')->alias('g')
->field('g.id, g.chatroomId, g.name, g.avatar, g.ownerWechatId,wa.nickname as ownerNickname,wa.avatar as ownerAvatar,wa.alias as ownerAlias')
->join('wechat_account wa', 'g.ownerWechatId = wa.wechatId')
->whereIn('g.id', $groupIds)
->select();
}
// 将群组信息添加到返回数据中
$library['selectedGroups'] = $groupsInfo;
}
return json([
@@ -163,15 +219,21 @@ class ContentLibraryController extends Controller
Db::startTrans();
try {
$keywordInclude = isset($param['keywordInclude']) ? json_encode($param['keywordInclude'],256) : json_encode([]);
$keywordExclude = isset($param['keywordExclude']) ? json_encode($param['keywordExclude'],256) : json_encode([]);
$sourceType = isset($param['sourceType']) ? $param['sourceType'] : 1;
// 构建数据
$data = [
'name' => $param['name'],
// 数据来源配置
'sourceFriends' => isset($param['friends']) ? json_encode($param['friends']) : '[]', // 选择的微信好友
'sourceGroups' => isset($param['groups']) ? json_encode($param['groups']) : '[]', // 选择的微信群
'sourceFriends' => $sourceType == 1 ? json_encode($param['friends']) : json_encode([]), // 选择的微信好友
'sourceGroups' => $sourceType == 2 ? json_encode($param['groups']) : json_encode([]), // 选择的微信群
// 关键词配置
'keywordInclude' => isset($param['keywordInclude']) ? json_encode($param['keywordInclude']) : '[]', // 包含的关键词
'keywordExclude' => isset($param['keywordExclude']) ? json_encode($param['keywordExclude']) : '[]', // 排除的关键词
'keywordInclude' => $keywordInclude, // 包含的关键词
'keywordExclude' => $keywordExclude, // 排除的关键词
// AI配置
'aiEnabled' => isset($param['aiEnabled']) ? $param['aiEnabled'] : 0, // 是否启用AI
'aiPrompt' => isset($param['aiPrompt']) ? $param['aiPrompt'] : '', // AI提示词
@@ -180,7 +242,7 @@ class ContentLibraryController extends Controller
'timeStart' => isset($param['startTime']) ? strtotime($param['startTime']) : 0, // 开始时间(转换为时间戳)
'timeEnd' => isset($param['endTime']) ? strtotime($param['endTime']) : 0, // 结束时间(转换为时间戳)
// 来源类型
'sourceType' => isset($param['sourceType']) ? $param['sourceType'] : 0, // 1=好友2=群3=好友和群
'sourceType' => $sourceType, // 1=好友2=群3=好友和群
// 基础信息
'status' => isset($param['status']) ? $param['status'] : 0, // 状态0=禁用1=启用
'userId' => $this->request->userInfo['id'],
@@ -240,9 +302,27 @@ class ContentLibraryController extends Controller
Db::startTrans();
try {
$keywordInclude = isset($param['keywordInclude']) ? json_encode($param['keywordInclude'],256) : json_encode([]);
$keywordExclude = isset($param['keywordExclude']) ? json_encode($param['keywordExclude'],256) : json_encode([]);
// 更新内容库基本信息
$library->name = $param['name'];
$library->description = isset($param['description']) ? $param['description'] : '';
$library->sourceType = isset($param['sourceType']) ? $param['sourceType'] : 1;
$library->sourceFriends = $param['sourceType'] == 1 ? json_encode($param['friends']) : json_encode([]);
$library->sourceGroups = $param['sourceType'] == 2 ? json_encode($param['groups']) : json_encode([]);
$library->keywordInclude = $keywordInclude;
$library->keywordExclude = $keywordExclude;
$library->aiEnabled = isset($param['aiEnabled']) ? $param['aiEnabled'] : 0;
$library->aiPrompt = isset($param['aiPrompt']) ? $param['aiPrompt'] : '';
$library->timeEnabled = isset($param['timeEnabled']) ? $param['timeEnabled'] : 0;
$library->timeStart = isset($param['startTime']) ? strtotime($param['startTime']) : 0;
$library->timeEnd = isset($param['endTime']) ? strtotime($param['endTime']) : 0;
$library->status = isset($param['status']) ? $param['status'] : 0;
$library->updateTime = time();
$library->save();
Db::commit();

View File

@@ -0,0 +1,136 @@
<?php
namespace app\cunkebao\controller\chatroom;
use app\cunkebao\controller\BaseController;
use app\cunkebao\model\WechatChatroom;
use think\Db;
/**
* 群聊管理控制器
*/
class GetChatroomListV1Controller extends BaseController
{
/**
* 获取群聊列表
* @return \think\response\Json
*/
public function index()
{
$page = $this->request->param('page', 1);
$limit = $this->request->param('limit', 20);
$keyword = $this->request->param('keyword', '');
try {
$where = [];
if ($this->getUserInfo('isAdmin') == 1) {
$where[] = ['g.companyId', '=', $this->getUserInfo('companyId')];
$where[] = ['g.deleteTime', '=', 0];
} else {
$where[] = ['g.companyId', '=', $this->getUserInfo('companyId')];
$where[] = ['g.deleteTime', '=', 0];
//$where[] = ['g.userId', '=', $this->getUserInfo('id')];
}
if(!empty($keyword)){
$where[] = ['g.name', 'like', '%'.$keyword.'%'];
}
$data = WechatChatroom::alias('g')
->field(['g.id', 'g.chatroomId', 'g.name', 'g.avatar','g.ownerWechatId', 'g.identifier', 'g.createTime',
'wa.nickname as ownerNickname','wa.avatar as ownerAvatar','wa.alias as ownerAlias'])
->Join('wechat_account wa', 'g.ownerWechatId = wa.wechatId', 'LEFT')
->where($where);
$total = $data->count();
$list = $data->page($page, $limit)->order('g.id DESC')->select();
return json([
'code' => 200,
'msg' => '获取成功',
'data' => [
'list' => $list,
'total' => $total,
]
]);
} catch (\Exception $e) {
return json([
'code' => $e->getCode(),
'msg' => $e->getMessage()
]);
}
}
/**
* 获取群成员列表
* @return \think\response\Json
*/
public function getMemberList()
{
$page = $this->request->param('page', 1);
$limit = $this->request->param('limit', 20);
$keyword = $this->request->param('keyword', '');
$groupId = $this->request->param('groupId', 0);
if (empty($groupId)) {
return json([
'code' => 400,
'msg' => '群ID不能为空'
]);
}
try {
$where = [];
$where[] = ['m.groupId', '=', $groupId];
$where[] = ['m.deleteTime', '=', 0];
// 如果有搜索关键词
if (!empty($keyword)) {
$where[] = ['m.nickname|m.identifier', 'like', '%'.$keyword.'%'];
}
$data = Db::name('wechat_group_member')
->alias('m')
->field([
'm.id',
'm.identifier',
'm.customerIs',
'wa.nickname',
'wa.avatar',
'm.groupId',
'm.createTime',
'g.name as groupName',
'g.chatroomId'
])
->join('wechat_group g', 'm.groupId = g.id', 'LEFT')
->join('wechat_account wa', 'wa.wechatId = m.identifier', 'LEFT')
->where($where);
$total = $data->count();
$list = $data->page($page, $limit)
->order('m.id DESC')
->select();
// 格式化时间
foreach ($list as &$item) {
if (!empty($item['createTime'])) {
$item['createTime'] = date('Y-m-d H:i:s', $item['createTime']);
}
}
return json([
'code' => 200,
'msg' => '获取成功',
'data' => [
'list' => $list,
'total' => $total,
]
]);
} catch (\Exception $e) {
return json([
'code' => $e->getCode() ?: 500,
'msg' => $e->getMessage()
]);
}
}
}

View File

@@ -5,6 +5,7 @@ use app\common\model\Device as DeviceModel;
use app\common\model\DeviceUser as DeviceUserModel;
use app\common\model\WechatFriend;
use app\cunkebao\controller\BaseController;
use think\Db;
/**
* 设备管理控制器
@@ -26,29 +27,29 @@ class GetFriendListV1Controller extends BaseController
$where = [];
if ($this->getUserInfo('isAdmin') == 1) {
$where['wf.companyId'] = $this->getUserInfo('companyId');
$where['wf.deleteTime'] = 0;
$where[] = ['wf.companyId','=',$this->getUserInfo('companyId')];
$where[] = ['wf.deleteTime','=',0];
} else {
$where['wf.companyId'] = $this->getUserInfo('companyId');
$where['wf.deleteTime'] = 0;
//$where['userId'] = $this->getUserInfo('id');
$where[] = ['wf.companyId','=',$this->getUserInfo('companyId')];
$where[] = ['wf.deleteTime','=',0];
//$where[] = ['wf.userId','=',$this->getUserInfo('id')];
}
if($keyword){
$where['wa1.nickname'] = ['like','%'.$keyword.'%'];
if(!empty($keyword)){
$where[] = ['wa1.nickname','like','%'.$keyword.'%'];
}
$data = WechatFriend::alias('wf')
->field(['wa1.nickname','wa1.avatar','wa1.alias','wf.wechatId','wa2.nickname as ownerNickname','wa2.alias as ownerAlias','wa2.wechatId as ownerWechatId','wf.createTime'])
->field(['wa1.nickname','wa1.avatar','wa1.alias','wf.id','wf.wechatId','wa2.nickname as ownerNickname','wa2.alias as ownerAlias','wa2.wechatId as ownerWechatId','wf.createTime'])
->Join('wechat_account wa1','wf.wechatId = wa1.wechatId')
->Join('wechat_account wa2','wf.ownerWechatId = wa2.wechatId')
->where($where);
$total = $data->count();
$list = $data->page($page, $limit)->select();
$list = $data->page($page, $limit)->order('wf.id DESC')->select();

View File

@@ -0,0 +1,15 @@
<?php
namespace app\cunkebao\model;
use think\Model;
/**
* 微信好友模型类
*/
class WechatChatroom extends Model
{
// 设置表名
protected $name = 'wechat_group';
}