GroupController.class.php.bak.20200603 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace Home\Controller;
  3. use Think\Controller;
  4. class GroupController extends Controller {//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'=>'0');
  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=0')->select();
  66. $this->assign('role',$list);
  67. $this->assign('action','/home/Group/add');
  68. $this->display();
  69. }
  70. }
  71. public function edit(){
  72. $group=M('auth_group');
  73. $data=I();
  74. if (session('power')==2){
  75. echo json_encode(array('statusCode'=>300,'message'=>'本账号没有该权限,如需开通请联系管理员' ));
  76. // $this->mtReturn(300,'本账号没有该权限,如需开通请联系管理员',1);
  77. }else {
  78. if(isset($data['type'])){
  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=0')->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/Group/edit');
  121. $this->display('edit');
  122. }
  123. }
  124. }
  125. public function perm(){
  126. $data=I();
  127. $group=M('auth_rule');
  128. if(!isset($data['list'])){//查询权限菜单
  129. $list = $group->query('SELECT p.id,p.title,p.pid,p.cid,p.cpid,p.ctitle,p.allid FROM V_PERM02 AS p');
  130. echo json_encode($list);
  131. }elseif($data['list']=='get2'){//菜单功能
  132. }elseif($data['list']=='set2'){//修改
  133. $data=I();
  134. $perms='';
  135. foreach($data as $key => $value){
  136. if(substr($key,0,9) == "checkbox_"){
  137. $perms.=substr($key,9).',';
  138. }
  139. }
  140. $rules=M('auth_group');
  141. $set=$rules->save(array('id'=> $data['permid'],'rules'=> $perms));
  142. if($set)
  143. echo json_encode(array('statusCode'=>200,'message'=> '保存成功'));
  144. else
  145. echo json_encode(array('statusCode'=>300,'message'=> '保存失败'));
  146. }else{
  147. $sel=$group->query("SELECT rules FROM sp_auth_group WHERE id=".$data['id']);//是否有权限
  148. $v=explode(',',$sel[0]['rules']);
  149. $this->assign('pers',json_encode(array_filter($v)));
  150. $this->assign('permid',$data['id']);
  151. $this->assign('action','/home/Group/perm');
  152. $this->display('perm');
  153. }
  154. }
  155. public function del(){
  156. $group=M('auth_group');
  157. $data=I();
  158. if (session('power')==2){
  159. // $this->mtReturn(300,'本账号没有该权限,如需开通请联系管理员',1);
  160. echo json_encode(array('statusCode'=>300,'message'=>'本账号没有该权限,如需开通请联系管理员' ));
  161. }else {
  162. $res=$group->where('id='.$data['id'])->delete();
  163. if($res)
  164. echo json_encode(array('statusCode'=>200,'message'=>'删除成功' ));
  165. else
  166. echo json_encode(array('statusCode'=>200,'message'=>'删除失败' ));
  167. }
  168. }
  169. }