RoleController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace App\Http\Controllers\Admin\Base;
  3. use App\Http\Controllers\Admin\Controller;
  4. use App\Services\Admin\Role;
  5. use Request;
  6. use App\Services\Admin\Acl;
  7. class RoleController extends Controller
  8. {
  9. private $level;
  10. private $_service;
  11. private $_serviceDepartments;
  12. /**
  13. * 初始化Service
  14. */
  15. public function __construct()
  16. {
  17. parent::__construct();
  18. if(!$this->_service) $this->_service = new Role();
  19. $this->level = isset($this->_getRoleNode()->level)?$this->_getRoleNode()->level:'';
  20. }
  21. /**
  22. * 列表
  23. */
  24. function index()
  25. {
  26. if($this->_user['is_root']){
  27. $search['level'] = 0;
  28. }else{
  29. $search['level'] = $this->level;
  30. }
  31. $request = Request::all();
  32. $search['keyword'] = Request::input('keyword');
  33. $orderby = array();
  34. if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  35. $orderby[$request['sort_field']] = $request['sort_field_by'];
  36. }
  37. $list = $this->_service->search($search, $orderby);
  38. return view('admin.base.role.index', compact('list'));
  39. }
  40. /**
  41. * 创建
  42. */
  43. public function create()
  44. {
  45. if(Request::method() == 'POST'){
  46. if(intval(Request::input('info.level')) < $this->level){
  47. $this->showWarning('你无权创建该等级的角色!', urldecode(Request::input('_referer')));
  48. }
  49. if($this->_service->create(Request::input('info'))){
  50. $this->showMessage('操作成功', U( 'Base/Role/index'));
  51. }else{
  52. $this->showMessage('操作失败', U( 'Base/Role/index'));
  53. }
  54. }
  55. $level = $this->level;
  56. // return view('admin.base.role.edit', compact('level', 'Departments'));
  57. return view('admin.base.role.edit', compact('level'));
  58. }
  59. /**
  60. * 更新
  61. */
  62. public function update()
  63. {
  64. if(Request::method() == 'POST'){
  65. if(intval(Request::input('info.level')) < $this->level){
  66. $this->showWarning('你无权创建该等级的角色!', urldecode(Request::input('_referer')));
  67. }
  68. if($this->_service->update(Request::input('id'), Request::input('info'))){
  69. $this->showMessage('操作成功', urldecode(Request::input('_referer')));
  70. }else{
  71. $this->showWarning('操作失败', urldecode(Request::input('_referer')));
  72. }
  73. }
  74. $data = $this->_service->find(Request::input('id'));
  75. $level = $this->level;
  76. // return view('admin.base.role.edit', compact('data', 'level', 'Departments'));
  77. return view('admin.base.role.edit', compact('data', 'level'));
  78. }
  79. /**
  80. * 更新
  81. */
  82. public function auth()
  83. {
  84. $id = Request::input('id');
  85. $objAcl = new Acl();
  86. if(Request::method() == 'POST'){
  87. $menuIds = Request::input('menu_ids');
  88. if($this->_user['is_root']) {
  89. $allMenus = false;
  90. }else{
  91. $allMenus = array();
  92. foreach ($this->_user['menus'] as $value) {
  93. $allMenus[] = $value['id'];
  94. }
  95. }
  96. $ok = $objAcl->setRole($id, $menuIds,$allMenus);
  97. if($ok) {
  98. $arr['status'] = SUCESS_CODE;
  99. }else{
  100. $arr['status'] = SERVER_ERROR;
  101. }
  102. exit(json_encode($arr));
  103. }
  104. $hasPermissions = $objAcl->getAccessIDs($id);
  105. $role = session(LOGIN_MARK_SESSION_KEY);
  106. //为ztree做数据准备
  107. $zTree = []; $all = [];
  108. foreach($role['menus'] as $key => $value)
  109. {
  110. $arr = ['id' => $value['id'], 'pId' => $value['pid'],
  111. 'name' => $value['name'] . " (" . $value['path'] . ")",
  112. 'open' => true];
  113. if(in_array($value['id'], $hasPermissions)) $arr['checked'] = true;
  114. $zTree[] = $arr;
  115. $all[] = $value['id'];
  116. }
  117. $data = $this->_service->find($id);
  118. return view('admin.base.role.auth', compact('data','zTree','all'));
  119. }
  120. /**
  121. * 更新状态
  122. */
  123. public function status()
  124. {
  125. $bool = $this->_service->updateStatus(Request::input('id'), Request::input('status'));
  126. if($bool) {
  127. $this->showMessage('操作成功');
  128. }else{
  129. $this->showWarning('操作失败');
  130. }
  131. }
  132. /**
  133. * 删除
  134. */
  135. public function destroy()
  136. {
  137. $bool = $this->_service->destroy(Request::input('id'));
  138. if($bool) {
  139. $this->showMessage('操作成功');
  140. }else{
  141. $this->showWarning("操作失败");
  142. }
  143. }
  144. /**
  145. * 获取角色权限节点(level越小权限越大)
  146. */
  147. private function _getRoleNode()
  148. {
  149. return $this->_service->getLevelNode($this->_user['admin_role_id']);
  150. }
  151. /**
  152. * 获取树形结构
  153. */
  154. private function _getTreeByDepartmentId()
  155. {
  156. if($this->_user['is_root']){
  157. $department_id = 0;
  158. }else{
  159. $department_id = intval($this->_user['department_id']);
  160. }
  161. return $this->_serviceDepartments->getTreeByDepartmentId($department_id);
  162. }
  163. }