SignController.php 7.7 KB

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