From 148d2fdc4d8a62eba6c9fd25cc43e65a324521b6 Mon Sep 17 00:00:00 2001 From: xiaochong0302 Date: Mon, 22 Jun 2020 20:02:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A5=BD=E5=8F=8B=E7=94=B3=E8=AF=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Caches/ImHotGroupList.php | 70 ++++++++ app/Caches/ImHotUserList.php | 73 +++++++++ .../Web/Controllers/MessengerController.php | 83 ++++++++-- app/Http/Web/Controllers/PublicController.php | 11 ++ app/Http/Web/Services/Messenger.php | 151 +++++++++++++++++- app/Http/Web/Views/messenger/find.volt | 34 ++++ app/Http/Web/Views/messenger/find_groups.volt | 20 +++ app/Http/Web/Views/messenger/find_users.volt | 20 +++ app/Http/Web/Views/templates/chapter.volt | 82 ---------- app/Http/Web/Views/templates/full.volt | 2 +- app/Http/Web/Views/templates/layer.volt | 2 + app/Http/Web/Views/user/show.volt | 13 +- app/Library/Security.php | 1 - app/Models/ImFriendMessage.php | 28 +++- app/Models/{ImFriend.php => ImFriendUser.php} | 4 +- app/Models/User.php | 9 +- app/Repos/ImChatGroupUser.php | 25 +++ app/Repos/ImFriendGroup.php | 4 +- app/Repos/ImFriendUser.php | 25 +++ app/Repos/User.php | 8 +- app/Traits/Auth.php | 10 +- app/Validators/ImChatGroup.php | 2 +- app/Validators/ImChatGroupUser.php | 33 ++++ app/Validators/ImFriendUser.php | 40 +++++ app/Validators/User.php | 15 +- config/errors.php | 26 +++ public/static/web/css/common.css | 41 +++-- public/static/web/js/common.js | 21 ++- public/static/web/js/im.find.js | 38 +++++ public/static/web/js/im.js | 10 +- public/static/web/js/user.show.js | 18 +++ 31 files changed, 761 insertions(+), 158 deletions(-) create mode 100644 app/Caches/ImHotGroupList.php create mode 100644 app/Caches/ImHotUserList.php create mode 100644 app/Http/Web/Views/messenger/find.volt create mode 100644 app/Http/Web/Views/messenger/find_groups.volt create mode 100644 app/Http/Web/Views/messenger/find_users.volt delete mode 100644 app/Http/Web/Views/templates/chapter.volt rename app/Models/{ImFriend.php => ImFriendUser.php} (92%) create mode 100644 app/Repos/ImChatGroupUser.php create mode 100644 app/Repos/ImFriendUser.php create mode 100644 app/Validators/ImChatGroupUser.php create mode 100644 app/Validators/ImFriendUser.php create mode 100644 public/static/web/js/im.find.js create mode 100644 public/static/web/js/user.show.js diff --git a/app/Caches/ImHotGroupList.php b/app/Caches/ImHotGroupList.php new file mode 100644 index 00000000..b17c2dcc --- /dev/null +++ b/app/Caches/ImHotGroupList.php @@ -0,0 +1,70 @@ +lifetime; + } + + public function getKey($id = null) + { + return 'im_hot_group_list'; + } + + public function getContent($id = null) + { + $limit = 12; + + $groups = $this->findHotGroups($limit); + + if ($groups->count() == 0) { + return []; + } + + return $this->handleContent($groups); + } + + /** + * @param ImChatGroupModel[] $groups + * @return array + */ + protected function handleContent($groups) + { + $result = []; + + foreach ($groups as $group) { + $result[] = [ + 'id' => $group->id, + 'name' => $group->name, + 'avatar' => $group->avatar, + 'about' => $group->about, + ]; + } + + return $result; + } + + /** + * @param int $limit + * @return ResultsetInterface|Resultset|ImChatGroupModel[] + */ + public function findHotGroups($limit = 12) + { + return ImChatGroupModel::query() + ->where('deleted = 0') + ->orderBy('user_count DESC') + ->limit($limit) + ->execute(); + } + +} diff --git a/app/Caches/ImHotUserList.php b/app/Caches/ImHotUserList.php new file mode 100644 index 00000000..8662167c --- /dev/null +++ b/app/Caches/ImHotUserList.php @@ -0,0 +1,73 @@ +lifetime; + } + + public function getKey($id = null) + { + return 'im_hot_user_list'; + } + + public function getContent($id = null) + { + $limit = 12; + + $users = $this->findHotUsers($limit); + + if ($users->count() == 0) { + return []; + } + + return $this->handleContent($users); + } + + /** + * @param UserModel[] $users + * @return array + */ + protected function handleContent($users) + { + $result = []; + + foreach ($users as $user) { + $result[] = [ + 'id' => $user->id, + 'name' => $user->name, + 'avatar' => $user->avatar, + 'about' => $user->about, + 'sign' => $user->sign, + 'location' => $user->location, + 'vip' => $user->vip, + ]; + } + + return $result; + } + + /** + * @param int $limit + * @return ResultsetInterface|Resultset|UserModel[] + */ + public function findHotUsers($limit = 12) + { + return UserModel::query() + ->where('deleted = 0') + ->orderBy('follower_count DESC') + ->limit($limit) + ->execute(); + } + +} diff --git a/app/Http/Web/Controllers/MessengerController.php b/app/Http/Web/Controllers/MessengerController.php index f7a69217..0e0cc804 100644 --- a/app/Http/Web/Controllers/MessengerController.php +++ b/app/Http/Web/Controllers/MessengerController.php @@ -15,7 +15,7 @@ class MessengerController extends LayerController use ResponseTrait; /** - * @Get("/init", name="im.init") + * @Get("/init", name="web.im.init") */ public function initAction() { @@ -27,7 +27,7 @@ class MessengerController extends LayerController } /** - * @Get("/group/members", name="im.group_members") + * @Get("/group/members", name="web.im.group_members") */ public function groupMembersAction() { @@ -39,7 +39,7 @@ class MessengerController extends LayerController } /** - * @Get("/msg/box", name="im.msg_box") + * @Get("/msg/box", name="web.im.msg_box") */ public function messageBoxAction() { @@ -47,7 +47,7 @@ class MessengerController extends LayerController } /** - * @Get("/chat/log", name="im.chat_log") + * @Get("/chat/log", name="web.im.chat_log") */ public function chatLogAction() { @@ -61,7 +61,7 @@ class MessengerController extends LayerController } /** - * @Get("/chat/history", name="im.chat_history") + * @Get("/chat/history", name="web.im.chat_history") */ public function chatHistoryAction() { @@ -73,15 +73,47 @@ class MessengerController extends LayerController } /** - * @Get("/find", name="im.find") + * @Get("/find", name="web.im.find") */ public function findAction() { + $service = new MessengerService(); + $usersPager = $service->getHotUsers(); + $groupsPager = $service->getHotGroups(); + + $usersPager->items = kg_array_object($usersPager->items); + $groupsPager->items = kg_array_object($groupsPager->items); + + $this->view->setVar('users_pager', $usersPager); + $this->view->setVar('groups_pager', $groupsPager); } /** - * @Post("/user/bind", name="im.bind_user") + * @Get("/search", name="web.im.search") + */ + public function searchAction() + { + $query = $this->request->getQuery('query', ['trim', 'string']); + $type = $this->request->getQuery('type', ['trim', 'string']); + + $service = new MessengerService(); + + if ($type == 'user') { + $pager = $service->searchUsers($query); + $this->view->pick('messenger/find_users'); + } else { + $pager = $service->searchGroups($query); + $this->view->pick('messenger/find_groups'); + } + + $pager->items = kg_array_object($pager->items); + + $this->view->setVar('pager', $pager); + } + + /** + * @Post("/user/bind", name="web.im.bind_user") */ public function bindUserAction() { @@ -93,7 +125,7 @@ class MessengerController extends LayerController } /** - * @Post("/msg/send", name="im.send_msg") + * @Post("/msg/send", name="web.im.send_msg") */ public function sendMessageAction() { @@ -105,14 +137,14 @@ class MessengerController extends LayerController } /** - * @Post("/img/upload", name="im.upload_img") + * @Post("/img/upload", name="web.im.upload_img") */ public function uploadImageAction() { } /** - * @Post("/file/upload", name="im.upload_file") + * @Post("/file/upload", name="web.im.upload_file") */ public function uploadFileAction() { @@ -120,7 +152,7 @@ class MessengerController extends LayerController } /** - * @Post("/stats/update", name="im.update_stats") + * @Post("/stats/update", name="web.im.update_stats") */ public function updateStatsAction() { @@ -128,30 +160,47 @@ class MessengerController extends LayerController } /** - * @Post("/sign/update", name="im.update_sign") + * @Post("/sign/update", name="web.web.im.update_sign") */ - public function updateSignAction() + public function updateSignatureAction() { + $service = new MessengerService(); + $service->updateSignature(); + + return $this->jsonSuccess(); } /** - * @Post("/friend/apply", name="im.apply_friend") + * @Post("/friend/apply", name="web.im.apply_friend") */ public function applyFriendAction() { + $service = new MessengerService(); + + $service->applyFriend(); + + $content = ['msg' => '发送申请成功,请等待对方通过']; + + return $this->jsonSuccess($content); } /** - * @Post("/group/apply", name="im.apply_group") + * @Post("/group/apply", name="web.im.apply_group") */ public function applyGroupAction() { + $service = new MessengerService(); + $service->applyGroup(); + + $content = ['msg' => '发送申请成功,请等待管理员通过']; + + return $this->jsonSuccess($content); } /** - * @Post("/friend/approve", name="im.approve_friend") + * @Post("/friend/approve", name="web.im.approve_friend") */ public function approveFriendAction() { @@ -159,7 +208,7 @@ class MessengerController extends LayerController } /** - * @Post("/group/approve", name="im.approve_group") + * @Post("/group/approve", name="web.web.im.approve_group") */ public function approveGroupAction() { diff --git a/app/Http/Web/Controllers/PublicController.php b/app/Http/Web/Controllers/PublicController.php index 435d120d..799f074d 100644 --- a/app/Http/Web/Controllers/PublicController.php +++ b/app/Http/Web/Controllers/PublicController.php @@ -2,6 +2,7 @@ namespace App\Http\Web\Controllers; +use App\Library\Security; use App\Models\ContentImage as ContentImageModel; use App\Services\Frontend\Chapter\Learning as LearningService; use App\Services\Storage as StorageService; @@ -52,6 +53,16 @@ class PublicController extends \Phalcon\Mvc\Controller exit; } + /** + * @Post("/token/refresh", name="web.refresh_token") + */ + public function refreshTokenAction() + { + $security = new Security(); + + return $this->jsonSuccess(); + } + /** * @Post("/{id:[0-9]+}/learning", name="web.learning") */ diff --git a/app/Http/Web/Services/Messenger.php b/app/Http/Web/Services/Messenger.php index 47f9c281..db74863e 100644 --- a/app/Http/Web/Services/Messenger.php +++ b/app/Http/Web/Services/Messenger.php @@ -3,14 +3,19 @@ namespace App\Http\Web\Services; use App\Builders\ImMessageList as ImMessageListBuilder; +use App\Caches\ImHotGroupList as ImHotGroupListCache; +use App\Caches\ImHotUserList as ImHotUserListCache; use App\Library\Paginator\Query as PagerQuery; use App\Models\ImFriendMessage as ImFriendMessageModel; +use App\Models\ImFriendUser as ImFriendUserModel; use App\Repos\ImChatGroup as ImChatGroupRepo; use App\Repos\ImFriendMessage as ImFriendMessageRepo; +use App\Repos\ImFriendUser as ImFriendUserRepo; use App\Repos\ImGroupMessage as ImGroupMessageRepo; use App\Repos\User as UserRepo; use App\Validators\ImChatGroup as ImChatGroupValidator; use App\Validators\ImMessage as ImMessageValidator; +use App\Validators\User as UserValidator; use GatewayClient\Gateway; class Messenger extends Service @@ -39,6 +44,46 @@ class Messenger extends Service ]; } + public function searchUsers($query) + { + + } + + public function searchGroups($query) + { + + } + + public function getHotUsers() + { + $cache = new ImHotUserListCache(); + + $items = $cache->get(); + + $pager = new \stdClass(); + + $pager->total_items = count($items); + $pager->total_pages = 1; + $pager->items = $items; + + return $pager; + } + + public function getHotGroups() + { + $cache = new ImHotGroupListCache(); + + $items = $cache->get(); + + $pager = new \stdClass(); + + $pager->total_items = count($items); + $pager->total_pages = 1; + $pager->items = $items; + + return $pager; + } + public function getGroupUsers() { $id = $this->request->getQuery('id'); @@ -194,12 +239,106 @@ class Messenger extends Service } } + public function updateSignature() + { + $sign = $this->request->getPost('sign'); + + $user = $this->getLoginUser(); + + $validator = new UserValidator(); + + $validator->checkSign($sign); + + $user->update(['sign' => $sign]); + + return $user; + } + + public function applyFriend() + { + $friendId = $this->request->getPost('friend_id'); + + $user = $this->getLoginUser(); + + $userValidator = new UserValidator(); + + $friend = $userValidator->checkUser($friendId); + + $friendUserRepo = new ImFriendUserRepo(); + + $friendUser = $friendUserRepo->findFriendUser($user->id, $friend->id); + + if (!$friendUser) { + $model = new ImFriendUserModel(); + $model->user_id = $user->id; + $model->friend_id = $friend->id; + $model->create(); + } + + /** + * @todo 向对方发好友申请的系统消息 + */ + } + + public function approveFriend() + { + $friendId = $this->request->getPost('friend_id'); + + $user = $this->getLoginUser(); + + $userValidator = new UserValidator(); + + $friend = $userValidator->checkUser($friendId); + + $friendUserRepo = new ImFriendUserRepo(); + + $friendUser = $friendUserRepo->findFriendUser($user->id, $friend->id); + + if (!$friendUser) { + $model = new ImFriendUserModel(); + $model->user_id = $user->id; + $model->friend_id = $friend->id; + $model->create(); + } + + /** + * @todo 向对方发通过好友申请的系统消息 + */ + } + + public function refuseFriend() + { + $friendId = $this->request->getPost('friend_id'); + + $user = $this->getLoginUser(); + + $userValidator = new UserValidator(); + + $friend = $userValidator->checkUser($friendId); + + /** + * @todo 向对方发拒绝添加好友的系统消息 + */ + } + + public function applyGroup() + { + } + + public function approveGroup() + { + } + + public function refuseGroup() + { + } + protected function handleFriendList($userId) { $userRepo = new UserRepo(); $friendGroups = $userRepo->findImFriendGroups($userId); - $friends = $userRepo->findImFriends($userId); + $friendUsers = $userRepo->findImFriendUsers($userId); $items = []; @@ -211,11 +350,11 @@ class Messenger extends Service } } - if ($friends->count() == 0) { + if ($friendUsers->count() == 0) { return $items; } - $userIds = kg_array_column($friends->toArray(), 'friend_id'); + $userIds = kg_array_column($friendUsers->toArray(), 'friend_id'); $users = $userRepo->findByIds($userIds); @@ -232,9 +371,9 @@ class Messenger extends Service } foreach ($items as $key => $item) { - foreach ($friends as $friend) { - $userId = $friend->friend_id; - if ($item['id'] == $friend->group_id) { + foreach ($friendUsers as $friendUser) { + $userId = $friendUser->friend_id; + if ($item['id'] == $friendUser->group_id) { $items[$key]['list'][] = $userMappings[$userId]; } else { $items[0]['list'][] = $userMappings[$userId]; diff --git a/app/Http/Web/Views/messenger/find.volt b/app/Http/Web/Views/messenger/find.volt new file mode 100644 index 00000000..91c3adee --- /dev/null +++ b/app/Http/Web/Views/messenger/find.volt @@ -0,0 +1,34 @@ +{% extends 'templates/layer.volt' %} + +{% block content %} + + + +
+
+
    +
  • 成员
  • +
  • 群组
  • +
+
+
+ {{ partial('messenger/find_users',{'pager':users_pager}) }} +
+
+ {{ partial('messenger/find_groups',{'pager':groups_pager}) }} +
+
+
+
+ +{% endblock %} + +{% block include_js %} + + {{ js_include('web/js/im.find.js') }} + +{% endblock %} \ No newline at end of file diff --git a/app/Http/Web/Views/messenger/find_groups.volt b/app/Http/Web/Views/messenger/find_groups.volt new file mode 100644 index 00000000..135d89bc --- /dev/null +++ b/app/Http/Web/Views/messenger/find_groups.volt @@ -0,0 +1,20 @@ +{% if pager.total_pages > 0 %} +
+
+ {% for item in pager.items %} +
+
+
+ {{ item.name }} +
+
{{ item.name }}
+ +
+
+ {% endfor %} +
+
+ {{ partial('partials/pager_ajax') }} +{% endif %} \ No newline at end of file diff --git a/app/Http/Web/Views/messenger/find_users.volt b/app/Http/Web/Views/messenger/find_users.volt new file mode 100644 index 00000000..e08d748f --- /dev/null +++ b/app/Http/Web/Views/messenger/find_users.volt @@ -0,0 +1,20 @@ +{% if pager.total_pages > 0 %} +
+
+ {% for item in pager.items %} +
+
+
+ {{ item.name }} +
+
{{ item.name }}
+ +
+
+ {% endfor %} +
+
+ {{ partial('partials/pager_ajax') }} +{% endif %} \ No newline at end of file diff --git a/app/Http/Web/Views/templates/chapter.volt b/app/Http/Web/Views/templates/chapter.volt deleted file mode 100644 index 7d1954a3..00000000 --- a/app/Http/Web/Views/templates/chapter.volt +++ /dev/null @@ -1,82 +0,0 @@ - - - - - - - - - {{ site_seo.getTitle() }} - {{ icon_link('favicon.ico') }} - {{ css_link('lib/layui/css/layui.css') }} - {{ css_link('web/css/common.css') }} - {% block link_css %}{% endblock %} - {% block inline_css %}{% endblock %} - - -{% set course_url = url({'for':'web.course.show','id':chapter.course.id}) %} - - -
-
- -
- 203 - 300 - 50 -
-
- 203 - 300 - 50 -
-
-
- {% block content %}{% endblock %} -
-
- -
-
-
-
-
    -
  • 目录
  • -
  • 讨论
  • -
  • 资料
  • -
-
-
-
-
-
-
-
-{{ js_include('lib/layui/layui.all.js') }} -{{ js_include('web/js/common.js') }} - - -{% block include_js %}{% endblock %} -{% block inline_js %}{% endblock %} - - \ No newline at end of file diff --git a/app/Http/Web/Views/templates/full.volt b/app/Http/Web/Views/templates/full.volt index 4d87664e..32eb3fcf 100644 --- a/app/Http/Web/Views/templates/full.volt +++ b/app/Http/Web/Views/templates/full.volt @@ -13,7 +13,7 @@ {% block link_css %}{% endblock %} {% block inline_css %}{% endblock %} - +