Role.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace app\admin\controller;
  3. /**
  4. * 角色管理
  5. */
  6. class Role extends Admin{
  7. private $Role = null;
  8. protected function _initialize(){
  9. parent::_initialize();
  10. $this->Role = model('Role');
  11. }
  12. public function index(){
  13. $role = $this->user['role'];
  14. $this->assign('role',$role);
  15. $this->assign('meta_title','角色列表');
  16. return $this->fetch();
  17. }
  18. public function load(){
  19. $where = [];
  20. $role = $this->user['role'];
  21. $where['pid'] = 1;
  22. $page = input('get.page');
  23. $limit = input('get.limit');
  24. $list = $this->Role->where($where)->paginate($limit,false,['page'=>$page]);
  25. $data = [];
  26. foreach ($list as $key => $value) {
  27. $data[$key]['id'] = $value['id'];
  28. $data[$key]['name'] = $value['name'];
  29. $data[$key]['cname'] = $value['cname'];
  30. $data[$key]['description'] = $value['description'];
  31. }
  32. return json(['data'=>$data,'count'=>$list->total(), 'code'=>0,'msg'=>'加载成功']);
  33. }
  34. public function add(){
  35. if ($this->request->isPost()) {
  36. $role = $this->user['role'];
  37. $this->Role->pid = $role['id'];
  38. $this->Role->name = input('post.name');
  39. $this->Role->cname = input('post.cname');
  40. $this->Role->description = input('post.description');
  41. $result = $this->Role->save();
  42. if ($result) {
  43. return json(['data'=>$this->Role->id,'code'=>0,'msg'=>'添加角色成功']);
  44. }
  45. return json(['data'=>$_POST,'code'=>1,'msg'=>'添加角色失败']);
  46. }else{
  47. $this->assign('meta_title','添加角色');
  48. return $this->fetch();
  49. }
  50. }
  51. public function edit(){
  52. if ($this->request->isPost()) {
  53. $id = input('post.id');
  54. $role = $this->Role->get($id);
  55. if ($role) {
  56. $role->name = input('post.name');
  57. $role->cname = input('post.cname');
  58. $role->description = input('post.description');
  59. $result = $role->save();
  60. if ($result) {
  61. return json(['data'=>null,'code'=>0,'msg'=>'编辑角色成功']);
  62. }
  63. return json(['data'=>$_POST,'code'=>1,'msg'=>'编辑角色失败']);
  64. }else{
  65. return json(['data'=>$_POST,'code'=>1,'msg'=>'参数错误']);
  66. }
  67. }else{
  68. $id = input('param.id');
  69. $role = $this->Role->where(['id'=>$id])->find();
  70. $this->assign('role',$role);
  71. $this->assign('meta_title','编辑角色');
  72. return $this->fetch();
  73. }
  74. }
  75. public function delete(){
  76. if ($this->request->isPost()) {
  77. $id = input('post.id');
  78. $role = $this->Role->where(['id'=>$id])->find();
  79. if (!$role) {
  80. return json(['data'=>$_POST,'code'=>1,'msg'=>'参数错误']);
  81. }
  82. $result = $role->delete();
  83. if ($result) {
  84. return json(['data'=>null,'code'=>0,'msg'=>'删除成功']);
  85. }
  86. return json(['data'=>null,'code'=>1,'msg'=>'删除失败']);
  87. }
  88. }
  89. public function loadnavis(){
  90. $role_id = input('get.role_id');
  91. $role = $this->Role->where(['id'=>$role_id])->find();
  92. if (!$role) {
  93. $this->error('参数错误');
  94. }
  95. $navis = $role['navis'];
  96. $navis_arr = [];
  97. if (!empty($navis)) {
  98. $navis_arr = explode(',', $navis);
  99. }
  100. $Navi = model('Navi');
  101. $pid = input('get.pid',0);
  102. $level = input('get.level',3);
  103. $list = model('Navi')->tree($pid,$this->user['role']['name'],[],$level);
  104. foreach ($list as $key => $value) {
  105. if (in_array($value['id'], $navis_arr)) {
  106. $list[$key]['checked'] = true;
  107. }
  108. }
  109. return json(['data'=>$list,'code'=>0,'msg'=>'加载成功']);
  110. }
  111. public function auth(){
  112. if ($this->request->isPost()) {
  113. $id = input('post.id');
  114. $role = $this->Role->where(['id'=>$id])->find();
  115. if (!$role) {
  116. return json(['data'=>$_POST,'code'=>1,'msg'=>'参数错误']);
  117. }
  118. $navis = input('navis/a',[]);
  119. $ids = [];
  120. $func = function($arr) use(&$func,&$ids){
  121. foreach ($arr as $key => $value) {
  122. $id = $value['id'];
  123. array_push($ids, $id);
  124. if (isset($value['children'])) {
  125. $func($value['children']);
  126. }
  127. }
  128. };
  129. $func($navis);
  130. $ids_str = implode(',', $ids);
  131. $role->navis = $ids_str;
  132. $result = $role->save();
  133. if ($result) {
  134. return json(['data'=>null,'code'=>0,'msg'=>'授权成功']);
  135. }
  136. return json(['data'=>$ids_str,'code'=>1,'msg'=>'授权失败']);
  137. }else{
  138. $role = $this->user['role'];
  139. $Navi = model('Navi');
  140. $navis = $Navi->navis(0,$role['name'],[]);
  141. $this->assign('navis',$navis);
  142. $id = input('param.id');
  143. $role = $this->Role->where(['id'=>$id])->find();
  144. if (!$role) {
  145. $this->error('参数错误');
  146. }
  147. $this->assign('role',$role);
  148. $this->assign('meta_title','角色授权');
  149. return $this->fetch();
  150. }
  151. }
  152. }