RoleController.class.php.bak.20200601 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace Home\Controller;
  3. use Think\Controller;
  4. class RoleController extends ComController {//Com
  5. public function index(){
  6. $data=I();
  7. if(isset($data['list'])){
  8. $group=M('auth_group');
  9. $getpage=$data['pageCurrent'];//第几页
  10. $getsize=$data['pageSize'];//每页条数
  11. $page=($getpage-1)*$getsize;
  12. $map= array('type'=>'1');
  13. $pingyinid = $data['pinyin'];
  14. if(!empty($pingyinid)){
  15. $map['title']=$pingyinid;
  16. }
  17. $count= $group->where($map)->count(id);// 查询满足要求的总记录数
  18. $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();
  19. $info = array(
  20. 'totalRow' => $count,
  21. //'pageSize' => $count,//分页时不传
  22. 'list' => $list,
  23. );
  24. echo json_encode($info);
  25. }else{
  26. $this->display();
  27. }
  28. }
  29. public function add(){
  30. $group=M('auth_group');
  31. $data=I();
  32. if(isset($data['type'])){
  33. $rules = array(
  34. array('title','','角色名称已存在!',0,'unique'),
  35. array('pid','require','父角色不为空!'),
  36. array('title','require','角色名称不为空!'),
  37. );
  38. if (!$group->validate($rules)->create()){
  39. echo json_encode(array('statusCode'=>'300','message'=> $group->getError()));
  40. }else{
  41. $type=$data['type'];
  42. $title=$data['title'];
  43. $sort=$data['sort'];
  44. $pidname=$data['pid'];
  45. $repid = $group->field('id,level')->where(array('title'=> $pidname))->select();
  46. $pid=$repid[0]['level']+1;
  47. if($pidname=='顶级'){
  48. $repid[0]['id']=0;
  49. $pid=1;
  50. }
  51. $add=array(
  52. 'type'=> $type,
  53. 'pid'=> $repid[0]['id'],
  54. 'title'=> $title,
  55. 'sort'=> $sort,
  56. 'level'=> $pid
  57. );
  58. $res=$group->add($add);
  59. if($res)
  60. echo json_encode(array('statusCode'=>200,'message'=>'添加成功' ));
  61. else
  62. echo json_encode(array('statusCode'=>300,'message'=>'添加失败' ));
  63. }
  64. }else{
  65. $list = $group->field('id,type,title,level,pid,sort,rules')->where('type=1')->select();
  66. $this->assign('role',$list);
  67. $this->assign('action','/home/Role/add');
  68. $this->display();
  69. }
  70. }
  71. public function edit(){
  72. $group=M('auth_group');
  73. $data=I();
  74. if(isset($data['type'])){
  75. $roletitle=$group->field('id')->where('type=1 and title="'.$data['title'].'" and id!='.$data['roleid'])->select();
  76. if(count($roletitle) > 0){
  77. exit(json_encode(array('statusCode'=>300,'message'=>'角色已存在!' )));
  78. }
  79. $rules = array(
  80. array('pid','require','父角色不为空!'),
  81. array('title','require','角色名称不为空!'),
  82. );
  83. if (!$group->validate($rules)->create()){
  84. echo json_encode(array('statusCode'=>'300','message'=> $group->getError()));
  85. }else{
  86. $type=$data['type'];
  87. $title=$data['title'];
  88. $sort=$data['sort'];
  89. $pidname=$data['pid'];
  90. $repid = $group->field('id,level')->where(array('title'=> $pidname))->select();
  91. $pid=$repid[0]['level']+1;
  92. if($pidname=='顶级'){
  93. $repid[0]['id']=0;
  94. $pid=1;
  95. }
  96. $add=array(
  97. 'id'=> $data['roleid'],
  98. 'type'=> $type,
  99. 'pid'=> $repid[0]['id'],
  100. 'title'=> $title,
  101. 'sort'=> $sort,
  102. 'level'=> $pid
  103. );
  104. $res=$group->save($add);
  105. if($res)
  106. echo json_encode(array('statusCode'=>200,'message'=>'修改成功' ));
  107. else
  108. echo json_encode(array('statusCode'=>300,'message'=>'修改失败' ));
  109. }
  110. }else{
  111. $list = $group->field('id,type,title,level,pid,sort,rules')->where('type=1')->select();
  112. $this->assign('role',$list);
  113. $res=$group->where(array('id'=> $data['id']))->select();
  114. $this->assign('roleid',$data['id']);
  115. $this->assign('title',$res[0]['title']);
  116. $this->assign('sort',$res[0]['sort']);
  117. $pidarr=$group->field('title')->where(array('id'=> $res[0]['pid']))->select();
  118. if($pidarr[0]['title']==''){$pidarr[0]['title']='顶级';}
  119. $this->assign('pid',$pidarr[0]['title']);
  120. $this->assign('action','/home/Role/edit');
  121. $this->display('edit');
  122. }
  123. }
  124. public function perm(){
  125. $data=I();
  126. $group=M('auth_rule');
  127. if(!isset($data['list'])){//查询权限菜单
  128. $count= $group->count(id);// 查询满足要求的总记录数
  129. $where = $group->query("SELECT rules from sp_auth_group WHERE id =(SELECT posname FROM sp_user WHERE username='".$_SESSION['zt_username']."')");
  130. $wheres = substr($where[0]['rules'],0,strlen($where[0]['rules'])-1);
  131. $wheres=' where p.id in('.$wheres.')';
  132. if($_SESSION['zt_username']=='admin'){
  133. $wheres='';
  134. }
  135. $list = $group->query('SELECT p.id,p.title,p.pid,p.cid,p.cpid,p.ctitle,p.allid FROM V_PERM02 AS p '.$wheres);
  136. $info = array(
  137. 'totalRow' => $count,
  138. 'pageSize' => $count,//分页时不传
  139. 'list' => $list,
  140. );
  141. //$_SESSION['CCC']=$count;
  142. //$_SESSION['DDD']=print_r($list,true);
  143. echo json_encode($info);
  144. }elseif($data['list']=='get2'){//菜单功能
  145. }elseif($data['list']=='set2'){//修改
  146. $data=I();
  147. $perms='';
  148. foreach($data as $key => $value){
  149. if(substr($key,0,9) == "checkbox_"){
  150. $perms.=substr($key,9).',';
  151. }
  152. }
  153. $rules=M('auth_group');
  154. $set=$rules->save(array('id'=> $data['permid'],'rules'=> $perms,'dataperm'=>$data['dataperm']));
  155. if($set)
  156. echo json_encode(array('statusCode'=>200,'message'=> '保存成功'));
  157. else
  158. echo json_encode(array('statusCode'=>300,'message'=> '保存失败'));
  159. }else{
  160. $sel=$group->query("SELECT rules,dataperm FROM sp_auth_group WHERE id=".$data['id']);//是否有权限
  161. $v=explode(',',$sel[0]['rules']);
  162. $this->assign('pers',json_encode(array_filter($v)));
  163. $this->assign('permid',$data['id']);
  164. $this->assign('dataperm',$sel[0]['dataperm']);
  165. $this->assign('action','/home/Role/perm');
  166. $this->display('perm');
  167. }
  168. }
  169. public function del(){
  170. $group=M('auth_group');
  171. $data=I();
  172. $yes=M('auth_group_access')->where('group_id='.$data['id'])->count();
  173. if($yes > 0){
  174. $this->mtReturn(300,'删除失败,请却认当前角色无用户存在!');
  175. }
  176. $res=$group->where('id='.$data['id'])->delete();
  177. if($res)
  178. $this->mtReturn(200,'删除成功'.$data['id'] );
  179. else
  180. $this->mtReturn(300,'删除失败' );
  181. }
  182. }