MenuController.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace backend\modules\common\controllers;
  3. use Yii;
  4. use yii\data\ActiveDataProvider;
  5. use common\enums\AppEnum;
  6. use common\traits\Curd;
  7. use common\models\common\Menu;
  8. use common\helpers\ResultHelper;
  9. use backend\controllers\BaseController;
  10. /**
  11. * Class MenuController
  12. * @package backend\modules\common\controllers
  13. */
  14. class MenuController extends BaseController
  15. {
  16. use Curd;
  17. /**
  18. * @var \yii\db\ActiveRecord
  19. */
  20. public $modelClass = Menu::class;
  21. /**
  22. * @return string
  23. */
  24. public function actionIndex()
  25. {
  26. $cateId = Yii::$app->request->get('cate_id', Yii::$app->services->menuCate->findFirstId(AppEnum::BACKEND));
  27. $query = $this->modelClass::find()
  28. ->orderBy('sort asc, id asc')
  29. ->filterWhere(['cate_id' => $cateId])
  30. ->andWhere(['app_id' => AppEnum::BACKEND]);
  31. $dataProvider = new ActiveDataProvider([
  32. 'query' => $query,
  33. 'pagination' => false
  34. ]);
  35. return $this->render('index', [
  36. 'dataProvider' => $dataProvider,
  37. 'cates' => Yii::$app->services->menuCate->findDefault(AppEnum::BACKEND),
  38. 'cateId' => $cateId,
  39. ]);
  40. }
  41. /**
  42. * 编辑/创建
  43. *
  44. * @return mixed
  45. */
  46. public function actionEdit()
  47. {
  48. $id = Yii::$app->request->get('id', null);
  49. $model = $this->findModel($id);
  50. $model->pid = Yii::$app->request->get('pid', null) ?? $model->pid; // 父id
  51. $model->cate_id = Yii::$app->request->get('cate_id', null) ?? $model->cate_id; // 分类id
  52. $model->app_id = AppEnum::BACKEND;
  53. if ($model->load(Yii::$app->request->post())) {
  54. if (!$model->save()) {
  55. return ResultHelper::json(422, $this->getError($model));
  56. }
  57. return ResultHelper::json(200, '保存成功');
  58. }
  59. $menuCate = Yii::$app->services->menuCate->findById($model->cate_id);
  60. $this->layout = '@backend/views/layouts/blank';
  61. return $this->render('edit', [
  62. 'model' => $model,
  63. 'cates' => Yii::$app->services->menuCate->getDefaultMap(AppEnum::BACKEND),
  64. 'menuDropDownList' => Yii::$app->services->menu->getDropDown($menuCate, AppEnum::BACKEND, $id),
  65. ]);
  66. }
  67. /**
  68. * 删除
  69. *
  70. * @param $id
  71. * @return mixed
  72. * @throws \Throwable
  73. * @throws \yii\db\StaleObjectException
  74. */
  75. public function actionDelete($id)
  76. {
  77. if (($model = $this->findModel($id))->delete()) {
  78. return $this->message("删除成功", $this->redirect(['index', 'cate_id' => $model->cate_id]));
  79. }
  80. return $this->message("删除失败", $this->redirect(['index', 'cate_id' => $model->cate_id]), 'error');
  81. }
  82. }
粤ICP备19079148号