|
|
@@ -0,0 +1,119 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace addons\LsActivity\api\modules\v1\controllers;
|
|
|
+
|
|
|
+use Yii;
|
|
|
+use yii\data\ActiveDataProvider;
|
|
|
+use yii\data\Pagination;
|
|
|
+use yii\web\NotFoundHttpException;
|
|
|
+use common\enums\StatusEnum;
|
|
|
+use addons\LsActivity\common\models\Activity;
|
|
|
+use api\controllers\OnAuthController;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 文章接口
|
|
|
+ *
|
|
|
+ * Class ArticleController
|
|
|
+ * @package addons\TinyBlog\api\modules\v1\controllers
|
|
|
+ * @author jianyan74 <751393839@qq.com>
|
|
|
+ */
|
|
|
+class ActivityController extends OnAuthController
|
|
|
+{
|
|
|
+ /**
|
|
|
+ * @var Activity
|
|
|
+ */
|
|
|
+ public $modelClass = Activity::class;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 不用进行登录验证的方法
|
|
|
+ * 例如: ['index', 'update', 'create', 'view', 'delete']
|
|
|
+ * 默认全部需要验证
|
|
|
+ *
|
|
|
+ * @var array
|
|
|
+ */
|
|
|
+ protected $authOptional = ['index', 'view', 'list'];
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 首页
|
|
|
+ *
|
|
|
+ * @return ActiveDataProvider
|
|
|
+ */
|
|
|
+ public function actionIndex()
|
|
|
+ {
|
|
|
+ return new ActiveDataProvider([
|
|
|
+ 'query' => $this->modelClass::find()
|
|
|
+ ->where(['status' => StatusEnum::ENABLED])
|
|
|
+ ->select(['id', 'title', 'cover', 'author', 'content', 'description', 'view'])
|
|
|
+ ->orderBy('sort asc, id desc')
|
|
|
+ ->asArray(),
|
|
|
+ 'pagination' => [
|
|
|
+ 'pageSize' => $this->pageSize,
|
|
|
+ 'validatePage' => false,// 超出分页不返回data
|
|
|
+ ],
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 自定义装修可用
|
|
|
+ *
|
|
|
+ * 修改数据格式返回
|
|
|
+ *
|
|
|
+ * @return array|mixed
|
|
|
+ */
|
|
|
+ public function actionList()
|
|
|
+ {
|
|
|
+ $keyword = Yii::$app->request->get('keyword');
|
|
|
+
|
|
|
+ $data = $this->modelClass::find()
|
|
|
+ ->select(['id', 'title', 'description', 'cover', 'author','created_at', 'view'])
|
|
|
+ // ->where(['status' => StatusEnum::ENABLED])
|
|
|
+ ->andFilterWhere(['like', 'title', $keyword]);
|
|
|
+ $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $this->pageSize]);
|
|
|
+ $models = $data->offset($pages->offset)
|
|
|
+ ->orderBy('id desc')
|
|
|
+ ->limit($pages->limit)
|
|
|
+ ->asArray()
|
|
|
+ ->all();
|
|
|
+
|
|
|
+ return [
|
|
|
+ 'list' => $models,
|
|
|
+ 'pages' => [
|
|
|
+ 'totalCount' => $pages->totalCount,
|
|
|
+ 'pageSize' => $pages->pageSize,
|
|
|
+ ]
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查看
|
|
|
+ *
|
|
|
+ * @return ActiveDataProvider
|
|
|
+ */
|
|
|
+ // public function actionView($id)
|
|
|
+ // {
|
|
|
+ // return new ActiveDataProvider([
|
|
|
+ // 'query' => $this->modelClass::find()
|
|
|
+ // ->where(['id' => $id])
|
|
|
+ // ->select(['id', 'title', 'cover', 'author', 'content', 'description', 'created_at','view'])
|
|
|
+ // ->asArray(),
|
|
|
+ // ]);
|
|
|
+ // }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param $id
|
|
|
+ * @return \yii\db\ActiveRecord
|
|
|
+ * @throws NotFoundHttpException
|
|
|
+ */
|
|
|
+ protected function findModel($id)
|
|
|
+ {
|
|
|
+ /* @var $model \yii\db\ActiveRecord */
|
|
|
+ if (empty($id) || !($model = $this->modelClass::find()->where([
|
|
|
+ 'id' => $id,
|
|
|
+ 'status' => StatusEnum::ENABLED,
|
|
|
+ ])->one())) {
|
|
|
+ throw new NotFoundHttpException('请求的数据不存在');
|
|
|
+ }
|
|
|
+
|
|
|
+ return $model;
|
|
|
+ }
|
|
|
+}
|