From f60ab0f0c1acb8e576bb0580613729ddf6698cb4 Mon Sep 17 00:00:00 2001 From: gzydong <837215079@qq.com> Date: Thu, 25 Mar 2021 17:32:36 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E7=BE=A4=E7=BB=84=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E6=95=B0=E6=8D=AE=E8=A1=A8=E7=BB=93=E6=9E=84=EF=BC=8C?= =?UTF-8?q?=E5=8F=8A=E4=BF=AE=E6=94=B9=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 | 4 +- app/Controller/Api/V1/GroupController.php | 82 +++---- app/Controller/Api/V1/TalkController.php | 10 +- app/Controller/WebSocketController.php | 4 +- app/Model/Group/Group.php | 112 +++++++++ app/Model/Group/GroupMember.php | 96 ++++++++ .../{UsersGroupNotice.php => GroupNotice.php} | 35 +-- app/Model/Group/UsersGroup.php | 88 ------- app/Model/Group/UsersGroupMember.php | 85 ------- app/Service/EmoticonService.php | 4 +- app/Service/GroupService.php | 217 ++++++++---------- app/Service/MessageHandleService.php | 4 +- app/Service/TalkService.php | 77 +++---- .../2020_11_04_153616_create_group_table.php | 46 ++++ ..._11_04_153616_create_users_group_table.php | 52 ----- ...11_04_153626_create_group_member_table.php | 45 ++++ ...153626_create_users_group_member_table.php | 41 ---- ...1_04_153636_create_group_notice_table.php} | 22 +- 18 files changed, 526 insertions(+), 498 deletions(-) create mode 100644 app/Model/Group/Group.php create mode 100644 app/Model/Group/GroupMember.php rename app/Model/Group/{UsersGroupNotice.php => GroupNotice.php} (50%) delete mode 100644 app/Model/Group/UsersGroup.php delete mode 100644 app/Model/Group/UsersGroupMember.php create mode 100644 migrations/2020_11_04_153616_create_group_table.php delete mode 100644 migrations/2020_11_04_153616_create_users_group_table.php create mode 100644 migrations/2020_11_04_153626_create_group_member_table.php delete mode 100644 migrations/2020_11_04_153626_create_users_group_member_table.php rename migrations/{2020_11_04_153636_create_users_group_notice_table.php => 2020_11_04_153636_create_group_notice_table.php} (55%) diff --git a/app/Controller/Api/V1/DownloadController.php b/app/Controller/Api/V1/DownloadController.php index 1ba8f36..2ff2c15 100644 --- a/app/Controller/Api/V1/DownloadController.php +++ b/app/Controller/Api/V1/DownloadController.php @@ -20,7 +20,7 @@ use App\Middleware\JWTAuthMiddleware; use App\Model\Article\ArticleAnnex; use App\Model\Chat\ChatRecord; use App\Model\Chat\ChatRecordsFile; -use App\Model\Group\UsersGroup; +use App\Model\Group\Group; use App\Service\UploadService; use Hyperf\HttpServer\Contract\ResponseInterface; @@ -64,7 +64,7 @@ class DownloadController extends CController return $this->response->fail('非法请求...'); } } else { - if (!UsersGroup::isMember($recordsInfo->receive_id, $user_id)) { + if (!Group::isMember($recordsInfo->receive_id, $user_id)) { return $this->response->fail('非法请求...'); } } diff --git a/app/Controller/Api/V1/GroupController.php b/app/Controller/Api/V1/GroupController.php index d7ddf7a..412896e 100644 --- a/app/Controller/Api/V1/GroupController.php +++ b/app/Controller/Api/V1/GroupController.php @@ -20,9 +20,9 @@ use App\Middleware\JWTAuthMiddleware; use Hyperf\Amqp\Producer; use App\Model\UsersFriend; use App\Model\UsersChatList; -use App\Model\Group\UsersGroup; -use App\Model\Group\UsersGroupMember; -use App\Model\Group\UsersGroupNotice; +use App\Model\Group\Group; +use App\Model\Group\GroupMember; +use App\Model\Group\GroupNotice; use App\Amqp\Producer\ChatMessageProducer; use App\Service\SocketRoomService; use App\Service\GroupService; @@ -217,7 +217,7 @@ class GroupController extends CController 'avatar' => 'present|url' ]); - $result = UsersGroup::where('id', $params['group_id'])->where('user_id', $this->uid())->update([ + $result = Group::where('id', $params['group_id'])->where('user_id', $this->uid())->update([ 'group_name' => $params['group_name'], 'group_profile' => $params['group_profile'], 'avatar' => $params['avatar'] @@ -281,13 +281,13 @@ class GroupController extends CController { $group_id = $this->request->input('group_id', 0); - $user_id = $this->uid(); - $groupInfo = UsersGroup::leftJoin('users', 'users.id', '=', 'users_group.user_id') - ->where('users_group.id', $group_id)->where('users_group.status', 0)->first([ - 'users_group.id', 'users_group.user_id', - 'users_group.group_name', - 'users_group.group_profile', 'users_group.avatar', - 'users_group.created_at', + $user_id = $this->uid(); + $groupInfo = Group::leftJoin('users', 'users.id', '=', 'group.user_id') + ->where('group.id', $group_id)->where('group.status', 0)->first([ + 'group.id', 'group.user_id', + 'group.group_name', + 'group.group_profile', 'group.avatar', + 'group.created_at', 'users.nickname' ]); @@ -295,7 +295,7 @@ class GroupController extends CController return $this->response->success([]); } - $notice = UsersGroupNotice::where('group_id', $group_id) + $notice = GroupNotice::where('group_id', $group_id) ->where('is_delete', 0) ->orderBy('id', 'desc') ->first(['title', 'content']); @@ -308,7 +308,7 @@ class GroupController extends CController 'created_at' => $groupInfo->created_at, 'is_manager' => $groupInfo->user_id == $user_id, 'manager_nickname' => $groupInfo->nickname, - 'visit_card' => UsersGroupMember::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, 'notice' => $notice ? $notice->toArray() : [] ]); @@ -327,7 +327,7 @@ class GroupController extends CController 'visit_card' => 'required|max:20' ]); - $isTrue = UsersGroupMember::where('group_id', $params['group_id']) + $isTrue = GroupMember::where('group_id', $params['group_id']) ->where('user_id', $this->uid()) ->where('status', 0) ->update(['visit_card' => $params['visit_card']]); @@ -345,9 +345,9 @@ class GroupController extends CController public function getInviteFriends() { $group_id = $this->request->input('group_id', 0); - $friends = UsersFriend::getUserFriends($this->uid()); + $friends = UsersFriend::getUserFriends($this->uid()); if ($group_id > 0 && $friends) { - if ($ids = UsersGroupMember::getGroupMemberIds($group_id)) { + if ($ids = GroupMember::getGroupMemberIds($group_id)) { foreach ($friends as $k => $item) { if (in_array($item['id'], $ids)) unset($friends[$k]); } @@ -378,23 +378,23 @@ class GroupController extends CController */ public function getGroupMembers() { - $user_id = $this->uid(); + $user_id = $this->uid(); $group_id = $this->request->input('group_id', 0); // 判断用户是否是群成员 - if (!UsersGroup::isMember($group_id, $user_id)) { + if (!Group::isMember($group_id, $user_id)) { return $this->response->fail('非法操作...'); } - $members = UsersGroupMember::select([ - 'users_group_member.id', 'users_group_member.group_owner as is_manager', 'users_group_member.visit_card', - 'users_group_member.user_id', 'users.avatar', 'users.nickname', 'users.gender', + $members = GroupMember::select([ + 'group_member.id', 'group_member.group_owner as is_manager', 'group_member.visit_card', + 'group_member.user_id', 'users.avatar', 'users.nickname', 'users.gender', 'users.motto', ]) - ->leftJoin('users', 'users.id', '=', 'users_group_member.user_id') + ->leftJoin('users', 'users.id', '=', 'group_member.user_id') ->where([ - ['users_group_member.group_id', '=', $group_id], - ['users_group_member.status', '=', 0], + ['group_member.group_id', '=', $group_id], + ['group_member.status', '=', 0], ])->orderBy('is_manager', 'desc')->get()->toArray(); return $this->response->success($members); @@ -407,27 +407,27 @@ class GroupController extends CController */ public function getGroupNotices() { - $user_id = $this->uid(); + $user_id = $this->uid(); $group_id = $this->request->input('group_id', 0); // 判断用户是否是群成员 - if (!UsersGroup::isMember($group_id, $user_id)) { + if (!Group::isMember($group_id, $user_id)) { return $this->response->fail('非管理员禁止操作...'); } - $rows = UsersGroupNotice::leftJoin('users', 'users.id', '=', 'users_group_notice.user_id') + $rows = GroupNotice::leftJoin('users', 'users.id', '=', 'group_notice.user_id') ->where([ - ['users_group_notice.group_id', '=', $group_id], - ['users_group_notice.is_delete', '=', 0] + ['group_notice.group_id', '=', $group_id], + ['group_notice.is_delete', '=', 0] ]) - ->orderBy('users_group_notice.id', 'desc') + ->orderBy('group_notice.id', 'desc') ->get([ - 'users_group_notice.id', - 'users_group_notice.user_id', - 'users_group_notice.title', - 'users_group_notice.content', - 'users_group_notice.created_at', - 'users_group_notice.updated_at', + 'group_notice.id', + 'group_notice.user_id', + 'group_notice.title', + 'group_notice.content', + 'group_notice.created_at', + 'group_notice.updated_at', 'users.avatar', 'users.nickname', ])->toArray(); @@ -452,13 +452,13 @@ class GroupController extends CController $user_id = $this->uid(); // 判断用户是否是管理员 - if (!UsersGroup::isManager($user_id, $params['group_id'])) { + if (!Group::isManager($user_id, $params['group_id'])) { return $this->response->fail('非管理员禁止操作...'); } // 判断是否是新增数据 if (empty($data['notice_id'])) { - $result = UsersGroupNotice::create([ + $result = GroupNotice::create([ 'group_id' => $params['group_id'], 'title' => $params['title'], 'content' => $params['content'], @@ -475,7 +475,7 @@ class GroupController extends CController return $this->response->success([], '添加群公告信息成功...'); } - $result = UsersGroupNotice::where('id', $data['notice_id'])->update([ + $result = GroupNotice::where('id', $data['notice_id'])->update([ 'title' => $data['title'], 'content' => $data['content'], 'updated_at' => date('Y-m-d H:i:s') @@ -502,11 +502,11 @@ class GroupController extends CController $user_id = $this->uid(); // 判断用户是否是管理员 - if (!UsersGroup::isManager($user_id, $params['group_id'])) { + if (!Group::isManager($user_id, $params['group_id'])) { return $this->response->fail('非法操作...'); } - $result = UsersGroupNotice::where('id', $params['notice_id']) + $result = GroupNotice::where('id', $params['notice_id']) ->where('group_id', $params['group_id']) ->update([ 'is_delete' => 1, diff --git a/app/Controller/Api/V1/TalkController.php b/app/Controller/Api/V1/TalkController.php index c6d7c72..95eb158 100644 --- a/app/Controller/Api/V1/TalkController.php +++ b/app/Controller/Api/V1/TalkController.php @@ -24,7 +24,7 @@ use App\Model\FileSplitUpload; use App\Model\User; use App\Model\UsersChatList; use App\Model\UsersFriend; -use App\Model\Group\UsersGroup; +use App\Model\Group\Group; use App\Service\TalkService; use App\Service\UploadService; use App\Amqp\Producer\ChatMessageProducer; @@ -101,7 +101,7 @@ class TalkController extends CController return $this->response->fail('暂不属于好友关系,无法进行聊天...'); } } else { - if (!UsersGroup::isMember($params['receive_id'], $user_id)) { + if (!Group::isMember($params['receive_id'], $user_id)) { return $this->response->fail('暂不属于群成员,无法进行群聊...'); } } @@ -133,7 +133,7 @@ class TalkController extends CController $data['name'] = $userInfo->nickname; $data['avatar'] = $userInfo->avatar; } else if ($result['type'] == 2) { - $groupInfo = UsersGroup::where('id', $result['group_id'])->first(['group_name', 'avatar']); + $groupInfo = Group::where('id', $result['group_id'])->first(['group_name', 'avatar']); $data['name'] = $groupInfo->group_name; $data['avatar'] = $groupInfo->avatar; } @@ -366,7 +366,7 @@ class TalkController extends CController $limit = 30; // 判断是否属于群成员 - if ($params['source'] == 2 && UsersGroup::isMember($params['receive_id'], $user_id) == false) { + if ($params['source'] == 2 && Group::isMember($params['receive_id'], $user_id) == false) { return $this->response->success([ 'rows' => [], 'record_id' => 0, @@ -428,7 +428,7 @@ class TalkController extends CController $limit = 30; // 判断是否属于群成员 - if ($params['source'] == 2 && UsersGroup::isMember($params['receive_id'], $user_id) == false) { + if ($params['source'] == 2 && Group::isMember($params['receive_id'], $user_id) == false) { return $this->response->success([ 'rows' => [], 'record_id' => 0, diff --git a/app/Controller/WebSocketController.php b/app/Controller/WebSocketController.php index c3ed237..c74bb44 100644 --- a/app/Controller/WebSocketController.php +++ b/app/Controller/WebSocketController.php @@ -27,7 +27,7 @@ use Phper666\JWTAuth\JWT; use App\Service\SocketClientService; use App\Service\MessageHandleService; use App\Service\SocketRoomService; -use App\Model\Group\UsersGroupMember; +use App\Model\Group\GroupMember; use App\Amqp\Producer\ChatMessageProducer; use App\Support\SocketIOParser; @@ -99,7 +99,7 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos $this->socketClientService->bindRelation($request->fd, $userInfo['user_id']); // 加入群聊 - $groupIds = UsersGroupMember::getUserGroupIds($userInfo['user_id']); + $groupIds = GroupMember::getUserGroupIds($userInfo['user_id']); foreach ($groupIds as $group_id) { $this->socketRoomService->addRoomMember($userInfo['user_id'], $group_id); } diff --git a/app/Model/Group/Group.php b/app/Model/Group/Group.php new file mode 100644 index 0000000..6a2086e --- /dev/null +++ b/app/Model/Group/Group.php @@ -0,0 +1,112 @@ + 'integer', + 'max_num' => 'integer', + 'is_overt' => 'integer', + 'is_mute' => 'integer', + '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::where('id', $group_id)->where('creator_id', $user_id)->where('leader', $leader)->exists(); + } + + /** + * 判断群组是否已解散 + * + * @param int $group_id 群ID + * @return bool + */ + public static function isDismiss(int $group_id) + { + return self::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 new file mode 100644 index 0000000..8bf2ddd --- /dev/null +++ b/app/Model/Group/GroupMember.php @@ -0,0 +1,96 @@ + 'integer', + 'user_id' => 'integer', + 'leader' => 'integer', + 'is_mute' => 'integer', + 'is_quit' => 'integer', + 'created_at' => 'datetime', + 'deleted_at' => 'datetime', + ]; + + /** + * 获取聊天群成员ID + * + * @param int $group_id 群聊ID + * @return array + */ + public static function getGroupMemberIds(int $group_id) + { + return self::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::where('group_id', $group_id)->where('user_id', $user_id)->value('visit_card') ?? ""; + } + + /** + * 获取用户的所有群ID + * + * @param int $user_id 用户ID + * @return array + */ + public static function getUserGroupIds(int $user_id) + { + return self::where('user_id', $user_id)->where('is_quit', 0)->pluck('group_id')->toArray() ?? []; + } +} diff --git a/app/Model/Group/UsersGroupNotice.php b/app/Model/Group/GroupNotice.php similarity index 50% rename from app/Model/Group/UsersGroupNotice.php rename to app/Model/Group/GroupNotice.php index 81db3fe..ab225ce 100644 --- a/app/Model/Group/UsersGroupNotice.php +++ b/app/Model/Group/GroupNotice.php @@ -1,5 +1,4 @@ 'integer', 'group_id' => 'integer', - 'user_id' => 'integer', + 'creator_id' => 'integer', + 'is_top' => 'integer', 'is_delete' => 'integer', + 'is_confirm' => 'integer', 'created_at' => 'datetime', - 'updated_at' => 'datetime' + 'updated_at' => 'datetime', + 'deleted_at' => 'datetime', ]; } diff --git a/app/Model/Group/UsersGroup.php b/app/Model/Group/UsersGroup.php deleted file mode 100644 index ed48a83..0000000 --- a/app/Model/Group/UsersGroup.php +++ /dev/null @@ -1,88 +0,0 @@ - 'integer', - 'user_id' => 'integer', - 'status' => 'integer', - 'created_at' => 'datetime' - ]; - - /** - * 获取群聊成员 - */ - public function members() - { - return $this->hasMany(UsersGroupMember::class, 'group_id', 'id'); - } - - /** - * 判断用户是否是管理员 - * - * @param int $user_id 用户ID - * @param int $group_id 群ID - * @return mixed - */ - public static function isManager(int $user_id, int $group_id) - { - return self::where('id', $group_id)->where('user_id', $user_id)->exists(); - } - - /** - * 判断用户是否是群成员 - * - * @param int $group_id 群ID - * @param int $user_id 用户ID - * @return bool - */ - public static function isMember(int $group_id, int $user_id) - { - return UsersGroupMember::where('group_id', $group_id)->where('user_id', $user_id)->where('status', 0)->exists(); - } -} diff --git a/app/Model/Group/UsersGroupMember.php b/app/Model/Group/UsersGroupMember.php deleted file mode 100644 index 1b9f813..0000000 --- a/app/Model/Group/UsersGroupMember.php +++ /dev/null @@ -1,85 +0,0 @@ - 'integer', - 'group_id' => 'integer', - 'user_id' => 'integer', - 'group_owner' => 'integer', - 'status' => 'integer', - 'created_at' => 'datetime' - ]; - - /** - * 获取聊天群成员ID - * - * @param int $group_id 群聊ID - * @return mixed - */ - public static function getGroupMemberIds(int $group_id) - { - return self::where('group_id', $group_id)->where('status', 0)->pluck('user_id')->toArray(); - } - - /** - * 获取用户的群名片 - * - * @param int $user_id 用户ID - * @param int $group_id 群ID - * @return mixed - */ - 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'); - } - - /** - * 获取用户的所有群ID - * - * @param int $user_id - * @return array - */ - public static function getUserGroupIds(int $user_id) - { - return self::where('user_id', $user_id)->where('status', 0)->pluck('group_id')->toArray(); - } -} diff --git a/app/Service/EmoticonService.php b/app/Service/EmoticonService.php index 49fce21..33ab70d 100644 --- a/app/Service/EmoticonService.php +++ b/app/Service/EmoticonService.php @@ -5,7 +5,7 @@ namespace App\Service; use App\Model\Chat\ChatRecord; use App\Model\Chat\ChatRecordsFile; use App\Model\EmoticonDetail; -use App\Model\Group\UsersGroup; +use App\Model\Group\Group; use App\Model\UsersEmoticon; /** @@ -107,7 +107,7 @@ class EmoticonService extends BaseService return [false, []]; } } else { - if (!UsersGroup::isMember($result->receive_id, $user_id)) { + if (!Group::isMember($result->receive_id, $user_id)) { return [false, []]; } } diff --git a/app/Service/GroupService.php b/app/Service/GroupService.php index f91fa85..1f71523 100644 --- a/app/Service/GroupService.php +++ b/app/Service/GroupService.php @@ -5,8 +5,8 @@ namespace App\Service; use App\Cache\LastMsgCache; use App\Model\Chat\ChatRecord; use App\Model\Chat\ChatRecordsInvite; -use App\Model\Group\UsersGroup; -use App\Model\Group\UsersGroupMember; +use App\Model\Group\Group; +use App\Model\Group\GroupMember; use App\Model\UsersChatList; use Hyperf\DbConnection\Db; use Exception; @@ -25,26 +25,23 @@ class GroupService extends BaseService */ public function getGroups(int $user_id): array { - $fields = [ - 'users_group.id', 'users_group.group_name', 'users_group.avatar', - 'users_group.group_profile', 'users_group.user_id as group_user_id' - ]; - - $items = UsersGroupMember::join('users_group', 'users_group.id', '=', 'users_group_member.group_id') + $items = GroupMember::join('group', 'group.id', '=', 'group_member.group_id', 'inner', [ + ['group.is_dismiss', '=', 0] + ]) ->where([ - ['users_group_member.user_id', '=', $user_id], - ['users_group_member.status', '=', 0] + ['group_member.user_id', '=', $user_id], + ['group_member.is_quit', '=', 0] ]) ->orderBy('id', 'desc') - ->get($fields)->toArray(); + ->get([ + 'group.id', + 'group.group_name', + 'group.avatar', + 'group.group_profile', + 'group.leader', + ])->toArray(); foreach ($items as $key => $item) { - // 判断当前用户是否是群主 - $items[$key]['isGroupLeader'] = $item['group_user_id'] == $user_id; - - //删除无关字段 - unset($items[$key]['group_user_id']); - // 是否消息免打扰 $items[$key]['not_disturb'] = UsersChatList::where([ ['uid', '=', $user_id], @@ -67,30 +64,31 @@ class GroupService extends BaseService public function create(int $user_id, array $group_info, $friend_ids = []) { $friend_ids[] = $user_id; - $groupMember = []; - $chatList = []; + $groupMember = []; + $chatList = []; Db::beginTransaction(); try { - $insRes = UsersGroup::create([ - 'user_id' => $user_id, + $insRes = Group::create([ + 'creator_id' => $user_id, 'group_name' => $group_info['name'], 'avatar' => $group_info['avatar'], - 'group_profile' => $group_info['profile'], - 'status' => 0, + 'profile' => $group_info['profile'], + 'max_num' => Group::MAX_MEMBER_NUM, + 'is_overt' => 0, + 'is_mute' => 0, + 'is_dismiss' => 0, 'created_at' => date('Y-m-d H:i:s') ]); - if (!$insRes) { - throw new Exception('创建群失败'); - } - foreach ($friend_ids as $k => $uid) { $groupMember[] = [ 'group_id' => $insRes->id, 'user_id' => $uid, - 'group_owner' => $user_id == $uid ? 1 : 0, - 'status' => 0, + 'leader' => $user_id == $uid ? 2 : 0, + 'is_mute' => 0, + 'is_quit' => 0, + 'user_card' => 0, 'created_at' => date('Y-m-d H:i:s'), ]; @@ -105,7 +103,7 @@ class GroupService extends BaseService ]; } - if (!Db::table('users_group_member')->insert($groupMember)) { + if (!Db::table('group_member')->insert($groupMember)) { throw new Exception('创建群成员信息失败'); } @@ -121,10 +119,6 @@ class GroupService extends BaseService 'created_at' => date('Y-m-d H:i:s') ]); - if (!$result) { - throw new Exception('创建群成员的聊天列表失败'); - } - ChatRecordsInvite::create([ 'record_id' => $result->id, 'type' => 1, @@ -135,7 +129,6 @@ class GroupService extends BaseService Db::commit(); } catch (Exception $e) { Db::rollBack(); - logger()->error($e); return [false, 0]; } @@ -154,26 +147,15 @@ class GroupService extends BaseService */ public function dismiss(int $group_id, int $user_id) { - if (!UsersGroup::where('id', $group_id)->where('status', 0)->exists()) { + $group = Group::where('id', $group_id)->first(['creator_id', 'is_dismiss']); + if (!$group || $group->creator_id != $user_id || $group->is_dismiss == 1) { return false; } - //判断执行者是否属于群主 - if (!UsersGroup::isManager($user_id, $group_id)) { - return false; - } - - Db::beginTransaction(); - try { - UsersGroup::where('id', $group_id)->update(['status' => 1]); - UsersGroupMember::where('group_id', $group_id)->update(['status' => 1]); - Db::commit(); - } catch (Exception $e) { - Db::rollBack(); - return false; - } - - return true; + return Group::where('id', $group_id)->where('creator_id', $user_id)->update([ + 'is_dismiss' => 1, + 'dismissed_at' => date('Y-m-d H:i:s'), + ]) ? true : false; } /** @@ -186,31 +168,47 @@ class GroupService extends BaseService */ public function invite(int $user_id, int $group_id, $friend_ids = []) { - $info = UsersGroupMember::select(['id', 'status'])->where('group_id', $group_id)->where('user_id', $user_id)->first(); - - //判断主动邀请方是否属于聊天群成员 - if (!$info && $info->status == 1) { + if (empty($friend_ids)) { return [false, 0]; } - if (empty($friend_ids)) { + $info = GroupMember::where('group_id', $group_id)->where('user_id', $user_id)->first(['id', 'is_quit']); + + // 判断主动邀请方是否属于聊天群成员 + if (!$info && $info->is_quit == 1) { return [false, 0]; } $updateArr = $insertArr = $updateArr1 = $insertArr1 = []; - $members = UsersGroupMember::where('group_id', $group_id)->whereIn('user_id', $friend_ids)->get(['id', 'user_id', 'status'])->keyBy('user_id')->toArray(); + $members = GroupMember::where('group_id', $group_id)->whereIn('user_id', $friend_ids)->get(['id', 'user_id', 'is_quit'])->keyBy('user_id')->toArray(); $chatArr = UsersChatList::where('group_id', $group_id)->whereIn('uid', $friend_ids)->get(['id', 'uid', 'status'])->keyBy('uid')->toArray(); foreach ($friend_ids as $uid) { if (!isset($members[$uid])) {//存在聊天群成员记录 - $insertArr[] = ['group_id' => $group_id, 'user_id' => $uid, 'group_owner' => 0, 'status' => 0, 'created_at' => date('Y-m-d H:i:s')]; + $insertArr[] = [ + 'group_id' => $group_id, + 'user_id' => $uid, + 'leader' => 0, + 'is_mute' => 0, + 'is_quit' => 0, + 'user_card' => '', + 'created_at' => date('Y-m-d H:i:s') + ]; } else if ($members[$uid]['status'] == 1) { $updateArr[] = $members[$uid]['id']; } if (!isset($chatArr[$uid])) { - $insertArr1[] = ['type' => 2, 'uid' => $uid, 'friend_id' => 0, 'group_id' => $group_id, 'status' => 1, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')]; + $insertArr1[] = [ + 'type' => 2, + 'uid' => $uid, + 'friend_id' => 0, + 'group_id' => $group_id, + 'status' => 1, + 'created_at' => date('Y-m-d H:i:s'), + 'updated_at' => date('Y-m-d H:i:s') + ]; } else if ($chatArr[$uid]['status'] == 0) { $updateArr1[] = $chatArr[$uid]['id']; } @@ -218,15 +216,24 @@ class GroupService extends BaseService try { if ($updateArr) { - UsersGroupMember::whereIn('id', $updateArr)->update(['status' => 0]); + GroupMember::whereIn('id', $updateArr)->update([ + 'leader' => 0, + 'is_mute' => 0, + 'is_quit' => 0, + 'user_card' => '', + 'created_at' => date('Y-m-d H:i:s') + ]); } if ($insertArr) { - Db::table('users_group_member')->insert($insertArr); + Db::table('group_member')->insert($insertArr); } if ($updateArr1) { - UsersChatList::whereIn('id', $updateArr1)->update(['status' => 1, 'created_at' => date('Y-m-d H:i:s')]); + UsersChatList::whereIn('id', $updateArr1)->update([ + 'status' => 1, + 'created_at' => date('Y-m-d H:i:s') + ]); } if ($insertArr1) { @@ -241,17 +248,13 @@ class GroupService extends BaseService 'created_at' => date('Y-m-d H:i:s') ]); - if (!$result) throw new Exception('添加群通知记录失败1'); - - $result2 = ChatRecordsInvite::create([ + ChatRecordsInvite::create([ 'record_id' => $result->id, 'type' => 1, 'operate_user_id' => $user_id, 'user_ids' => implode(',', $friend_ids) ]); - if (!$result2) throw new Exception('添加群通知记录失败2'); - Db::commit(); } catch (\Exception $e) { Db::rollBack(); @@ -271,41 +274,35 @@ class GroupService extends BaseService */ public function quit(int $user_id, int $group_id) { - $record_id = 0; + if (Group::isManager($user_id, $group_id)) { + return [false, 0]; + } + Db::beginTransaction(); try { - $res = UsersGroupMember::where('group_id', $group_id)->where('user_id', $user_id)->where('group_owner', 0)->update(['status' => 1]); - if ($res) { - UsersChatList::where('uid', $user_id)->where('type', 2)->where('group_id', $group_id)->update(['status' => 0]); + GroupMember::where('group_id', $group_id)->where('user_id', $user_id)->update([ + 'is_quit' => 1, + 'deleted_at' => date('Y-m-d H:i:s'), + ]); - $result = ChatRecord::create([ - 'msg_type' => 3, - 'source' => 2, - 'user_id' => 0, - 'receive_id' => $group_id, - 'content' => $user_id, - 'created_at' => date('Y-m-d H:i:s') - ]); + UsersChatList::where('uid', $user_id)->where('type', 2)->where('group_id', $group_id)->update(['status' => 0]); + $result = ChatRecord::create([ + 'msg_type' => 3, + 'source' => 2, + 'user_id' => 0, + 'receive_id' => $group_id, + 'content' => $user_id, + 'created_at' => date('Y-m-d H:i:s') + ]); - if (!$result) { - throw new Exception('添加群通知记录失败 : quitGroupChat'); - } + $record_id = $result->id; - $result2 = ChatRecordsInvite::create([ - 'record_id' => $result->id, - 'type' => 2, - 'operate_user_id' => $user_id, - 'user_ids' => $user_id - ]); - - if (!$result2) { - throw new Exception('添加群通知记录失败2 : quitGroupChat'); - } - - UsersChatList::where('uid', $user_id)->where('group_id', $group_id)->update(['status' => 0, 'updated_at' => date('Y-m-d H:i:s')]); - - $record_id = $result->id; - } + ChatRecordsInvite::create([ + 'record_id' => $result->id, + 'type' => 2, + 'operate_user_id' => $user_id, + 'user_ids' => $user_id + ]); Db::commit(); } catch (Exception $e) { @@ -326,16 +323,16 @@ class GroupService extends BaseService */ public function removeMember(int $group_id, int $user_id, array $member_ids) { - if (!UsersGroup::isManager($user_id, $group_id)) { + if (!Group::isManager($user_id, $group_id, [1, 2])) { return [false, 0]; } Db::beginTransaction(); try { - //更新用户状态 - if (!UsersGroupMember::where('group_id', $group_id)->whereIn('user_id', $member_ids)->where('group_owner', 0)->update(['status' => 1])) { - throw new Exception('修改群成员状态失败'); - } + GroupMember::where('group_id', $group_id)->whereIn('user_id', $member_ids)->whereIn('leader', [0, 1])->update([ + 'is_quit' => 1, + 'deleted_at' => date('Y-m-d H:i:s'), + ]); $result = ChatRecord::create([ 'msg_type' => 3, @@ -345,24 +342,14 @@ class GroupService extends BaseService 'created_at' => date('Y-m-d H:i:s') ]); - if (!$result) { - throw new Exception('添加群通知记录失败1'); - } - - $result2 = ChatRecordsInvite::create([ + ChatRecordsInvite::create([ 'record_id' => $result->id, 'type' => 3, 'operate_user_id' => $user_id, 'user_ids' => implode(',', $member_ids) ]); - if (!$result2) { - throw new Exception('添加群通知记录失败2'); - } - - foreach ($member_ids as $member_id) { - UsersChatList::where('uid', $member_id)->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(); } catch (Exception $e) { diff --git a/app/Service/MessageHandleService.php b/app/Service/MessageHandleService.php index aadf1c2..628938d 100644 --- a/app/Service/MessageHandleService.php +++ b/app/Service/MessageHandleService.php @@ -11,7 +11,7 @@ use App\Amqp\Producer\ChatMessageProducer; use App\Cache\LastMsgCache; use App\Cache\UnreadTalkCache; use App\Model\Chat\ChatRecord; -use App\Model\Group\UsersGroup; +use App\Model\Group\Group; use App\Model\UsersFriend; class MessageHandleService @@ -62,7 +62,7 @@ class MessageHandleService } } else if ($data['source_type'] == 2) {//群聊 //判断是否属于群成员 - if (!UsersGroup::isMember((int)$data['receive_user'], (int)$data['send_user'])) { + if (!Group::isMember((int)$data['receive_user'], (int)$data['send_user'])) { return; } } diff --git a/app/Service/TalkService.php b/app/Service/TalkService.php index 4de45a2..7abd379 100644 --- a/app/Service/TalkService.php +++ b/app/Service/TalkService.php @@ -10,7 +10,7 @@ use App\Model\Chat\ChatRecordsCode; use App\Model\Chat\ChatRecordsFile; use App\Model\Chat\ChatRecordsForward; use App\Model\Chat\ChatRecordsInvite; -use App\Model\Group\UsersGroup; +use App\Model\Group\Group; use App\Model\User; use App\Model\UsersChatList; use App\Model\UsersFriend; @@ -38,41 +38,38 @@ class TalkService extends BaseService $rows = UsersChatList::from('users_chat_list as list') ->leftJoin('users', 'users.id', '=', 'list.friend_id') - ->leftJoin('users_group as group', 'group.id', '=', 'list.group_id') + ->leftJoin('group', 'group.id', '=', 'list.group_id') ->where('list.uid', $user_id) ->where('list.status', 1) ->orderBy('updated_at', 'desc') ->get($filed) ->toArray(); - if (!$rows) { - return []; - } - + if (!$rows) return []; $socketFDService = make(SocketClientService::class); - $runIdAll = $socketFDService->getServerRunIdAll(); + $runIdAll = $socketFDService->getServerRunIdAll(); $rows = array_map(function ($item) use ($user_id, $socketFDService, $runIdAll) { - $data['id'] = $item['id']; - $data['type'] = $item['type']; - $data['friend_id'] = $item['friend_id']; - $data['group_id'] = $item['group_id']; - $data['name'] = '';//对方昵称/群名称 - $data['unread_num'] = 0;//未读消息数量 - $data['avatar'] = '';//默认头像 + $data['id'] = $item['id']; + $data['type'] = $item['type']; + $data['friend_id'] = $item['friend_id']; + $data['group_id'] = $item['group_id']; + $data['name'] = '';//对方昵称/群名称 + $data['unread_num'] = 0;//未读消息数量 + $data['avatar'] = '';//默认头像 $data['remark_name'] = '';//好友备注 - $data['msg_text'] = '......'; - $data['updated_at'] = $item['updated_at']; - $data['online'] = 0; + $data['msg_text'] = '......'; + $data['updated_at'] = $item['updated_at']; + $data['online'] = 0; $data['not_disturb'] = $item['not_disturb']; - $data['is_top'] = $item['is_top']; + $data['is_top'] = $item['is_top']; if ($item['type'] == 1) { - $data['name'] = $item['nickname']; - $data['avatar'] = $item['user_avatar']; + $data['name'] = $item['nickname']; + $data['avatar'] = $item['user_avatar']; $data['unread_num'] = make(UnreadTalkCache::class)->get($user_id, $item['friend_id']); - $data['online'] = $socketFDService->isOnlineAll($item['friend_id'], $runIdAll); + $data['online'] = $socketFDService->isOnlineAll($item['friend_id'], $runIdAll); $remark = FriendRemarkCache::get($user_id, $item['friend_id']); if ($remark) { @@ -88,13 +85,13 @@ class TalkService extends BaseService } } } else { - $data['name'] = strval($item['group_name']); + $data['name'] = strval($item['group_name']); $data['avatar'] = $item['group_avatar']; } $records = LastMsgCache::get($item['type'] == 1 ? $item['friend_id'] : $item['group_id'], $item['type'] == 1 ? $user_id : 0); if ($records) { - $data['msg_text'] = $records['text']; + $data['msg_text'] = $records['text']; $data['updated_at'] = $records['created_at']; } @@ -174,10 +171,10 @@ class TalkService extends BaseService } foreach ($rows as $k => $row) { - $rows[$k]['file'] = []; + $rows[$k]['file'] = []; $rows[$k]['code_block'] = []; - $rows[$k]['forward'] = []; - $rows[$k]['invite'] = []; + $rows[$k]['forward'] = []; + $rows[$k]['invite'] = []; switch ($row['msg_type']) { case 2://2:文件消息 @@ -306,7 +303,7 @@ class TalkService extends BaseService //判断是否有权限查看 if ($result->source == 1 && ($result->user_id != $user_id && $result->receive_id != $user_id)) { return []; - } else if ($result->source == 2 && !UsersGroup::isMember($result->receive_id, $user_id)) { + } else if ($result->source == 2 && !Group::isMember($result->receive_id, $user_id)) { return []; } @@ -357,7 +354,7 @@ class TalkService extends BaseService } // 判读是否属于群消息并且判断是否是群成员 - if ($source == 2 && !UsersGroup::isMember($receive_id, $user_id)) { + if ($source == 2 && !Group::isMember($receive_id, $user_id)) { return false; } @@ -394,7 +391,7 @@ class TalkService extends BaseService return [false, '非法操作', []]; } } else if ($result->source == 2) { - if (!UsersGroup::isMember($result->receive_id, $user_id)) { + if (!Group::isMember($result->receive_id, $user_id)) { return [false, '非法操作', []]; } } @@ -426,12 +423,12 @@ class TalkService extends BaseService return []; } } else if ($result->source == 2) { - if (!UsersGroup::isMember($result->receive_id, $user_id)) { + if (!Group::isMember($result->receive_id, $user_id)) { return []; } } - $fileInfo = null; + $fileInfo = null; $codeBlock = null; if ($result->msg_type == 2) { $fileInfo = ChatRecordsFile::where('record_id', $record_id)->first(); @@ -516,7 +513,7 @@ class TalkService extends BaseService //验证是否有权限转发 if ($source == 2) {//群聊消息 //判断是否是群聊成员 - if (!UsersGroup::isMember($receive_id, $user_id)) { + if (!Group::isMember($receive_id, $user_id)) { return false; } @@ -687,13 +684,13 @@ class TalkService extends BaseService Db::beginTransaction(); try { $message['created_at'] = date('Y-m-d H:i:s'); - $insert = ChatRecord::create($message); + $insert = ChatRecord::create($message); if (!$insert) { throw new Exception('插入聊天记录失败...'); } - $fileInfo['record_id'] = $insert->id; + $fileInfo['record_id'] = $insert->id; $fileInfo['created_at'] = date('Y-m-d H:i:s'); if (!ChatRecordsFile::create($fileInfo)) { throw new Exception('插入聊天记录(文件消息)失败...'); @@ -720,12 +717,12 @@ class TalkService extends BaseService Db::beginTransaction(); try { $message['created_at'] = date('Y-m-d H:i:s'); - $insert = ChatRecord::create($message); + $insert = ChatRecord::create($message); if (!$insert) { throw new Exception('插入聊天记录失败...'); } - $codeBlock['record_id'] = $insert->id; + $codeBlock['record_id'] = $insert->id; $codeBlock['created_at'] = date('Y-m-d H:i:s'); if (!ChatRecordsCode::create($codeBlock)) { throw new Exception('插入聊天记录(代码消息)失败...'); @@ -752,12 +749,12 @@ class TalkService extends BaseService Db::beginTransaction(); try { $message['created_at'] = date('Y-m-d H:i:s'); - $insert = ChatRecord::create($message); + $insert = ChatRecord::create($message); if (!$insert) { throw new Exception('插入聊天记录失败...'); } - $emoticon['record_id'] = $insert->id; + $emoticon['record_id'] = $insert->id; $emoticon['created_at'] = date('Y-m-d H:i:s'); if (!ChatRecordsFile::create($emoticon)) { throw new Exception('插入聊天记录(代码消息)失败...'); @@ -784,12 +781,12 @@ class TalkService extends BaseService Db::beginTransaction(); try { $message['created_at'] = date('Y-m-d H:i:s'); - $insert = ChatRecord::create($message); + $insert = ChatRecord::create($message); if (!$insert) { throw new Exception('插入聊天记录失败...'); } - $emoticon['record_id'] = $insert->id; + $emoticon['record_id'] = $insert->id; $emoticon['created_at'] = date('Y-m-d H:i:s'); if (!ChatRecordsFile::create($emoticon)) { throw new Exception('插入聊天记录(代码消息)失败...'); diff --git a/migrations/2020_11_04_153616_create_group_table.php b/migrations/2020_11_04_153616_create_group_table.php new file mode 100644 index 0000000..72adda7 --- /dev/null +++ b/migrations/2020_11_04_153616_create_group_table.php @@ -0,0 +1,46 @@ +unsignedInteger('id', true)->comment('群ID'); + $table->unsignedInteger('creator_id')->default(0)->comment('创建者ID(群主ID)'); + $table->string('group_name', 30)->default('')->charset('utf8mb4')->comment('群名称'); + $table->string('profile', 100)->default('')->comment('群介绍'); + $table->string('avatar', 200)->default('')->comment('群头像'); + $table->unsignedSmallInteger('max_num')->default(200)->comment('最大群成员数量'); + $table->tinyInteger('is_overt')->default(0)->comment('是否公开可见[0:否;1:是;]'); + $table->tinyInteger('is_mute')->default(0)->comment('是否全员禁言 [0:否;1:是;],提示:不包含群主或管理员'); + $table->tinyInteger('is_dismiss')->default(0)->comment('是否已解散[0:否;1:是;]'); + $table->dateTime('created_at')->nullable()->comment('创建时间'); + $table->dateTime('dismissed_at')->nullable()->comment('解散时间'); + + $table->charset = 'utf8'; + $table->collation = 'utf8_general_ci'; + $table->engine = 'InnoDB'; + + $table->index(['group_id', 'is_delete', 'is_top', 'updated_at'], 'idx_group_id_is_delete_is_top_updated_at'); + }); + + $prefix = config('databases.default.prefix'); + DB::statement("ALTER TABLE `{$prefix}group` comment '聊天群组表'"); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('group'); + } +} diff --git a/migrations/2020_11_04_153616_create_users_group_table.php b/migrations/2020_11_04_153616_create_users_group_table.php deleted file mode 100644 index 8ff01f2..0000000 --- a/migrations/2020_11_04_153616_create_users_group_table.php +++ /dev/null @@ -1,52 +0,0 @@ -unsignedInteger('id', true)->comment('群ID'); - $table->unsignedInteger('user_id')->default(0)->comment('用户ID'); - $table->string('group_name', 30)->default('')->charset('utf8mb4')->comment('群名称'); - $table->string('group_profile', 100)->default('')->comment('群介绍'); - $table->tinyInteger('status')->default(0)->comment('群状态[0:正常;1:已解散]'); - $table->string('avatar', 255)->default('')->comment('群头像'); - - //预留字段 - //$table->string('invite_mode', 255)->default(0)->comment('邀请方式[0:全员邀请;1:全员邀请需群主确认;2:仅群主可邀请;]'); - - $table->dateTime('created_at')->nullable()->comment('创建时间'); - }); - - // 预留表(后期邀请入群需同意后进入) - /* - Schema::create('users_group_invite', function (Blueprint $table) { - $table->unsignedInteger('id', true)->comment('自增ID'); - $table->unsignedInteger('record_id')->default(0)->comment('消息记录ID'); - $table->unsignedInteger('group_id')->default(0)->comment('群ID'); - $table->unsignedInteger('user_id', 30)->default(0)->comment('用户ID'); - $table->tinyInteger('status')->default(0)->comment('邀请状态[0:等待同意;1:已同意;2:拒绝邀请]'); - $table->dateTime('created_at')->nullable()->comment('创建时间'); - }); - */ - - $prefix = config('databases.default.prefix'); - DB::statement("ALTER TABLE `{$prefix}users_group` comment '用户聊天群'"); - } - - /** - * Reverse the migrations. - */ - public function down(): void - { - Schema::dropIfExists('users_group'); - } -} diff --git a/migrations/2020_11_04_153626_create_group_member_table.php b/migrations/2020_11_04_153626_create_group_member_table.php new file mode 100644 index 0000000..c6fa3bf --- /dev/null +++ b/migrations/2020_11_04_153626_create_group_member_table.php @@ -0,0 +1,45 @@ +unsignedInteger('id', true)->comment('群成员ID'); + $table->unsignedInteger('group_id')->default(0)->comment('群ID'); + $table->unsignedInteger('user_id')->default(0)->comment('用户ID'); + $table->tinyInteger('leader')->comment('成员属性[0:普通成员;1:管理员;2:群主;]'); + $table->tinyInteger('is_mute')->default(0)->comment('是否禁言[0:否;1:是;]'); + $table->tinyInteger('is_quit')->default(0)->comment('是否退群[0:否;1:是;]'); + $table->string('user_card', 20)->default('')->comment('群名片'); + $table->dateTime('created_at')->nullable()->comment('入群时间'); + $table->dateTime('deleted_at')->nullable()->comment('退群时间'); + + $table->charset = 'utf8'; + $table->collation = 'utf8_general_ci'; + $table->engine = 'InnoDB'; + + $table->unique(['group_id', 'user_id'], 'uk_group_id_user_id'); + $table->index(['user_id', 'group_id'], 'idx_user_id_group_id'); + }); + + $prefix = config('databases.default.prefix'); + DB::statement("ALTER TABLE `{$prefix}group_member` comment '聊天群组成员表'"); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('group_member'); + } +} diff --git a/migrations/2020_11_04_153626_create_users_group_member_table.php b/migrations/2020_11_04_153626_create_users_group_member_table.php deleted file mode 100644 index 397520e..0000000 --- a/migrations/2020_11_04_153626_create_users_group_member_table.php +++ /dev/null @@ -1,41 +0,0 @@ -unsignedInteger('id', true)->comment('自增ID'); - $table->unsignedInteger('group_id')->default(0)->comment('群ID'); - $table->unsignedInteger('user_id')->default(0)->comment('用户ID'); - $table->tinyInteger('group_owner')->nullable()->comment('是否为群主[0:否;1:是]'); - $table->tinyInteger('status')->default(0)->comment('退群状态[0:正常状态;1:已退群]'); - $table->string('visit_card', 20)->default('')->comment('用户群名片'); - $table->dateTime('created_at')->nullable()->comment('入群时间'); - - $table->charset = 'utf8'; - $table->collation = 'utf8_general_ci'; - $table->engine = 'InnoDB'; - - $table->index(['group_id', 'status'], 'idx_group_id_status'); - }); - - $prefix = config('databases.default.prefix'); - DB::statement("ALTER TABLE `{$prefix}users_group_member` comment '群聊成员'"); - } - - /** - * Reverse the migrations. - */ - public function down(): void - { - Schema::dropIfExists('users_group_member'); - } -} diff --git a/migrations/2020_11_04_153636_create_users_group_notice_table.php b/migrations/2020_11_04_153636_create_group_notice_table.php similarity index 55% rename from migrations/2020_11_04_153636_create_users_group_notice_table.php rename to migrations/2020_11_04_153636_create_group_notice_table.php index 3538109..4130c52 100644 --- a/migrations/2020_11_04_153636_create_users_group_notice_table.php +++ b/migrations/2020_11_04_153636_create_group_notice_table.php @@ -4,7 +4,8 @@ use Hyperf\Database\Schema\Schema; use Hyperf\Database\Schema\Blueprint; use Hyperf\Database\Migrations\Migration; use Hyperf\DbConnection\Db; -class CreateUsersGroupNoticeTable extends Migration + +class CreateGroupNoticeTable extends Migration { /** * Run the migrations. @@ -12,12 +13,15 @@ class CreateUsersGroupNoticeTable extends Migration public function up(): void { Schema::create('users_group_notice', function (Blueprint $table) { - $table->unsignedInteger('id', true)->comment('公告ID'); - $table->unsignedInteger('group_id')->default(0)->comment('群ID'); - $table->unsignedInteger('user_id')->default(0)->comment('创建者用户ID'); - $table->string('title', 30)->default('')->charset('utf8mb4')->comment('公告标题'); + $table->unsignedInteger('id', true)->comment('群公告ID'); + $table->unsignedInteger('group_id')->default(0)->comment('群组ID'); + $table->unsignedInteger('creator_id')->default(0)->comment('创建者用户ID'); + $table->string('title', 50)->default('')->charset('utf8mb4')->comment('公告标题'); $table->text('content')->charset('utf8mb4')->comment('公告内容'); - $table->tinyInteger('is_delete')->default(0)->comment('是否删除[0:否;1:已删除]'); + $table->tinyInteger('is_top')->default(0)->comment('是否置顶[0:否;1:是;]'); + $table->tinyInteger('is_delete')->default(0)->comment('是否删除[0:否;1:是;]'); + $table->tinyInteger('is_confirm')->default(0)->comment('是否需群成员确认公告[0:否;1:是;]'); + $table->json('confirm_users')->nullable()->comment('已确认成员'); $table->dateTime('created_at')->nullable()->comment('创建时间'); $table->dateTime('updated_at')->nullable()->comment('更新时间'); $table->dateTime('deleted_at')->nullable()->comment('删除时间'); @@ -26,11 +30,11 @@ class CreateUsersGroupNoticeTable extends Migration $table->collation = 'utf8_general_ci'; $table->engine = 'InnoDB'; - $table->index(['group_id'], 'idx_group_id'); + $table->index(['group_id', 'is_delete', 'is_top', 'updated_at'], 'idx_group_id_is_delete_is_top_updated_at'); }); $prefix = config('databases.default.prefix'); - DB::statement("ALTER TABLE `{$prefix}users_group_notice` comment '群组公告表'"); + DB::statement("ALTER TABLE `{$prefix}group_notice` comment '群组公告表'"); } /** @@ -38,6 +42,6 @@ class CreateUsersGroupNoticeTable extends Migration */ public function down(): void { - Schema::dropIfExists('users_group_notice'); + Schema::dropIfExists('group_notice'); } }