Curd.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace common\traits;
  3. use Yii;
  4. use yii\data\Pagination;
  5. use yii\base\InvalidConfigException;
  6. use common\helpers\ResultHelper;
  7. use common\enums\StatusEnum;
  8. use common\helpers\ArrayHelper;
  9. /**
  10. * Trait Curd
  11. * @property \yii\db\ActiveRecord|\yii\base\Model $modelClass
  12. * @package common\traits
  13. */
  14. trait Curd
  15. {
  16. /**
  17. * @throws InvalidConfigException
  18. */
  19. public function init()
  20. {
  21. parent::init();
  22. if ($this->modelClass === null) {
  23. throw new InvalidConfigException('"modelClass" 属性必须设置.');
  24. }
  25. }
  26. /**
  27. * 首页
  28. *
  29. * @return mixed
  30. */
  31. public function actionIndex()
  32. {
  33. $data = $this->modelClass::find()
  34. ->where(['>=', 'status', StatusEnum::DISABLED]);
  35. $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $this->pageSize]);
  36. $models = $data->offset($pages->offset)
  37. ->orderBy('id desc')
  38. ->limit($pages->limit)
  39. ->all();
  40. return $this->render($this->action->id, [
  41. 'models' => $models,
  42. 'pages' => $pages
  43. ]);
  44. }
  45. /**
  46. * 编辑/创建
  47. *
  48. * @return mixed
  49. */
  50. public function actionEdit()
  51. {
  52. $id = Yii::$app->request->get('id', null);
  53. $model = $this->findModel($id);
  54. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  55. return $this->referrer();
  56. }
  57. return $this->render($this->action->id, [
  58. 'model' => $model,
  59. ]);
  60. }
  61. /**
  62. * 伪删除
  63. *
  64. * @param $id
  65. * @return mixed
  66. */
  67. public function actionDestroy($id)
  68. {
  69. if (!($model = $this->modelClass::findOne($id))) {
  70. return $this->message("找不到数据", $this->redirect(Yii::$app->request->referrer), 'error');
  71. }
  72. $model->status = StatusEnum::DELETE;
  73. if ($model->save()) {
  74. return $this->message("删除成功", $this->redirect(Yii::$app->request->referrer));
  75. }
  76. return $this->message("删除失败", $this->redirect(Yii::$app->request->referrer), 'error');
  77. }
  78. /**
  79. * 删除
  80. *
  81. * @param $id
  82. * @return mixed
  83. * @throws \Throwable
  84. * @throws \yii\db\StaleObjectException
  85. */
  86. public function actionDelete($id)
  87. {
  88. if ($this->findModel($id)->delete()) {
  89. return $this->message("删除成功", $this->redirect(Yii::$app->request->referrer));
  90. }
  91. return $this->message("删除失败", $this->redirect(Yii::$app->request->referrer), 'error');
  92. }
  93. /**
  94. * ajax更新排序/状态
  95. *
  96. * @param $id
  97. * @return array
  98. */
  99. public function actionAjaxUpdate($id)
  100. {
  101. if (!($model = $this->modelClass::findOne($id))) {
  102. return ResultHelper::json(404, '找不到数据');
  103. }
  104. $model->attributes = ArrayHelper::filter(Yii::$app->request->get(), ['sort', 'status']);
  105. if (!$model->save()) {
  106. return ResultHelper::json(422, $this->getError($model));
  107. }
  108. return ResultHelper::json(200, '修改成功');
  109. }
  110. /**
  111. * ajax编辑/创建
  112. *
  113. * @return mixed|string|\yii\web\Response
  114. * @throws \yii\base\ExitException
  115. */
  116. public function actionAjaxEdit()
  117. {
  118. $id = Yii::$app->request->get('id');
  119. $model = $this->findModel($id);
  120. // ajax 校验
  121. $this->activeFormValidate($model);
  122. if ($model->load(Yii::$app->request->post())) {
  123. return $model->save()
  124. ? $this->redirect(Yii::$app->request->referrer)
  125. : $this->message($this->getError($model), $this->redirect(Yii::$app->request->referrer), 'error');
  126. }
  127. return $this->renderAjax($this->action->id, [
  128. 'model' => $model,
  129. ]);
  130. }
  131. /**
  132. * 返回模型
  133. *
  134. * @param $id
  135. * @return \yii\db\ActiveRecord
  136. */
  137. protected function findModel($id)
  138. {
  139. /* @var $model \yii\db\ActiveRecord */
  140. if (empty($id) || empty($model = $this->modelClass::findOne($id))) {
  141. $model = new $this->modelClass;
  142. return $model->loadDefaultValues();
  143. }
  144. return $model;
  145. }
  146. }
粤ICP备19079148号