From 3cea1388fb1c2333889c4f932afd0ce54a15ad39 Mon Sep 17 00:00:00 2001 From: gzydong <837215079@qq.com> Date: Sun, 16 Jan 2022 14:22:53 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=85=BC=E5=AE=B9=E5=BC=80=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Api/V1/Group/GroupController.php | 174 ++++++++++++++--- app/Controller/Api/V1/GroupController.php | 178 ------------------ app/Controller/Api/V1/TalkController.php | 4 +- app/Model/Group/Group.php | 4 +- app/Model/Group/GroupMember.php | 2 + .../Talk/{TalkList.php => TalkSession.php} | 2 +- app/Service/Group/GroupService.php | 37 ++-- app/Service/TalkListService.php | 20 +- 8 files changed, 182 insertions(+), 239 deletions(-) delete mode 100644 app/Controller/Api/V1/GroupController.php rename app/Model/Talk/{TalkList.php => TalkSession.php} (98%) diff --git a/app/Controller/Api/V1/Group/GroupController.php b/app/Controller/Api/V1/Group/GroupController.php index e6ae4cc..228477b 100644 --- a/app/Controller/Api/V1/Group/GroupController.php +++ b/app/Controller/Api/V1/Group/GroupController.php @@ -3,9 +3,13 @@ declare(strict_types=1); namespace App\Controller\Api\V1\Group; +use App\Constants\TalkModeConstant; use App\Controller\Api\V1\CController; +use App\Model\Group\Group; +use App\Model\Group\GroupNotice; use App\Service\Group\GroupMemberService; use App\Service\Group\GroupService; +use App\Service\TalkListService; use Hyperf\Di\Annotation\Inject; use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\RequestMapping; @@ -46,33 +50,145 @@ class GroupController extends CController ]); } - // public function Detail() - // { - // - // } - // - // public function Create() - // { - // - // } - // - // public function Dismiss() - // { - // - // } - // - // public function Invite() - // { - // - // } - // - // public function SignOut() - // { - // - // } - // - // public function Setting() - // { - // - // } + /** + * 创建群组 + * @RequestMapping(path="create", methods="post") + */ + public function create(): ResponseInterface + { + $params = $this->request->inputs(['name', 'ids']); + + $this->validate($params, [ + 'name' => 'required', + 'ids' => 'required|ids' + ]); + + [$isTrue, $group] = $this->groupService->create($this->uid(), [ + 'name' => $params['name'], + 'avatar' => $params['avatar'] ?? '', + 'profile' => $params['profile'] ?? '' + ], parse_ids($params['ids'])); + + if (!$isTrue) return $this->response->fail('创建群聊失败,请稍后再试!'); + + return $this->response->success(['group_id' => $group->id]); + } + + /** + * 解散群组接口 + * @RequestMapping(path="dismiss", methods="post") + */ + public function dismiss(): ResponseInterface + { + $params = $this->request->inputs(['group_id']); + + $this->validate($params, [ + 'group_id' => 'required|integer' + ]); + + $isTrue = $this->groupService->dismiss($params['group_id'], $this->uid()); + if (!$isTrue) { + return $this->response->fail('群组解散失败!'); + } + + return $this->response->success([], '群组解散成功...'); + } + + /** + * 邀请好友加入群组接口 + * @RequestMapping(path="invite", methods="post") + */ + public function invite(): ResponseInterface + { + $params = $this->request->inputs(['group_id', 'ids']); + + $this->validate($params, [ + 'group_id' => 'required|integer', + 'ids' => 'required|ids' + ]); + + $isTrue = $this->groupService->invite($this->uid(), intval($params['group_id']), parse_ids($params['ids'])); + if (!$isTrue) { + return $this->response->fail('邀请好友加入群聊失败!'); + } + + return $this->response->success([], '好友已成功加入群聊...'); + } + + /** + * 退出群组接口 + * @RequestMapping(path="secede", methods="post") + */ + public function secede(): ResponseInterface + { + $params = $this->request->inputs(['group_id']); + + $this->validate($params, [ + 'group_id' => 'required|integer' + ]); + + if (!$this->groupService->quit($this->uid(), intval($params['group_id']))) { + return $this->response->fail('退出群组失败!'); + } + + return $this->response->success([], '已成功退出群组...'); + } + + /** + * 获取群信息接口 + * @RequestMapping(path="detail", methods="get") + */ + public function detail(TalkListService $service): ResponseInterface + { + $group_id = (int)$this->request->input('group_id', 0); + $user_id = $this->uid(); + + $groupInfo = Group::leftJoin('users', 'users.id', '=', 'group.creator_id') + ->where('group.id', $group_id)->where('group.is_dismiss', 0)->first([ + 'group.id', + 'group.creator_id', + 'group.group_name', + 'group.profile', + 'group.avatar', + 'group.created_at', + 'users.nickname' + ]); + + if (!$groupInfo) return $this->response->success(); + + $notice = GroupNotice::where('group_id', $group_id)->where('is_delete', 0)->orderBy('id', 'desc')->first(['title', 'content']); + + return $this->response->success([ + 'group_id' => $groupInfo->id, + 'group_name' => $groupInfo->group_name, + 'profile' => $groupInfo->profile, + 'avatar' => $groupInfo->avatar, + 'created_at' => $groupInfo->created_at, + 'is_manager' => $groupInfo->creator_id == $user_id, + 'manager_nickname' => $groupInfo->nickname, + '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() : [] + ]); + } + + /** + * 编辑群组信息 + * @RequestMapping(path="setting", methods="post") + */ + public function Setting(): ResponseInterface + { + $params = $this->request->inputs(['group_id', 'group_name', 'profile', 'avatar']); + + $this->validate($params, [ + 'group_id' => 'required|integer', + 'group_name' => 'required|max:30', + 'profile' => 'present|max:100', + 'avatar' => 'present|url' + ]); + + return $this->groupService->update($params['group_id'], $this->uid(), $params) + ? $this->response->success([], '群组信息修改成功...') + : $this->response->fail('群组信息修改失败!'); + } } diff --git a/app/Controller/Api/V1/GroupController.php b/app/Controller/Api/V1/GroupController.php deleted file mode 100644 index 932d886..0000000 --- a/app/Controller/Api/V1/GroupController.php +++ /dev/null @@ -1,178 +0,0 @@ -request->inputs(['group_name', 'uids']); - $this->validate($params, [ - 'group_name' => 'required', - 'uids' => 'required|ids' - ]); - - [$isTrue, $group] = $this->groupService->create($this->uid(), [ - 'name' => $params['group_name'], - 'avatar' => $params['avatar'] ?? '', - 'profile' => $params['group_profile'] ?? '' - ], parse_ids($params['uids'])); - - if (!$isTrue) return $this->response->fail('创建群聊失败,请稍后再试!'); - - return $this->response->success([ - 'group_id' => $group->id - ]); - } - - /** - * 解散群组接口 - * @RequestMapping(path="dismiss", methods="post") - */ - public function dismiss(): ResponseInterface - { - $params = $this->request->inputs(['group_id']); - $this->validate($params, [ - 'group_id' => 'required|integer' - ]); - - $isTrue = $this->groupService->dismiss($params['group_id'], $this->uid()); - if (!$isTrue) { - return $this->response->fail('群组解散失败!'); - } - - return $this->response->success([], '群组解散成功...'); - } - - /** - * 邀请好友加入群组接口 - * @RequestMapping(path="invite", methods="post") - */ - public function invite(): ResponseInterface - { - $params = $this->request->inputs(['group_id', 'uids']); - $this->validate($params, [ - 'group_id' => 'required|integer', - 'uids' => 'required|ids' - ]); - - $isTrue = $this->groupService->invite($this->uid(), $params['group_id'], parse_ids($params['uids'])); - if (!$isTrue) { - return $this->response->fail('邀请好友加入群聊失败!'); - } - - return $this->response->success([], '好友已成功加入群聊...'); - } - - /** - * 退出群组接口 - * @RequestMapping(path="secede", methods="post") - */ - public function secede(): ResponseInterface - { - $params = $this->request->inputs(['group_id']); - $this->validate($params, [ - 'group_id' => 'required|integer' - ]); - - if (!$this->groupService->quit($this->uid(), $params['group_id'])) { - return $this->response->fail('退出群组失败!'); - } - - return $this->response->success([], '已成功退出群组...'); - } - - /** - * 编辑群组信息 - * @RequestMapping(path="edit", methods="post") - */ - public function editDetail(): ResponseInterface - { - $params = $this->request->inputs(['group_id', 'group_name', 'profile', 'avatar']); - $this->validate($params, [ - 'group_id' => 'required|integer', - 'group_name' => 'required|max:30', - 'profile' => 'present|max:100', - 'avatar' => 'present|url' - ]); - - return $this->groupService->update($params['group_id'], $this->uid(), $params) - ? $this->response->success([], '群组信息修改成功...') - : $this->response->fail('群组信息修改失败!'); - } - - /** - * 获取群信息接口 - * @RequestMapping(path="detail", methods="get") - */ - public function detail(TalkListService $service): ResponseInterface - { - $group_id = $this->request->input('group_id', 0); - $user_id = $this->uid(); - - $groupInfo = Group::leftJoin('users', 'users.id', '=', 'group.creator_id') - ->where('group.id', $group_id)->where('group.is_dismiss', 0)->first([ - 'group.id', - 'group.creator_id', - 'group.group_name', - 'group.profile', - 'group.avatar', - 'group.created_at', - 'users.nickname' - ]); - - if (!$groupInfo) return $this->response->success(); - - $notice = GroupNotice::where('group_id', $group_id)->where('is_delete', 0)->orderBy('id', 'desc')->first(['title', 'content']); - - return $this->response->success([ - 'group_id' => $groupInfo->id, - 'group_name' => $groupInfo->group_name, - 'profile' => $groupInfo->profile, - 'avatar' => $groupInfo->avatar, - 'created_at' => $groupInfo->created_at, - 'is_manager' => $groupInfo->creator_id == $user_id, - 'manager_nickname' => $groupInfo->nickname, - '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() : [] - ]); - } -} diff --git a/app/Controller/Api/V1/TalkController.php b/app/Controller/Api/V1/TalkController.php index 1d0d55c..9ac033a 100644 --- a/app/Controller/Api/V1/TalkController.php +++ b/app/Controller/Api/V1/TalkController.php @@ -7,7 +7,7 @@ use App\Cache\Repository\LockRedis; use App\Cache\UnreadTalkCache; use App\Constants\TalkMessageType; use App\Constants\TalkModeConstant; -use App\Model\Talk\TalkList; +use App\Model\Talk\TalkSession; use App\Service\Group\GroupMemberService; use App\Service\UserFriendService; use App\Support\UserRelation; @@ -95,7 +95,7 @@ class TalkController extends CController return $this->response->fail('创建失败!'); } - $data = TalkList::item([ + $data = TalkSession::item([ 'id' => $result['id'], 'talk_type' => $result['talk_type'], 'receiver_id' => $result['receiver_id'], diff --git a/app/Model/Group/Group.php b/app/Model/Group/Group.php index de7d7ae..ce42439 100644 --- a/app/Model/Group/Group.php +++ b/app/Model/Group/Group.php @@ -40,6 +40,7 @@ class Group extends BaseModel 'is_mute', 'is_dismiss', 'created_at', + 'updated_at', 'dismissed_at', ]; @@ -49,6 +50,7 @@ class Group extends BaseModel 'is_overt' => 'integer', 'is_mute' => 'integer', 'is_dismiss' => 'integer', - 'created_at' => 'datetime' + 'created_at' => 'datetime', + 'updated_at' => 'datetime', ]; } diff --git a/app/Model/Group/GroupMember.php b/app/Model/Group/GroupMember.php index d6e453b..afb7d88 100644 --- a/app/Model/Group/GroupMember.php +++ b/app/Model/Group/GroupMember.php @@ -31,6 +31,7 @@ class GroupMember extends BaseModel 'is_quit', 'user_card', 'created_at', + 'updated_at', 'deleted_at', ]; @@ -41,6 +42,7 @@ class GroupMember extends BaseModel 'is_mute' => 'integer', 'is_quit' => 'integer', 'created_at' => 'datetime', + 'updated_at' => 'datetime', 'deleted_at' => 'datetime', ]; } diff --git a/app/Model/Talk/TalkList.php b/app/Model/Talk/TalkSession.php similarity index 98% rename from app/Model/Talk/TalkList.php rename to app/Model/Talk/TalkSession.php index d300b08..26bcd7a 100644 --- a/app/Model/Talk/TalkList.php +++ b/app/Model/Talk/TalkSession.php @@ -21,7 +21,7 @@ use App\Model\BaseModel; * @property \Carbon\Carbon $updated_at 更新时间 * @package App\Model */ -class TalkList extends BaseModel +class TalkSession extends BaseModel { protected $table = 'talk_session'; diff --git a/app/Service/Group/GroupService.php b/app/Service/Group/GroupService.php index 4cf3aad..03b6c1d 100644 --- a/app/Service/Group/GroupService.php +++ b/app/Service/Group/GroupService.php @@ -13,7 +13,7 @@ use App\Model\Talk\TalkRecords; use App\Model\Talk\TalkRecordsInvite; use App\Model\Group\Group; use App\Model\Group\GroupMember; -use App\Model\Talk\TalkList; +use App\Model\Talk\TalkSession; use Hyperf\DbConnection\Db; use Exception; use App\Service\BaseService; @@ -62,7 +62,8 @@ class GroupService extends BaseService 'is_overt' => 0, 'is_mute' => 0, 'is_dismiss' => 0, - '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'), ]); foreach ($friend_ids as $friend_id) { @@ -71,6 +72,7 @@ class GroupService extends BaseService 'user_id' => $friend_id, 'leader' => $user_id == $friend_id ? 2 : 0, 'created_at' => date('Y-m-d H:i:s'), + 'updated_at' => date('Y-m-d H:i:s'), ]; $chatList[] = [ @@ -86,7 +88,7 @@ class GroupService extends BaseService throw new Exception('创建群成员信息失败'); } - if (!TalkList::insert($chatList)) { + if (!TalkSession::insert($chatList)) { throw new Exception('创建群成员的聊天列表失败'); } @@ -109,6 +111,7 @@ class GroupService extends BaseService Db::commit(); } catch (Exception $e) { Db::rollBack(); + logger()->error($e->getMessage()); return [false, null]; } @@ -206,7 +209,8 @@ class GroupService extends BaseService $updateArr = $insertArr = $updateArr1 = $insertArr1 = []; $members = GroupMember::where('group_id', $group_id)->whereIn('user_id', $friend_ids)->get(['id', 'user_id', 'is_quit'])->keyBy('user_id')->toArray(); - $chatArr = TalkList::where('talk_type', TalkModeConstant::GROUP_CHAT) + + $chatArr = TalkSession::where('talk_type', TalkModeConstant::GROUP_CHAT) ->where('receiver_id', $group_id) ->whereIn('user_id', $friend_ids) ->get(['id', 'user_id', 'is_delete']) @@ -217,7 +221,8 @@ class GroupService extends BaseService $insertArr[] = [ 'group_id' => $group_id, 'user_id' => $uid, - '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') ]; } else if ($members[$uid]['is_quit'] == 1) { $updateArr[] = $members[$uid]['id']; @@ -243,25 +248,20 @@ class GroupService extends BaseService 'leader' => 0, 'is_mute' => 0, 'is_quit' => 0, - 'user_card' => '', - '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') ]); } - if ($insertArr) { - GroupMember::insert($insertArr); - } - if ($updateArr1) { - TalkList::whereIn('id', $updateArr1)->update([ + TalkSession::whereIn('id', $updateArr1)->update([ 'is_delete' => 0, 'created_at' => date('Y-m-d H:i:s') ]); } - if ($insertArr1) { - TalkList::insert($insertArr1); - } + $insertArr && GroupMember::insert($insertArr); + $insertArr1 && TalkSession::insert($insertArr1); $result = TalkRecords::create([ 'talk_type' => TalkModeConstant::GROUP_CHAT, @@ -282,6 +282,7 @@ class GroupService extends BaseService Db::commit(); } catch (Exception $e) { Db::rollBack(); + var_dump($e->getMessage()); return false; } @@ -345,7 +346,7 @@ class GroupService extends BaseService 'user_ids' => $user_id ]); - TalkList::where([ + TalkSession::where([ ['talk_type', '=', TalkModeConstant::GROUP_CHAT], ['user_id', '=', $user_id], ['receiver_id', '=', $group_id], @@ -409,7 +410,7 @@ class GroupService extends BaseService 'user_ids' => implode(',', $member_ids) ]); - TalkList::whereIn('user_id', $member_ids)->where('receiver_id', $group_id)->where('talk_type', TalkModeConstant::GROUP_CHAT)->update([ + TalkSession::whereIn('user_id', $member_ids)->where('receiver_id', $group_id)->where('talk_type', TalkModeConstant::GROUP_CHAT)->update([ 'is_delete' => 1, 'updated_at' => date('Y-m-d H:i:s') ]); @@ -472,7 +473,7 @@ class GroupService extends BaseService $list = []; if ($items) { - $list = TalkList::query()->where('user_id', $user_id) + $list = TalkSession::query()->where('user_id', $user_id) ->where('talk_type', TalkModeConstant::GROUP_CHAT) ->whereIn('receiver_id', array_column($items, 'id')) ->get(['receiver_id', 'is_disturb'])->keyBy('receiver_id')->toArray(); diff --git a/app/Service/TalkListService.php b/app/Service/TalkListService.php index 514fbf6..720bf2f 100644 --- a/app/Service/TalkListService.php +++ b/app/Service/TalkListService.php @@ -7,7 +7,7 @@ use App\Cache\LastMessage; use App\Cache\ServerRunID; use App\Cache\UnreadTalkCache; use App\Constants\TalkModeConstant; -use App\Model\Talk\TalkList; +use App\Model\Talk\TalkSession; use Carbon\Carbon; class TalkListService @@ -23,7 +23,7 @@ class TalkListService */ public function create(int $user_id, int $receiver_id, int $talk_type, bool $is_robot = false): array { - $result = TalkList::updateOrCreate([ + $result = TalkSession::updateOrCreate([ 'talk_type' => $talk_type, 'user_id' => $user_id, 'receiver_id' => $receiver_id, @@ -53,7 +53,7 @@ class TalkListService */ public function top(int $user_id, int $list_id, $is_top = true): bool { - return (bool)TalkList::query()->where([ + return (bool)TalkSession::query()->where([ ['id', '=', $list_id], ['user_id', '=', $user_id], ])->update([ @@ -71,7 +71,7 @@ class TalkListService */ public function delete(int $user_id, int $list_id): bool { - return (bool)TalkList::query()->where([ + return (bool)TalkSession::query()->where([ ['id', '=', $list_id], ['user_id', '=', $user_id], ])->update([ @@ -90,7 +90,7 @@ class TalkListService */ public function deleteByType(int $user_id, int $receiver_id, int $talk_type): bool { - return (bool)TalkList::query()->where([ + return (bool)TalkSession::query()->where([ ['user_id', '=', $user_id], ['talk_type', '=', $talk_type], ['receiver_id', '=', $receiver_id], @@ -114,7 +114,7 @@ class TalkListService 'group.group_name', 'group.avatar as group_avatar' ]; - $rows = TalkList::from('talk_session as list') + $rows = TalkSession::from('talk_session as list') ->leftJoin('users', function ($join) { $join->on('users.id', '=', 'list.receiver_id')->where('list.talk_type', '=', TalkModeConstant::PRIVATE_CHAT); }) @@ -131,7 +131,7 @@ class TalkListService $runIdAll = ServerRunID::getInstance()->getServerRunIdAll(); return array_map(function ($item) use ($user_id, $runIdAll) { - $data = TalkList::item([ + $data = TalkSession::item([ 'id' => $item['id'], 'talk_type' => $item['talk_type'], 'receiver_id' => $item['receiver_id'], @@ -173,7 +173,7 @@ class TalkListService */ public function disturb(int $user_id, int $receiver_id, int $talk_type, int $is_disturb): bool { - $result = TalkList::query()->where([ + $result = TalkSession::query()->where([ ['user_id', '=', $user_id], ['talk_type', '=', $talk_type], ['receiver_id', '=', $receiver_id], @@ -183,7 +183,7 @@ class TalkListService return false; } - return (bool)TalkList::query()->where('id', $result->id)->update([ + return (bool)TalkSession::query()->where('id', $result->id)->update([ 'is_disturb' => $is_disturb, 'updated_at' => date('Y-m-d H:i:s') ]); @@ -199,7 +199,7 @@ class TalkListService */ public function isDisturb(int $user_id, int $receiver_id, int $talk_type): bool { - return (bool)TalkList::query()->where([ + return (bool)TalkSession::query()->where([ ['user_id', '=', $user_id], ['talk_type', '=', $talk_type], ['receiver_id', '=', $receiver_id],