MerchantCurd.php 4.4 KB

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