123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- <?php
- namespace Home\Controller;
- use Think\Controller;
- class RoleController extends ComController {//Com
- public function index(){
- $data=I();
- if(isset($data['list'])){
- $group=M('auth_group');
- $getpage=$data['pageCurrent'];//第几页
- $getsize=$data['pageSize'];//每页条数
- $page=($getpage-1)*$getsize;
- $map= array('type'=>'1');
- $pingyinid = $data['pinyin'];
- if(!empty($pingyinid)){
- $map['title']=$pingyinid;
- }
- $count= $group->where($map)->count(id);// 查询满足要求的总记录数
- $list = $group->field('id,type,title,level,(SELECT title FROM sp_auth_group a WHERE id=sp_auth_group.pid)as pid,sort,rules')->where($map)->limit($page,$getsize)->select();
- $info = array(
- 'totalRow' => $count,
- //'pageSize' => $count,//分页时不传
- 'list' => $list,
- );
- echo json_encode($info);
- }else{
- $this->display();
- }
- }
- public function add(){
- $group=M('auth_group');
- $data=I();
- if(isset($data['type'])){
- $rules = array(
- array('title','','角色名称已存在!',0,'unique'),
- array('pid','require','父角色不为空!'),
- array('title','require','角色名称不为空!'),
- );
- if (!$group->validate($rules)->create()){
- echo json_encode(array('statusCode'=>'300','message'=> $group->getError()));
-
- }else{
- $type=$data['type'];
- $title=$data['title'];
- $sort=$data['sort'];
- $pidname=$data['pid'];
- $repid = $group->field('id,level')->where(array('title'=> $pidname))->select();
- $pid=$repid[0]['level']+1;
- if($pidname=='顶级'){
- $repid[0]['id']=0;
- $pid=1;
- }
- $add=array(
- 'type'=> $type,
- 'pid'=> $repid[0]['id'],
- 'title'=> $title,
- 'sort'=> $sort,
- 'level'=> $pid
- );
- $res=$group->add($add);
- if($res)
- echo json_encode(array('statusCode'=>200,'message'=>'添加成功' ));
- else
- echo json_encode(array('statusCode'=>300,'message'=>'添加失败' ));
- }
- }else{
- $list = $group->field('id,type,title,level,pid,sort,rules')->where('type=1')->select();
- $this->assign('role',$list);
- $this->assign('action','/home/Role/add');
- $this->display();
- }
- }
- public function edit(){
- $group=M('auth_group');
- $data=I();
- if(isset($data['type'])){
- $roletitle=$group->field('id')->where('type=1 and title="'.$data['title'].'" and id!='.$data['roleid'])->select();
- if(count($roletitle) > 0){
- exit(json_encode(array('statusCode'=>300,'message'=>'角色已存在!' )));
- }
- $rules = array(
- array('pid','require','父角色不为空!'),
- array('title','require','角色名称不为空!'),
- );
- if (!$group->validate($rules)->create()){
- echo json_encode(array('statusCode'=>'300','message'=> $group->getError()));
- }else{
- $type=$data['type'];
- $title=$data['title'];
- $sort=$data['sort'];
- $pidname=$data['pid'];
- $repid = $group->field('id,level')->where(array('title'=> $pidname))->select();
- $pid=$repid[0]['level']+1;
- if($pidname=='顶级'){
- $repid[0]['id']=0;
- $pid=1;
- }
- $add=array(
- 'id'=> $data['roleid'],
- 'type'=> $type,
- 'pid'=> $repid[0]['id'],
- 'title'=> $title,
- 'sort'=> $sort,
- 'level'=> $pid
- );
- $res=$group->save($add);
- if($res)
- echo json_encode(array('statusCode'=>200,'message'=>'修改成功' ));
- else
- echo json_encode(array('statusCode'=>300,'message'=>'修改失败' ));
- }
- }else{
- $list = $group->field('id,type,title,level,pid,sort,rules')->where('type=1')->select();
- $this->assign('role',$list);
- $res=$group->where(array('id'=> $data['id']))->select();
- $this->assign('roleid',$data['id']);
- $this->assign('title',$res[0]['title']);
- $this->assign('sort',$res[0]['sort']);
- $pidarr=$group->field('title')->where(array('id'=> $res[0]['pid']))->select();
- if($pidarr[0]['title']==''){$pidarr[0]['title']='顶级';}
- $this->assign('pid',$pidarr[0]['title']);
- $this->assign('action','/home/Role/edit');
- $this->display('edit');
- }
- }
- public function perm(){
- $data=I();
- $group=M('auth_rule');
- if(!isset($data['list'])){//查询权限菜单
- $count= $group->count(id);// 查询满足要求的总记录数
- $where = $group->query("SELECT rules from sp_auth_group WHERE id =(SELECT posname FROM sp_user WHERE username='".$_SESSION['zt_username']."')");
- $wheres = substr($where[0]['rules'],0,strlen($where[0]['rules'])-1);
- $wheres=' where p.id in('.$wheres.')';
- if($_SESSION['zt_username']=='admin'){
- $wheres='';
- }
- $list = $group->query('SELECT p.id,p.title,p.pid,p.cid,p.cpid,p.ctitle,p.allid FROM V_PERM02 AS p '.$wheres);
- $info = array(
- 'totalRow' => $count,
- 'pageSize' => $count,//分页时不传
- 'list' => $list,
- );
- //$_SESSION['CCC']=$count;
- //$_SESSION['DDD']=print_r($list,true);
- echo json_encode($info);
- }elseif($data['list']=='get2'){//菜单功能
- }elseif($data['list']=='set2'){//修改
- $data=I();
- $perms='';
- foreach($data as $key => $value){
- if(substr($key,0,9) == "checkbox_"){
- $perms.=substr($key,9).',';
- }
- }
- $rules=M('auth_group');
- $set=$rules->save(array('id'=> $data['permid'],'rules'=> $perms,'dataperm'=>$data['dataperm']));
- if($set)
- echo json_encode(array('statusCode'=>200,'message'=> '保存成功'));
- else
- echo json_encode(array('statusCode'=>300,'message'=> '保存失败'));
- }else{
- $sel=$group->query("SELECT rules,dataperm FROM sp_auth_group WHERE id=".$data['id']);//是否有权限
- $v=explode(',',$sel[0]['rules']);
- $this->assign('pers',json_encode(array_filter($v)));
- $this->assign('permid',$data['id']);
- $this->assign('dataperm',$sel[0]['dataperm']);
- $this->assign('action','/home/Role/perm');
- $this->display('perm');
- }
- }
- public function del(){
- $group=M('auth_group');
- $data=I();
- $yes=M('auth_group_access')->where('group_id='.$data['id'])->count();
- if($yes > 0){
- $this->mtReturn(300,'删除失败,请却认当前角色无用户存在!');
- }
- $res=$group->where('id='.$data['id'])->delete();
- if($res)
- $this->mtReturn(200,'删除成功'.$data['id'] );
- else
- $this->mtReturn(300,'删除失败' );
- }
-
- }
|