| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- <?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\Sign;
- use addons\LsActivity\common\models\Activity;
- use api\controllers\OnAuthController;
- use common\helpers\ResultHelper;
- use common\helpers\ArrayHelper;
- /**
- * 文章接口
- *
- * Class ArticleController
- * @package addons\TinyBlog\api\modules\v1\controllers
- * @author jianyan74 <751393839@qq.com>
- */
- class SignController extends OnAuthController
- {
- /**
- * @var Sign
- */
- public $modelClass = Sign::class;
- /**
- * 不用进行登录验证的方法
- * 例如: ['index', 'update', 'create', 'view', 'delete']
- * 默认全部需要验证
- *
- * @var array
- */
- protected $authOptional = ['index', 'view', 'list','update','create','get-my-sign'];
- /**
- * 首页
- *
- * @return ActiveDataProvider
- */
- public function actionIndex()
- {
- return new ActiveDataProvider([
- 'query' => $this->modelClass::find()
- ->where(['status' => StatusEnum::ENABLED])
- ->select(['*'])
- ->orderBy('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(['*'])
- ->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)
- // {
- // $uid = Yii::$app->request->get('uid');
- // $aid = Yii::$app->request->get('aid');
- // $data = $this->modelClass::find()
- // ->select(['*'])
- // ->where(['uid' => $uid, 'aid' => $aid])->one();
- // return $data;
- // }
- /**
- * @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;
- }
- /**
- * 编辑/创建
- *
- * @return mixed
- */
- public function actionEdit()
- {
- $id = Yii::$app->request->get('id', null);
- $model = $this->findModel($id);
- $model->status= StatusEnum::ENABLED;
- // $model->updated_at = time();
- if ($model->load(Yii::$app->request->post()))
- {
- return $model->save()
- ? $this->message('保存成功', $this->redirect(['index']))
- : $this->message('保存失败', $this->redirect(['index']), 'error');
- }
- return $this->render($this->action->id, [
- 'model' => $model,
- ]);
- }
- /**
- * 更新
- *
- * @param $id
- * @return bool|mixed
- * @throws NotFoundHttpException
- */
- public function actionUpdate($id)
- {
- // $model = new LoginForm();
- $id = Yii::$app->request->get('id', null);
- $uid = Yii::$app->request->get('uid');
- $aid = Yii::$app->request->get('aid');
- $model = $this->findModel($id);
- // $model->attributes = Yii::$app->request->post();
- $model->status= StatusEnum::ENABLED;
- // $data = Yii::$app->request->post();
- // $data = ArrayHelper::filter($data, [
- // 'uid',
- // 'aid',
- // ]);
- // $model = $this->findModel($id);
- $model->uid = $uid;
- $model->aid = $aid;
- // $model->attributes = $data;
- if (!$model->save()) {
- return ResultHelper::json(422, $this->getError($model));
- }
- return 'ok';
- }
-
- /**
- * 创建
- *
- * @return mixed|\yii\db\ActiveRecord
- */
- public function actionCreate()
- {
- $params = Yii::$app->request->post('params');
- $aid = $params["aid"];
- $uid = $params["uid"];
- // $model = $this->modelClass::find()->where([
- // 'aid' => $aid,
- // 'uid' => $uid,
- // 'status' => StatusEnum::ENABLED,
- // ])->one();
- if ($model = $this->modelClass::find()->where([
- 'aid' => $aid,
- 'uid' => $uid,
- // 'status' => StatusEnum::ENABLED,
- ])->one()) {
- if($model->status !== StatusEnum::ENABLED){
- $model->status = '1';
- if (!$model->save()) {
- return ResultHelper::json(422, $this->getError($model));
- }
- }else
- {
- return ResultHelper::json(422, '已经报名!');
- }
- }
-
- $model = new $this->modelClass();
- // $params["status"] = StatusEnum::ENABLED;
- $model->attributes = $params;
- if (!$model->save()) {
- return ResultHelper::json(422, $this->getError($model));
- }
- return "OK";
- }
- /**
- * 获取报名情况
- *
- * @return mixed|\yii\db\ActiveRecord
- */
- public function actionGetSignStatus()
- {
- $uid = Yii::$app->request->get('uid');
- $aid = Yii::$app->request->get('aid');
- if (!($model = $this->modelClass::find()
- ->select(['*'])
- ->where(['uid' => $uid, 'aid' => $aid])->one()))
- {
- //throw new NotFoundHttpException('请求的数据不存在');
- return "OK";
- }
-
- return $model;
- }
- /**
- * 我的报名
- *
- * @return mixed|\yii\db\ActiveRecord
- */
- public function actionGetMySign()
- {
- $uid = Yii::$app->request->get('uid');
- // $model = $this->modelClass::find()->where(['uid' => $uid])->joinWith("activity")->all();
- $model = Activity::find()->where(['status' => StatusEnum::ENABLED])->joinWith("sign")->where(['uid' => $uid])->all();
-
- return $model;
- // return $this->modelClass()::getSign($uid);
- // return new ActiveDataProvider([
- // 'query' => Activity::find()
- // ->where(['status' => StatusEnum::ENABLED])
- // ->select(['*'])
- // ->orderBy('id desc')
- // ->asArray(),
- // 'pagination' => [
- // 'pageSize' => $this->pageSize,
- // 'validatePage' => false,// 超出分页不返回data
- // ],
- // ]);
- }
- /**
- * {@inheritdoc}
- */
- protected function verbs()
- {
- return [
- 'index' => ['GET', 'POST', 'HEAD', 'OPTIONS'],
- 'view' => ['GET', 'HEAD', 'OPTIONS'],
- 'create' => ['POST', 'OPTIONS'],
- 'update' => ['PUT', 'PATCH', 'OPTIONS' , 'POST'],
- 'delete' => ['DELETE', 'OPTIONS'],
- ];
- }
- }
|