SignController.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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\Sign;
  9. use api\controllers\OnAuthController;
  10. use common\helpers\ResultHelper;
  11. use common\helpers\ArrayHelper;
  12. /**
  13. * 文章接口
  14. *
  15. * Class ArticleController
  16. * @package addons\TinyBlog\api\modules\v1\controllers
  17. * @author jianyan74 <751393839@qq.com>
  18. */
  19. class SignController extends OnAuthController
  20. {
  21. /**
  22. * @var Sign
  23. */
  24. public $modelClass = Sign::class;
  25. /**
  26. * 不用进行登录验证的方法
  27. * 例如: ['index', 'update', 'create', 'view', 'delete']
  28. * 默认全部需要验证
  29. *
  30. * @var array
  31. */
  32. protected $authOptional = ['index', 'view', 'list','update','create'];
  33. /**
  34. * 首页
  35. *
  36. * @return ActiveDataProvider
  37. */
  38. public function actionIndex()
  39. {
  40. return new ActiveDataProvider([
  41. 'query' => $this->modelClass::find()
  42. ->where(['status' => StatusEnum::ENABLED])
  43. ->select(['*'])
  44. ->orderBy('id desc')
  45. ->asArray(),
  46. 'pagination' => [
  47. 'pageSize' => $this->pageSize,
  48. 'validatePage' => false,// 超出分页不返回data
  49. ],
  50. ]);
  51. }
  52. /**
  53. * 自定义装修可用
  54. *
  55. * 修改数据格式返回
  56. *
  57. * @return array|mixed
  58. */
  59. public function actionList()
  60. {
  61. $keyword = Yii::$app->request->get('keyword');
  62. $data = $this->modelClass::find()
  63. ->select(['*'])
  64. ->where(['status' => StatusEnum::ENABLED]);
  65. // ->andFilterWhere(['like', 'title', $keyword]);
  66. $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $this->pageSize]);
  67. $models = $data->offset($pages->offset)
  68. ->orderBy('id desc')
  69. ->limit($pages->limit)
  70. ->asArray()
  71. ->all();
  72. return [
  73. 'list' => $models,
  74. 'pages' => [
  75. 'totalCount' => $pages->totalCount,
  76. 'pageSize' => $pages->pageSize,
  77. ]
  78. ];
  79. }
  80. /**
  81. * 查看
  82. *
  83. * @return ActiveDataProvider
  84. */
  85. // public function actionView($id)
  86. // {
  87. // $uid = Yii::$app->request->get('uid');
  88. // $aid = Yii::$app->request->get('aid');
  89. // $data = $this->modelClass::find()
  90. // ->select(['*'])
  91. // ->where(['uid' => $uid, 'aid' => $aid])->one();
  92. // return $data;
  93. // }
  94. /**
  95. * @param $id
  96. * @return \yii\db\ActiveRecord
  97. * @throws NotFoundHttpException
  98. */
  99. protected function findModel($id)
  100. {
  101. /* @var $model \yii\db\ActiveRecord */
  102. if (empty($id) || !($model = $this->modelClass::find()->where([
  103. 'id' => $id,
  104. 'status' => StatusEnum::ENABLED,
  105. ])->one())) {
  106. throw new NotFoundHttpException('请求的数据不存在');
  107. }
  108. return $model;
  109. }
  110. /**
  111. * 编辑/创建
  112. *
  113. * @return mixed
  114. */
  115. public function actionEdit()
  116. {
  117. $id = Yii::$app->request->get('id', null);
  118. $model = $this->findModel($id);
  119. $model->status= StatusEnum::ENABLED;
  120. // $model->updated_at = time();
  121. if ($model->load(Yii::$app->request->post()))
  122. {
  123. return $model->save()
  124. ? $this->message('保存成功', $this->redirect(['index']))
  125. : $this->message('保存失败', $this->redirect(['index']), 'error');
  126. }
  127. return $this->render($this->action->id, [
  128. 'model' => $model,
  129. ]);
  130. }
  131. /**
  132. * 更新
  133. *
  134. * @param $id
  135. * @return bool|mixed
  136. * @throws NotFoundHttpException
  137. */
  138. public function actionUpdate($id)
  139. {
  140. // $model = new LoginForm();
  141. $id = Yii::$app->request->get('id', null);
  142. $uid = Yii::$app->request->get('uid');
  143. $aid = Yii::$app->request->get('aid');
  144. $model = $this->findModel($id);
  145. // $model->attributes = Yii::$app->request->post();
  146. $model->status= StatusEnum::ENABLED;
  147. // $data = Yii::$app->request->post();
  148. // $data = ArrayHelper::filter($data, [
  149. // 'uid',
  150. // 'aid',
  151. // ]);
  152. // $model = $this->findModel($id);
  153. $model->uid = $uid;
  154. $model->aid = $aid;
  155. // $model->attributes = $data;
  156. if (!$model->save()) {
  157. return ResultHelper::json(422, $this->getError($model));
  158. }
  159. return 'ok';
  160. }
  161. /**
  162. * 创建
  163. *
  164. * @return mixed|\yii\db\ActiveRecord
  165. */
  166. public function actionCreate()
  167. {
  168. $params = Yii::$app->request->post('params');
  169. $aid = $params["aid"];
  170. $uid = $params["uid"];
  171. // $model = $this->modelClass::find()->where([
  172. // 'aid' => $aid,
  173. // 'uid' => $uid,
  174. // 'status' => StatusEnum::ENABLED,
  175. // ])->one();
  176. if ($model = $this->modelClass::find()->where([
  177. 'aid' => $aid,
  178. 'uid' => $uid,
  179. // 'status' => StatusEnum::ENABLED,
  180. ])->one()) {
  181. if($model->status !== StatusEnum::ENABLED){
  182. $model->status = '1';
  183. if (!$model->save()) {
  184. return ResultHelper::json(422, $this->getError($model));
  185. }
  186. }else
  187. {
  188. return ResultHelper::json(422, '已经报名!');
  189. }
  190. }
  191. $model = new $this->modelClass();
  192. // $params["status"] = StatusEnum::ENABLED;
  193. $model->attributes = $params;
  194. if (!$model->save()) {
  195. return ResultHelper::json(422, $this->getError($model));
  196. }
  197. return "OK";
  198. }
  199. /**
  200. * 获取报名情况
  201. *
  202. * @return mixed|\yii\db\ActiveRecord
  203. */
  204. public function actionGetSignStatus()
  205. {
  206. $uid = Yii::$app->request->get('uid');
  207. $aid = Yii::$app->request->get('aid');
  208. if (!($model = $this->modelClass::find()
  209. ->select(['*'])
  210. ->where(['uid' => $uid, 'aid' => $aid])->one()))
  211. {
  212. //throw new NotFoundHttpException('请求的数据不存在');
  213. return "OK";
  214. }
  215. return $model;
  216. }
  217. /**
  218. * {@inheritdoc}
  219. */
  220. protected function verbs()
  221. {
  222. return [
  223. 'index' => ['GET', 'POST', 'HEAD', 'OPTIONS'],
  224. 'view' => ['GET', 'HEAD', 'OPTIONS'],
  225. 'create' => ['POST', 'OPTIONS'],
  226. 'update' => ['PUT', 'PATCH', 'OPTIONS' , 'POST'],
  227. 'delete' => ['DELETE', 'OPTIONS'],
  228. ];
  229. }
  230. }
粤ICP备19079148号