RoleController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: chenkuan
  5. * Date: 2017/12/19
  6. * Time: 上午10:21
  7. */
  8. namespace frontend\controllers;
  9. use backend\helpers\PaginationHelper;
  10. use backend\helpers\ValidatorHelper;
  11. use frontend\models\Role;
  12. class RoleController extends BaseController
  13. {
  14. /**
  15. * 获取角色管理列表
  16. */
  17. public function actionGetRoleList()
  18. {
  19. $request = \Yii::$app->getRequest()->get();
  20. $data['pageNumber'] = isset($request['pageNumber']) ? (int)$request['pageNumber'] : 1;
  21. $data['pageSize'] = isset($request['pageSize']) ? (int)$request['pageSize'] : 20;
  22. $data['orderBy'] = isset($request['orderBy']) ? $request['orderBy'] : 'id';
  23. $data['order'] = isset($request['order']) ? $request['order'] : 'asc';
  24. $data['keyword'] = isset($request['keyword']) ? $request['keyword'] : '';
  25. $data['searchBy'] = isset($request['searchBy']) ? $request['searchBy'] : 'name';
  26. $data = ValidatorHelper::validateData($data, [
  27. ['pageNumber', 'integer', 'min' => 1],
  28. ['pageSize', 'integer', 'min' => 1],
  29. ['orderBy', 'string'],
  30. ['order', 'string'],
  31. ['keyword', 'string'],
  32. ], $errors);
  33. if ($data == false) {
  34. return $this->outJson(0, '', $errors);
  35. }
  36. $query = Role::find()->orderBy($data['orderBy'].' '.$data['order']);
  37. if ($data['searchBy'] == 'name' && !empty($data['keyword'])) {
  38. $query->andFilterWhere(['like', 'name', $data['keyword']]);
  39. }
  40. $result = PaginationHelper::queryPage($query, $data['pageNumber'], $data['pageSize']);
  41. return $this->outJson(1, $result);
  42. }
  43. /**
  44. * 保存/新增
  45. */
  46. public function actionSaveRole()
  47. {
  48. $data = \Yii::$app->getRequest()->post();
  49. $data['authorityList'] = !empty($data['authorityList']) ? $data['authorityList'] : [];
  50. if (empty($data['id'])) {
  51. $model = new Role();
  52. $model->create_date = date('Y-m-d H:i:s', time());
  53. array_push($data['authorityList'], 'ROLE_BASE');
  54. } else {
  55. $model = Role::findOne(['id' => $data['id']]);
  56. if ($model->is_system && $model->id == 1) {
  57. return $this->outJson(0, [], '系统内置角色不允许修改!');
  58. }
  59. $model->modify_date = date('Y-m-d H:i:s', time());
  60. if (!in_array('ROLE_BASE', json_decode($model->authority_list_store, true))) {
  61. array_push($data['authorityList'], 'ROLE_BASE');
  62. }
  63. }
  64. $authority_list_store = json_encode($data['authorityList']);
  65. $model->is_system = 0;
  66. $model->authority_list_store = $authority_list_store;
  67. $model->setAttributes($data);
  68. if ($model->save()) {
  69. return $this->outJson(1, $model->id);
  70. } else {
  71. return $this->outJson(0, [], '保存失败');
  72. }
  73. }
  74. /**
  75. * 获取详情
  76. */
  77. public function actionGetRoleDetail()
  78. {
  79. $id = \Yii::$app->getRequest()->get('id', '');
  80. $rs = Role::find()->where(['id' => $id])->asArray()->one();
  81. if ($rs) {
  82. $rs['authority_list_store'] = json_decode($rs['authority_list_store'], true);
  83. }
  84. return $this->outJson(1, $rs);
  85. }
  86. /**
  87. * 删除
  88. */
  89. public function actionDeleteRole()
  90. {
  91. $ids = \Yii::$app->getRequest()->post('ids', '');
  92. if ($ids && !in_array(1, $ids)) {
  93. $rs = Role::deleteAll(['in', 'id', $ids]);
  94. return $this->outJson(1, $rs);
  95. } else {
  96. return $this->outJson(0, [], '系统内置角色不允许删除');
  97. }
  98. }
  99. /**
  100. * 新增用户,权限组列表的获取
  101. */
  102. public function actionGetRoleListForAdmin()
  103. {
  104. $list = Role::find()->select('id,name')->asArray()->all();
  105. if ($list) {
  106. return $this->outJson(1, $list);
  107. } else {
  108. return $this->outJson(0, [], '获取失败');
  109. }
  110. }
  111. }