diff --git a/app/Http/Controllers/Api/DialogController.php b/app/Http/Controllers/Api/DialogController.php index d92cf739e..cc6ac6b1a 100755 --- a/app/Http/Controllers/Api/DialogController.php +++ b/app/Http/Controllers/Api/DialogController.php @@ -11,7 +11,6 @@ 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; @@ -598,14 +597,7 @@ 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), - ] - ]); + $dialog->pushMsg("groupAdd", WebSocketDialog::formatData($dialog, $user->userid), $userids); return Base::retSuccess('创建成功', $dialog); } @@ -714,14 +706,7 @@ class DialogController extends AbstractController } // $dialog->joinGroup($userids); - PushTask::push([ - 'userid' => $userids, - 'msg' => [ - 'type' => 'dialog', - 'mode' => 'groupJoin', - 'data' => WebSocketDialog::formatData($dialog, $user->userid), - ] - ]); + $dialog->pushMsg("groupJoin", WebSocketDialog::formatData($dialog, $user->userid), $userids); return Base::retSuccess('添加成功'); } @@ -771,16 +756,7 @@ class DialogController extends AbstractController } // $dialog->exitGroup($userids); - PushTask::push([ - 'userid' => $userids, - 'msg' => [ - 'type' => 'dialog', - 'mode' => 'groupExit', - 'data' => [ - 'id' => $dialog->id, - ], - ] - ]); + $dialog->pushMsg("groupExit", null, $userids); return Base::retSuccess($type === 'remove' ? '移出成功' : '退出成功'); } @@ -816,16 +792,7 @@ class DialogController extends AbstractController } // $dialog->deleteDialog(); - PushTask::push([ - 'userid' => $dialog->dialogUser->pluck('userid')->toArray(), - 'msg' => [ - 'type' => 'dialog', - 'mode' => 'groupDelete', - 'data' => [ - 'id' => $dialog->id, - ], - ] - ]); + $dialog->pushMsg("groupDelete"); return Base::retSuccess('解散成功'); } } diff --git a/app/Models/WebSocketDialog.php b/app/Models/WebSocketDialog.php index 79da3fec3..704dde6d8 100644 --- a/app/Models/WebSocketDialog.php +++ b/app/Models/WebSocketDialog.php @@ -3,7 +3,9 @@ namespace App\Models; use App\Exceptions\ApiException; +use App\Tasks\PushTask; use Carbon\Carbon; +use Hhxsv5\LaravelS\Swoole\Task\Task; use Illuminate\Database\Eloquent\SoftDeletes; /** @@ -122,6 +124,35 @@ class WebSocketDialog extends AbstractModel return true; } + /** + * 推送消息 + * @param $action + * @param array $data 发送内容,默认为[id=>项目ID] + * @param array $userid 指定会员,默认为群组所有成员 + * @return void + */ + public function pushMsg($action, $data = null, $userid = null) + { + if ($data === null) { + $data = ['id' => $this->id]; + } + // + if ($userid === null) { + $userid = $this->dialogUser->pluck('userid')->toArray(); + } + // + $params = [ + 'userid' => $userid, + 'msg' => [ + 'type' => 'dialog', + 'mode' => $action, + 'data' => $data, + ] + ]; + $task = new PushTask($params, false); + Task::deliver($task); + } + /** * 获取对话(同时检验对话身份) * @param $dialog_id