优化群动作通知

This commit is contained in:
kuaifan 2022-04-14 23:32:58 +08:00
parent 6ca13bf263
commit 74c20fc57e
3 changed files with 112 additions and 82 deletions

View File

@ -11,6 +11,7 @@ use App\Models\WebSocketDialogMsg;
use App\Models\WebSocketDialogMsgRead;
use App\Models\WebSocketDialogUser;
use App\Module\Base;
use App\Tasks\PushTask;
use Carbon\Carbon;
use Request;
use Response;
@ -597,6 +598,14 @@ class DialogController extends AbstractController
if (empty($dialog)) {
return Base::retError('创建群组失败');
}
PushTask::push([
'userid' => $userids,
'msg' => [
'type' => 'dialog',
'mode' => 'groupAdd',
'data' => WebSocketDialog::formatData($dialog, $user->userid),
]
]);
return Base::retSuccess('创建成功', $dialog);
}
@ -697,8 +706,22 @@ class DialogController extends AbstractController
if ($dialog->owner_id != $user->userid) {
return Base::retError('仅限群主操作');
}
if ($dialog->type !== 'group') {
return Base::retError('此操作仅限群组');
}
if ($dialog->group_type !== 'user') {
return Base::retError('此操作仅限个人群组');
}
//
$dialog->joinGroup($userids);
PushTask::push([
'userid' => $userids,
'msg' => [
'type' => 'dialog',
'mode' => 'groupJoin',
'data' => WebSocketDialog::formatData($dialog, $user->userid),
]
]);
return Base::retSuccess('添加成功');
}
@ -740,8 +763,24 @@ class DialogController extends AbstractController
if ($type === 'remove' && $dialog->owner_id != $user->userid) {
return Base::retError('仅限群主操作');
}
if ($dialog->type !== 'group') {
return Base::retError('此操作仅限群组');
}
if ($dialog->group_type !== 'user') {
return Base::retError('此操作仅限个人群组');
}
//
$dialog->exitGroup($userids);
PushTask::push([
'userid' => $userids,
'msg' => [
'type' => 'dialog',
'mode' => 'groupExit',
'data' => [
'id' => $dialog->id,
],
]
]);
return Base::retSuccess($type === 'remove' ? '移出成功' : '退出成功');
}
@ -769,8 +808,24 @@ class DialogController extends AbstractController
if ($dialog->owner_id != $user->userid) {
return Base::retError('仅限群主操作');
}
if ($dialog->type !== 'group') {
return Base::retError('此操作仅限群组');
}
if ($dialog->group_type !== 'user') {
return Base::retError('此操作仅限个人群组');
}
//
$dialog->disbandGroup();
$dialog->deleteDialog();
PushTask::push([
'userid' => $dialog->dialogUser->pluck('userid')->toArray(),
'msg' => [
'type' => 'dialog',
'mode' => 'groupDelete',
'data' => [
'id' => $dialog->id,
],
]
]);
return Base::retSuccess('解散成功');
}
}

View File

@ -49,6 +49,52 @@ class WebSocketDialog extends AbstractModel
return $this->hasMany(WebSocketDialogUser::class, 'dialog_id', 'id');
}
/**
* 加入聊天室
* @param int|array $userid 加入的会员ID或会员ID组
* @return bool
*/
public function joinGroup($userid)
{
AbstractModel::transaction(function () use ($userid) {
foreach (is_array($userid) ? $userid : [$userid] as $value) {
if ($value > 0) {
WebSocketDialogUser::updateInsert([
'dialog_id' => $this->id,
'userid' => $value,
]);
}
}
});
return true;
}
/**
* 退出聊天室
* @param int|array $userid 加入的会员ID或会员ID组
* @return bool
*/
public function exitGroup($userid)
{
$builder = WebSocketDialogUser::whereDialogId($this->id);
if (is_array($userid)) {
$builder->whereIn('userid', $userid);
} else {
$builder->whereUserid($userid);
}
$builder->chunkById(100, function($list) {
/** @var WebSocketDialogUser $item */
foreach ($list as $item) {
if ($item->userid == $this->owner_id) {
// 群主不可退出
continue;
}
$item->delete();
}
});
return true;
}
/**
* 删除会话
* @return bool
@ -76,87 +122,6 @@ class WebSocketDialog extends AbstractModel
return true;
}
/**
* 加入聊天室
* @param int|array $userid 加入的会员ID或会员ID组
* @return bool
*/
public function joinGroup($userid)
{
if ($this->type !== 'group') {
throw new ApiException('此操作仅限群组');
}
if ($this->group_type !== 'user') {
throw new ApiException('此操作仅限个人群组');
}
AbstractModel::transaction(function () use ($userid) {
foreach (is_array($userid) ? $userid : [$userid] as $value) {
if ($value > 0) {
WebSocketDialogUser::updateInsert([
'dialog_id' => $this->id,
'userid' => $value,
]);
}
}
});
return true;
}
/**
* 退出聊天室
* @param int|array $userid 加入的会员ID或会员ID组
* @return bool
*/
public function exitGroup($userid)
{
if ($this->type !== 'group') {
throw new ApiException('此操作仅限群组');
}
if ($this->group_type !== 'user') {
throw new ApiException('此操作仅限个人群组');
}
$builder = WebSocketDialogUser::whereDialogId($this->id);
if (is_array($userid)) {
$builder->whereIn('userid', $userid);
} else {
$builder->whereUserid($userid);
}
$builder->chunkById(100, function($list) {
/** @var WebSocketDialogUser $item */
foreach ($list as $item) {
if ($item->userid == $this->owner_id) {
// 群主不可退出
continue;
}
$item->delete();
}
});
return true;
}
/**
* 解散群组
* @return bool
*/
public function disbandGroup()
{
if ($this->type !== 'group') {
throw new ApiException('此操作仅限群组');
}
if ($this->group_type !== 'user') {
throw new ApiException('此操作仅限个人群组');
}
return AbstractModel::transaction(function() {
WebSocketDialogUser::whereDialogId($this->id)->chunkById(100, function($list) {
/** @var WebSocketDialogUser $item */
foreach ($list as $item) {
$item->delete();
}
});
return $this->delete();
});
}
/**
* 获取对话(同时检验对话身份)
* @param $dialog_id

View File

@ -2284,6 +2284,16 @@ export default {
dispatch("saveDialogMsg", data)
}
break;
case 'groupAdd':
case 'groupJoin':
// 群组添加、加入
dispatch("saveDialog", data)
break;
case 'groupExit':
case 'groupDelete':
// 群组退出、解散
dispatch("forgetDialog", data.id)
break;
}
})(msgDetail);
break;