// +---------------------------------------------------------------------- declare (strict_types = 1); namespace addon\articles\controller; use addon\articles\model\Article; use addon\articles\model\ArticleTopic; use addon\articles\model\ArticleTopicArticle; use think\facade\Cache; /** * 前台专题聚合页 * * /articles/topic 专题列表 * /articles/topic/ 专题详情(专题内文章列表) */ class Topic extends ArticlesFrontend { /** * 专题列表 */ public function index() { $topics = Cache::remember('articles:topics', function () { $list = ArticleTopic::scope('enable') ->order('sort', 'desc') ->order('id', 'desc') ->select(); $rows = []; foreach ($list as $t) { $rows[] = [ 'id' => $t->id, 'title' => $t->title, 'alias' => $t->alias, 'description' => $t->description, 'cover' => $t->cover, 'count' => ArticleTopicArticle::where('tid', $t->id)->count(), ]; } return $rows; }, 300); $this->view->assign([ 'topics' => $topics, 'nav_active' => '', ]); return $this->fetch('topic/index'); } /** * 专题详情(文章聚合) */ public function detail($id = 0) { $topic = ArticleTopic::scope('enable')->find((int) $id); if (!$topic) { return $this->result->setStatusCode(404)->error('专题不存在或已下线', 404); } $page = (int) input('page', 1); $ids = ArticleTopicArticle::articleIdsOf((int) $topic->id); $list = []; if ($ids) { // 只取已发布且到点的文章,并保持专题内排序 $list = Article::with(['category']) ->field(Article::listFields()) ->whereIn('id', $ids) ->scope('normal') ->orderRaw('FIELD(id,' . implode(',', $ids) . ')') ->paginate(['list_rows' => 10, 'page' => $page]); } $this->view->assign([ 'topic' => $topic, 'list' => $list ? $list->items() : [], 'page' => $list ? $list->render() : '', 'nav_active' => '', ]); return $this->fetch('topic/detail'); } }