Grant.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace app\sys\controller;
  3. use app\logics\GrantLogic;
  4. use app\model\Employee;
  5. use app\model\Grant as GrantModel;
  6. use app\model\Permission;
  7. use think\facade\View;
  8. class Grant
  9. {
  10. public function list()
  11. {
  12. $list = GrantModel::where(['root_id' => request()->employee->root_id])->select();
  13. return ['code' => 0, 'data' => $list];
  14. }
  15. public function add()
  16. {
  17. $grant = GrantModel::where(['root_id'=>request()->employee->root_id, 'type'=>'m'])->find();
  18. if($grant == null) $grant = GrantModel::where(['root_id' => 0, 'type'=>'m'])->find();
  19. $arr = [['id', 'in', (array)$grant->permission]];
  20. $data = (new Permission())->getTree($arr, request()->employee->root_id);
  21. View::assign('data', $data);
  22. return View::fetch();
  23. }
  24. public function add_grant()
  25. {
  26. $params = input();
  27. $grant = GrantModel::where(['id' => request()->employee->grant_id])->find();
  28. // $auth = Permission::where([['id', 'in', $params['auth']], ['id', 'in', (array)$grant->permission]])->column('id');
  29. $auth = Permission::where([['id', 'in', $params['auth']]])->column('id');
  30. $menu1 = Permission::where([['id', 'in', $auth], ['pid', '<>', 0]])->column('pid');
  31. $menu2 = Permission::where([['id', 'in', $menu1], ['pid', '<>', 0]])->column('pid');
  32. $ids = array_merge($auth, $menu1, $menu2);
  33. $ids = array_unique($ids);
  34. sort($ids);
  35. $data = [
  36. 'permission' => array_values($ids),
  37. 'name' => $params['title'],
  38. 'root_id' => request()->employee->root_id
  39. ];
  40. GrantModel::create($data);
  41. return json(['code' => 0, 'msg' => '权限组添加成功']);
  42. }
  43. public function edit()
  44. {
  45. $gant_id = input('grant_id');
  46. $grant = GrantModel::where(['root_id'=>request()->employee->root_id, 'type'=>'m'])->find();
  47. if($grant == null) $grant = GrantModel::where(['root_id' => 0, 'type'=>'m'])->find();
  48. $arr = [['id', 'in', (array)$grant->permission]];
  49. $data = (new Permission())->getTree($arr, request()->employee->root_id);
  50. View::assign('data', $data);
  51. $grant = GrantModel::where(['id' => $gant_id, 'root_id' => request()->employee->root_id])->find();
  52. View::assign('permission', (array)$grant->permission);
  53. View::assign('grant_name', $grant->name);
  54. return View::fetch();
  55. }
  56. public function edit_grant()
  57. {
  58. $params = input();
  59. $grant = GrantModel::where(['id' => $params['grant_id'], 'root_id' => request()->employee->root_id])->find();
  60. if (empty($grant)) return json(['code' => 1, 'msg' => '数据不存在']);
  61. $parent = GrantModel::where(['id' => request()->employee->grant_id])->find();
  62. // 防止超出权限范围
  63. // $auth = Permission::where([['id', 'in', $params['auth']], ['id', 'in', (array)$parent->permission]])->column('id');
  64. $auth = Permission::where([['id', 'in', $params['auth']]])->column('id');
  65. $menu1 = Permission::where([['id', 'in', $auth], ['pid', '<>', 0]])->column('pid');
  66. $menu2 = Permission::where([['id', 'in', $menu1], ['pid', '<>', 0]])->column('pid');
  67. $ids = array_merge($auth, $menu1, $menu2);
  68. $ids = array_unique($ids);
  69. sort($ids);
  70. $data = [
  71. 'permission' => array_values($ids),
  72. 'name' => $params['title']
  73. ];
  74. $grant->save($data);
  75. return json(['code' => 0, 'msg' => '权限组修改成功']);
  76. }
  77. public function del($id)
  78. {
  79. $grant = GrantModel::where(['id' => $id, 'root_id' => request()->employee->root_id])->find();
  80. if (empty($grant)) return json(['code' => 0, 'msg' => '权限组删除成功']);
  81. $use = Employee::where(['grant_id' => $id])->count();
  82. if ($use) return json(['code' => 1, 'msg' => '权限组还在使用中,不可删除。']);
  83. $grant->delete();
  84. return json(['code' => 0, 'msg' => '权限组删除成功']);
  85. }
  86. }