From fddaa3573e7f888408b789b46e28f5a57966ef6f Mon Sep 17 00:00:00 2001 From: xiaochong0302 Date: Sat, 15 Aug 2020 20:41:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E7=BE=A4=E7=BB=84?= =?UTF-8?q?=E5=92=8C=E7=94=A8=E6=88=B7=E7=9A=84=E5=85=A8=E6=96=87=E6=90=9C?= =?UTF-8?q?=E7=B4=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Caches/HelpList.php | 4 +- app/Caches/IndexFreeCourseList.php | 14 +- app/Caches/IndexNewCourseList.php | 14 +- app/Caches/IndexVipCourseList.php | 14 +- app/Console/Tasks/GroupIndexTask.php | 128 ++++++++++++++++++ app/Console/Tasks/UserIndexTask.php | 128 ++++++++++++++++++ app/Http/Web/Controllers/HelpController.php | 4 +- app/Http/Web/Controllers/SearchController.php | 42 ++++-- app/Http/Web/Views/help/index.volt | 4 +- app/Http/Web/Views/index/index.volt | 10 +- app/Http/Web/Views/partials/header.volt | 32 ++--- app/Http/Web/Views/search/group.volt | 23 ++++ app/Http/Web/Views/search/index.volt | 18 ++- app/Http/Web/Views/search/user.volt | 33 +++++ app/Services/Frontend/Search/Course.php | 82 +++++++++++ app/Services/Frontend/Search/Group.php | 75 ++++++++++ app/Services/Frontend/Search/User.php | 76 +++++++++++ app/Services/Search/CourseSearcher.php | 98 +------------- app/Services/Search/GroupDocument.php | 60 ++++++++ app/Services/Search/GroupSearcher.php | 25 ++++ app/Services/Search/Searcher.php | 100 ++++++++++++++ app/Services/Search/UserDocument.php | 48 +++++++ app/Services/Search/UserSearcher.php | 25 ++++ app/Services/Syncer/GroupIndex.php | 47 +++++++ app/Services/Syncer/UserIndex.php | 47 +++++++ config/xs.group.default.ini | 28 ++++ config/xs.group.ini | 28 ++++ config/xs.user.default.ini | 33 +++++ config/xs.user.ini | 33 +++++ public/static/web/css/common.css | 43 ++++-- 30 files changed, 1154 insertions(+), 162 deletions(-) create mode 100644 app/Console/Tasks/GroupIndexTask.php create mode 100644 app/Console/Tasks/UserIndexTask.php create mode 100644 app/Http/Web/Views/search/group.volt create mode 100644 app/Http/Web/Views/search/user.volt create mode 100644 app/Services/Frontend/Search/Course.php create mode 100644 app/Services/Frontend/Search/Group.php create mode 100644 app/Services/Frontend/Search/User.php create mode 100644 app/Services/Search/GroupDocument.php create mode 100644 app/Services/Search/GroupSearcher.php create mode 100644 app/Services/Search/Searcher.php create mode 100644 app/Services/Search/UserDocument.php create mode 100644 app/Services/Search/UserSearcher.php create mode 100644 app/Services/Syncer/GroupIndex.php create mode 100644 app/Services/Syncer/UserIndex.php create mode 100644 config/xs.group.default.ini create mode 100644 config/xs.group.ini create mode 100644 config/xs.user.default.ini create mode 100644 config/xs.user.ini diff --git a/app/Caches/HelpList.php b/app/Caches/HelpList.php index 2b4c8ce2..0d0b31e6 100644 --- a/app/Caches/HelpList.php +++ b/app/Caches/HelpList.php @@ -42,13 +42,13 @@ class HelpList extends Cache 'name' => $category->name, ]; - $item['list'] = []; + $item['helps'] = []; $helps = $this->findHelps($category->id); if ($helps->count() > 0) { foreach ($helps as $help) { - $item['list'][] = [ + $item['helps'][] = [ 'id' => $help->id, 'title' => $help->title, ]; diff --git a/app/Caches/IndexFreeCourseList.php b/app/Caches/IndexFreeCourseList.php index 0f4dcc47..b6c69d67 100644 --- a/app/Caches/IndexFreeCourseList.php +++ b/app/Caches/IndexFreeCourseList.php @@ -28,8 +28,6 @@ class IndexFreeCourseList extends Cache public function getContent($id = null) { - $result = []; - $categoryLimit = 5; $courseLimit = 10; @@ -40,13 +38,19 @@ class IndexFreeCourseList extends Cache return []; } + $result = []; + foreach ($categories as $category) { - $categoryItem = [ + $item = []; + + $item['category'] = [ 'id' => $category->id, 'name' => $category->name, ]; + $item['courses'] = []; + $courses = $this->findCategoryCourses($category->id, $courseLimit); if ($courses->count() == 0) { @@ -69,9 +73,9 @@ class IndexFreeCourseList extends Cache ]; } - $categoryItem['courses'] = $categoryCourses; + $item['courses'] = $categoryCourses; - $result[] = $categoryItem; + $result[] = $item; } return $result; diff --git a/app/Caches/IndexNewCourseList.php b/app/Caches/IndexNewCourseList.php index 76582509..7cb627cf 100644 --- a/app/Caches/IndexNewCourseList.php +++ b/app/Caches/IndexNewCourseList.php @@ -28,8 +28,6 @@ class IndexNewCourseList extends Cache public function getContent($id = null) { - $result = []; - $categoryLimit = 5; $courseLimit = 10; @@ -40,13 +38,19 @@ class IndexNewCourseList extends Cache return []; } + $result = []; + foreach ($categories as $category) { - $categoryItem = [ + $item = []; + + $item['category'] = [ 'id' => $category->id, 'name' => $category->name, ]; + $item['courses'] = []; + $courses = $this->findCategoryCourses($category->id, $courseLimit); if ($courses->count() == 0) { @@ -69,9 +73,9 @@ class IndexNewCourseList extends Cache ]; } - $categoryItem['courses'] = $categoryCourses; + $item['courses'] = $categoryCourses; - $result[] = $categoryItem; + $result[] = $item; } return $result; diff --git a/app/Caches/IndexVipCourseList.php b/app/Caches/IndexVipCourseList.php index e97d1879..ab869db0 100644 --- a/app/Caches/IndexVipCourseList.php +++ b/app/Caches/IndexVipCourseList.php @@ -28,8 +28,6 @@ class IndexVipCourseList extends Cache public function getContent($id = null) { - $result = []; - $categoryLimit = 5; $courseLimit = 10; @@ -40,13 +38,19 @@ class IndexVipCourseList extends Cache return []; } + $result = []; + foreach ($categories as $category) { - $categoryItem = [ + $item = []; + + $item['category'] = [ 'id' => $category->id, 'name' => $category->name, ]; + $item['courses'] = []; + $courses = $this->findCategoryCourses($category->id, $courseLimit); if ($courses->count() == 0) { @@ -69,9 +73,9 @@ class IndexVipCourseList extends Cache ]; } - $categoryItem['courses'] = $categoryCourses; + $item['courses'] = $categoryCourses; - $result[] = $categoryItem; + $result[] = $item; } return $result; diff --git a/app/Console/Tasks/GroupIndexTask.php b/app/Console/Tasks/GroupIndexTask.php new file mode 100644 index 00000000..2cd79419 --- /dev/null +++ b/app/Console/Tasks/GroupIndexTask.php @@ -0,0 +1,128 @@ +searchGroups($query); + + var_export($result); + } + + /** + * 清空索引 + * + * @command: php console.php group_index clean + */ + public function cleanAction() + { + $this->cleanGroupIndex(); + } + + /** + * 重建索引 + * + * @command: php console.php group_index rebuild + */ + public function rebuildAction() + { + $this->rebuildGroupIndex(); + } + + /** + * 清空索引 + */ + protected function cleanGroupIndex() + { + $handler = new GroupSearcher(); + + $index = $handler->getXS()->getIndex(); + + echo "start clean index" . PHP_EOL; + + $index->clean(); + + echo "end clean index" . PHP_EOL; + } + + /** + * 重建索引 + */ + protected function rebuildGroupIndex() + { + $groups = $this->findGroups(); + + if ($groups->count() == 0) { + return; + } + + $handler = new GroupSearcher(); + + $documenter = new GroupDocument(); + + $index = $handler->getXS()->getIndex(); + + echo "start rebuild index" . PHP_EOL; + + $index->beginRebuild(); + + foreach ($groups as $group) { + $document = $documenter->setDocument($group); + $index->add($document); + } + + $index->endRebuild(); + + echo "end rebuild index" . PHP_EOL; + } + + /** + * 搜索课程 + * + * @param string $query + * @return array + * @throws \XSException + */ + protected function searchGroups($query) + { + $handler = new GroupSearcher(); + + return $handler->search($query); + } + + /** + * 查找课程 + * + * @return ResultsetInterface|Resultset|GroupModel[] + */ + protected function findGroups() + { + return GroupModel::query() + ->where('published = 1') + ->execute(); + } + +} diff --git a/app/Console/Tasks/UserIndexTask.php b/app/Console/Tasks/UserIndexTask.php new file mode 100644 index 00000000..3ec4ce51 --- /dev/null +++ b/app/Console/Tasks/UserIndexTask.php @@ -0,0 +1,128 @@ +searchUsers($query); + + var_export($result); + } + + /** + * 清空索引 + * + * @command: php console.php user_index clean + */ + public function cleanAction() + { + $this->cleanUserIndex(); + } + + /** + * 重建索引 + * + * @command: php console.php user_index rebuild + */ + public function rebuildAction() + { + $this->rebuildUserIndex(); + } + + /** + * 清空索引 + */ + protected function cleanUserIndex() + { + $handler = new UserSearcher(); + + $index = $handler->getXS()->getIndex(); + + echo "start clean index" . PHP_EOL; + + $index->clean(); + + echo "end clean index" . PHP_EOL; + } + + /** + * 重建索引 + */ + protected function rebuildUserIndex() + { + $users = $this->findUsers(); + + if ($users->count() == 0) { + return; + } + + $handler = new UserSearcher(); + + $documenter = new UserDocument(); + + $index = $handler->getXS()->getIndex(); + + echo "start rebuild index" . PHP_EOL; + + $index->beginRebuild(); + + foreach ($users as $user) { + $document = $documenter->setDocument($user); + $index->add($document); + } + + $index->endRebuild(); + + echo "end rebuild index" . PHP_EOL; + } + + /** + * 搜索课程 + * + * @param string $query + * @return array + * @throws \XSException + */ + protected function searchUsers($query) + { + $handler = new UserSearcher(); + + return $handler->search($query); + } + + /** + * 查找课程 + * + * @return ResultsetInterface|Resultset|UserModel[] + */ + protected function findUsers() + { + return UserModel::query() + ->where('deleted = 0') + ->execute(); + } + +} diff --git a/app/Http/Web/Controllers/HelpController.php b/app/Http/Web/Controllers/HelpController.php index 8f833609..cc3e300f 100644 --- a/app/Http/Web/Controllers/HelpController.php +++ b/app/Http/Web/Controllers/HelpController.php @@ -18,11 +18,11 @@ class HelpController extends Controller { $service = new HelpListService(); - $helps = $service->handle(); + $items = $service->handle(); $this->seo->prependTitle('帮助'); - $this->view->setVar('helps', $helps); + $this->view->setVar('items', $items); } /** diff --git a/app/Http/Web/Controllers/SearchController.php b/app/Http/Web/Controllers/SearchController.php index 5900ae20..6891a2b4 100644 --- a/app/Http/Web/Controllers/SearchController.php +++ b/app/Http/Web/Controllers/SearchController.php @@ -2,9 +2,9 @@ namespace App\Http\Web\Controllers; -use App\Services\Frontend\Search\CourseHotQuery as CourseHotQueryService; -use App\Services\Frontend\Search\CourseList as CourseListService; -use App\Services\Frontend\Search\CourseRelatedQuery as CourseRelatedQueryService; +use App\Services\Frontend\Search\Course as CourseSearchService; +use App\Services\Frontend\Search\Group as GroupSearchService; +use App\Services\Frontend\Search\User as UserSearchService; use App\Traits\Response as ResponseTrait; /** @@ -20,7 +20,8 @@ class SearchController extends Controller */ public function indexAction() { - $query = $this->request->get('query', ['trim']); + $query = $this->request->get('query', ['trim', 'string']); + $type = $this->request->get('type', ['trim'], 'course'); if (empty($query)) { return $this->response->redirect(['for' => 'web.course.list']); @@ -28,21 +29,38 @@ class SearchController extends Controller $this->seo->prependTitle(['搜索', $query]); - $service = new CourseHotQueryService(); + $service = $this->getSearchService($type); - $hotQueries = $service->handle(); + $hotQueries = $service->hotQuery(); - $service = new CourseRelatedQueryService(); + $relatedQueries = $service->relatedQuery($query); - $relatedQueries = $service->handle($query); - - $service = new CourseListService(); - - $pager = $service->handle(); + $pager = $service->search(); $this->view->setVar('hot_queries', $hotQueries); $this->view->setVar('related_queries', $relatedQueries); $this->view->setVar('pager', $pager); } + /** + * @param string $type + * @return CourseSearchService|GroupSearchService|UserSearchService + */ + protected function getSearchService($type) + { + switch ($type) { + case 'group': + $service = new GroupSearchService; + break; + case 'user': + $service = new UserSearchService(); + break; + default: + $service = new CourseSearchService(); + break; + } + + return $service; + } + } diff --git a/app/Http/Web/Views/help/index.volt b/app/Http/Web/Views/help/index.volt index 97cae244..32bb00a8 100644 --- a/app/Http/Web/Views/help/index.volt +++ b/app/Http/Web/Views/help/index.volt @@ -13,12 +13,12 @@
- {% for item in helps %} + {% for item in items %}

{{ item.category.name }}

    - {% for help in item.list %} + {% for help in item.helps %}
  • {{ help.title }}
  • {% endfor %}
diff --git a/app/Http/Web/Views/index/index.volt b/app/Http/Web/Views/index/index.volt index d9eeb68c..294af308 100644 --- a/app/Http/Web/Views/index/index.volt +++ b/app/Http/Web/Views/index/index.volt @@ -4,21 +4,21 @@ {{ partial('macros/course') }} - {%- macro category_courses(courses) %} + {%- macro category_courses(items) %}
    - {% for category in courses %} + {% for item in items %} {% set class = loop.first ? 'layui-this' : 'none' %} -
  • {{ category.name }}
  • +
  • {{ item.category.name }}
  • {% endfor %}
- {% for category in courses %} + {% for item in items %} {% set class = loop.first ? 'layui-tab-item layui-show' : 'layui-tab-item' %}
- {% for course in category.courses %} + {% for course in item.courses %}
{{ course_card(course) }}
diff --git a/app/Http/Web/Views/partials/header.volt b/app/Http/Web/Views/partials/header.volt index 990cf5cf..47a0ef05 100644 --- a/app/Http/Web/Views/partials/header.volt +++ b/app/Http/Web/Views/partials/header.volt @@ -17,20 +17,18 @@
-{% set query = request.get('query','striptags','') %} - - +{% set query = request.get('query',['trim','striptags'],'') %} +{% set type = request.get('type',['trim','string'],'course') %}
- {% if auth_user.id > 0 %} -
+ {% endif %} + +
\ No newline at end of file diff --git a/app/Http/Web/Views/search/group.volt b/app/Http/Web/Views/search/group.volt new file mode 100644 index 00000000..ceb4706a --- /dev/null +++ b/app/Http/Web/Views/search/group.volt @@ -0,0 +1,23 @@ +
+ {% for item in pager.items %} + {% set group_url = url({'for':'web.im_group.show','id':item.id}) %} + {% set item.about = item.about ? item.about : '这个家伙真懒,什么也没有留下!' %} +
+
+ + {{ item.name }} + +
+
+ +
{{ item.about }}
+
+ 组长:{{ item.owner.name }} + 组员:{{ item.user_count }} +
+
+
+ {% endfor %} +
diff --git a/app/Http/Web/Views/search/index.volt b/app/Http/Web/Views/search/index.volt index a0a21d09..d7821e65 100644 --- a/app/Http/Web/Views/search/index.volt +++ b/app/Http/Web/Views/search/index.volt @@ -2,11 +2,12 @@ {% block content %} + {{ partial('macros/course') }} + + {% set types = {'course':'课程','group':'群组','user':'用户'} %} {% set type = request.get('type','trim','course') %} {% set query = request.get('query','striptags','') %} - {{ partial('macros/course') }} -