['cid'] ]; } /** * @notes 自定查询条件 * @return array * @author 段誉 * @date 2022/10/25 16:53 */ public function queryWhere() { $where[] = ['is_show', '=', 1]; if (!empty($this->params['keyword'])) { $where[] = ['title', 'like', '%' . $this->params['keyword'] . '%']; } return $where; } /** * @notes 获取文章列表 * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * @author 段誉 * @date 2022/9/16 18:55 */ public function lists(): array { $orderRaw = 'sort desc, id desc'; $sortType = $this->params['sort'] ?? 'default'; // 最新排序 if ($sortType == 'new') { $orderRaw = 'id desc'; } // 最热排序 if ($sortType == 'hot') { $orderRaw = 'click_actual + click_virtual desc, id desc'; } $field = 'id,cid,title,desc,image,click_virtual,click_actual,create_time'; $result = Article::field($field) ->where($this->queryWhere()) ->where($this->searchWhere) ->orderRaw($orderRaw) ->append(['click']) ->hidden(['click_virtual', 'click_actual']) ->limit($this->limitOffset, $this->limitLength) ->select()->toArray(); $articleIds = array_column($result, 'id'); $collectIds = ArticleCollect::where(['user_id' => $this->userId, 'status' => YesNoEnum::YES]) ->whereIn('article_id', $articleIds) ->column('article_id'); foreach ($result as &$item) { $item['collect'] = in_array($item['id'], $collectIds); } return $result; } /** * @notes 获取文章数量 * @return int * @author 段誉 * @date 2022/9/16 18:55 */ public function count(): int { return Article::where($this->searchWhere) ->where($this->queryWhere()) ->count(); } }