request->isAjax) { $query = Role::find(); $data = []; $params = Yii::$app->request->get('Role'); $query = mergeParams($query,$params); $roleList = $query->all(); foreach($roleList as $role) { $data[] = array('role_id'=>$role->role_id,'role_name'=>$role->role_name,'description'=>$role->description,'disabledTxt'=>Resource::disableOptions($role->disabled),'disabled'=>$role->disabled); } echo_json($data); } $this->tableTitle = array( array('field'=>'role_id','title'=>Role::getAttributeName('role_id'),'align'=>'center','sortable'=>true,'class'=>'col-md-2'), array('field'=>'role_name','title'=>Role::getAttributeName('role_name'),'align'=>'center'), array('field'=>'description','title'=>Role::getAttributeName('description'),'align'=>'center'), array('field'=>'disabled','title'=>Role::getAttributeName('disabled'),'align'=>'center','formatter'=>'switchFormatter'), array('field'=>'operate','title'=>Role::getAttributeName('operate'),'align'=>'center','events'=>'window.operateEvents','formatter'=>'operateFormatter'), ); $this->tableConfig = array('table'=>Role::shortTableName(),'url'=>$this->createRealUrl(['admin/role/list']),'setFieldUrl'=>$this->createRealUrl(['admin/role/setfield']),'idField'=>Role::modelPrimaryKey(),'checkbox'=>0,'dropmenu'=>1,'pagination'=>false,'refresh'=>true); return $this->render('list',array('model'=>new Role())); } function actionAdd() { $model = new Role(); if(Yii::$app->request->isAjax&&$model->load(Yii::$app->request->post())){ if(!$model->validate()) { $msgdata = ['error' => 1,'msg' => $model->returnFirstError()]; } else { if($model->save()) { $msgdata = ['error' => 0,'msg' => '操作成功']; } else { $msgdata = ['error' => 1,'msg' => '操作失败']; } } echo_json($msgdata); } return $this->renderAjax('add',array('model'=>$model)); } function actionEdit() { $rold_id = $this->getKeyId('role_id'); $model = Role::findOne($rold_id); check_record_exists($model); if(Yii::$app->request->isAjax&&$model->load(Yii::$app->request->post())){ if(!$model->validate()) { $msgdata = ['error' => 1,'msg' => $model->returnFirstError()]; } else { if($model->save()) { $msgdata = ['error' => 0,'msg' => '操作成功']; } else { $msgdata = ['error' => 1,'msg' => '操作失败']; } } echo_json($msgdata); } return $this->renderAjax('add',array('model'=>$model)); } //权限设置 public function actionSettings() { $role_id = $this->getKeyId('role_id'); $model = Role::findOne($role_id); check_record_exists($model); if(Yii::$app->request->isAjax&&Yii::$app->request->post()) { RoleResource::deleteAll(['role_id'=>$role_id]); if(Yii::$app->request->post('innerIds')) { $innerIds = explode(",",Yii::$app->request->post('innerIds')); $rows = []; if(is_array($innerIds))foreach($innerIds as $innerId) { $rows[] = array('role_id'=>$role_id,'hash'=>$innerId); } if(!empty($rows)) { Yii::$app->db->createCommand()->batchInsert(RoleResource::tableName(), ['role_id','hash'], $rows)->execute(); } } $msgdata = ['error' => 0,'msg' => '操作成功']; echo_json($msgdata); } $hashs = RoleResource::find()->where(['role_id'=>$role_id])->select('hash')->column(); if (Yii::$app->request->isAjax&&Yii::$app->request->get('getdata')) { $data = []; $resultList = Resource::find()->where(['disabled'=>0])->orderBy(['parent_hash'=>SORT_ASC,'list_order'=>SORT_ASC])->all(); foreach($resultList as $resource) { $data[] = array('hash'=>$resource->hash,'parent_hash'=>$resource->parent_hash,'name'=>Yii::t('resource',$resource->name),'check'=>in_array($resource->hash,$hashs)?true:false); } echo_json($data); } $tableTitle = array( array('field'=>'hash','checkbox'=>false,'formatter'=>'checkboxFormatter','width'=>'20px'), array('field'=>'name','title'=>'菜单','align'=>'left'), ); $tableConfig = array('table'=>Resource::shortTableName(),'idField'=>Resource::modelPrimaryKey(),'url'=>$this->createRealUrl(['admin/role/settings','getdata'=>1,'role_id'=>$role_id]),'checkbox'=>1,'dropmenu'=>1,'tree'=>1,'parentIdField'=>'parent_hash','treeShowField'=>'name','expand'=>true,'refresh'=>true); return $this->renderAjax('settings',array('model'=>$model,'tableTitle'=>$tableTitle,'tableConfig'=>$tableConfig)); } //删除 public function actionDel() { $role_id = $this->getKeyId('role_id'); $model = Role::findOne($role_id); check_record_exists($model); if($model->delete()) { RoleResource::deleteAll(['role_id'=>$role_id]); $msgdata = ['error' => 0,'msg' => '操作成功!']; } else { $msgdata = ['error' => 1,'msg' => '操作失败!']; } echo_json($msgdata); } }