优化代码

main
gzydong 2021-07-28 23:08:07 +08:00
parent d75c33f9ec
commit 40103cd99c
13 changed files with 136 additions and 112 deletions

View File

@ -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('非法请求!');
}
}

View File

@ -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('非管理员禁止操作!');
}

View File

@ -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('暂不属于好友关系或群聊成员,无法查看聊天记录!');
}

View File

@ -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}.",

View File

@ -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']
]);
}
/**

View File

@ -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();
}
}

View File

@ -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() ?? [];
}
}

View File

@ -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, []];
}
}

View File

@ -0,0 +1,74 @@
<?php
declare(strict_types=1);
namespace App\Service\Group;
use App\Model\Group\GroupMember;
use App\Service\BaseService;
class GroupMemberService extends BaseService
{
/**
* 判断群成员权限
*
* @param int $group_id 群组ID
* @param int $user_id 用户ID
* @param int $leader 权限角色
* @return bool
*/
public function isAuth(int $group_id, int $user_id, int $leader = 2): bool
{
return GroupMember::query()->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') ?? "";
}
}

View File

@ -1,12 +1,13 @@
<?php
namespace App\Service;
namespace App\Service\Group;
use _HumbugBox39a196d4601e\Nette\Neon\Exception;
use Exception;
use App\Model\Group\Group;
use App\Model\Group\GroupNotice;
use App\Service\BaseService;
class GroupNoticeService
class GroupNoticeService extends BaseService
{
public function create(int $user_id, array $params)
{
@ -43,12 +44,11 @@ class GroupNoticeService
public function delete(int $notice_id, int $user_id)
{
$notice = GroupNotice::where('id', $notice_id)->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);
}

View File

@ -1,7 +1,7 @@
<?php
declare(strict_types=1);
namespace App\Service;
namespace App\Service\Group;
use App\Cache\LastMessage;
use App\Cache\SocketRoom;
@ -16,6 +16,7 @@ use App\Model\Group\GroupMember;
use App\Model\Talk\TalkList;
use Hyperf\DbConnection\Db;
use Exception;
use App\Service\BaseService;
/**
* Class GroupService
@ -24,6 +25,17 @@ use Exception;
*/
class GroupService extends BaseService
{
/**
* 判断群组是否已解散
*
* @param int $group_id 群ID
* @return bool
*/
public function isDismiss(int $group_id): bool
{
return Group::query()->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;
}

View File

@ -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);
}

View File

@ -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;