From 12ceda62df9a0cec2794e55e1883a488c3a6d22f Mon Sep 17 00:00:00 2001 From: xiaochong0302 Date: Mon, 11 Dec 2023 12:23:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=97=AE=E9=A2=98=E6=8E=A8?= =?UTF-8?q?=E8=8D=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Caches/FeaturedArticleList.php | 2 +- app/Caches/FeaturedCourseList.php | 2 +- app/Caches/FeaturedQuestionList.php | 74 +++++++++++++++++++ app/Http/Admin/Services/Question.php | 4 + app/Http/Admin/Views/question/edit_basic.volt | 6 +- app/Http/Admin/Views/question/list.volt | 4 + app/Http/Admin/Views/question/search.volt | 6 +- app/Http/Home/Views/search/index.volt | 4 +- .../Home/Views/widget/featured_articles.volt | 2 +- .../Home/Views/widget/featured_questions.volt | 20 +++++ app/Models/Question.php | 10 ++- app/Repos/Question.php | 40 +++++----- app/Services/Logic/Article/ArticleList.php | 4 +- app/Validators/Question.php | 10 ++- db/migrations/20231108085025.php | 19 +++++ public/static/home/css/common.css | 8 +- 16 files changed, 180 insertions(+), 35 deletions(-) create mode 100644 app/Caches/FeaturedQuestionList.php create mode 100644 app/Http/Home/Views/widget/featured_questions.volt diff --git a/app/Caches/FeaturedArticleList.php b/app/Caches/FeaturedArticleList.php index bd5fc0f2..61d718e2 100644 --- a/app/Caches/FeaturedArticleList.php +++ b/app/Caches/FeaturedArticleList.php @@ -75,7 +75,7 @@ class FeaturedArticleList extends Cache ->where('featured = 1') ->andWhere('published = 1') ->andWhere('deleted = 0') - ->orderBy('id DESC') + ->orderBy('RAND()') ->limit($limit) ->execute(); } diff --git a/app/Caches/FeaturedCourseList.php b/app/Caches/FeaturedCourseList.php index cb2817a8..8243ce1d 100644 --- a/app/Caches/FeaturedCourseList.php +++ b/app/Caches/FeaturedCourseList.php @@ -77,7 +77,7 @@ class FeaturedCourseList extends Cache ->where('featured = 1') ->andWhere('published = 1') ->andWhere('deleted = 0') - ->orderBy('id DESC') + ->orderBy('RAND()') ->limit($limit) ->execute(); } diff --git a/app/Caches/FeaturedQuestionList.php b/app/Caches/FeaturedQuestionList.php new file mode 100644 index 00000000..bbb599fb --- /dev/null +++ b/app/Caches/FeaturedQuestionList.php @@ -0,0 +1,74 @@ +findQuestions($limit); + + if ($questions->count() == 0) { + return []; + } + + $result = []; + + foreach ($questions as $question) { + + $result[] = [ + 'id' => $question->id, + 'title' => $question->title, + 'cover' => $question->cover, + 'favorite_count' => $question->favorite_count, + 'answer_count' => $question->answer_count, + 'view_count' => $question->view_count, + 'like_count' => $question->like_count, + ]; + } + + return $result; + } + + /** + * @param int $limit + * @return ResultsetInterface|Resultset|QuestionModel[] + */ + protected function findQuestions($limit = 5) + { + return QuestionModel::query() + ->where('featured = 1') + ->andWhere('published = 1') + ->andWhere('deleted = 0') + ->orderBy('RAND()') + ->limit($limit) + ->execute(); + } + +} diff --git a/app/Http/Admin/Services/Question.php b/app/Http/Admin/Services/Question.php index 9e69c819..e17a687d 100644 --- a/app/Http/Admin/Services/Question.php +++ b/app/Http/Admin/Services/Question.php @@ -170,6 +170,10 @@ class Question extends Service $data['anonymous'] = $validator->checkAnonymousStatus($post['anonymous']); } + if (isset($post['featured'])) { + $data['featured'] = $validator->checkFeatureStatus($post['featured']); + } + if (isset($post['closed'])) { $data['closed'] = $validator->checkCloseStatus($post['closed']); } diff --git a/app/Http/Admin/Views/question/edit_basic.volt b/app/Http/Admin/Views/question/edit_basic.volt index 9df6661f..af731d39 100644 --- a/app/Http/Admin/Views/question/edit_basic.volt +++ b/app/Http/Admin/Views/question/edit_basic.volt @@ -33,10 +33,10 @@
- +
- - + +
diff --git a/app/Http/Admin/Views/question/list.volt b/app/Http/Admin/Views/question/list.volt index 25190239..f5efc030 100644 --- a/app/Http/Admin/Views/question/list.volt +++ b/app/Http/Admin/Views/question/list.volt @@ -37,6 +37,7 @@ + @@ -46,6 +47,7 @@ 问题信息 统计信息 状态 + 推荐 关闭 操作 @@ -87,6 +89,8 @@

{{ publish_status(item.published) }} + diff --git a/app/Http/Admin/Views/question/search.volt b/app/Http/Admin/Views/question/search.volt index b03e952c..cf5a92fc 100644 --- a/app/Http/Admin/Views/question/search.volt +++ b/app/Http/Admin/Views/question/search.volt @@ -54,10 +54,10 @@
- +
- - + +
diff --git a/app/Http/Home/Views/search/index.volt b/app/Http/Home/Views/search/index.volt index 59c2a956..0533bb49 100644 --- a/app/Http/Home/Views/search/index.volt +++ b/app/Http/Home/Views/search/index.volt @@ -8,13 +8,13 @@ {% set type = request.get('type','trim','course') %} {% set query = request.get('query','striptags','') %} -
+
- +
diff --git a/app/Http/Home/Views/widget/featured_articles.volt b/app/Http/Home/Views/widget/featured_articles.volt index 18a2e213..a3486b21 100644 --- a/app/Http/Home/Views/widget/featured_articles.volt +++ b/app/Http/Home/Views/widget/featured_articles.volt @@ -5,7 +5,7 @@
{% for article in articles %} {% set article_url = url({'for':'home.article.show','id':article.id}) %} -
+
diff --git a/app/Http/Home/Views/widget/featured_questions.volt b/app/Http/Home/Views/widget/featured_questions.volt new file mode 100644 index 00000000..4aad80ce --- /dev/null +++ b/app/Http/Home/Views/widget/featured_questions.volt @@ -0,0 +1,20 @@ +{% if questions|length > 0 %} +
+
推荐问题
+
+ +
+
+{% endif %} \ No newline at end of file diff --git a/app/Models/Question.php b/app/Models/Question.php index c06d35a3..c6f3d809 100644 --- a/app/Models/Question.php +++ b/app/Models/Question.php @@ -149,6 +149,13 @@ class Question extends Model */ public $published = self::PUBLISH_PENDING; + /** + * 推荐标识 + * + * @var integer + */ + public $featured = 0; + /** * 删除标识 * @@ -318,9 +325,10 @@ class Question extends Model public static function sortTypes() { return [ - 'latest' => '最新提问', + 'latest' => '最新问题', 'active' => '最新回答', 'unanswered' => '尚未回答', + 'featured' => '推荐问题', ]; } diff --git a/app/Repos/Question.php b/app/Repos/Question.php index 8b874716..8db156ad 100644 --- a/app/Repos/Question.php +++ b/app/Repos/Question.php @@ -58,6 +58,20 @@ class Question extends Repository } } + if (!empty($where['published'])) { + if (is_array($where['published'])) { + $builder->inWhere('published', $where['published']); + } else { + $builder->andWhere('published = :published:', ['published' => $where['published']]); + } + } + + if (!empty($where['create_time'][0]) && !empty($where['create_time'][1])) { + $startTime = strtotime($where['create_time'][0]); + $endTime = strtotime($where['create_time'][1]); + $builder->betweenWhere('create_time', $startTime, $endTime); + } + if (!empty($where['owner_id'])) { $builder->andWhere('owner_id = :owner_id:', ['owner_id' => $where['owner_id']]); } @@ -74,33 +88,23 @@ class Question extends Repository $builder->andWhere('closed = :closed:', ['closed' => $where['closed']]); } + if (isset($where['featured'])) { + $builder->andWhere('featured = :featured:', ['featured' => $where['featured']]); + } + if (isset($where['solved'])) { $builder->andWhere('solved = :solved:', ['solved' => $where['solved']]); } - if (!empty($where['published'])) { - if (is_array($where['published'])) { - $builder->inWhere('published', $where['published']); - } else { - $builder->andWhere('published = :published:', ['published' => $where['published']]); - } - } - - if (!empty($where['create_time'][0]) && !empty($where['create_time'][1])) { - $startTime = strtotime($where['create_time'][0]); - $endTime = strtotime($where['create_time'][1]); - $builder->betweenWhere('create_time', $startTime, $endTime); - } - if (isset($where['deleted'])) { $builder->andWhere('deleted = :deleted:', ['deleted' => $where['deleted']]); } - if ($sort == 'unanswered') { + if ($sort == 'featured') { + $builder->andWhere('featured = 1'); + } elseif ($sort == 'unanswered') { $builder->andWhere('answer_count = 0'); - } - - if ($sort == 'reported') { + } elseif ($sort == 'reported') { $builder->andWhere('report_count > 0'); } diff --git a/app/Services/Logic/Article/ArticleList.php b/app/Services/Logic/Article/ArticleList.php index 53147885..d04b53e2 100644 --- a/app/Services/Logic/Article/ArticleList.php +++ b/app/Services/Logic/Article/ArticleList.php @@ -82,8 +82,8 @@ class ArticleList extends LogicService foreach ($articles as $article) { - if (!empty($question['cover']) && !Text::startsWith($question['cover'], 'http')) { - $question['cover'] = $cosUrl . $question['cover']; + if (!empty($article['cover']) && !Text::startsWith($article['cover'], 'http')) { + $article['cover'] = $cosUrl . $article['cover']; } $article['tags'] = json_decode($article['tags'], true); diff --git a/app/Validators/Question.php b/app/Validators/Question.php index 5f3d66b7..28ac33ad 100644 --- a/app/Validators/Question.php +++ b/app/Validators/Question.php @@ -11,7 +11,6 @@ use App\Caches\MaxQuestionId as MaxQuestionIdCache; use App\Caches\Question as QuestionCache; use App\Exceptions\BadRequest as BadRequestException; use App\Models\Question as QuestionModel; -use App\Models\Reason as ReasonModel; use App\Repos\Question as QuestionRepo; use App\Services\EditorStorage as EditorStorageService; @@ -151,6 +150,15 @@ class Question extends Validator return $status; } + public function checkFeatureStatus($status) + { + if (!in_array($status, [0, 1])) { + throw new BadRequestException('question.invalid_feature_status'); + } + + return $status; + } + public function checkCloseStatus($status) { if (!in_array($status, [0, 1])) { diff --git a/db/migrations/20231108085025.php b/db/migrations/20231108085025.php index 557e4c74..771a8111 100644 --- a/db/migrations/20231108085025.php +++ b/db/migrations/20231108085025.php @@ -14,6 +14,7 @@ final class V20231108085025 extends AbstractMigration public function up() { $this->alterVipTable(); + $this->alterQuestionTable(); $this->alterArticleTable(); $this->alterConsultTable(); $this->alterResourceTable(); @@ -45,6 +46,24 @@ final class V20231108085025 extends AbstractMigration $table->save(); } + protected function alterQuestionTable() + { + $table = $this->table('kg_question'); + + if (!$table->hasColumn('featured')) { + $table->addColumn('featured', 'integer', [ + 'null' => false, + 'default' => '0', + 'limit' => MysqlAdapter::INT_REGULAR, + 'signed' => false, + 'comment' => '推荐标识', + 'after' => 'published', + ]); + } + + $table->save(); + } + protected function alterArticleTable() { $table = $this->table('kg_article'); diff --git a/public/static/home/css/common.css b/public/static/home/css/common.css index 231bcd9a..e8ca5767 100644 --- a/public/static/home/css/common.css +++ b/public/static/home/css/common.css @@ -330,8 +330,12 @@ height: 100%; } -.query-input { - width: 720px; +.search-form .query-input { + width: 696px; +} + +.search-form .search-btn { + padding: 0 30px; } .search-tab {