no message

This commit is contained in:
kuaifan 2022-04-15 00:07:45 +08:00
parent 8df3611e7d
commit 8c05d8791d
3 changed files with 71 additions and 77 deletions

View File

@ -597,8 +597,9 @@ class DialogController extends AbstractController
if (empty($dialog)) {
return Base::retError('创建群组失败');
}
$dialog->pushMsg("groupAdd", $dialog->formatData($user->userid), $userids);
return Base::retSuccess('创建成功', $dialog);
$data = WebSocketDialog::find($dialog->id)?->formatData($user->userid);
$dialog->pushMsg("groupAdd", $data, $userids);
return Base::retSuccess('创建成功', $data);
}
/**
@ -630,10 +631,7 @@ class DialogController extends AbstractController
return Base::retError('群名称最长限制100个字');
}
//
$dialog = WebSocketDialog::checkDialog($dialog_id);
if ($dialog->owner_id != $user->userid) {
return Base::retError('仅限群主操作');
}
$dialog = WebSocketDialog::checkDialog($dialog_id, true);
//
$dialog->name = $chatName;
$dialog->save();
@ -694,17 +692,9 @@ class DialogController extends AbstractController
return Base::retError('请选择群成员');
}
//
$dialog = WebSocketDialog::checkDialog($dialog_id);
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 = WebSocketDialog::checkDialog($dialog_id, true);
//
$dialog->checkGroup();
$dialog->joinGroup($userids);
$dialog->pushMsg("groupJoin", $dialog->formatData($user->userid), $userids);
return Base::retSuccess('添加成功');
@ -744,17 +734,9 @@ class DialogController extends AbstractController
return Base::retError('请选择群成员');
}
//
$dialog = WebSocketDialog::checkDialog($dialog_id);
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 = WebSocketDialog::checkDialog($dialog_id, $type === 'remove');
//
$dialog->checkGroup();
$dialog->exitGroup($userids);
$dialog->pushMsg("groupExit", null, $userids);
return Base::retSuccess($type === 'remove' ? '移出成功' : '退出成功');
@ -776,21 +758,13 @@ class DialogController extends AbstractController
*/
public function group__disband()
{
$user = User::auth();
User::auth();
//
$dialog_id = intval(Request::input('dialog_id'));
//
$dialog = WebSocketDialog::checkDialog($dialog_id);
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 = WebSocketDialog::checkDialog($dialog_id, true);
//
$dialog->checkGroup();
$dialog->deleteDialog();
$dialog->pushMsg("groupDelete");
return Base::retSuccess('解散成功');

View File

@ -51,6 +51,45 @@ class WebSocketDialog extends AbstractModel
return $this->hasMany(WebSocketDialogUser::class, 'dialog_id', 'id');
}
/**
* 格式化对话
* @param int $userid 会员ID
* @return $this
*/
public function formatData($userid)
{
// 最后消息
$last_msg = WebSocketDialogMsg::whereDialogId($this->id)->orderByDesc('id')->first();
$this->last_msg = $last_msg;
// 未读信息
$this->unread = WebSocketDialogMsgRead::whereDialogId($this->id)->whereUserid($userid)->whereReadAt(null)->count();
$this->mark_unread = $this->mark_unread ?? WebSocketDialogUser::whereDialogId($this->id)->whereUserid($userid)->value('mark_unread');
// 对话人数
$builder = WebSocketDialogUser::whereDialogId($this->id);
$this->people = $builder->count();
// 对方信息
$this->dialog_user = null;
$this->group_info = null;
$this->top_at = $this->top_at ?? WebSocketDialogUser::whereDialogId($this->id)->whereUserid($userid)->value('top_at');
switch ($this->type) {
case "user":
$dialog_user = $builder->where('userid', '!=', $userid)->first();
$this->name = User::userid2nickname($dialog_user->userid);
$this->dialog_user = $dialog_user;
break;
case "group":
if ($this->group_type === 'project') {
$this->group_info = Project::withTrashed()->select(['id', 'name', 'archived_at', 'deleted_at'])->whereDialogId($this->id)->first()?->cancelAppend()->cancelHidden();
$this->name = $this->group_info ? $this->group_info->name : '';
} elseif ($this->group_type === 'task') {
$this->group_info = ProjectTask::withTrashed()->select(['id', 'name', 'complete_at', 'archived_at', 'deleted_at'])->whereDialogId($this->id)->first()?->cancelAppend()->cancelHidden();
$this->name = $this->group_info ? $this->group_info->name : '';
}
break;
}
return $this;
}
/**
* 加入聊天室
* @param int|array $userid 加入的会员ID或会员ID组
@ -124,6 +163,20 @@ class WebSocketDialog extends AbstractModel
return true;
}
/**
* 检查群组类型
* @return void
*/
public function checkGroup($groupType = 'user')
{
if ($this->type !== 'group') {
throw new ApiException('仅限群组操作');
}
if ($this->group_type !== $groupType) {
throw new ApiException('操作的群组类型错误');
}
}
/**
* 推送消息
* @param $action
@ -156,9 +209,10 @@ class WebSocketDialog extends AbstractModel
/**
* 获取对话(同时检验对话身份)
* @param $dialog_id
* @param bool $checkOwner 是否校验群组身份
* @return self
*/
public static function checkDialog($dialog_id)
public static function checkDialog($dialog_id, $checkOwner = false)
{
$dialog = WebSocketDialog::find($dialog_id);
if (empty($dialog)) {
@ -166,6 +220,10 @@ class WebSocketDialog extends AbstractModel
}
//
$userid = User::userid();
if ($checkOwner === true && $dialog->owner_id != $userid) {
throw new ApiException('仅限群主操作');
}
//
if ($dialog->type === 'group' && $dialog->group_type === 'task') {
// 任务群对话校验是否在项目内
$project_id = intval(ProjectTask::whereDialogId($dialog->id)->value('project_id'));
@ -181,45 +239,6 @@ class WebSocketDialog extends AbstractModel
return $dialog;
}
/**
* 格式化对话
* @param int $userid 会员ID
* @return $this
*/
public function formatData($userid)
{
// 最后消息
$last_msg = WebSocketDialogMsg::whereDialogId($this->id)->orderByDesc('id')->first();
$this->last_msg = $last_msg;
// 未读信息
$this->unread = WebSocketDialogMsgRead::whereDialogId($this->id)->whereUserid($userid)->whereReadAt(null)->count();
$this->mark_unread = $this->mark_unread ?? WebSocketDialogUser::whereDialogId($this->id)->whereUserid($userid)->value('mark_unread');
// 对话人数
$builder = WebSocketDialogUser::whereDialogId($this->id);
$this->people = $builder->count();
// 对方信息
$this->dialog_user = null;
$this->group_info = null;
$this->top_at = $this->top_at ?? WebSocketDialogUser::whereDialogId($this->id)->whereUserid($userid)->value('top_at');
switch ($this->type) {
case "user":
$dialog_user = $builder->where('userid', '!=', $userid)->first();
$this->name = User::userid2nickname($dialog_user->userid);
$this->dialog_user = $dialog_user;
break;
case "group":
if ($this->group_type === 'project') {
$this->group_info = Project::withTrashed()->select(['id', 'name', 'archived_at', 'deleted_at'])->whereDialogId($this->id)->first()?->cancelAppend()->cancelHidden();
$this->name = $this->group_info ? $this->group_info->name : '';
} elseif ($this->group_type === 'task') {
$this->group_info = ProjectTask::withTrashed()->select(['id', 'name', 'complete_at', 'archived_at', 'deleted_at'])->whereDialogId($this->id)->first()?->cancelAppend()->cancelHidden();
$this->name = $this->group_info ? $this->group_info->name : '';
}
break;
}
return $this;
}
/**
* 创建聊天室
* @param string $name 聊天室名称

View File

@ -562,6 +562,7 @@ export default {
$A.messageSuccess(msg);
this.createGroupShow = false;
this.createGroupData = {};
this.$store.dispatch("saveDialog", data);
this.goForward({name: 'manage-messenger', params: {dialogId: data.id}});
}).catch(({msg}) => {
$A.modalError(msg);