| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- <?php
- /**
- * Created by PhpStorm.
- * User: chenkuan
- * Date: 2017/12/19
- * Time: 上午10:21
- */
- namespace frontend\controllers;
- use backend\helpers\PaginationHelper;
- use backend\helpers\ValidatorHelper;
- use frontend\models\Role;
- class RoleController extends BaseController
- {
- /**
- * 获取角色管理列表
- */
- public function actionGetRoleList()
- {
- $request = \Yii::$app->getRequest()->get();
- $data['pageNumber'] = isset($request['pageNumber']) ? (int)$request['pageNumber'] : 1;
- $data['pageSize'] = isset($request['pageSize']) ? (int)$request['pageSize'] : 20;
- $data['orderBy'] = isset($request['orderBy']) ? $request['orderBy'] : 'id';
- $data['order'] = isset($request['order']) ? $request['order'] : 'asc';
- $data['keyword'] = isset($request['keyword']) ? $request['keyword'] : '';
- $data['searchBy'] = isset($request['searchBy']) ? $request['searchBy'] : 'name';
- $data = ValidatorHelper::validateData($data, [
- ['pageNumber', 'integer', 'min' => 1],
- ['pageSize', 'integer', 'min' => 1],
- ['orderBy', 'string'],
- ['order', 'string'],
- ['keyword', 'string'],
- ], $errors);
- if ($data == false) {
- return $this->outJson(0, '', $errors);
- }
- $query = Role::find()->orderBy($data['orderBy'].' '.$data['order']);
- if ($data['searchBy'] == 'name' && !empty($data['keyword'])) {
- $query->andFilterWhere(['like', 'name', $data['keyword']]);
- }
- $result = PaginationHelper::queryPage($query, $data['pageNumber'], $data['pageSize']);
- return $this->outJson(1, $result);
- }
- /**
- * 保存/新增
- */
- public function actionSaveRole()
- {
- $data = \Yii::$app->getRequest()->post();
- $data['authorityList'] = !empty($data['authorityList']) ? $data['authorityList'] : [];
- if (empty($data['id'])) {
- $model = new Role();
- $model->create_date = date('Y-m-d H:i:s', time());
- array_push($data['authorityList'], 'ROLE_BASE');
- } else {
- $model = Role::findOne(['id' => $data['id']]);
- if ($model->is_system && $model->id == 1) {
- return $this->outJson(0, [], '系统内置角色不允许修改!');
- }
- $model->modify_date = date('Y-m-d H:i:s', time());
- if (!in_array('ROLE_BASE', json_decode($model->authority_list_store, true))) {
- array_push($data['authorityList'], 'ROLE_BASE');
- }
- }
- $authority_list_store = json_encode($data['authorityList']);
- $model->is_system = 0;
- $model->authority_list_store = $authority_list_store;
- $model->setAttributes($data);
- if ($model->save()) {
- return $this->outJson(1, $model->id);
- } else {
- return $this->outJson(0, [], '保存失败');
- }
- }
- /**
- * 获取详情
- */
- public function actionGetRoleDetail()
- {
- $id = \Yii::$app->getRequest()->get('id', '');
- $rs = Role::find()->where(['id' => $id])->asArray()->one();
- if ($rs) {
- $rs['authority_list_store'] = json_decode($rs['authority_list_store'], true);
- }
- return $this->outJson(1, $rs);
- }
- /**
- * 删除
- */
- public function actionDeleteRole()
- {
- $ids = \Yii::$app->getRequest()->post('ids', '');
- if ($ids && !in_array(1, $ids)) {
- $rs = Role::deleteAll(['in', 'id', $ids]);
- return $this->outJson(1, $rs);
- } else {
- return $this->outJson(0, [], '系统内置角色不允许删除');
- }
- }
- /**
- * 新增用户,权限组列表的获取
- */
- public function actionGetRoleListForAdmin()
- {
- $list = Role::find()->select('id,name')->asArray()->all();
- if ($list) {
- return $this->outJson(1, $list);
- } else {
- return $this->outJson(0, [], '获取失败');
- }
- }
- }
|