fix: 修复退出群组不完全的问题

This commit is contained in:
kuaifan 2024-11-23 01:30:32 +08:00
parent bab37530e4
commit f28b99b516
3 changed files with 111 additions and 23 deletions

View File

@ -7,8 +7,9 @@ use Cache;
use Request;
use Redirect;
use Response;
use App\Module\Doo;
use App\Models\File;
use App\Models\UserTransfer;
use App\Module\Doo;
use App\Module\Base;
use App\Module\Extranet;
use App\Module\RandomColor;
@ -24,6 +25,7 @@ use App\Tasks\CloseMeetingRoomTask;
use App\Tasks\UnclaimedTaskRemindTask;
use Hhxsv5\LaravelS\Swoole\Task\Task;
use Laravolt\Avatar\Avatar;
use Swoole\Coroutine;
/**
@ -259,6 +261,27 @@ class IndexController extends InvokeController
return "success";
}
/**
* 迁移辅助路由
* @return array
*/
public function migration__userdialog()
{
if (Request::header('app-key') !== env('APP_KEY')) {
return Base::retError("key error");
}
go(function() {
Coroutine::sleep(3);
UserTransfer::orderBy('id')->chunkById(10, function ($transfers) {
/** @var UserTransfer $transfer */
foreach ($transfers as $transfer) {
$transfer->exitDialog();
}
});
});
return Base::retSuccess('success');
}
/**
* 桌面客户端发布
*/

View File

@ -50,29 +50,58 @@ class UserTransfer extends AbstractModel
// 移交文件
File::transfer($this->original_userid, $this->new_userid);
// 离职移出群组
WebSocketDialog::select(['web_socket_dialogs.*'])
->join('web_socket_dialog_users as u', 'web_socket_dialogs.id', '=', 'u.dialog_id')
->where('web_socket_dialogs.type', 'group')
->where('web_socket_dialogs.group_type', '!=', 'okr')
->where('u.userid', $this->original_userid)
->orderByDesc('web_socket_dialogs.id')
->chunk(100, function($list) {
/** @var WebSocketDialog $dialog */
foreach ($list as $dialog) {
// 离职员工退出群
$dialog->exitGroup($this->original_userid, 'remove', false, false);
if ($dialog->owner_id === $this->original_userid) {
// 如果是群主则把交接人设为群主
$dialog->owner_id = $this->new_userid;
if ($dialog->save()) {
$dialog->joinGroup($this->new_userid, 0);
$dialog->pushMsg("groupUpdate", [
'id' => $dialog->id,
'owner_id' => $dialog->owner_id,
]);
}
$this->exitDialog();
}
/**
* 退出群组
* @return void
*/
public function exitDialog()
{
$lastId = 0;
$limit = 100;
while (true) {
$query = WebSocketDialog::select(['web_socket_dialogs.*'])
->join('web_socket_dialog_users as u', 'web_socket_dialogs.id', '=', 'u.dialog_id')
->where('web_socket_dialogs.type', 'group')
->where('web_socket_dialogs.group_type', '!=', 'okr')
->where('u.userid', $this->original_userid)
->orderBy('web_socket_dialogs.id')
->limit($limit);
if ($lastId) {
$query->where('web_socket_dialogs.id', '>', $lastId);
}
$list = $query->get();
// 没有数据了就退出
if ($list->isEmpty()) {
break;
}
// 记录最后一条记录的ID
$lastId = $list->last()->id;
// 离职员工退出群
foreach ($list as $dialog) {
$dialog->exitGroup($this->original_userid, 'remove', false, false);
if ($dialog->owner_id === $this->original_userid) {
// 如果是群主则把交接人设为群主
$dialog->owner_id = $this->new_userid;
if ($dialog->save()) {
$dialog->joinGroup($this->new_userid, 0);
$dialog->pushMsg("groupUpdate", [
'id' => $dialog->id,
'owner_id' => $dialog->owner_id,
]);
}
}
});
}
// 如果返回的数据少于限制数,说明已经是最后一批
if ($list->count() < $limit) {
break;
}
}
}
}

View File

@ -0,0 +1,36 @@
<?php
use App\Models\UserTransfer;
use Illuminate\Database\Migrations\Migration;
class ProcessUserTransferDialog extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
if (UserTransfer::count() === 0) {
return;
}
try {
\App\Module\Ihttp::ihttp_request('http://127.0.0.1:' . config('laravels.listen_port') . '/migration/userdialog', [], [
'app-key' => env('APP_KEY')
], 10);
} catch (\Throwable $e) {
info($e);
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}