IndexController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace addons\TinyBlog\frontend\controllers;
  3. use Yii;
  4. use yii\data\Pagination;
  5. use yii\web\UnprocessableEntityHttpException;
  6. use common\enums\StatusEnum;
  7. use addons\TinyBlog\common\models\Article;
  8. /**
  9. * Class IndexController
  10. * @package addons\TinyBlog\frontend\controllers
  11. * @author jianyan74 <751393839@qq.com>
  12. */
  13. class IndexController extends BaseController
  14. {
  15. /**
  16. * @return string
  17. */
  18. public function actionIndex()
  19. {
  20. $keyword = Yii::$app->request->get('keyword');
  21. $data = Article::find()
  22. ->select(['id', 'merchant_id', 'cate_id', 'title', 'description', 'cover', 'author', 'view', 'created_at'])
  23. ->where(['status' => StatusEnum::ENABLED])
  24. ->andFilterWhere(['like', 'title', $keyword])
  25. ->andFilterWhere(['merchant_id' => $this->getMerchantId()]);
  26. $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $this->pageSize]);
  27. $models = $data->offset($pages->offset)
  28. ->orderBy('sort asc, id desc')
  29. ->with(['cate'])
  30. ->limit($pages->limit)
  31. ->asArray()
  32. ->all();
  33. return $this->render($this->action->id, [
  34. 'keyword' => $keyword,
  35. 'models' => $models,
  36. 'pages' => $pages,
  37. 'adv' => Yii::$app->tinyBlogService->adv->newest(),
  38. ]);
  39. }
  40. /**
  41. * @return string
  42. */
  43. public function actionList()
  44. {
  45. $cateId = Yii::$app->request->get('cate_id');
  46. $cate = Yii::$app->tinyBlogService->cate->findById($cateId);
  47. $data = Article::find()
  48. ->select(['id', 'merchant_id', 'cate_id', 'title', 'description', 'cover', 'author', 'view', 'created_at'])
  49. ->where(['status' => StatusEnum::ENABLED])
  50. ->andFilterWhere(['cate_id' => $cateId])
  51. ->andFilterWhere(['merchant_id' => $this->getMerchantId()]);
  52. $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $this->pageSize]);
  53. $models = $data->offset($pages->offset)
  54. ->orderBy('sort asc, id desc')
  55. ->with(['cate'])
  56. ->limit($pages->limit)
  57. ->asArray()
  58. ->all();
  59. if (empty($cate)) {
  60. throw new UnprocessableEntityHttpException('找不到分类内容...');
  61. }
  62. return $this->render($this->action->id, [
  63. 'models' => $models,
  64. 'cate' => $cate,
  65. 'pages' => $pages,
  66. ]);
  67. }
  68. /**
  69. * @return string
  70. */
  71. public function actionTag()
  72. {
  73. $tag = Yii::$app->request->get('tag');
  74. $data = Article::find()
  75. ->select([
  76. Article::tableName().'.id',
  77. Article::tableName().'.merchant_id',
  78. 'cate_id',
  79. Article::tableName().'.title',
  80. 'description',
  81. 'cover',
  82. 'author',
  83. 'view',
  84. Article::tableName().'.created_at',
  85. ])
  86. ->where([Article::tableName().'.status' => StatusEnum::ENABLED])
  87. ->with(['cate', 'merchant'])
  88. ->anyTagValues($tag)
  89. ->andFilterWhere(['merchant_id' => $this->getMerchantId()]);
  90. $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $this->pageSize]);
  91. $models = $data->offset($pages->offset)
  92. ->orderBy(Article::tableName().'.sort asc, id desc')
  93. ->limit($pages->limit)
  94. ->asArray()
  95. ->all();
  96. if (empty($tag)) {
  97. throw new UnprocessableEntityHttpException('找不到标签...');
  98. }
  99. return $this->render($this->action->id, [
  100. 'models' => $models,
  101. 'tag' => $tag,
  102. 'pages' => $pages,
  103. ]);
  104. }
  105. /**
  106. * @return string
  107. */
  108. public function actionView($id)
  109. {
  110. $model = Article::find()->where(['id' => $id, 'status' => StatusEnum::ENABLED])->one();
  111. if (empty($model)) {
  112. throw new UnprocessableEntityHttpException('找不到文章内容...');
  113. }
  114. $model->view += 1;
  115. Article::updateAllCounters(['view' => 1], ['id' => $model['id']]);
  116. return $this->render($this->action->id, [
  117. 'model' => $model,
  118. 'prev' => Yii::$app->tinyBlogService->article->getPrev($id),
  119. 'next' => Yii::$app->tinyBlogService->article->getNext($id),
  120. 'recommend' => Yii::$app->tinyBlogService->article->recommend(),
  121. ]);
  122. }
  123. }
粤ICP备19079148号