ActivityController.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace addons\LsActivity\api\modules\v1\controllers;
  3. use Yii;
  4. use yii\data\ActiveDataProvider;
  5. use yii\data\Pagination;
  6. use yii\web\NotFoundHttpException;
  7. use common\enums\StatusEnum;
  8. use addons\LsActivity\common\models\Activity;
  9. use api\controllers\OnAuthController;
  10. /**
  11. * 文章接口
  12. *
  13. * Class ArticleController
  14. * @package addons\TinyBlog\api\modules\v1\controllers
  15. * @author jianyan74 <751393839@qq.com>
  16. */
  17. class ActivityController extends OnAuthController
  18. {
  19. /**
  20. * @var Activity
  21. */
  22. public $modelClass = Activity::class;
  23. /**
  24. * 不用进行登录验证的方法
  25. * 例如: ['index', 'update', 'create', 'view', 'delete']
  26. * 默认全部需要验证
  27. *
  28. * @var array
  29. */
  30. protected $authOptional = ['index', 'view', 'list'];
  31. /**
  32. * 首页
  33. *
  34. * @return ActiveDataProvider
  35. */
  36. public function actionIndex()
  37. {
  38. return new ActiveDataProvider([
  39. 'query' => $this->modelClass::find()
  40. ->where(['status' => StatusEnum::ENABLED])
  41. ->select(['id', 'title', 'cover', 'author', 'content', 'description','created_at', 'view'])
  42. ->orderBy('sort asc, id desc')
  43. ->asArray(),
  44. 'pagination' => [
  45. 'pageSize' => $this->pageSize,
  46. 'validatePage' => false,// 超出分页不返回data
  47. ],
  48. ]);
  49. }
  50. /**
  51. * 自定义装修可用
  52. *
  53. * 修改数据格式返回
  54. *
  55. * @return array|mixed
  56. */
  57. public function actionList()
  58. {
  59. $keyword = Yii::$app->request->get('keyword');
  60. $data = $this->modelClass::find()
  61. ->select(['id', 'title', 'description', 'cover', 'author','created_at', 'view'])
  62. ->where(['status' => StatusEnum::ENABLED])
  63. ->andFilterWhere(['like', 'title', $keyword]);
  64. $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $this->pageSize]);
  65. $models = $data->offset($pages->offset)
  66. ->orderBy('id desc')
  67. ->limit($pages->limit)
  68. ->asArray()
  69. ->all();
  70. return [
  71. 'list' => $models,
  72. 'pages' => [
  73. 'totalCount' => $pages->totalCount,
  74. 'pageSize' => $pages->pageSize,
  75. ]
  76. ];
  77. }
  78. /**
  79. * 查看
  80. *
  81. * @return ActiveDataProvider
  82. */
  83. // public function actionView($id)
  84. // {
  85. // return new ActiveDataProvider([
  86. // 'query' => $this->modelClass::find()
  87. // ->where(['id' => $id])
  88. // ->select(['id', 'title', 'cover', 'author', 'content', 'description', 'created_at','view'])
  89. // ->asArray(),
  90. // ]);
  91. // }
  92. }
粤ICP备19079148号