ExtendConfigTrait.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace common\traits;
  3. use Yii;
  4. use common\enums\StatusEnum;
  5. use common\enums\ExtendConfigNameEnum;
  6. use common\enums\ExtendConfigTypeEnum;
  7. use common\models\base\SearchModel;
  8. use common\models\extend\Config;
  9. use common\helpers\ArrayHelper;
  10. use common\helpers\ResultHelper;
  11. /**
  12. * 扩展配置
  13. *
  14. * Trait ExtendConfigTrait
  15. * @package common\traits
  16. */
  17. trait ExtendConfigTrait
  18. {
  19. /**
  20. * @return mixed
  21. * @throws \yii\web\NotFoundHttpException
  22. */
  23. public function actionIndex()
  24. {
  25. $searchModel = new SearchModel([
  26. 'model' => Config::class,
  27. 'scenario' => 'default',
  28. 'partialMatchAttributes' => ['title'], // 模糊查询
  29. 'defaultOrder' => [
  30. 'id' => SORT_DESC,
  31. ],
  32. 'pageSize' => $this->pageSize,
  33. ]);
  34. $dataProvider = $searchModel
  35. ->search(Yii::$app->request->queryParams);
  36. $dataProvider->query
  37. ->andWhere(['type' => $this->type])
  38. ->andWhere(['>=', 'status', StatusEnum::DISABLED])
  39. ->andWhere(['merchant_id' => Yii::$app->services->merchant->getNotNullId()]);
  40. return $this->render('@backend/modules/extend/views/config/index', [
  41. 'dataProvider' => $dataProvider,
  42. 'searchModel' => $searchModel,
  43. 'nameMap' => ExtendConfigNameEnum::getGroupValue($this->type),
  44. 'title' => ExtendConfigTypeEnum::getValue($this->type)
  45. ]);
  46. }
  47. /**
  48. * 编辑/创建
  49. *
  50. * @return mixed
  51. */
  52. public function actionEdit()
  53. {
  54. $id = Yii::$app->request->get('id', null);
  55. $name = Yii::$app->request->get('name');
  56. $model = $this->findModel($id);
  57. $model->type = $this->type;
  58. $model->name = $name;
  59. $dataModel = ExtendConfigNameEnum::getModelValue($name);
  60. $dataModel->attributes = $model->data;
  61. if (
  62. $dataModel->load(Yii::$app->request->post()) &&
  63. $model->load(Yii::$app->request->post()) &&
  64. $model->validate()
  65. ) {
  66. $model->data = ArrayHelper::toArray($dataModel);
  67. $model->save();
  68. return $this->referrer();
  69. }
  70. return $this->render('@backend/modules/extend/views/config/edit', [
  71. 'model' => $model,
  72. 'dataModel' => $dataModel,
  73. ]);
  74. }
  75. /**
  76. * 删除
  77. *
  78. * @param $id
  79. * @return mixed
  80. * @throws \Throwable
  81. * @throws \yii\db\StaleObjectException
  82. */
  83. public function actionDelete($id)
  84. {
  85. if ($this->findModel($id)->delete()) {
  86. return $this->message("删除成功", $this->redirect(Yii::$app->request->referrer));
  87. }
  88. return $this->message("删除失败", $this->redirect(Yii::$app->request->referrer), 'error');
  89. }
  90. /**
  91. * 伪删除
  92. *
  93. * @param $id
  94. * @return mixed
  95. */
  96. public function actionDestroy($id)
  97. {
  98. if (!($model = $this->findModel($id))) {
  99. return $this->message("找不到数据", $this->redirect(Yii::$app->request->referrer), 'error');
  100. }
  101. $model->status = StatusEnum::DELETE;
  102. if ($model->save()) {
  103. return $this->message("删除成功", $this->redirect(Yii::$app->request->referrer));
  104. }
  105. return $this->message("删除失败", $this->redirect(Yii::$app->request->referrer), 'error');
  106. }
  107. /**
  108. * ajax更新排序/状态
  109. *
  110. * @param $id
  111. * @return array
  112. */
  113. public function actionAjaxUpdate($id)
  114. {
  115. if (!($model = Config::findOne($id))) {
  116. return ResultHelper::json(404, '找不到数据');
  117. }
  118. $model->attributes = ArrayHelper::filter(Yii::$app->request->get(), ['sort', 'status']);
  119. if (!$model->save()) {
  120. return ResultHelper::json(422, $this->getError($model));
  121. }
  122. return ResultHelper::json(200, '修改成功');
  123. }
  124. /**
  125. * 返回模型
  126. *
  127. * @param $id
  128. * @return \yii\db\ActiveRecord
  129. */
  130. protected function findModel($id)
  131. {
  132. /* @var $model \yii\db\ActiveRecord */
  133. if (empty($id) || empty(($model = Config::findOne(['id' => $id, 'merchant_id' => $this->getMerchantId()])))) {
  134. $model = new Config();
  135. return $model->loadDefaultValues();
  136. }
  137. return $model;
  138. }
  139. }
粤ICP备19079148号