From 40103cd99cbab42ff27c60561cd47cbf1efe089c Mon Sep 17 00:00:00 2001 From: gzydong <837215079@qq.com> Date: Wed, 28 Jul 2021 23:08:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Controller/Api/V1/DownloadController.php | 3 +- app/Controller/Api/V1/GroupController.php | 22 ++++-- app/Controller/Api/V1/TalkController.php | 6 +- app/Controller/IndexController.php | 2 - app/Controller/WebSocketController.php | 14 ++-- app/Model/Group/Group.php | 44 ----------- app/Model/Group/GroupMember.php | 34 --------- app/Service/EmoticonService.php | 3 +- app/Service/Group/GroupMemberService.php | 74 +++++++++++++++++++ .../{ => Group}/GroupNoticeService.php | 14 ++-- app/Service/{ => Group}/GroupService.php | 18 ++++- app/Service/TalkService.php | 11 +-- app/Support/UserRelation.php | 3 +- 13 files changed, 136 insertions(+), 112 deletions(-) create mode 100644 app/Service/Group/GroupMemberService.php rename app/Service/{ => Group}/GroupNoticeService.php (90%) rename app/Service/{ => Group}/GroupService.php (94%) diff --git a/app/Controller/Api/V1/DownloadController.php b/app/Controller/Api/V1/DownloadController.php index 4516a8a..e2c5dc1 100644 --- a/app/Controller/Api/V1/DownloadController.php +++ b/app/Controller/Api/V1/DownloadController.php @@ -10,6 +10,7 @@ namespace App\Controller\Api\V1; +use App\Service\Group\GroupMemberService; use Hyperf\Di\Annotation\Inject; use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\RequestMapping; @@ -65,7 +66,7 @@ class DownloadController extends CController return $this->response->fail('非法请求!'); } } else { - if (!Group::isMember($recordsInfo->receiver_id, $user_id)) { + if (!di()->get(GroupMemberService::class)->isMember($recordsInfo->receiver_id, $user_id)) { return $this->response->fail('非法请求!'); } } diff --git a/app/Controller/Api/V1/GroupController.php b/app/Controller/Api/V1/GroupController.php index b7f91ef..e168389 100644 --- a/app/Controller/Api/V1/GroupController.php +++ b/app/Controller/Api/V1/GroupController.php @@ -11,7 +11,8 @@ namespace App\Controller\Api\V1; use App\Constants\TalkModeConstant; -use App\Service\GroupNoticeService; + +use App\Service\Group\GroupMemberService; use App\Service\TalkListService; use App\Service\UserService; use Hyperf\Di\Annotation\Inject; @@ -22,7 +23,8 @@ use App\Middleware\JWTAuthMiddleware; use App\Model\Group\Group; use App\Model\Group\GroupMember; use App\Model\Group\GroupNotice; -use App\Service\GroupService; +use App\Service\Group\GroupService; +use App\Service\Group\GroupNoticeService; use Psr\Http\Message\ResponseInterface; /** @@ -40,6 +42,12 @@ class GroupController extends CController */ private $groupService; + /** + * @inject + * @var GroupMemberService + */ + private $groupMemberService; + /** * 创建群组 * @RequestMapping(path="create", methods="post") @@ -214,7 +222,7 @@ class GroupController extends CController 'created_at' => $groupInfo->created_at, 'is_manager' => $groupInfo->creator_id == $user_id, 'manager_nickname' => $groupInfo->nickname, - 'visit_card' => GroupMember::visitCard($user_id, $group_id), + 'visit_card' => $this->groupMemberService->getVisitCard($group_id, $user_id), 'is_disturb' => (int)$service->isDisturb($user_id, $group_id, TalkModeConstant::GROUP_CHAT), 'notice' => $notice ? $notice->toArray() : [] ]); @@ -252,7 +260,7 @@ class GroupController extends CController $group_id = $this->request->input('group_id', 0); $friends = $service->getUserFriends($this->uid()); if ($group_id > 0 && $friends) { - if ($ids = GroupMember::getGroupMemberIds($group_id)) { + if ($ids = $this->groupMemberService->getMemberIds($group_id)) { foreach ($friends as $k => $item) { if (in_array($item['id'], $ids)) unset($friends[$k]); } @@ -289,7 +297,7 @@ class GroupController extends CController $group_id = $this->request->input('group_id', 0); // 判断用户是否是群成员 - if (!Group::isMember($group_id, $user_id)) { + if (!$this->groupMemberService->isMember($group_id, $user_id)) { return $this->response->fail('非法操作!'); } @@ -324,7 +332,7 @@ class GroupController extends CController $group_id = $this->request->input('group_id', 0); // 判断用户是否是群成员 - if (!Group::isMember($group_id, $user_id)) { + if (!$this->groupMemberService->isMember($group_id, $user_id)) { return $this->response->fail('非管理员禁止操作!'); } @@ -352,7 +360,7 @@ class GroupController extends CController $user_id = $this->uid(); // 判断用户是否是管理员 - if (!Group::isManager($user_id, $params['group_id'])) { + if (!$this->groupMemberService->isAuth($params['group_id'], $user_id)) { return $this->response->fail('非管理员禁止操作!'); } diff --git a/app/Controller/Api/V1/TalkController.php b/app/Controller/Api/V1/TalkController.php index 3901347..3bc274b 100644 --- a/app/Controller/Api/V1/TalkController.php +++ b/app/Controller/Api/V1/TalkController.php @@ -16,6 +16,7 @@ use App\Cache\UnreadTalkCache; use App\Constants\TalkMessageType; use App\Constants\TalkModeConstant; use App\Model\Talk\TalkList; +use App\Service\Group\GroupMemberService; use App\Service\UserFriendService; use App\Support\UserRelation; use Hyperf\Di\Annotation\Inject; @@ -226,7 +227,8 @@ class TalkController extends CController ]); $user_id = $this->uid(); - if ($params['talk_type'] == TalkModeConstant::GROUP_CHAT && !Group::isMember((int)$params['receiver_id'], $user_id)) { + + if ($params['talk_type'] == TalkModeConstant::GROUP_CHAT && !di()->get(GroupMemberService::class)->isMember((int)$params['receiver_id'], $user_id)) { return $this->response->fail('暂不属于好友关系或群聊成员,无法查看聊天记录!'); } @@ -281,7 +283,7 @@ class TalkController extends CController ]); $user_id = $this->uid(); - if ($params['talk_type'] == TalkModeConstant::GROUP_CHAT && !Group::isMember((int)$params['receiver_id'], $user_id)) { + if ($params['talk_type'] == TalkModeConstant::GROUP_CHAT && !di()->get(GroupMemberService::class)->isMember((int)$params['receiver_id'], $user_id)) { return $this->response->fail('暂不属于好友关系或群聊成员,无法查看聊天记录!'); } diff --git a/app/Controller/IndexController.php b/app/Controller/IndexController.php index d9a0227..c6129ce 100644 --- a/app/Controller/IndexController.php +++ b/app/Controller/IndexController.php @@ -12,8 +12,6 @@ class IndexController extends AbstractController $user = $this->request->input('user', 'Hyperf'); $method = $this->request->getMethod(); - var_dump($this->request->all()); - return [ 'method' => $method, 'message' => "Hello {$user}.", diff --git a/app/Controller/WebSocketController.php b/app/Controller/WebSocketController.php index da0199a..38f8dbc 100644 --- a/app/Controller/WebSocketController.php +++ b/app/Controller/WebSocketController.php @@ -12,6 +12,7 @@ declare(strict_types=1); namespace App\Controller; use App\Cache\SocketRoom; +use App\Service\Group\GroupMemberService; use App\Service\Message\ReceiveHandleService; use Hyperf\Di\Annotation\Inject; use App\Constants\TalkEventConstant; @@ -71,7 +72,7 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos $this->client->bind($request->fd, $user_id); // 加入群聊 - $groupIds = GroupMember::getUserGroupIds($user_id); + $groupIds = di()->get(GroupMemberService::class)->getUserGroupIds($user_id); foreach ($groupIds as $group_id) { SocketRoom::getInstance()->addRoomMember(strval($group_id), strval($user_id)); } @@ -97,11 +98,14 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos $result = json_decode($frame->data, true); - if (isset(ReceiveHandleService::EVENTS[$result['event']])) { - call_user_func_array([$this->receiveHandle, ReceiveHandleService::EVENTS[$result['event']]], [ - $server, $frame, $result['data'] - ]); + if (!isset(ReceiveHandleService::EVENTS[$result['event']])) { + return; } + + // 回调处理 + call_user_func_array([$this->receiveHandle, ReceiveHandleService::EVENTS[$result['event']]], [ + $server, $frame, $result['data'] + ]); } /** diff --git a/app/Model/Group/Group.php b/app/Model/Group/Group.php index f765fab..de7d7ae 100644 --- a/app/Model/Group/Group.php +++ b/app/Model/Group/Group.php @@ -51,48 +51,4 @@ class Group extends BaseModel 'is_dismiss' => 'integer', 'created_at' => 'datetime' ]; - - /** - * 获取群聊成员 - */ - public function members() - { - return $this->hasMany(GroupMember::class, 'group_id', 'id'); - } - - /** - * 判断用户是否是管理员 - * - * @param int $user_id 用户ID - * @param int $group_id 群ID - * @param int|array $leader 管理员类型[0:普通成员;1:管理员;2:群主;] - * @return bool - */ - public static function isManager(int $user_id, int $group_id, $leader = 2) - { - return self::query()->where('id', $group_id)->where('creator_id', $user_id)->exists(); - } - - /** - * 判断群组是否已解散 - * - * @param int $group_id 群ID - * @return bool - */ - public static function isDismiss(int $group_id) - { - return self::query()->where('id', $group_id)->where('is_dismiss', 1)->exists(); - } - - /** - * 判断用户是否是群成员 - * - * @param int $group_id 群ID - * @param int $user_id 用户ID - * @return bool - */ - public static function isMember(int $group_id, int $user_id) - { - return GroupMember::where('group_id', $group_id)->where('user_id', $user_id)->where('is_quit', 0)->exists(); - } } diff --git a/app/Model/Group/GroupMember.php b/app/Model/Group/GroupMember.php index 70dc70e..d6e453b 100644 --- a/app/Model/Group/GroupMember.php +++ b/app/Model/Group/GroupMember.php @@ -43,38 +43,4 @@ class GroupMember extends BaseModel 'created_at' => 'datetime', 'deleted_at' => 'datetime', ]; - - /** - * 获取聊天群成员ID - * - * @param int $group_id 群聊ID - * @return array - */ - public static function getGroupMemberIds(int $group_id) - { - return self::query()->where('group_id', $group_id)->where('is_quit', 0)->pluck('user_id')->toArray() ?? []; - } - - /** - * 获取用户的群名片 - * - * @param int $user_id 用户ID - * @param int $group_id 群ID - * @return string - */ - public static function visitCard(int $user_id, int $group_id) - { - return self::query()->where('group_id', $group_id)->where('user_id', $user_id)->value('user_card') ?? ""; - } - - /** - * 获取用户的所有群ID - * - * @param int $user_id 用户ID - * @return array - */ - public static function getUserGroupIds(int $user_id) - { - return self::query()->where('user_id', $user_id)->where('is_quit', 0)->pluck('group_id')->toArray() ?? []; - } } diff --git a/app/Service/EmoticonService.php b/app/Service/EmoticonService.php index 1ea8a84..36e2d51 100644 --- a/app/Service/EmoticonService.php +++ b/app/Service/EmoticonService.php @@ -9,6 +9,7 @@ use App\Model\Talk\TalkRecordsFile; use App\Model\EmoticonItem; use App\Model\Group\Group; use App\Model\UsersEmoticon; +use App\Service\Group\GroupMemberService; /** * 表情服务层 @@ -106,7 +107,7 @@ class EmoticonService extends BaseService return [false, []]; } } else { - if (!Group::isMember($result->receiver_id, $user_id)) { + if (!di()->get(GroupMemberService::class)->isMember($result->receiver_id, $user_id)) { return [false, []]; } } diff --git a/app/Service/Group/GroupMemberService.php b/app/Service/Group/GroupMemberService.php new file mode 100644 index 0000000..0a172a4 --- /dev/null +++ b/app/Service/Group/GroupMemberService.php @@ -0,0 +1,74 @@ +where([ + 'group_id' => $group_id, + 'user_id' => $user_id, + 'leader' => $leader, + 'is_quit' => 0, + ])->exists(); + } + + /** + * 判断用户是否是群成员 + * + * @param int $group_id 群ID + * @param int $user_id 用户ID + * @return bool + */ + public function isMember(int $group_id, int $user_id): bool + { + return GroupMember::where('group_id', $group_id)->where('user_id', $user_id)->where('is_quit', 0)->exists(); + } + + /** + * 获取所有群成员ID + * + * @param int $group_id 群组ID + * @return array + */ + public function getMemberIds(int $group_id): array + { + return GroupMember::query()->where('group_id', $group_id)->where('is_quit', 0)->pluck('user_id')->toArray(); + } + + /** + * 获取用户的所有群ID + * + * @param int $user_id 用户ID + * @return array + */ + public function getUserGroupIds(int $user_id): array + { + return GroupMember::query()->where('user_id', $user_id)->where('is_quit', 0)->pluck('group_id')->toArray(); + } + + /** + * 获取群成员名片 + * + * @param int $group_id 群组ID + * @param int $user_id 用户ID + * @return string + */ + public function getVisitCard(int $group_id, int $user_id): string + { + return GroupMember::query()->where('group_id', $group_id)->where('user_id', $user_id)->value('user_card') ?? ""; + } +} diff --git a/app/Service/GroupNoticeService.php b/app/Service/Group/GroupNoticeService.php similarity index 90% rename from app/Service/GroupNoticeService.php rename to app/Service/Group/GroupNoticeService.php index 56344a3..f290696 100644 --- a/app/Service/GroupNoticeService.php +++ b/app/Service/Group/GroupNoticeService.php @@ -1,12 +1,13 @@ first(); - if (!$notice) { - return false; - } + + if (!$notice) return false; // 判断用户是否是管理员 - if (!Group::isManager($user_id, $notice->group_id)) { + if (!di()->get(GroupMemberService::class)->isAuth($notice->group_id, $user_id)) { throw new Exception('非管理员,无法进行操作!', 403); } diff --git a/app/Service/GroupService.php b/app/Service/Group/GroupService.php similarity index 94% rename from app/Service/GroupService.php rename to app/Service/Group/GroupService.php index 004c5b5..ba29e43 100644 --- a/app/Service/GroupService.php +++ b/app/Service/Group/GroupService.php @@ -1,7 +1,7 @@ where('id', $group_id)->where('is_dismiss', 1)->exists(); + } + /** * 创建群组 * @@ -303,7 +315,7 @@ class GroupService extends BaseService public function quit(int $user_id, int $group_id) { // 判断是否属于管理员 - if (Group::isManager($user_id, $group_id)) { + if (di()->get(GroupMemberService::class)->isAuth($group_id, $user_id)) { return false; } @@ -368,7 +380,7 @@ class GroupService extends BaseService */ public function removeMember(int $group_id, int $user_id, array $member_ids) { - if (!Group::isManager($user_id, $group_id)) { + if (di()->get(GroupMemberService::class)->isAuth($group_id, $user_id)) { return false; } diff --git a/app/Service/TalkService.php b/app/Service/TalkService.php index efa1c1e..f032aa4 100644 --- a/app/Service/TalkService.php +++ b/app/Service/TalkService.php @@ -6,6 +6,7 @@ use App\Constants\TalkEventConstant; use App\Constants\TalkMessageType; use App\Constants\TalkModeConstant; use App\Event\TalkEvent; +use App\Service\Group\GroupMemberService; use App\Service\Message\FormatMessageService; use Exception; use App\Model\Group\Group; @@ -115,7 +116,7 @@ class TalkService extends BaseService // 判断是否有权限查看 if ($result->talk_type == TalkModeConstant::PRIVATE_CHAT && ($result->user_id != $user_id && $result->receiver_id != $user_id)) { return []; - } else if ($result->talk_type == TalkModeConstant::GROUP_CHAT && !Group::isMember($result->receiver_id, $user_id)) { + } else if ($result->talk_type == TalkModeConstant::GROUP_CHAT && !di()->get(GroupMemberService::class)->isMember($result->receiver_id, $user_id)) { return []; } @@ -168,7 +169,7 @@ class TalkService extends BaseService } // 判读是否属于群消息并且判断是否是群成员 - if ($talk_type == TalkModeConstant::GROUP_CHAT && !Group::isMember($receiver_id, $user_id)) { + if ($talk_type == TalkModeConstant::GROUP_CHAT && !di()->get(GroupMemberService::class)->isMember($receiver_id, $user_id)) { return false; } @@ -205,7 +206,7 @@ class TalkService extends BaseService return [false, '非法操作', []]; } } else if ($result->talk_type == TalkModeConstant::GROUP_CHAT) { - if (!Group::isMember($result->receiver_id, $user_id)) { + if (!di()->get(GroupMemberService::class)->isMember($result->receiver_id, $user_id)) { return [false, '非法操作', []]; } } @@ -245,7 +246,7 @@ class TalkService extends BaseService return []; } } else if ($result->talk_type == TalkModeConstant::GROUP_CHAT) { - if (!Group::isMember($result->receiver_id, $user_id)) { + if (!di()->get(GroupMemberService::class)->isMember($result->receiver_id, $user_id)) { return []; } } @@ -352,7 +353,7 @@ class TalkService extends BaseService ]); })->whereIn('msg_type', $msg_type)->where('talk_type', $talk_type)->where('is_revoke', 0); } else { - if (!Group::isMember($receiver_id, $user_id)) return []; + if (!di()->get(GroupMemberService::class)->isMember($receiver_id, $user_id)) return []; $sqlObj = $sqlObj->where('receiver_id', $receiver_id)->whereIn('msg_type', $msg_type)->where('talk_type', TalkModeConstant::GROUP_CHAT)->where('is_revoke', 0); } diff --git a/app/Support/UserRelation.php b/app/Support/UserRelation.php index fab994a..1414845 100644 --- a/app/Support/UserRelation.php +++ b/app/Support/UserRelation.php @@ -4,6 +4,7 @@ namespace App\Support; use App\Constants\TalkModeConstant; use App\Model\Group\Group; +use App\Service\Group\GroupMemberService; use App\Service\UserFriendService; class UserRelation @@ -21,7 +22,7 @@ class UserRelation if ($talk_type == TalkModeConstant::PRIVATE_CHAT) { return di()->get(UserFriendService::class)->isFriend($user_id, $receiver_id, true); } else if ($talk_type == TalkModeConstant::GROUP_CHAT) { - return Group::isMember($receiver_id, $user_id); + return di()->get(GroupMemberService::class)->isMember($receiver_id, $user_id); } return false;