群操作完结
This commit is contained in:
@@ -191,6 +191,39 @@ class GroupController extends ApiController
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 转让群
|
||||
*/
|
||||
public function tarsgroup(Request $request): Response
|
||||
{
|
||||
$group_id = $request->post('group_id');
|
||||
$object_user_id = $request->post('object_user_id'); // 目标ID
|
||||
if(empty($group_id) || empty($object_user_id)) {
|
||||
return $this->error('group_id 或 object_id 不能为空');
|
||||
}
|
||||
// 判断当前人是不是群主
|
||||
$owner = Db::name('chat_group_member')->where([
|
||||
'group_id' => $group_id,
|
||||
'user_id' => $this->uid,
|
||||
'quit_time' => null,
|
||||
'role' => 1
|
||||
])->find();
|
||||
if(!$owner) {
|
||||
return $this->error('你当前不是群主,无法转让');
|
||||
}
|
||||
|
||||
$res = Db::name('chat_group_member')->where([
|
||||
'group_id' => $group_id,
|
||||
'user_id' => $this->uid,
|
||||
'quit_time' => null,
|
||||
'role' => 1
|
||||
])->update(['user_id' => $object_user_id, 'object_user_id' => $this->uid]);
|
||||
if($res) {
|
||||
return $this->success();
|
||||
}
|
||||
return $this->error();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -199,56 +232,53 @@ class GroupController extends ApiController
|
||||
*/
|
||||
public function setAnnouncement(Request $request): Response
|
||||
{
|
||||
$uid = $request->uid;
|
||||
$groupId = $request->post('group_id', 0);
|
||||
$announcement = $request->post('announcement', '');
|
||||
$groupId = $request->post('group_id');
|
||||
$announcement = $request->post('announcement');
|
||||
|
||||
if (!$groupId) {
|
||||
return json(['code' => 400, 'msg' => '缺少group_id']);
|
||||
if (!$groupId || !$announcement) {
|
||||
return $this->error('缺少group_id或公共内容不能为空');
|
||||
}
|
||||
|
||||
// 验证权限
|
||||
$role = ChatGroupMember::where([
|
||||
$role = Db::name('chat_group_member')->where([
|
||||
'group_id' => $groupId,
|
||||
'user_id' => $uid,
|
||||
'user_id' => $this->uid,
|
||||
'quit_time' => null,
|
||||
'is_kicked' => 0
|
||||
])->value('role');
|
||||
if (!in_array($role, [1, 2])) { // 1=群主,2=管理员
|
||||
return json(['code' => 403, 'msg' => '仅群主和管理员可设置群公告']);
|
||||
return $this->error('仅群主和管理员可设置群公告');
|
||||
}
|
||||
|
||||
// 更新公告
|
||||
ChatGroup::where('id', $groupId)->update([
|
||||
Db::name('chat_group')->where('id', $groupId)->update([
|
||||
'announcement' => $announcement,
|
||||
'updated_at' => time()
|
||||
'updated_time' => d()
|
||||
]);
|
||||
|
||||
return json(['code' => 200, 'msg' => '群公告设置成功']);
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置群免打扰
|
||||
* 设置群免打扰 (废弃)
|
||||
*/
|
||||
public function setDoNotDisturb(Request $request): Response
|
||||
{
|
||||
$uid = $request->uid;
|
||||
$groupId = $request->post('group_id', 0);
|
||||
$status = $request->post('status', 0); // 0=关闭,1=开启
|
||||
$groupId = $request->post('group_id');
|
||||
$status = $request->post('status'); // 0=关闭,1=开启
|
||||
|
||||
if (!$groupId) {
|
||||
return json(['code' => 400, 'msg' => '缺少group_id']);
|
||||
return $this->error('缺少group_id');
|
||||
}
|
||||
|
||||
// 验证是否在群内
|
||||
$isMember = ChatGroupMember::where([
|
||||
$isMember = Db::name('chat_group_member')->where([
|
||||
'group_id' => $groupId,
|
||||
'user_id' => $uid,
|
||||
'user_id' => $this->uid,
|
||||
'quit_time' => null,
|
||||
'is_kicked' => 0
|
||||
])->exists();
|
||||
if (!$isMember) {
|
||||
return json(['code' => 400, 'msg' => '不在群内']);
|
||||
return $this->error('不在群内');
|
||||
}
|
||||
|
||||
// 生成会话ID
|
||||
@@ -268,63 +298,67 @@ class GroupController extends ApiController
|
||||
*/
|
||||
public function muteMember(Request $request): Response
|
||||
{
|
||||
$uid = $request->uid;
|
||||
$groupId = $request->post('group_id', 0);
|
||||
$targetUid = $request->post('target_uid', 0);
|
||||
$muteTime = $request->post('mute_time', 3600); // 默认禁言1小时
|
||||
|
||||
$groupId = $request->post('group_id');
|
||||
$targetUid = $request->post('target_uid');
|
||||
$muteTime = $request->post('mute_time')?:3600; // 默认禁言1小时
|
||||
if (!$groupId || !$targetUid) {
|
||||
return json(['code' => 400, 'msg' => '缺少group_id或target_uid']);
|
||||
}
|
||||
|
||||
// 验证操作人权限
|
||||
$operatorRole = ChatGroupMember::where([
|
||||
$operatorRole = Db::name('chat_group_member')->where([
|
||||
'group_id' => $groupId,
|
||||
'user_id' => $uid,
|
||||
'user_id' => $this->uid,
|
||||
'quit_time' => null,
|
||||
'is_kicked' => 0
|
||||
])->value('role');
|
||||
if (!in_array($operatorRole, [1, 2])) {
|
||||
return json(['code' => 403, 'msg' => '仅群主和管理员可禁言']);
|
||||
return $this->error('仅群主和管理员可禁言');
|
||||
}
|
||||
|
||||
// 验证被禁言用户是否在群内
|
||||
$targetIsMember = ChatGroupMember::where([
|
||||
$targetIsMember = Db::name('chat_group_member')->where([
|
||||
'group_id' => $groupId,
|
||||
'user_id' => $targetUid,
|
||||
'quit_time' => null,
|
||||
'is_kicked' => 0
|
||||
])->exists();
|
||||
])->find();
|
||||
if (!$targetIsMember) {
|
||||
return json(['code' => 400, 'msg' => '被禁言用户不在群内']);
|
||||
return $this->error('被禁言用户不在群内');
|
||||
}
|
||||
|
||||
// 不能禁言群主
|
||||
$groupOwner = ChatGroup::where('id', $groupId)->value('owner_id');
|
||||
$groupOwner = Db::name('chat_group')->where('id', $groupId)->value('owner_id');
|
||||
if ($targetUid == $groupOwner) {
|
||||
return json(['code' => 403, 'msg' => '不能禁言群主']);
|
||||
return $this->error('不能禁言群主');
|
||||
}
|
||||
|
||||
// 不能禁言自己
|
||||
if ($targetUid == $uid) {
|
||||
return json(['code' => 400, 'msg' => '不能禁言自己']);
|
||||
if ($targetUid == $this->uid) {
|
||||
return $this->error('不能禁言自己');
|
||||
}
|
||||
|
||||
$now = time();
|
||||
$expireTime = $muteTime > 0 ? $now + $muteTime : null;
|
||||
|
||||
$res = Db::name('chat_group_mute')->where(['group_id' => $groupId, 'user_id' => $targetUid])->find();
|
||||
// 新增/更新禁言记录
|
||||
ChatGroupMute::updateOrCreate(
|
||||
['group_id' => $groupId, 'user_id' => $targetUid],
|
||||
[
|
||||
if($res) {
|
||||
Db::name('chat_group_mute')->where(['group_id' => $groupId, 'user_id' => $targetUid])->save([
|
||||
'mute_time' => $muteTime,
|
||||
'expire_time' => $expireTime,
|
||||
'operator_id' => $uid,
|
||||
'created_at' => $now
|
||||
]
|
||||
);
|
||||
'expire_time' => date('Y-m-d H:i:s', $expireTime),
|
||||
'operator_id' => $this->uid,
|
||||
'created_time' => d()
|
||||
]);
|
||||
}else {
|
||||
Db::name('chat_group_mute')->insert([
|
||||
'group_id' => $groupId,
|
||||
'mute_time' => $muteTime,
|
||||
'user_id' => $targetUid,
|
||||
'expire_time' => date('Y-m-d H:i:s', $expireTime),
|
||||
'operator_id' => $this->uid,
|
||||
'created_time' => d()
|
||||
]);
|
||||
}
|
||||
|
||||
return json(['code' => 200, 'msg' => "禁言成功,时长{$muteTime}秒"]);
|
||||
// 发送通知 某某已被禁言
|
||||
|
||||
return $this->success("禁言成功,时长{$muteTime}秒");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -332,32 +366,24 @@ class GroupController extends ApiController
|
||||
*/
|
||||
public function unmuteMember(Request $request): Response
|
||||
{
|
||||
$uid = $request->uid;
|
||||
$groupId = $request->post('group_id', 0);
|
||||
$targetUid = $request->post('target_uid', 0);
|
||||
|
||||
$groupId = $request->post('group_id');
|
||||
$targetUid = $request->post('target_uid');
|
||||
$muteTime = $request->post('mute_time')?:3600; // 默认禁言1小时
|
||||
if (!$groupId || !$targetUid) {
|
||||
return json(['code' => 400, 'msg' => '缺少group_id或target_uid']);
|
||||
}
|
||||
|
||||
// 验证操作人权限
|
||||
$operatorRole = ChatGroupMember::where([
|
||||
$operatorRole = Db::name('chat_group_member')->where([
|
||||
'group_id' => $groupId,
|
||||
'user_id' => $uid,
|
||||
'user_id' => $this->uid,
|
||||
'quit_time' => null,
|
||||
'is_kicked' => 0
|
||||
])->value('role');
|
||||
if (!in_array($operatorRole, [1, 2])) {
|
||||
return json(['code' => 403, 'msg' => '仅群主和管理员可解除禁言']);
|
||||
return $this->error('仅群主和管理员可禁言');
|
||||
}
|
||||
|
||||
// 删除禁言记录
|
||||
ChatGroupMute::where([
|
||||
'group_id' => $groupId,
|
||||
'user_id' => $targetUid
|
||||
])->delete();
|
||||
|
||||
return json(['code' => 200, 'msg' => '解除禁言成功']);
|
||||
Db::name('chat_group_mute')->where(['group_id' => $groupId, 'user_id' => $targetUid])->delete();
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -365,59 +391,58 @@ class GroupController extends ApiController
|
||||
*/
|
||||
public function kickMember(Request $request): Response
|
||||
{
|
||||
$uid = $request->uid;
|
||||
$groupId = $request->post('group_id', 0);
|
||||
$targetUid = $request->post('target_uid', 0);
|
||||
|
||||
$groupId = $request->post('group_id');
|
||||
$targetUid = $request->post('target_uid');
|
||||
if (!$groupId || !$targetUid) {
|
||||
return json(['code' => 400, 'msg' => '缺少group_id或target_uid']);
|
||||
}
|
||||
|
||||
// 验证操作人权限
|
||||
$operatorRole = ChatGroupMember::where([
|
||||
$operatorRole = Db::name('chat_group_member')->where([
|
||||
'group_id' => $groupId,
|
||||
'user_id' => $uid,
|
||||
'user_id' => $this->uid,
|
||||
'quit_time' => null,
|
||||
'is_kicked' => 0
|
||||
])->value('role');
|
||||
if (!in_array($operatorRole, [1, 2])) {
|
||||
return json(['code' => 403, 'msg' => '仅群主和管理员可踢人']);
|
||||
return $this->error('仅群主和管理员可踢人');
|
||||
}
|
||||
|
||||
// 验证被踢用户是否在群内
|
||||
$targetMember = ChatGroupMember::where([
|
||||
$targetIsMember = Db::name('chat_group_member')->where([
|
||||
'group_id' => $groupId,
|
||||
'user_id' => $targetUid,
|
||||
'quit_time' => null,
|
||||
'is_kicked' => 0
|
||||
])->find();
|
||||
if (!$targetMember) {
|
||||
return json(['code' => 400, 'msg' => '被踢用户不在群内']);
|
||||
if (!$targetIsMember) {
|
||||
return $this->error('被踢用户不在群内');
|
||||
}
|
||||
|
||||
// 不能踢群主
|
||||
$groupOwner = ChatGroup::where('id', $groupId)->value('owner_id');
|
||||
$groupOwner = Db::name('chat_group')->where('id', $groupId)->value('owner_id');
|
||||
if ($targetUid == $groupOwner) {
|
||||
return json(['code' => 403, 'msg' => '不能踢群主']);
|
||||
return $this->error( '不能踢群主');
|
||||
}
|
||||
|
||||
// 不能踢自己
|
||||
if ($targetUid == $uid) {
|
||||
return json(['code' => 400, 'msg' => '不能踢自己']);
|
||||
if ($targetUid == $this->uid) {
|
||||
return $this->error('不能踢自己');
|
||||
}
|
||||
|
||||
// 标记为被踢
|
||||
$targetMember->quit_time = time();
|
||||
$targetMember->is_kicked = 1;
|
||||
$targetMember->save();
|
||||
|
||||
Db::name('chat_group_member')->where([
|
||||
'group_id' => $groupId,
|
||||
'user_id' => $targetUid,
|
||||
])->update([
|
||||
'quit_time' => d(),
|
||||
'is_kicked' => 1
|
||||
]);
|
||||
// 删除被踢用户的禁言记录
|
||||
ChatGroupMute::where([
|
||||
Db::name('chat_group_mute')->where([
|
||||
'group_id' => $groupId,
|
||||
'user_id' => $targetUid
|
||||
])->delete();
|
||||
|
||||
return json(['code' => 200, 'msg' => '踢人成功']);
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -425,35 +450,29 @@ class GroupController extends ApiController
|
||||
*/
|
||||
public function getMembers(Request $request): Response
|
||||
{
|
||||
$uid = $request->uid;
|
||||
$groupId = $request->get('group_id', 0);
|
||||
|
||||
$groupId = $request->post('group_id');
|
||||
if (!$groupId) {
|
||||
return json(['code' => 400, 'msg' => '缺少group_id']);
|
||||
return $this->error('缺少group_id');
|
||||
}
|
||||
|
||||
// 验证是否在群内
|
||||
$isMember = ChatGroupMember::where([
|
||||
// 验证被踢用户是否在群内
|
||||
$targetIsMember = Db::name('chat_group_member')->where([
|
||||
'group_id' => $groupId,
|
||||
'user_id' => $uid,
|
||||
'user_id' => $this->uid,
|
||||
'quit_time' => null,
|
||||
'is_kicked' => 0
|
||||
])->exists();
|
||||
if (!$isMember) {
|
||||
return json(['code' => 403, 'msg' => '不在群内,无法获取成员列表']);
|
||||
])->find();
|
||||
if (!$targetIsMember) {
|
||||
return $this->error('用户不在群内');
|
||||
}
|
||||
|
||||
// 获取成员列表
|
||||
$members = ChatGroupMember::where([
|
||||
$members = Db::name('chat_group_member')->where([
|
||||
'group_id' => $groupId,
|
||||
'quit_time' => null,
|
||||
'is_kicked' => 0
|
||||
])->join('chat_user', 'chat_group_member.user_id', 'chat_user.id')
|
||||
->select([
|
||||
'chat_group_member.user_id', 'chat_group_member.role',
|
||||
'chat_user.username', 'chat_user.avatar', 'chat_user.type'
|
||||
])->get()->toArray();
|
||||
|
||||
return json(['code' => 200, 'msg' => 'success', 'data' => $members]);
|
||||
])
|
||||
->alias('chat')
|
||||
->leftJoin('chat_user user', 'chat.user_id = user.user_id')
|
||||
->select();
|
||||
return $this->success($members);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user