提交 1d72bee7 编写于 作者: xchenhao's avatar xchenhao

get newest articles

上级 5a96b51d
......@@ -45,8 +45,9 @@ class Article extends \yii\db\ActiveRecord
// 版头图
const ATTR_BANNER = 0b10;
// 首页播放量最高的文章数量
const HOMEPAGE_COUNT_TOP_VIEW_ARTICLE = 9;
// 首页文章数量
const HOMEPAGE_ARTICLE_COUNT = 9;
// 版头图数量
const BANNER_COUNT = 3;
......@@ -139,14 +140,15 @@ class Article extends \yii\db\ActiveRecord
}
/**
* 获取播放量最高的文章
* 获取文章
*
* @param int $page
* @param int $page_size
* @param string $order_by 例:id, create_time, view_count
* @return array
* @throws \Exception
*/
public static function getTopViewArticles(int $page = 1, int $page_size = self::HOMEPAGE_COUNT_TOP_VIEW_ARTICLE): array
public static function getArticles(int $page = 1, int $page_size = self::HOMEPAGE_ARTICLE_COUNT, string $order_by = 'id'): array
{
if ($page_size < 0) {
throw new \Exception('参数错误');
......@@ -156,7 +158,7 @@ class Article extends \yii\db\ActiveRecord
->select('id, category_id, title, intro, cover, create_time')
->where(['status' => self::STATUS_PASS])
->andWhere('attr & :banner = 0', [':banner' => self::ATTR_BANNER])
->orderBy('view_count DESC');
->orderBy([$order_by => SORT_DESC]);
$items = $query
->offset($page_size * ($page - 1))
->limit($page_size)
......
......@@ -54,9 +54,9 @@ class ArticleController extends Controller
* @return string
* @throws \Exception
*/
public function actionIndex(int $page = 1, $page_size = Article::HOMEPAGE_COUNT_TOP_VIEW_ARTICLE, $list = 0)
public function actionIndex(int $page = 1, $page_size = Article::HOMEPAGE_ARTICLE_COUNT, $list = 0)
{
$articles = Article::getTopViewArticles($page, $page_size);
$articles = Article::getArticles($page, $page_size);
$articles['pagination']->params = array_merge($_GET, ['list' => $list]);
$banners = Article::getBanners();
return $this->render('index', [
......@@ -90,7 +90,7 @@ class ArticleController extends Controller
* @param integer $list 文章显示方式(0 卡片,1 列表)
* @return string
*/
public function actionList(int $category_id, int $page = 1, $page_size = Article::HOMEPAGE_COUNT_TOP_VIEW_ARTICLE, $list = 0)
public function actionList(int $category_id, int $page = 1, $page_size = Article::HOMEPAGE_ARTICLE_COUNT, $list = 0)
{
$category = Category::findOne(['id' => $category_id]);
$view_tpl = 'list';
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册