ConfigController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace backend\modules\common\controllers;
  3. use Yii;
  4. use yii\web\NotFoundHttpException;
  5. use common\enums\StatusEnum;
  6. use common\models\base\SearchModel;
  7. use common\models\common\Config;
  8. use common\helpers\ResultHelper;
  9. use common\traits\Curd;
  10. use common\enums\ConfigTypeEnum;
  11. use common\enums\AppEnum;
  12. use backend\controllers\BaseController;
  13. /**
  14. * Class ConfigController
  15. * @package backend\modules\common\controllers
  16. */
  17. class ConfigController extends BaseController
  18. {
  19. use Curd;
  20. /**
  21. * @var Config
  22. */
  23. public $modelClass = Config::class;
  24. /**
  25. * 首页
  26. *
  27. * @return string
  28. * @throws NotFoundHttpException
  29. */
  30. public function actionIndex()
  31. {
  32. $searchModel = new SearchModel([
  33. 'model' => $this->modelClass,
  34. 'scenario' => 'default',
  35. 'partialMatchAttributes' => ['title', 'name'], // 模糊查询
  36. 'defaultOrder' => [
  37. 'cate_id' => SORT_ASC,
  38. 'sort' => SORT_ASC,
  39. ],
  40. 'pageSize' => $this->pageSize
  41. ]);
  42. $dataProvider = $searchModel
  43. ->search(Yii::$app->request->queryParams);
  44. $dataProvider->query
  45. ->andWhere(['app_id' => AppEnum::BACKEND])
  46. ->andWhere(['>=', 'status', StatusEnum::DISABLED]);
  47. return $this->render($this->action->id, [
  48. 'dataProvider' => $dataProvider,
  49. 'searchModel' => $searchModel,
  50. 'cateDropDownList' => Yii::$app->services->configCate->getDropDown(AppEnum::BACKEND)
  51. ]);
  52. }
  53. /**
  54. * 编辑/创建
  55. *
  56. * @return mixed|string|\yii\web\Response
  57. * @throws \yii\base\ExitException
  58. */
  59. public function actionAjaxEdit()
  60. {
  61. $id = Yii::$app->request->get('id');
  62. $model = $this->findModel($id);
  63. $model->app_id = AppEnum::BACKEND;
  64. // ajax 校验
  65. $this->activeFormValidate($model);
  66. if ($model->load(Yii::$app->request->post())) {
  67. return $model->save()
  68. ? $this->redirect(Yii::$app->request->referrer)
  69. : $this->message($this->getError($model), $this->redirect(Yii::$app->request->referrer), 'error');
  70. }
  71. return $this->renderAjax($this->action->id, [
  72. 'model' => $model,
  73. 'configTypeList' => ConfigTypeEnum::getMap(),
  74. 'cateDropDownList' => Yii::$app->services->configCate->getDropDown(AppEnum::BACKEND)
  75. ]);
  76. }
  77. /**
  78. * 网站设置
  79. *
  80. * @return string
  81. */
  82. public function actionEditAll()
  83. {
  84. return $this->render($this->action->id, [
  85. 'cates' => Yii::$app->services->configCate->getItemsMergeForConfig(AppEnum::BACKEND)
  86. ]);
  87. }
  88. /**
  89. * ajax批量更新数据
  90. *
  91. * @return array
  92. * @throws NotFoundHttpException
  93. * @throws \yii\base\InvalidConfigException
  94. */
  95. public function actionUpdateInfo()
  96. {
  97. if (Yii::$app->request->isAjax) {
  98. $config = Yii::$app->request->post('config', []);
  99. Yii::$app->services->config->updateAll(Yii::$app->id, 0, $config);
  100. return ResultHelper::json(200, "修改成功");
  101. }
  102. throw new NotFoundHttpException('请求出错!');
  103. }
  104. }
粤ICP备19079148号