ManagerController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace backend\controllers;
  3. use Yii;
  4. use common\enums\AppEnum;
  5. use common\traits\Curd;
  6. use common\enums\MemberTypeEnum;
  7. use common\enums\StatusEnum;
  8. use common\models\member\Member;
  9. use common\models\base\SearchModel;
  10. use common\forms\ManagerMemberForm;
  11. use common\helpers\ResultHelper;
  12. use backend\forms\ManagerRoleForm;
  13. use backend\forms\ManagerCreateForm;
  14. use backend\forms\ManagerUpdatePasswordForm;
  15. /**
  16. * Class ManagerController
  17. * @package backend\controllers
  18. * @author jianyan74 <751393839@qq.com>
  19. */
  20. class ManagerController extends BaseController
  21. {
  22. use Curd;
  23. /**
  24. * @var ManagerMemberForm
  25. */
  26. public $modelClass = ManagerMemberForm::class;
  27. /**
  28. * 首页
  29. *
  30. * @return string
  31. * @throws \yii\web\NotFoundHttpException
  32. */
  33. public function actionIndex()
  34. {
  35. // 获取当前用户权限的下面的所有用户id,除超级管理员
  36. $memberIds = Yii::$app->services->rbacAuthAssignment->getChildIds(AppEnum::BACKEND);
  37. $searchModel = new SearchModel([
  38. 'model' => Member::class,
  39. 'scenario' => 'default',
  40. 'partialMatchAttributes' => ['username'], // 模糊查询
  41. 'defaultOrder' => [
  42. 'id' => SORT_ASC,
  43. ],
  44. 'pageSize' => $this->pageSize,
  45. ]);
  46. $dataProvider = $searchModel
  47. ->search(Yii::$app->request->queryParams);
  48. $dataProvider->query
  49. ->andWhere(['>=', 'status', StatusEnum::DISABLED])
  50. ->andWhere(['type' => MemberTypeEnum::MANAGER])
  51. ->andFilterWhere(['in', 'id', $memberIds])
  52. ->with(['assignment.role']);
  53. return $this->render($this->action->id, [
  54. 'dataProvider' => $dataProvider,
  55. 'searchModel' => $searchModel,
  56. ]);
  57. }
  58. /**
  59. * ajax编辑/创建
  60. *
  61. * @return mixed|string|\yii\web\Response
  62. * @throws \yii\base\ExitException
  63. */
  64. public function actionCreate()
  65. {
  66. $model = new ManagerCreateForm();
  67. $model->roles = Yii::$app->services->rbacAuthAssignment->getRoleChildMap(AppEnum::BACKEND);
  68. // ajax 校验
  69. $this->activeFormValidate($model);
  70. if ($model->load(Yii::$app->request->post())) {
  71. return $model->create()
  72. ? $this->redirect(Yii::$app->request->referrer)
  73. : $this->message($this->getError($model), $this->redirect(Yii::$app->request->referrer), 'error');
  74. }
  75. return $this->renderAjax($this->action->id, [
  76. 'model' => $model,
  77. ]);
  78. }
  79. /**
  80. * 编辑/创建
  81. *
  82. * @return mixed
  83. */
  84. public function actionEdit()
  85. {
  86. $id = Yii::$app->request->get('id', null);
  87. $model = $this->findModel($id);
  88. if ($model->load(Yii::$app->request->post())) {
  89. if ($model->save()) {
  90. return ResultHelper::json(200, 'ok');
  91. }
  92. return ResultHelper::json(422, $this->getError($model));
  93. }
  94. return $this->render($this->action->id, [
  95. 'model' => $model,
  96. ]);
  97. }
  98. /**
  99. * ajax编辑/创建
  100. *
  101. * @return mixed|string|\yii\web\Response
  102. * @throws \yii\base\ExitException
  103. */
  104. public function actionUpdatePassword($id)
  105. {
  106. $manager = $this->findModel($id);
  107. $model = new ManagerUpdatePasswordForm();
  108. $model->manager = $manager;
  109. $model->username = $manager->username;
  110. // ajax 校验
  111. $this->activeFormValidate($model);
  112. if ($model->load(Yii::$app->request->post())) {
  113. return $model->save()
  114. ? $this->redirect(Yii::$app->request->referrer)
  115. : $this->message($this->getError($model), $this->redirect(Yii::$app->request->referrer), 'error');
  116. }
  117. return $this->renderAjax($this->action->id, [
  118. 'model' => $model,
  119. ]);
  120. }
  121. /**
  122. * @param $id
  123. * @return mixed|string|\yii\web\Response
  124. * @throws \yii\base\ExitException
  125. * @throws \yii\web\UnauthorizedHttpException
  126. */
  127. public function actionRole($id)
  128. {
  129. $model = new ManagerRoleForm();
  130. $model->id = $id;
  131. $model->roles = Yii::$app->services->rbacAuthAssignment->getRoleChildMap(AppEnum::BACKEND);
  132. $this->activeFormValidate($model);
  133. if ($model->load(Yii::$app->request->post())) {
  134. return $model->save()
  135. ? $this->redirect(['index'])
  136. : $this->message($this->getError($model), $this->redirect(['index']), 'error');
  137. }
  138. $model->role_ids = Yii::$app->services->rbacAuthAssignment->findRoleIdSByUserIdAndAppId($id, AppEnum::BACKEND);
  139. return $this->renderAjax($this->action->id, [
  140. 'id' => $id,
  141. 'model' => $model,
  142. ]);
  143. }
  144. }
粤ICP备19079148号