群操作完结
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
|
public function setAnnouncement(Request $request): Response
|
||||||
{
|
{
|
||||||
$uid = $request->uid;
|
$groupId = $request->post('group_id');
|
||||||
$groupId = $request->post('group_id', 0);
|
$announcement = $request->post('announcement');
|
||||||
$announcement = $request->post('announcement', '');
|
|
||||||
|
|
||||||
if (!$groupId) {
|
if (!$groupId || !$announcement) {
|
||||||
return json(['code' => 400, 'msg' => '缺少group_id']);
|
return $this->error('缺少group_id或公共内容不能为空');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 验证权限
|
// 验证权限
|
||||||
$role = ChatGroupMember::where([
|
$role = Db::name('chat_group_member')->where([
|
||||||
'group_id' => $groupId,
|
'group_id' => $groupId,
|
||||||
'user_id' => $uid,
|
'user_id' => $this->uid,
|
||||||
'quit_time' => null,
|
'quit_time' => null,
|
||||||
'is_kicked' => 0
|
'is_kicked' => 0
|
||||||
])->value('role');
|
])->value('role');
|
||||||
if (!in_array($role, [1, 2])) { // 1=群主,2=管理员
|
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,
|
'announcement' => $announcement,
|
||||||
'updated_at' => time()
|
'updated_time' => d()
|
||||||
]);
|
]);
|
||||||
|
return $this->success();
|
||||||
return json(['code' => 200, 'msg' => '群公告设置成功']);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置群免打扰
|
* 设置群免打扰 (废弃)
|
||||||
*/
|
*/
|
||||||
public function setDoNotDisturb(Request $request): Response
|
public function setDoNotDisturb(Request $request): Response
|
||||||
{
|
{
|
||||||
$uid = $request->uid;
|
$groupId = $request->post('group_id');
|
||||||
$groupId = $request->post('group_id', 0);
|
$status = $request->post('status'); // 0=关闭,1=开启
|
||||||
$status = $request->post('status', 0); // 0=关闭,1=开启
|
|
||||||
|
|
||||||
if (!$groupId) {
|
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,
|
'group_id' => $groupId,
|
||||||
'user_id' => $uid,
|
'user_id' => $this->uid,
|
||||||
'quit_time' => null,
|
'quit_time' => null,
|
||||||
'is_kicked' => 0
|
'is_kicked' => 0
|
||||||
])->exists();
|
])->exists();
|
||||||
if (!$isMember) {
|
if (!$isMember) {
|
||||||
return json(['code' => 400, 'msg' => '不在群内']);
|
return $this->error('不在群内');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 生成会话ID
|
// 生成会话ID
|
||||||
@@ -268,63 +298,67 @@ class GroupController extends ApiController
|
|||||||
*/
|
*/
|
||||||
public function muteMember(Request $request): Response
|
public function muteMember(Request $request): Response
|
||||||
{
|
{
|
||||||
$uid = $request->uid;
|
$groupId = $request->post('group_id');
|
||||||
$groupId = $request->post('group_id', 0);
|
$targetUid = $request->post('target_uid');
|
||||||
$targetUid = $request->post('target_uid', 0);
|
$muteTime = $request->post('mute_time')?:3600; // 默认禁言1小时
|
||||||
$muteTime = $request->post('mute_time', 3600); // 默认禁言1小时
|
|
||||||
|
|
||||||
if (!$groupId || !$targetUid) {
|
if (!$groupId || !$targetUid) {
|
||||||
return json(['code' => 400, 'msg' => '缺少group_id或target_uid']);
|
return json(['code' => 400, 'msg' => '缺少group_id或target_uid']);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 验证操作人权限
|
// 验证操作人权限
|
||||||
$operatorRole = ChatGroupMember::where([
|
$operatorRole = Db::name('chat_group_member')->where([
|
||||||
'group_id' => $groupId,
|
'group_id' => $groupId,
|
||||||
'user_id' => $uid,
|
'user_id' => $this->uid,
|
||||||
'quit_time' => null,
|
'quit_time' => null,
|
||||||
'is_kicked' => 0
|
'is_kicked' => 0
|
||||||
])->value('role');
|
])->value('role');
|
||||||
if (!in_array($operatorRole, [1, 2])) {
|
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,
|
'group_id' => $groupId,
|
||||||
'user_id' => $targetUid,
|
'user_id' => $targetUid,
|
||||||
'quit_time' => null,
|
'quit_time' => null,
|
||||||
'is_kicked' => 0
|
'is_kicked' => 0
|
||||||
])->exists();
|
])->find();
|
||||||
if (!$targetIsMember) {
|
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) {
|
if ($targetUid == $groupOwner) {
|
||||||
return json(['code' => 403, 'msg' => '不能禁言群主']);
|
return $this->error('不能禁言群主');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 不能禁言自己
|
// 不能禁言自己
|
||||||
if ($targetUid == $uid) {
|
if ($targetUid == $this->uid) {
|
||||||
return json(['code' => 400, 'msg' => '不能禁言自己']);
|
return $this->error('不能禁言自己');
|
||||||
}
|
}
|
||||||
|
|
||||||
$now = time();
|
$now = time();
|
||||||
$expireTime = $muteTime > 0 ? $now + $muteTime : null;
|
$expireTime = $muteTime > 0 ? $now + $muteTime : null;
|
||||||
|
|
||||||
|
$res = Db::name('chat_group_mute')->where(['group_id' => $groupId, 'user_id' => $targetUid])->find();
|
||||||
// 新增/更新禁言记录
|
// 新增/更新禁言记录
|
||||||
ChatGroupMute::updateOrCreate(
|
if($res) {
|
||||||
['group_id' => $groupId, 'user_id' => $targetUid],
|
Db::name('chat_group_mute')->where(['group_id' => $groupId, 'user_id' => $targetUid])->save([
|
||||||
[
|
|
||||||
'mute_time' => $muteTime,
|
'mute_time' => $muteTime,
|
||||||
'expire_time' => $expireTime,
|
'expire_time' => date('Y-m-d H:i:s', $expireTime),
|
||||||
'operator_id' => $uid,
|
'operator_id' => $this->uid,
|
||||||
'created_at' => $now
|
'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
|
public function unmuteMember(Request $request): Response
|
||||||
{
|
{
|
||||||
$uid = $request->uid;
|
$groupId = $request->post('group_id');
|
||||||
$groupId = $request->post('group_id', 0);
|
$targetUid = $request->post('target_uid');
|
||||||
$targetUid = $request->post('target_uid', 0);
|
$muteTime = $request->post('mute_time')?:3600; // 默认禁言1小时
|
||||||
|
|
||||||
if (!$groupId || !$targetUid) {
|
if (!$groupId || !$targetUid) {
|
||||||
return json(['code' => 400, 'msg' => '缺少group_id或target_uid']);
|
return json(['code' => 400, 'msg' => '缺少group_id或target_uid']);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 验证操作人权限
|
// 验证操作人权限
|
||||||
$operatorRole = ChatGroupMember::where([
|
$operatorRole = Db::name('chat_group_member')->where([
|
||||||
'group_id' => $groupId,
|
'group_id' => $groupId,
|
||||||
'user_id' => $uid,
|
'user_id' => $this->uid,
|
||||||
'quit_time' => null,
|
'quit_time' => null,
|
||||||
'is_kicked' => 0
|
'is_kicked' => 0
|
||||||
])->value('role');
|
])->value('role');
|
||||||
if (!in_array($operatorRole, [1, 2])) {
|
if (!in_array($operatorRole, [1, 2])) {
|
||||||
return json(['code' => 403, 'msg' => '仅群主和管理员可解除禁言']);
|
return $this->error('仅群主和管理员可禁言');
|
||||||
}
|
}
|
||||||
|
Db::name('chat_group_mute')->where(['group_id' => $groupId, 'user_id' => $targetUid])->delete();
|
||||||
// 删除禁言记录
|
return $this->success();
|
||||||
ChatGroupMute::where([
|
|
||||||
'group_id' => $groupId,
|
|
||||||
'user_id' => $targetUid
|
|
||||||
])->delete();
|
|
||||||
|
|
||||||
return json(['code' => 200, 'msg' => '解除禁言成功']);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -365,59 +391,58 @@ class GroupController extends ApiController
|
|||||||
*/
|
*/
|
||||||
public function kickMember(Request $request): Response
|
public function kickMember(Request $request): Response
|
||||||
{
|
{
|
||||||
$uid = $request->uid;
|
$groupId = $request->post('group_id');
|
||||||
$groupId = $request->post('group_id', 0);
|
$targetUid = $request->post('target_uid');
|
||||||
$targetUid = $request->post('target_uid', 0);
|
|
||||||
|
|
||||||
if (!$groupId || !$targetUid) {
|
if (!$groupId || !$targetUid) {
|
||||||
return json(['code' => 400, 'msg' => '缺少group_id或target_uid']);
|
return json(['code' => 400, 'msg' => '缺少group_id或target_uid']);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 验证操作人权限
|
// 验证操作人权限
|
||||||
$operatorRole = ChatGroupMember::where([
|
$operatorRole = Db::name('chat_group_member')->where([
|
||||||
'group_id' => $groupId,
|
'group_id' => $groupId,
|
||||||
'user_id' => $uid,
|
'user_id' => $this->uid,
|
||||||
'quit_time' => null,
|
'quit_time' => null,
|
||||||
'is_kicked' => 0
|
'is_kicked' => 0
|
||||||
])->value('role');
|
])->value('role');
|
||||||
if (!in_array($operatorRole, [1, 2])) {
|
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,
|
'group_id' => $groupId,
|
||||||
'user_id' => $targetUid,
|
'user_id' => $targetUid,
|
||||||
'quit_time' => null,
|
'quit_time' => null,
|
||||||
'is_kicked' => 0
|
'is_kicked' => 0
|
||||||
])->find();
|
])->find();
|
||||||
if (!$targetMember) {
|
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) {
|
if ($targetUid == $groupOwner) {
|
||||||
return json(['code' => 403, 'msg' => '不能踢群主']);
|
return $this->error( '不能踢群主');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 不能踢自己
|
// 不能踢自己
|
||||||
if ($targetUid == $uid) {
|
if ($targetUid == $this->uid) {
|
||||||
return json(['code' => 400, 'msg' => '不能踢自己']);
|
return $this->error('不能踢自己');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 标记为被踢
|
// 标记为被踢
|
||||||
$targetMember->quit_time = time();
|
Db::name('chat_group_member')->where([
|
||||||
$targetMember->is_kicked = 1;
|
'group_id' => $groupId,
|
||||||
$targetMember->save();
|
'user_id' => $targetUid,
|
||||||
|
])->update([
|
||||||
|
'quit_time' => d(),
|
||||||
|
'is_kicked' => 1
|
||||||
|
]);
|
||||||
// 删除被踢用户的禁言记录
|
// 删除被踢用户的禁言记录
|
||||||
ChatGroupMute::where([
|
Db::name('chat_group_mute')->where([
|
||||||
'group_id' => $groupId,
|
'group_id' => $groupId,
|
||||||
'user_id' => $targetUid
|
'user_id' => $targetUid
|
||||||
])->delete();
|
])->delete();
|
||||||
|
return $this->success();
|
||||||
return json(['code' => 200, 'msg' => '踢人成功']);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -425,35 +450,29 @@ class GroupController extends ApiController
|
|||||||
*/
|
*/
|
||||||
public function getMembers(Request $request): Response
|
public function getMembers(Request $request): Response
|
||||||
{
|
{
|
||||||
$uid = $request->uid;
|
$groupId = $request->post('group_id');
|
||||||
$groupId = $request->get('group_id', 0);
|
|
||||||
|
|
||||||
if (!$groupId) {
|
if (!$groupId) {
|
||||||
return json(['code' => 400, 'msg' => '缺少group_id']);
|
return $this->error('缺少group_id');
|
||||||
}
|
}
|
||||||
|
// 验证被踢用户是否在群内
|
||||||
// 验证是否在群内
|
$targetIsMember = Db::name('chat_group_member')->where([
|
||||||
$isMember = ChatGroupMember::where([
|
|
||||||
'group_id' => $groupId,
|
'group_id' => $groupId,
|
||||||
'user_id' => $uid,
|
'user_id' => $this->uid,
|
||||||
'quit_time' => null,
|
'quit_time' => null,
|
||||||
'is_kicked' => 0
|
'is_kicked' => 0
|
||||||
])->exists();
|
])->find();
|
||||||
if (!$isMember) {
|
if (!$targetIsMember) {
|
||||||
return json(['code' => 403, 'msg' => '不在群内,无法获取成员列表']);
|
return $this->error('用户不在群内');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取成员列表
|
// 获取成员列表
|
||||||
$members = ChatGroupMember::where([
|
$members = Db::name('chat_group_member')->where([
|
||||||
'group_id' => $groupId,
|
'group_id' => $groupId,
|
||||||
'quit_time' => null,
|
'quit_time' => null,
|
||||||
'is_kicked' => 0
|
'is_kicked' => 0
|
||||||
])->join('chat_user', 'chat_group_member.user_id', 'chat_user.id')
|
])
|
||||||
->select([
|
->alias('chat')
|
||||||
'chat_group_member.user_id', 'chat_group_member.role',
|
->leftJoin('chat_user user', 'chat.user_id = user.user_id')
|
||||||
'chat_user.username', 'chat_user.avatar', 'chat_user.type'
|
->select();
|
||||||
])->get()->toArray();
|
return $this->success($members);
|
||||||
|
|
||||||
return json(['code' => 200, 'msg' => 'success', 'data' => $members]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ Route::group('/api/chat', function () {
|
|||||||
Route::post('/join', app\chat\controller\GroupController::class . '@join'); // 加群
|
Route::post('/join', app\chat\controller\GroupController::class . '@join'); // 加群
|
||||||
Route::post('/getgrepurl', app\chat\controller\GroupController::class . '@getgrepurl'); // 群邀请参数
|
Route::post('/getgrepurl', app\chat\controller\GroupController::class . '@getgrepurl'); // 群邀请参数
|
||||||
Route::post('/quit', app\chat\controller\GroupController::class . '@quit'); // 退群
|
Route::post('/quit', app\chat\controller\GroupController::class . '@quit'); // 退群
|
||||||
|
Route::post('/tarsgroup', app\chat\controller\GroupController::class . '@tarsgroup'); // 转让群
|
||||||
Route::post('/announcement', app\chat\controller\GroupController::class . '@setAnnouncement'); // 群公告
|
Route::post('/announcement', app\chat\controller\GroupController::class . '@setAnnouncement'); // 群公告
|
||||||
Route::post('/do-not-disturb', app\chat\controller\GroupController::class . '@setDoNotDisturb'); // 免打扰
|
Route::post('/do-not-disturb', app\chat\controller\GroupController::class . '@setDoNotDisturb'); // 免打扰
|
||||||
Route::post('/mute', app\chat\controller\GroupController::class . '@muteMember'); // 禁言
|
Route::post('/mute', app\chat\controller\GroupController::class . '@muteMember'); // 禁言
|
||||||
|
|||||||
Reference in New Issue
Block a user