RoleController.class.php.bak.20200603 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 (session('power')==2){
  75. $this->mtReturn(300,'本账号没有该权限,如需开通请联系管理员',1);
  76. }else {
  77. if(isset($data['type'])){
  78. $roletitle=$group->field('id')->where('type=1 and title="'.$data['title'].'" and id!='.$data['roleid'])->select();
  79. if(count($roletitle) > 0){
  80. exit(json_encode(array('statusCode'=>300,'message'=>'角色已存在!' )));
  81. }
  82. $rules = array(
  83. array('pid','require','父角色不为空!'),
  84. array('title','require','角色名称不为空!'),
  85. );
  86. if (!$group->validate($rules)->create()){
  87. echo json_encode(array('statusCode'=>'300','message'=> $group->getError()));
  88. }else{
  89. $type=$data['type'];
  90. $title=$data['title'];
  91. $sort=$data['sort'];
  92. $pidname=$data['pid'];
  93. $repid = $group->field('id,level')->where(array('title'=> $pidname))->select();
  94. $pid=$repid[0]['level']+1;
  95. if($pidname=='顶级'){
  96. $repid[0]['id']=0;
  97. $pid=1;
  98. }
  99. $add=array(
  100. 'id'=> $data['roleid'],
  101. 'type'=> $type,
  102. 'pid'=> $repid[0]['id'],
  103. 'title'=> $title,
  104. 'sort'=> $sort,
  105. 'level'=> $pid
  106. );
  107. $res=$group->save($add);
  108. if($res)
  109. echo json_encode(array('statusCode'=>200,'message'=>'修改成功' ));
  110. else
  111. echo json_encode(array('statusCode'=>300,'message'=>'修改失败' ));
  112. }
  113. }else{
  114. $list = $group->field('id,type,title,level,pid,sort,rules')->where('type=1')->select();
  115. $this->assign('role',$list);
  116. $res=$group->where(array('id'=> $data['id']))->select();
  117. $this->assign('roleid',$data['id']);
  118. $this->assign('title',$res[0]['title']);
  119. $this->assign('sort',$res[0]['sort']);
  120. $pidarr=$group->field('title')->where(array('id'=> $res[0]['pid']))->select();
  121. if($pidarr[0]['title']==''){$pidarr[0]['title']='顶级';}
  122. $this->assign('pid',$pidarr[0]['title']);
  123. $this->assign('action','/home/Role/edit');
  124. $this->display('edit');
  125. }
  126. }
  127. }
  128. public function perm(){
  129. $data=I();
  130. $group=M('auth_rule');
  131. if(!isset($data['list'])){//查询权限菜单
  132. $count= $group->count(id);// 查询满足要求的总记录数
  133. $where = $group->query("SELECT rules from sp_auth_group WHERE id =(SELECT posname FROM sp_user WHERE username='".$_SESSION['zt_username']."')");
  134. $wheres = substr($where[0]['rules'],0,strlen($where[0]['rules'])-1);
  135. $wheres=' where p.id in('.$wheres.')';
  136. if($_SESSION['zt_username']=='admin'){
  137. $wheres='';
  138. }
  139. $list = $group->query('SELECT p.id,p.title,p.pid,p.cid,p.cpid,p.ctitle,p.allid FROM V_PERM02 AS p '.$wheres);
  140. $info = array(
  141. 'totalRow' => $count,
  142. 'pageSize' => $count,//分页时不传
  143. 'list' => $list,
  144. );
  145. //$_SESSION['CCC']=$count;
  146. //$_SESSION['DDD']=print_r($list,true);
  147. echo json_encode($info);
  148. }elseif($data['list']=='get2'){//菜单功能
  149. }elseif($data['list']=='set2'){//修改
  150. $data=I();
  151. $perms='';
  152. foreach($data as $key => $value){
  153. if(substr($key,0,9) == "checkbox_"){
  154. $perms.=substr($key,9).',';
  155. }
  156. }
  157. $rules=M('auth_group');
  158. $set=$rules->save(array('id'=> $data['permid'],'rules'=> $perms,'dataperm'=>$data['dataperm']));
  159. if($set)
  160. echo json_encode(array('statusCode'=>200,'message'=> '保存成功'));
  161. else
  162. echo json_encode(array('statusCode'=>300,'message'=> '保存失败'));
  163. }else{
  164. $sel=$group->query("SELECT rules,dataperm FROM sp_auth_group WHERE id=".$data['id']);//是否有权限
  165. $v=explode(',',$sel[0]['rules']);
  166. $this->assign('pers',json_encode(array_filter($v)));
  167. $this->assign('permid',$data['id']);
  168. $this->assign('dataperm',$sel[0]['dataperm']);
  169. $this->assign('action','/home/Role/perm');
  170. $this->display('perm');
  171. }
  172. }
  173. public function del(){
  174. $group=M('auth_group');
  175. $data=I();
  176. if (session('power')==2){
  177. $this->mtReturn(300,'本账号没有该权限,如需开通请联系管理员',1);
  178. }else {
  179. $yes=M('auth_group_access')->where('group_id='.$data['id'])->count();
  180. if($yes > 0){
  181. $this->mtReturn(300,'删除失败,请却认当前角色无用户存在!');
  182. }
  183. $res=$group->where('id='.$data['id'])->delete();
  184. if($res)
  185. $this->mtReturn(200,'删除成功'.$data['id'] );
  186. else
  187. $this->mtReturn(300,'删除失败' );
  188. }
  189. }
  190. }