From 8cfcc74b2af5a534dcfb53d840e03e3b05b91a2b Mon Sep 17 00:00:00 2001 From: koogua Date: Mon, 4 Oct 2021 18:56:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=80=80=E5=87=BA=E7=BE=A4?= =?UTF-8?q?=E7=BB=84=E5=92=8C=E8=A7=A3=E9=99=A4=E5=A5=BD=E5=8F=8B=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + .../Api/Controllers/ImFriendController.php | 30 ++++++++++ .../Api/Controllers/ImGroupController.php | 13 ++++ app/Services/Logic/Im/FriendQuit.php | 45 ++++++++++++++ app/Services/Logic/Im/GroupInfo.php | 2 +- app/Services/Logic/Im/GroupQuit.php | 60 +++++++++++++++++++ app/Services/Logic/Im/GroupUserList.php | 2 +- app/Services/Logic/ImGroupTrait.php | 2 +- app/Services/Logic/ImUserTrait.php | 22 +++++++ app/Validators/ImGroupUser.php | 9 +++ config/errors.php | 1 + 11 files changed, 184 insertions(+), 3 deletions(-) create mode 100644 app/Http/Api/Controllers/ImFriendController.php create mode 100644 app/Services/Logic/Im/FriendQuit.php create mode 100644 app/Services/Logic/Im/GroupQuit.php create mode 100644 app/Services/Logic/ImUserTrait.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 0cec2ad5..903b5edc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - 调整订单发货为每一分钟执行一次 - 增强课时安全性,无权限时不返回播放地址或内容 - 抽离出文章关闭,仅我可见操作 +- 增加退出群组和解除好友接口 - 增加删除文章和提问接口 - 增加首页推荐教师接口 - 增加微信公众号支付处理 diff --git a/app/Http/Api/Controllers/ImFriendController.php b/app/Http/Api/Controllers/ImFriendController.php new file mode 100644 index 00000000..958bcf7c --- /dev/null +++ b/app/Http/Api/Controllers/ImFriendController.php @@ -0,0 +1,30 @@ +handle($id); + + return $this->jsonSuccess(); + } + +} diff --git a/app/Http/Api/Controllers/ImGroupController.php b/app/Http/Api/Controllers/ImGroupController.php index 928cb225..e8d53e2f 100644 --- a/app/Http/Api/Controllers/ImGroupController.php +++ b/app/Http/Api/Controllers/ImGroupController.php @@ -9,6 +9,7 @@ namespace App\Http\Api\Controllers; use App\Services\Logic\Im\GroupInfo as GroupInfoService; use App\Services\Logic\Im\GroupList as GroupListService; +use App\Services\Logic\Im\GroupQuit as GroupQuitService; use App\Services\Logic\Im\GroupUserList as GroupUserListService; /** @@ -53,4 +54,16 @@ class ImGroupController extends Controller return $this->jsonPaginate($pager); } + /** + * @Post("/{id:[0-9]+}/quit", name="api.im_group.quit") + */ + public function quitAction($id) + { + $service = new GroupQuitService(); + + $service->handle($id); + + return $this->jsonSuccess(); + } + } diff --git a/app/Services/Logic/Im/FriendQuit.php b/app/Services/Logic/Im/FriendQuit.php new file mode 100644 index 00000000..b022ef85 --- /dev/null +++ b/app/Services/Logic/Im/FriendQuit.php @@ -0,0 +1,45 @@ +getLoginUser(); + + $user = $this->checkImUser($loginUser->id); + + $validator = new ImFriendUserValidator(); + + $friend = $validator->checkFriend($id); + + $friendUser = $validator->checkFriendUser($user->id, $friend->id); + + $friendUser->delete(); + + $this->decrUserFriendCount($user); + } + + protected function decrUserFriendCount(ImUserModel $user) + { + if ($user->friend_count > 0) { + $user->friend_count -= 1; + $user->update(); + } + } + +} diff --git a/app/Services/Logic/Im/GroupInfo.php b/app/Services/Logic/Im/GroupInfo.php index 490bf3a1..4074ec42 100644 --- a/app/Services/Logic/Im/GroupInfo.php +++ b/app/Services/Logic/Im/GroupInfo.php @@ -18,7 +18,7 @@ class GroupInfo extends LogicService public function handle($id) { - $group = $this->checkGroup($id); + $group = $this->checkImGroup($id); $userRepo = new UserRepo(); diff --git a/app/Services/Logic/Im/GroupQuit.php b/app/Services/Logic/Im/GroupQuit.php new file mode 100644 index 00000000..cdf00832 --- /dev/null +++ b/app/Services/Logic/Im/GroupQuit.php @@ -0,0 +1,60 @@ +getLoginUser(); + + $group = $this->checkImGroup($id); + + $user = $this->checkImUser($loginUser->id); + + $validator = new ImGroupUserValidator(); + + $groupUser = $validator->checkGroupUser($group->id, $user->id); + + $validator->checkIfAllowQuit($group->id, $user->id); + + $groupUser->delete(); + + $this->decrGroupUserCount($group); + + $this->decrUserGroupCount($user); + } + + protected function decrUserGroupCount(ImUserModel $user) + { + if ($user->group_count > 0) { + $user->group_count -= 1; + $user->update(); + } + } + + protected function decrGroupUserCount(ImGroupModel $group) + { + if ($group->user_count > 0) { + $group->user_count -= 1; + $group->update(); + } + } + +} diff --git a/app/Services/Logic/Im/GroupUserList.php b/app/Services/Logic/Im/GroupUserList.php index 9a674b8f..b77acb79 100644 --- a/app/Services/Logic/Im/GroupUserList.php +++ b/app/Services/Logic/Im/GroupUserList.php @@ -20,7 +20,7 @@ class GroupUserList extends LogicService public function handle($id) { - $group = $this->checkGroup($id); + $group = $this->checkImGroup($id); $pagerQuery = new PagerQuery(); diff --git a/app/Services/Logic/ImGroupTrait.php b/app/Services/Logic/ImGroupTrait.php index 739b4fb6..b3a11a73 100644 --- a/app/Services/Logic/ImGroupTrait.php +++ b/app/Services/Logic/ImGroupTrait.php @@ -12,7 +12,7 @@ use App\Validators\ImGroup as ImGroupValidator; trait ImGroupTrait { - public function checkGroup($id) + public function checkImGroup($id) { $validator = new ImGroupValidator(); diff --git a/app/Services/Logic/ImUserTrait.php b/app/Services/Logic/ImUserTrait.php new file mode 100644 index 00000000..3cc33145 --- /dev/null +++ b/app/Services/Logic/ImUserTrait.php @@ -0,0 +1,22 @@ +checkUser($id); + } + +} diff --git a/app/Validators/ImGroupUser.php b/app/Validators/ImGroupUser.php index 19e3fd5c..b02892fc 100644 --- a/app/Validators/ImGroupUser.php +++ b/app/Validators/ImGroupUser.php @@ -114,4 +114,13 @@ class ImGroupUser extends Validator } } + public function checkIfAllowQuit($groupId, $userId) + { + $group = $this->checkGroup($groupId); + + if ($group->owner_id == $userId) { + throw new BadRequestException('im_group_user.owner_quit_not_allowed'); + } + } + } diff --git a/config/errors.php b/config/errors.php index 6b283efb..90855cbe 100644 --- a/config/errors.php +++ b/config/errors.php @@ -464,6 +464,7 @@ $error['im_group_user.not_found'] = '群组关系不存在'; $error['im_group_user.remark_too_long'] = '验证信息太长(超过30字符)'; $error['im_group_user.has_joined'] = '已经加入过群组'; $error['im_group_user.join_not_allowed'] = '当前不允许加入群组'; +$error['im_group_user.owner_quit_not_allowed'] = '当前不允许群主退群'; $error['im_group_user.delete_owner_not_allowed'] = '当前不允许删除群主'; $error['im_friend_user.not_found'] = '好友关系不存在';