hyperf-chat/app/Service/GroupService.php

398 lines
13 KiB
PHP
Raw Normal View History

2021-01-28 20:07:14 +08:00
<?php
2021-03-26 22:03:11 +08:00
declare(strict_types=1);
2021-01-28 20:07:14 +08:00
namespace App\Service;
2021-05-20 22:23:48 +08:00
use App\Cache\LastMessage;
2021-07-05 21:52:44 +08:00
use App\Constants\TalkMsgType;
use App\Constants\TalkType;
use App\Model\Chat\TalkRecords;
use App\Model\Chat\TalkRecordsInvite;
use App\Model\Group\Group;
use App\Model\Group\GroupMember;
2021-07-05 21:52:44 +08:00
use App\Model\TalkList;
2021-01-28 20:07:14 +08:00
use Hyperf\DbConnection\Db;
use Exception;
/**
* Class GroupService
2021-03-26 22:03:11 +08:00
*
2021-01-28 20:07:14 +08:00
* @package App\Service
*/
class GroupService extends BaseService
{
/**
* 获取用户所在的群聊
*
* @param int $user_id 用户ID
* @return array
*/
public function getGroups(int $user_id): array
{
2021-03-26 22:03:11 +08:00
$items = GroupMember::join('group', 'group.id', '=', 'group_member.group_id')
2021-01-28 20:07:14 +08:00
->where([
['group_member.user_id', '=', $user_id],
['group_member.is_quit', '=', 0]
2021-01-28 20:07:14 +08:00
])
->orderBy('id', 'desc')
->get([
'group.id',
'group.group_name',
'group.avatar',
2021-03-26 22:03:11 +08:00
'group.profile',
'group_member.leader',
])->toArray();
2021-01-28 20:07:14 +08:00
2021-07-05 21:52:44 +08:00
$list = [];
if ($items) {
$list = TalkList::query()->where('user_id', $user_id)
->where('talk_type', TalkType::GROUP_CHAT)
->whereIn('receiver_id', array_column($items, 'id'))
->get(['receiver_id', 'is_disturb'])->keyBy('receiver_id')->toArray();
}
2021-03-26 22:03:11 +08:00
2021-01-28 20:07:14 +08:00
foreach ($items as $key => $item) {
2021-07-05 21:52:44 +08:00
$items[$key]['is_disturb'] = isset($list[$item['id']]) ? $list[$item['id']]['is_disturb'] : 0;
2021-01-28 20:07:14 +08:00
}
return $items;
}
/**
* 创建群组
*
2021-04-20 16:30:57 +08:00
* @param int $user_id 用户ID
2021-01-28 20:07:14 +08:00
* @param array $group_info 群聊名称
* @param array $friend_ids 好友的用户ID
* @return array
*/
public function create(int $user_id, array $group_info, $friend_ids = [])
{
2021-03-31 22:30:10 +08:00
$invite_ids = implode(',', $friend_ids);
2021-01-28 20:07:14 +08:00
$friend_ids[] = $user_id;
$groupMember = [];
$chatList = [];
2021-01-28 20:07:14 +08:00
Db::beginTransaction();
try {
$insRes = Group::create([
'creator_id' => $user_id,
2021-01-28 20:07:14 +08:00
'group_name' => $group_info['name'],
2021-03-26 22:03:11 +08:00
'avatar' => $group_info['avatar'],
'profile' => $group_info['profile'],
'max_num' => Group::MAX_MEMBER_NUM,
'is_overt' => 0,
'is_mute' => 0,
'is_dismiss' => 0,
2021-01-28 20:07:14 +08:00
'created_at' => date('Y-m-d H:i:s')
]);
2021-07-05 21:52:44 +08:00
foreach ($friend_ids as $friend_id) {
2021-01-28 20:07:14 +08:00
$groupMember[] = [
2021-03-26 22:03:11 +08:00
'group_id' => $insRes->id,
2021-07-05 21:52:44 +08:00
'user_id' => $friend_id,
'leader' => $user_id == $friend_id ? 2 : 0,
2021-01-28 20:07:14 +08:00
'created_at' => date('Y-m-d H:i:s'),
];
$chatList[] = [
2021-07-05 21:52:44 +08:00
'talk_type' => TalkType::GROUP_CHAT,
'user_id' => $friend_id,
'receiver_id' => $insRes->id,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s')
2021-01-28 20:07:14 +08:00
];
}
if (!Db::table('group_member')->insert($groupMember)) {
2021-01-28 20:07:14 +08:00
throw new Exception('创建群成员信息失败');
}
2021-07-05 21:52:44 +08:00
if (!Db::table('talk_list')->insert($chatList)) {
2021-01-28 20:07:14 +08:00
throw new Exception('创建群成员的聊天列表失败');
}
2021-07-05 21:52:44 +08:00
$result = TalkRecords::create([
'talk_type' => TalkType::GROUP_CHAT,
'user_id' => 0,
'receiver_id' => $insRes->id,
'msg_type' => TalkMsgType::GROUP_INVITE_MESSAGE,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s')
2021-01-28 20:07:14 +08:00
]);
2021-07-05 21:52:44 +08:00
TalkRecordsInvite::create([
2021-03-26 22:03:11 +08:00
'record_id' => $result->id,
'type' => 1,
2021-01-28 20:07:14 +08:00
'operate_user_id' => $user_id,
2021-03-31 22:30:10 +08:00
'user_ids' => $invite_ids
2021-01-28 20:07:14 +08:00
]);
Db::commit();
} catch (Exception $e) {
Db::rollBack();
return [false, 0];
}
2021-07-05 21:52:44 +08:00
LastMessage::getInstance()->save(TalkType::GROUP_CHAT, $user_id, $insRes->id, [
2021-05-20 22:23:48 +08:00
'text' => '[入群通知]',
'created_at' => date('Y-m-d H:i:s')
]);
2021-01-28 20:07:14 +08:00
return [true, ['record_id' => $result->id, 'group_id' => $insRes->id]];
}
/**
2021-03-26 22:03:11 +08:00
* 解散群组(群主权限)
2021-01-28 20:07:14 +08:00
*
* @param int $group_id 群ID
2021-04-20 16:30:57 +08:00
* @param int $user_id 用户ID
2021-01-28 20:07:14 +08:00
* @return bool
*/
public function dismiss(int $group_id, int $user_id)
{
$group = Group::where('id', $group_id)->first(['creator_id', 'is_dismiss']);
if (!$group || $group->creator_id != $user_id || $group->is_dismiss == 1) {
2021-01-28 20:07:14 +08:00
return false;
}
2021-03-26 22:03:11 +08:00
DB::transaction(function () use ($group_id, $user_id) {
Group::where('id', $group_id)->where('creator_id', $user_id)->update([
'is_dismiss' => 1,
'dismissed_at' => date('Y-m-d H:i:s'),
]);
GroupMember::where('group_id', $group_id)->update([
'is_quit' => 1,
'deleted_at' => date('Y-m-d H:i:s'),
]);
2021-07-05 21:52:44 +08:00
});
2021-03-26 22:03:11 +08:00
return true;
2021-01-28 20:07:14 +08:00
}
/**
* 邀请加入群组
*
2021-04-20 16:30:57 +08:00
* @param int $user_id 用户ID
* @param int $group_id 聊天群ID
2021-01-28 20:07:14 +08:00
* @param array $friend_ids 被邀请的用户ID
* @return array
*/
public function invite(int $user_id, int $group_id, $friend_ids = [])
{
2021-03-26 22:03:11 +08:00
if (!$friend_ids) return [false, 0];
2021-01-28 20:07:14 +08:00
$info = GroupMember::where('group_id', $group_id)->where('user_id', $user_id)->first(['id', 'is_quit']);
// 判断主动邀请方是否属于聊天群成员
if (!$info && $info->is_quit == 1) {
2021-01-28 20:07:14 +08:00
return [false, 0];
}
$updateArr = $insertArr = $updateArr1 = $insertArr1 = [];
$members = GroupMember::where('group_id', $group_id)->whereIn('user_id', $friend_ids)->get(['id', 'user_id', 'is_quit'])->keyBy('user_id')->toArray();
2021-07-05 21:52:44 +08:00
$chatArr = TalkList::where('talk_type', TalkType::GROUP_CHAT)
->where('receiver_id', $group_id)
->whereIn('user_id', $friend_ids)
->get(['id', 'user_id', 'is_delete'])
->keyBy('user_id')->toArray();
2021-01-28 20:07:14 +08:00
foreach ($friend_ids as $uid) {
2021-03-26 22:03:11 +08:00
if (!isset($members[$uid])) {
$insertArr[] = [
2021-03-26 22:03:11 +08:00
'group_id' => $group_id,
'user_id' => $uid,
'created_at' => date('Y-m-d H:i:s')
];
2021-07-05 21:52:44 +08:00
} else if ($members[$uid]['is_quit'] == 1) {
2021-01-28 20:07:14 +08:00
$updateArr[] = $members[$uid]['id'];
}
if (!isset($chatArr[$uid])) {
$insertArr1[] = [
2021-07-05 21:52:44 +08:00
'talk_type' => TalkType::GROUP_CHAT,
'user_id' => $uid,
'receiver_id' => $group_id,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s')
];
2021-07-05 21:52:44 +08:00
} else if ($chatArr[$uid]['is_delete'] == 1) {
2021-01-28 20:07:14 +08:00
$updateArr1[] = $chatArr[$uid]['id'];
}
}
try {
if ($updateArr) {
GroupMember::whereIn('id', $updateArr)->update([
2021-03-26 22:03:11 +08:00
'leader' => 0,
'is_mute' => 0,
'is_quit' => 0,
'user_card' => '',
'created_at' => date('Y-m-d H:i:s')
]);
2021-01-28 20:07:14 +08:00
}
if ($insertArr) {
Db::table('group_member')->insert($insertArr);
2021-01-28 20:07:14 +08:00
}
if ($updateArr1) {
2021-07-05 21:52:44 +08:00
TalkList::whereIn('id', $updateArr1)->update([
'is_delete' => 1,
'created_at' => date('Y-m-d H:i:s')
]);
2021-01-28 20:07:14 +08:00
}
if ($insertArr1) {
2021-07-05 21:52:44 +08:00
Db::table('talk_list')->insert($insertArr1);
2021-01-28 20:07:14 +08:00
}
2021-07-05 21:52:44 +08:00
$result = TalkRecords::create([
'talk_type' => TalkType::GROUP_CHAT,
'user_id' => 0,
'receiver_id' => $group_id,
'msg_type' => TalkMsgType::GROUP_INVITE_MESSAGE,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s')
2021-01-28 20:07:14 +08:00
]);
2021-07-05 21:52:44 +08:00
TalkRecordsInvite::create([
2021-03-26 22:03:11 +08:00
'record_id' => $result->id,
'type' => 1,
2021-01-28 20:07:14 +08:00
'operate_user_id' => $user_id,
2021-03-26 22:03:11 +08:00
'user_ids' => implode(',', $friend_ids)
2021-01-28 20:07:14 +08:00
]);
Db::commit();
2021-03-26 22:03:11 +08:00
} catch (Exception $e) {
2021-01-28 20:07:14 +08:00
Db::rollBack();
return [false, 0];
}
2021-07-05 21:52:44 +08:00
LastMessage::getInstance()->save(TalkType::GROUP_CHAT, $user_id, $group_id, [
2021-05-20 22:23:48 +08:00
'text' => '[入群通知]',
'created_at' => date('Y-m-d H:i:s')
]);
2021-01-28 20:07:14 +08:00
return [true, $result->id];
}
/**
2021-03-26 22:03:11 +08:00
* 退出群组(仅普通管理员及群成员)
2021-01-28 20:07:14 +08:00
*
2021-04-20 16:30:57 +08:00
* @param int $user_id 用户ID
2021-01-28 20:07:14 +08:00
* @param int $group_id 群组ID
* @return array
*/
public function quit(int $user_id, int $group_id)
{
2021-06-29 17:30:43 +08:00
// 判断是否属于管理员
if (Group::isManager($user_id, $group_id)) {
return [false, 0];
}
2021-01-28 20:07:14 +08:00
Db::beginTransaction();
try {
2021-03-26 22:03:11 +08:00
$count = GroupMember::where('group_id', $group_id)->where('user_id', $user_id)->where('is_quit', 0)->update([
'is_quit' => 1,
'deleted_at' => date('Y-m-d H:i:s'),
]);
2021-01-28 20:07:14 +08:00
2021-03-26 22:03:11 +08:00
if ($count == 0) {
throw new Exception('更新记录失败...');
}
2021-07-05 21:52:44 +08:00
$result = TalkRecords::create([
'talk_type' => TalkType::GROUP_CHAT,
'user_id' => 0,
'receiver_id' => $group_id,
'msg_type' => TalkMsgType::GROUP_INVITE_MESSAGE,
'content' => $user_id,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s')
]);
2021-01-28 20:07:14 +08:00
$record_id = $result->id;
2021-01-28 20:07:14 +08:00
2021-07-05 21:52:44 +08:00
TalkRecordsInvite::create([
2021-03-26 22:03:11 +08:00
'record_id' => $result->id,
'type' => 2,
'operate_user_id' => $user_id,
2021-03-26 22:03:11 +08:00
'user_ids' => $user_id
]);
2021-01-28 20:07:14 +08:00
2021-07-05 21:52:44 +08:00
TalkList::where([
['talk_type', '=', TalkType::GROUP_CHAT],
['user_id', '=', $user_id],
['receiver_id', '=', $group_id],
])->update([
'is_delete' => 1
]);
2021-03-26 22:03:11 +08:00
2021-01-28 20:07:14 +08:00
Db::commit();
} catch (Exception $e) {
Db::rollBack();
return [false, 0];
}
return [true, $record_id];
}
/**
* 踢出群组(管理员特殊权限)
*
2021-04-20 16:30:57 +08:00
* @param int $group_id 群ID
* @param int $user_id 操作用户ID
2021-01-28 20:07:14 +08:00
* @param array $member_ids 群成员ID
* @return array
*/
public function removeMember(int $group_id, int $user_id, array $member_ids)
{
2021-03-26 22:03:11 +08:00
if (!Group::isManager($user_id, $group_id)) {
2021-01-28 20:07:14 +08:00
return [false, 0];
}
Db::beginTransaction();
try {
2021-03-26 22:03:11 +08:00
$count = GroupMember::where('group_id', $group_id)->whereIn('user_id', $member_ids)->where('is_quit', 0)->update([
'is_quit' => 1,
'deleted_at' => date('Y-m-d H:i:s'),
]);
2021-01-28 20:07:14 +08:00
2021-03-26 22:03:11 +08:00
if ($count == 0) {
throw new Exception('更新记录失败...');
}
2021-07-05 21:52:44 +08:00
$result = TalkRecords::create([
'talk_type' => TalkType::GROUP_CHAT,
'user_id' => 0,
'receiver_id' => $group_id,
'msg_type' => TalkMsgType::GROUP_INVITE_MESSAGE,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
2021-01-28 20:07:14 +08:00
]);
2021-07-05 21:52:44 +08:00
TalkRecordsInvite::create([
2021-03-26 22:03:11 +08:00
'record_id' => $result->id,
'type' => 3,
2021-01-28 20:07:14 +08:00
'operate_user_id' => $user_id,
2021-03-26 22:03:11 +08:00
'user_ids' => implode(',', $member_ids)
2021-01-28 20:07:14 +08:00
]);
2021-07-05 21:52:44 +08:00
TalkList::whereIn('user_id', $member_ids)->where('receiver_id', $group_id)->where('talk_type', TalkType::GROUP_CHAT)->update([
'is_delete' => 1,
2021-03-26 22:03:11 +08:00
'updated_at' => date('Y-m-d H:i:s')
]);
2021-01-28 20:07:14 +08:00
Db::commit();
} catch (Exception $e) {
Db::rollBack();
return [false, 0];
}
return [true, $result->id];
}
}