AuthItemTrait.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace common\traits;
  3. use Yii;
  4. use yii\base\InvalidConfigException;
  5. use yii\data\ActiveDataProvider;
  6. use common\enums\StatusEnum;
  7. use common\enums\WhetherEnum;
  8. use common\models\rbac\AuthItem;
  9. use common\helpers\ArrayHelper;
  10. use common\helpers\ResultHelper;
  11. /**
  12. * Trait AuthItemTrait
  13. * @package common\traits
  14. * @property \yii\db\ActiveRecord|\yii\base\Model $modelClass
  15. * @property string $appId 应用id
  16. * @property string $viewPrefix 加载视图
  17. * @author jianyan74 <751393839@qq.com>
  18. */
  19. trait AuthItemTrait
  20. {
  21. /**
  22. * @throws InvalidConfigException
  23. */
  24. public function init()
  25. {
  26. parent::init();
  27. if ($this->modelClass === null) {
  28. throw new InvalidConfigException('"modelClass" 属性必须设置.');
  29. }
  30. if ($this->appId === null) {
  31. throw new InvalidConfigException('"appId" 属性必须设置.');
  32. }
  33. if ($this->viewPrefix === null) {
  34. throw new InvalidConfigException('"viewPrefix" 属性必须设置.');
  35. }
  36. }
  37. /**
  38. * Lists all Tree models.
  39. * @return mixed
  40. */
  41. public function actionIndex()
  42. {
  43. $query = $this->modelClass::find()
  44. ->where(['app_id' => $this->appId, 'is_addon' => WhetherEnum::DISABLED])
  45. ->andWhere(['>=', 'status', StatusEnum::DISABLED])
  46. ->orderBy('sort asc, created_at asc');
  47. $dataProvider = new ActiveDataProvider([
  48. 'query' => $query,
  49. 'pagination' => false
  50. ]);
  51. return $this->render($this->viewPrefix . $this->action->id, [
  52. 'dataProvider' => $dataProvider
  53. ]);
  54. }
  55. /**
  56. * 编辑/创建
  57. *
  58. * @return mixed|string|\yii\web\Response
  59. * @throws \yii\base\ExitException
  60. */
  61. public function actionAjaxEdit()
  62. {
  63. $id = Yii::$app->request->get('id', '');
  64. /** @var AuthItem $model */
  65. $model = $this->findModel($id);
  66. $model->pid = Yii::$app->request->get('pid', null) ?? $model->pid; // 父id
  67. $model->app_id = $this->appId;
  68. $model->is_addon = WhetherEnum::DISABLED;
  69. // ajax 校验
  70. $this->activeFormValidate($model);
  71. if ($model->load(Yii::$app->request->post())) {
  72. return $model->save()
  73. ? $this->redirect(['index'])
  74. : $this->message($this->getError($model), $this->redirect(['index']), 'error');
  75. }
  76. return $this->renderAjax($this->viewPrefix . $this->action->id, [
  77. 'model' => $model,
  78. 'dropDownList' => Yii::$app->services->rbacAuthItem->getDropDownForEdit($this->appId, $id),
  79. ]);
  80. }
  81. /**
  82. * 删除
  83. *
  84. * @param $id
  85. * @return mixed
  86. * @throws \Throwable
  87. * @throws \yii\db\StaleObjectException
  88. */
  89. public function actionDelete($id)
  90. {
  91. if ($this->findModel($id)->delete()) {
  92. return $this->message("删除成功", $this->redirect(['index']));
  93. }
  94. return $this->message("删除失败", $this->redirect(['index']), 'error');
  95. }
  96. /**
  97. * ajax更新排序/状态
  98. *
  99. * @param $id
  100. * @return array
  101. */
  102. public function actionAjaxUpdate($id)
  103. {
  104. if (!($model = $this->modelClass::findOne($id))) {
  105. return ResultHelper::json(404, '找不到数据');
  106. }
  107. $model->attributes = ArrayHelper::filter(Yii::$app->request->get(), ['sort', 'status']);
  108. if (!$model->save()) {
  109. return ResultHelper::json(422, $this->getError($model));
  110. }
  111. return ResultHelper::json(200, '修改成功');
  112. }
  113. /**
  114. * 返回模型
  115. *
  116. * @param $id
  117. * @return \yii\db\ActiveRecord
  118. */
  119. protected function findModel($id)
  120. {
  121. /* @var $model \yii\db\ActiveRecord */
  122. if (empty($id) || empty(($model = $this->modelClass::find()->where(['id' => $id, 'app_id' => $this->appId])->one()))) {
  123. $model = new $this->modelClass;
  124. return $model->loadDefaultValues();
  125. }
  126. return $model;
  127. }
  128. }
粤ICP备19079148号