重构群组相关代码

main
gzydong 2021-03-26 22:03:11 +08:00
parent f60ab0f0c1
commit 619d0234c1
7 changed files with 216 additions and 175 deletions

View File

@ -90,13 +90,13 @@ class GroupController extends CController
$this->socketRoomService->addRoomMember($uid, $data['group_id']); $this->socketRoomService->addRoomMember($uid, $data['group_id']);
} }
// ...消息推送队列 // ... 消息推送队列
$this->producer->produce( $this->producer->produce(
new ChatMessageProducer(SocketConstants::EVENT_TALK, [ new ChatMessageProducer(SocketConstants::EVENT_TALK, [
'sender' => $user_id, //发送者ID 'sender' => $user_id, // 发送者ID
'receive' => intval($data['group_id']), //接收者ID 'receive' => (int)$data['group_id'], // 接收者ID
'source' => 2, //接收者类型 1:好友;2:群组 'source' => 2, // 接收者类型[1:好友;2:群组;]
'record_id' => intval($data['record_id']) 'record_id' => (int)$data['record_id']
]) ])
); );
@ -124,7 +124,7 @@ class GroupController extends CController
$this->socketRoomService->delRoom($params['group_id']); $this->socketRoomService->delRoom($params['group_id']);
// ... 推送群消息(后期添加) // ... 推送群消息(预留)
return $this->response->success([], '群组解散成功...'); return $this->response->success([], '群组解散成功...');
} }
@ -158,9 +158,9 @@ class GroupController extends CController
// ...消息推送队列 // ...消息推送队列
$this->producer->produce( $this->producer->produce(
new ChatMessageProducer(SocketConstants::EVENT_TALK, [ new ChatMessageProducer(SocketConstants::EVENT_TALK, [
'sender' => $user_id, //发送者ID 'sender' => $user_id, // 发送者ID
'receive' => intval($params['group_id']), //接收者ID 'receive' => (int)$params['group_id'], // 接收者ID
'source' => 2, //接收者类型 1:好友;2:群组 'source' => 2, // 接收者类型[1:好友;2:群组;]
'record_id' => $record_id 'record_id' => $record_id
]) ])
); );
@ -192,9 +192,9 @@ class GroupController extends CController
// ...消息推送队列 // ...消息推送队列
$this->producer->produce( $this->producer->produce(
new ChatMessageProducer(SocketConstants::EVENT_TALK, [ new ChatMessageProducer(SocketConstants::EVENT_TALK, [
'sender' => $user_id, //发送者ID 'sender' => $user_id, // 发送者ID
'receive' => intval($params['group_id']), //接收者ID 'receive' => (int)$params['group_id'], // 接收者ID
'source' => 2, //接收者类型 1:好友;2:群组 'source' => 2, // 接收者类型[1:好友;2:群组;]
'record_id' => $record_id 'record_id' => $record_id
]) ])
); );
@ -209,22 +209,21 @@ class GroupController extends CController
*/ */
public function editDetail() public function editDetail()
{ {
$params = $this->request->inputs(['group_id', 'group_name', 'group_profile', 'avatar']); $params = $this->request->inputs(['group_id', 'group_name', 'profile', 'avatar']);
$this->validate($params, [ $this->validate($params, [
'group_id' => 'required|integer', 'group_id' => 'required|integer',
'group_name' => 'required|max:30', 'group_name' => 'required|max:30',
'group_profile' => 'required|max:100', 'profile' => 'present|max:100',
'avatar' => 'present|url' 'avatar' => 'present|url'
]); ]);
$result = Group::where('id', $params['group_id'])->where('user_id', $this->uid())->update([ $result = Group::where('id', $params['group_id'])->where('creator_id', $this->uid())->update([
'group_name' => $params['group_name'], 'group_name' => $params['group_name'],
'group_profile' => $params['group_profile'], 'profile' => $params['profile'],
'avatar' => $params['avatar'] 'avatar' => $params['avatar']
]); ]);
// 推送消息通知 // ... 推送消息通知(预留)
// ...
return $result return $result
? $this->response->success([], '群组信息修改成功...') ? $this->response->success([], '群组信息修改成功...')
@ -241,9 +240,11 @@ class GroupController extends CController
$params = $this->request->inputs(['group_id', 'members_ids']); $params = $this->request->inputs(['group_id', 'members_ids']);
$this->validate($params, [ $this->validate($params, [
'group_id' => 'required|integer', 'group_id' => 'required|integer',
'members_ids' => 'required|array' 'members_ids' => 'required|ids'
]); ]);
$params['members_ids'] = parse_ids($params['members_ids']);
$user_id = $this->uid(); $user_id = $this->uid();
if (in_array($user_id, $params['members_ids'])) { if (in_array($user_id, $params['members_ids'])) {
return $this->response->fail('群聊用户移除失败...'); return $this->response->fail('群聊用户移除失败...');
@ -259,12 +260,12 @@ class GroupController extends CController
$this->socketRoomService->delRoomMember($params['group_id'], $uid); $this->socketRoomService->delRoomMember($params['group_id'], $uid);
} }
// ...消息推送队列 // ... 消息推送队列
$this->producer->produce( $this->producer->produce(
new ChatMessageProducer(SocketConstants::EVENT_TALK, [ new ChatMessageProducer(SocketConstants::EVENT_TALK, [
'sender' => $user_id, //发送者ID 'sender' => $user_id, // 发送者ID
'receive' => intval($params['group_id']), //接收者ID 'receive' => (int)$params['group_id'], // 接收者ID
'source' => 2, //接收者类型 1:好友;2:群组 'source' => 2, // 接收者类型[1:好友;2:群组;]
'record_id' => $record_id 'record_id' => $record_id
]) ])
); );
@ -280,19 +281,21 @@ class GroupController extends CController
public function detail() public function detail()
{ {
$group_id = $this->request->input('group_id', 0); $group_id = $this->request->input('group_id', 0);
$user_id = $this->uid(); $user_id = $this->uid();
$groupInfo = Group::leftJoin('users', 'users.id', '=', 'group.user_id')
->where('group.id', $group_id)->where('group.status', 0)->first([ $groupInfo = Group::leftJoin('users', 'users.id', '=', 'group.creator_id')
'group.id', 'group.user_id', ->where('group.id', $group_id)->where('group.is_dismiss', 0)->first([
'group.id',
'group.creator_id',
'group.group_name', 'group.group_name',
'group.group_profile', 'group.avatar', 'group.profile',
'group.avatar',
'group.created_at', 'group.created_at',
'users.nickname' 'users.nickname'
]); ]);
if (!$groupInfo) { if (!$groupInfo) {
return $this->response->success([]); return $this->response->success();
} }
$notice = GroupNotice::where('group_id', $group_id) $notice = GroupNotice::where('group_id', $group_id)
@ -303,10 +306,10 @@ class GroupController extends CController
return $this->response->success([ return $this->response->success([
'group_id' => $groupInfo->id, 'group_id' => $groupInfo->id,
'group_name' => $groupInfo->group_name, 'group_name' => $groupInfo->group_name,
'group_profile' => $groupInfo->group_profile, 'group_profile' => $groupInfo->profile,
'avatar' => $groupInfo->avatar, 'avatar' => $groupInfo->avatar,
'created_at' => $groupInfo->created_at, 'created_at' => $groupInfo->created_at,
'is_manager' => $groupInfo->user_id == $user_id, 'is_manager' => $groupInfo->creator_id == $user_id,
'manager_nickname' => $groupInfo->nickname, 'manager_nickname' => $groupInfo->nickname,
'visit_card' => GroupMember::visitCard($user_id, $group_id), 'visit_card' => GroupMember::visitCard($user_id, $group_id),
'not_disturb' => UsersChatList::where('uid', $user_id)->where('group_id', $group_id)->where('type', 2)->value('not_disturb') ?? 0, 'not_disturb' => UsersChatList::where('uid', $user_id)->where('group_id', $group_id)->where('type', 2)->value('not_disturb') ?? 0,
@ -319,7 +322,7 @@ class GroupController extends CController
* *
* @RequestMapping(path="set-group-card", methods="post") * @RequestMapping(path="set-group-card", methods="post")
*/ */
public function setGroupCard() public function editGroupCard()
{ {
$params = $this->request->inputs(['group_id', 'visit_card']); $params = $this->request->inputs(['group_id', 'visit_card']);
$this->validate($params, [ $this->validate($params, [
@ -329,8 +332,7 @@ class GroupController extends CController
$isTrue = GroupMember::where('group_id', $params['group_id']) $isTrue = GroupMember::where('group_id', $params['group_id'])
->where('user_id', $this->uid()) ->where('user_id', $this->uid())
->where('status', 0) ->update(['user_card' => $params['visit_card']]);
->update(['visit_card' => $params['visit_card']]);
return $isTrue return $isTrue
? $this->response->success([], '群名片修改成功...') ? $this->response->success([], '群名片修改成功...')
@ -387,15 +389,20 @@ class GroupController extends CController
} }
$members = GroupMember::select([ $members = GroupMember::select([
'group_member.id', 'group_member.group_owner as is_manager', 'group_member.visit_card', 'group_member.id',
'group_member.user_id', 'users.avatar', 'users.nickname', 'users.gender', 'group_member.leader',
'group_member.user_card',
'group_member.user_id',
'users.avatar',
'users.nickname',
'users.gender',
'users.motto', 'users.motto',
]) ])
->leftJoin('users', 'users.id', '=', 'group_member.user_id') ->leftJoin('users', 'users.id', '=', 'group_member.user_id')
->where([ ->where([
['group_member.group_id', '=', $group_id], ['group_member.group_id', '=', $group_id],
['group_member.status', '=', 0], ['group_member.is_quit', '=', 0],
])->orderBy('is_manager', 'desc')->get()->toArray(); ])->orderBy('leader', 'desc')->get()->toArray();
return $this->response->success($members); return $this->response->success($members);
} }
@ -405,7 +412,7 @@ class GroupController extends CController
* *
* @RequestMapping(path="notices", methods="get") * @RequestMapping(path="notices", methods="get")
*/ */
public function getGroupNotices() public function getGroupNotice()
{ {
$user_id = $this->uid(); $user_id = $this->uid();
$group_id = $this->request->input('group_id', 0); $group_id = $this->request->input('group_id', 0);
@ -415,20 +422,25 @@ class GroupController extends CController
return $this->response->fail('非管理员禁止操作...'); return $this->response->fail('非管理员禁止操作...');
} }
$rows = GroupNotice::leftJoin('users', 'users.id', '=', 'group_notice.user_id') $rows = GroupNotice::leftJoin('users', 'users.id', '=', 'group_notice.creator_id')
->where([ ->where([
['group_notice.group_id', '=', $group_id], ['group_notice.group_id', '=', $group_id],
['group_notice.is_delete', '=', 0] ['group_notice.is_delete', '=', 0]
]) ])
->orderBy('group_notice.id', 'desc') ->orderBy('group_notice.is_top', 'desc')
->orderBy('group_notice.updated_at', 'desc')
->get([ ->get([
'group_notice.id', 'group_notice.id',
'group_notice.user_id', 'group_notice.creator_id',
'group_notice.title', 'group_notice.title',
'group_notice.content', 'group_notice.content',
'group_notice.is_top',
'group_notice.is_confirm',
'group_notice.confirm_users',
'group_notice.created_at', 'group_notice.created_at',
'group_notice.updated_at', 'group_notice.updated_at',
'users.avatar', 'users.nickname', 'users.avatar',
'users.nickname',
])->toArray(); ])->toArray();
return $this->response->success($rows); return $this->response->success($rows);
@ -441,11 +453,13 @@ class GroupController extends CController
*/ */
public function editNotice() public function editNotice()
{ {
$params = $this->request->inputs(['group_id', 'notice_id', 'title', 'content']); $params = $this->request->inputs(['group_id', 'notice_id', 'title', 'content', 'is_top', 'is_confirm']);
$this->validate($params, [ $this->validate($params, [
'group_id' => 'required|integer',
'notice_id' => 'required|integer', 'notice_id' => 'required|integer',
'group_id' => 'required|integer',
'title' => 'required|max:50', 'title' => 'required|max:50',
'is_top' => 'integer|in:0,1',
'is_confirm' => 'integer|in:0,1',
'content' => 'required' 'content' => 'required'
]); ]);
@ -457,12 +471,14 @@ class GroupController extends CController
} }
// 判断是否是新增数据 // 判断是否是新增数据
if (empty($data['notice_id'])) { if (empty($params['notice_id'])) {
$result = GroupNotice::create([ $result = GroupNotice::create([
'group_id' => $params['group_id'], 'group_id' => $params['group_id'],
'creator_id' => $user_id,
'title' => $params['title'], 'title' => $params['title'],
'content' => $params['content'], 'content' => $params['content'],
'user_id' => $user_id, 'is_top' => $params['is_top'],
'is_confirm' => $params['is_confirm'],
'created_at' => date('Y-m-d H:i:s'), 'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s') 'updated_at' => date('Y-m-d H:i:s')
]); ]);
@ -471,13 +487,15 @@ class GroupController extends CController
return $this->response->fail('添加群公告信息失败...'); return $this->response->fail('添加群公告信息失败...');
} }
// ... 推送群消息 // ... 推送群消息(预留)
return $this->response->success([], '添加群公告信息成功...'); return $this->response->success([], '添加群公告信息成功...');
} }
$result = GroupNotice::where('id', $data['notice_id'])->update([ $result = GroupNotice::where('id', $params['notice_id'])->update([
'title' => $data['title'], 'title' => $params['title'],
'content' => $data['content'], 'content' => $params['content'],
'is_top' => $params['is_top'],
'updated_at' => date('Y-m-d H:i:s') 'updated_at' => date('Y-m-d H:i:s')
]); ]);

View File

@ -27,6 +27,10 @@ class ValidatorFactoryResolvedListener implements ListenerInterface
// 注册了 ids 验证器(验证英文逗号拼接的整形数字字符串 例如:[1,2,3,4,5]) // 注册了 ids 验证器(验证英文逗号拼接的整形数字字符串 例如:[1,2,3,4,5])
$validatorFactory->extend('ids', function ($attribute, $value, $parameters, $validator) { $validatorFactory->extend('ids', function ($attribute, $value, $parameters, $validator) {
if (!is_string($value)) {
return false;
}
$arr = explode(',', $value); $arr = explode(',', $value);
foreach ($arr as $id) { foreach ($arr as $id) {
if (!check_int($id)) return false; if (!check_int($id)) return false;

View File

@ -84,7 +84,7 @@ class Group extends BaseModel
*/ */
public static function isManager(int $user_id, int $group_id, $leader = 2) public static function isManager(int $user_id, int $group_id, $leader = 2)
{ {
return self::where('id', $group_id)->where('creator_id', $user_id)->where('leader', $leader)->exists(); return self::where('id', $group_id)->where('creator_id', $user_id)->exists();
} }
/** /**

View File

@ -80,7 +80,7 @@ class GroupMember extends BaseModel
*/ */
public static function visitCard(int $user_id, int $group_id) public static function visitCard(int $user_id, int $group_id)
{ {
return self::where('group_id', $group_id)->where('user_id', $user_id)->value('visit_card') ?? ""; return self::where('group_id', $group_id)->where('user_id', $user_id)->value('user_card') ?? "";
} }
/** /**

View File

@ -1,4 +1,5 @@
<?php <?php
declare(strict_types=1);
namespace App\Service; namespace App\Service;
@ -13,6 +14,7 @@ use Exception;
/** /**
* Class GroupService * Class GroupService
*
* @package App\Service * @package App\Service
*/ */
class GroupService extends BaseService class GroupService extends BaseService
@ -25,9 +27,7 @@ class GroupService extends BaseService
*/ */
public function getGroups(int $user_id): array public function getGroups(int $user_id): array
{ {
$items = GroupMember::join('group', 'group.id', '=', 'group_member.group_id', 'inner', [ $items = GroupMember::join('group', 'group.id', '=', 'group_member.group_id')
['group.is_dismiss', '=', 0]
])
->where([ ->where([
['group_member.user_id', '=', $user_id], ['group_member.user_id', '=', $user_id],
['group_member.is_quit', '=', 0] ['group_member.is_quit', '=', 0]
@ -37,17 +37,17 @@ class GroupService extends BaseService
'group.id', 'group.id',
'group.group_name', 'group.group_name',
'group.avatar', 'group.avatar',
'group.group_profile', 'group.profile',
'group.leader', 'group_member.leader',
])->toArray(); ])->toArray();
foreach ($items as $key => $item) { $arr = UsersChatList::where([
// 是否消息免打扰
$items[$key]['not_disturb'] = UsersChatList::where([
['uid', '=', $user_id], ['uid', '=', $user_id],
['type', '=', 2], ['type', '=', 2],
['group_id', '=', $item['id']] ])->get(['group_id', 'not_disturb'])->keyBy('group_id')->toArray();
])->value('not_disturb');
foreach ($items as $key => $item) {
$items[$key]['not_disturb'] = $arr[$item['id']] ? $arr[$item['id']]['not_disturb'] : 0;
} }
return $items; return $items;
@ -88,7 +88,7 @@ class GroupService extends BaseService
'leader' => $user_id == $uid ? 2 : 0, 'leader' => $user_id == $uid ? 2 : 0,
'is_mute' => 0, 'is_mute' => 0,
'is_quit' => 0, 'is_quit' => 0,
'user_card' => 0, 'user_card' => '',
'created_at' => date('Y-m-d H:i:s'), 'created_at' => date('Y-m-d H:i:s'),
]; ];
@ -139,7 +139,7 @@ class GroupService extends BaseService
} }
/** /**
* 解散群组 * 解散群组(群主权限)
* *
* @param int $group_id 群ID * @param int $group_id 群ID
* @param int $user_id 用户ID * @param int $user_id 用户ID
@ -152,10 +152,19 @@ class GroupService extends BaseService
return false; return false;
} }
return Group::where('id', $group_id)->where('creator_id', $user_id)->update([ DB::transaction(function () use ($group_id, $user_id) {
Group::where('id', $group_id)->where('creator_id', $user_id)->update([
'is_dismiss' => 1, 'is_dismiss' => 1,
'dismissed_at' => date('Y-m-d H:i:s'), 'dismissed_at' => date('Y-m-d H:i:s'),
]) ? true : false; ]);
GroupMember::where('group_id', $group_id)->update([
'is_quit' => 1,
'deleted_at' => date('Y-m-d H:i:s'),
]);
}, 2);
return true;
} }
/** /**
@ -168,9 +177,7 @@ class GroupService extends BaseService
*/ */
public function invite(int $user_id, int $group_id, $friend_ids = []) public function invite(int $user_id, int $group_id, $friend_ids = [])
{ {
if (empty($friend_ids)) { if (!$friend_ids) return [false, 0];
return [false, 0];
}
$info = GroupMember::where('group_id', $group_id)->where('user_id', $user_id)->first(['id', 'is_quit']); $info = GroupMember::where('group_id', $group_id)->where('user_id', $user_id)->first(['id', 'is_quit']);
@ -185,7 +192,7 @@ class GroupService extends BaseService
$chatArr = UsersChatList::where('group_id', $group_id)->whereIn('uid', $friend_ids)->get(['id', 'uid', 'status'])->keyBy('uid')->toArray(); $chatArr = UsersChatList::where('group_id', $group_id)->whereIn('uid', $friend_ids)->get(['id', 'uid', 'status'])->keyBy('uid')->toArray();
foreach ($friend_ids as $uid) { foreach ($friend_ids as $uid) {
if (!isset($members[$uid])) {//存在聊天群成员记录 if (!isset($members[$uid])) {
$insertArr[] = [ $insertArr[] = [
'group_id' => $group_id, 'group_id' => $group_id,
'user_id' => $uid, 'user_id' => $uid,
@ -256,7 +263,7 @@ class GroupService extends BaseService
]); ]);
Db::commit(); Db::commit();
} catch (\Exception $e) { } catch (Exception $e) {
Db::rollBack(); Db::rollBack();
return [false, 0]; return [false, 0];
} }
@ -266,7 +273,7 @@ class GroupService extends BaseService
} }
/** /**
* 退出群组 * 退出群组(仅普通管理员及群成员)
* *
* @param int $user_id 用户ID * @param int $user_id 用户ID
* @param int $group_id 群组ID * @param int $group_id 群组ID
@ -280,12 +287,15 @@ class GroupService extends BaseService
Db::beginTransaction(); Db::beginTransaction();
try { try {
GroupMember::where('group_id', $group_id)->where('user_id', $user_id)->update([ $count = GroupMember::where('group_id', $group_id)->where('user_id', $user_id)->where('is_quit', 0)->update([
'is_quit' => 1, 'is_quit' => 1,
'deleted_at' => date('Y-m-d H:i:s'), 'deleted_at' => date('Y-m-d H:i:s'),
]); ]);
UsersChatList::where('uid', $user_id)->where('type', 2)->where('group_id', $group_id)->update(['status' => 0]); if ($count == 0) {
throw new Exception('更新记录失败...');
}
$result = ChatRecord::create([ $result = ChatRecord::create([
'msg_type' => 3, 'msg_type' => 3,
'source' => 2, 'source' => 2,
@ -304,6 +314,8 @@ class GroupService extends BaseService
'user_ids' => $user_id 'user_ids' => $user_id
]); ]);
UsersChatList::where('uid', $user_id)->where('type', 2)->where('group_id', $group_id)->update(['status' => 0]);
Db::commit(); Db::commit();
} catch (Exception $e) { } catch (Exception $e) {
Db::rollBack(); Db::rollBack();
@ -323,17 +335,21 @@ class GroupService extends BaseService
*/ */
public function removeMember(int $group_id, int $user_id, array $member_ids) public function removeMember(int $group_id, int $user_id, array $member_ids)
{ {
if (!Group::isManager($user_id, $group_id, [1, 2])) { if (!Group::isManager($user_id, $group_id)) {
return [false, 0]; return [false, 0];
} }
Db::beginTransaction(); Db::beginTransaction();
try { try {
GroupMember::where('group_id', $group_id)->whereIn('user_id', $member_ids)->whereIn('leader', [0, 1])->update([ $count = GroupMember::where('group_id', $group_id)->whereIn('user_id', $member_ids)->where('is_quit', 0)->update([
'is_quit' => 1, 'is_quit' => 1,
'deleted_at' => date('Y-m-d H:i:s'), 'deleted_at' => date('Y-m-d H:i:s'),
]); ]);
if ($count == 0) {
throw new Exception('更新记录失败...');
}
$result = ChatRecord::create([ $result = ChatRecord::create([
'msg_type' => 3, 'msg_type' => 3,
'source' => 2, 'source' => 2,
@ -349,7 +365,10 @@ class GroupService extends BaseService
'user_ids' => implode(',', $member_ids) 'user_ids' => implode(',', $member_ids)
]); ]);
UsersChatList::whereIn('uid', $member_ids)->where('group_id', $group_id)->update(['status' => 0, 'updated_at' => date('Y-m-d H:i:s')]); UsersChatList::whereIn('uid', $member_ids)->where('group_id', $group_id)->update([
'status' => 0,
'updated_at' => date('Y-m-d H:i:s')
]);
Db::commit(); Db::commit();
} catch (Exception $e) { } catch (Exception $e) {

View File

@ -48,7 +48,7 @@ return [
], ],
'settings' => [ 'settings' => [
'enable_coroutine' => true, 'enable_coroutine' => true,
'worker_num' => swoole_cpu_num() * 4, 'worker_num' => 1,
'pid_file' => BASE_PATH . '/runtime/hyperf.pid', 'pid_file' => BASE_PATH . '/runtime/hyperf.pid',
'open_tcp_nodelay' => true, 'open_tcp_nodelay' => true,
'max_coroutine' => 100000, 'max_coroutine' => 100000,