Auth.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\service\AuthService;
  4. use app\common\model\AuthGroupAccessModel;
  5. use app\common\model\AuthGroupModel;
  6. use app\common\model\AuthRuleModel;
  7. use app\Request;
  8. use think\App;
  9. use app\BaseController;
  10. use think\facade\View;
  11. // 部门管理
  12. class Auth extends BaseController
  13. {
  14. private $service;
  15. public function __construct(App $app)
  16. {
  17. $this->service = new AuthService();
  18. parent::__construct($app);
  19. }
  20. public function index()
  21. {
  22. View::assign([
  23. 'list' => $this->service->authGroupModel->findByPaginate($this->request->param()),
  24. ]);
  25. return view();
  26. }
  27. // public function add() {
  28. // if ($this->request->isAjax()) {
  29. // $params = $this->request->param();
  30. // $res = $this->service->authGroupModel->save([
  31. // 'name' => format_string($params['name'] ?? null),
  32. // 'rules' => format_string($params['rule_ids'] ?? null),
  33. // 'describe' => format_string($params['describe'] ?? null),
  34. // ]);
  35. // return $this->ok($res);
  36. // }
  37. // return view();
  38. // }
  39. //
  40. // public function edit() {
  41. // $params = $this->request->param();
  42. // $id = $params['id'];
  43. // $group = $this->service->authGroupModel->findById($id);
  44. // if ($this->request->isAjax()) {
  45. // $res = $this->service->authGroupModel->where('id', $params['id'])->save([
  46. // 'name' => format_string($params['name'] ?? null),
  47. // 'rules' => format_string($params['rule_ids'] ?? null),
  48. // 'describe' => format_string($params['describe'] ?? null),
  49. // 'update_time' => time()
  50. // ]);
  51. // return $this->ok($res);
  52. // }
  53. // View::assign([
  54. // 'group' => $group,
  55. // ]);
  56. // return view();
  57. // }
  58. /**
  59. * @param null $admin_id
  60. * @return \think\response\Json
  61. * @throws \think\db\exception\DataNotFoundException
  62. * @throws \think\db\exception\DbException
  63. * @throws \think\db\exception\ModelNotFoundException
  64. */
  65. public function findAuthRuleByAdminId($admin_id = null) {
  66. $all_rules = $this->service->authRuleModel->findByIds(["*"]);
  67. $format_rules = array_map(function ($data) {
  68. return [
  69. 'id' => $data['id'],
  70. 'title' => $data['name'],
  71. 'pid' => $data['pid'],
  72. 'field' => $data['name'],
  73. 'spread' => true,
  74. 'checked' => false
  75. ];
  76. }, $all_rules);
  77. if(!isset($admin_id))
  78. return $this->ok(recursion($format_rules));
  79. $rules = $this->service->loadRuleByAdminId($admin_id);
  80. $in_rule_ids = array_map(function ($data) {
  81. return $data['id'];
  82. }, $rules);
  83. foreach ($format_rules as &$item) $item['checked'] = in_array($item['id'], $in_rule_ids);
  84. return $this->ok(recursion($format_rules));
  85. }
  86. /**
  87. * @param null $group_id
  88. * @return \think\response\Json
  89. * @throws \think\db\exception\DataNotFoundException
  90. * @throws \think\db\exception\DbException
  91. * @throws \think\db\exception\ModelNotFoundException
  92. */
  93. public function findAuthRuleByGroupId($group_id = null) {
  94. $format_rules = array_map(function ($data) {
  95. return [
  96. 'id' => $data['id'],
  97. 'title' => $data['name'],
  98. 'pid' => $data['pid'],
  99. 'field' => $data['name'],
  100. 'spread' => true,
  101. 'checked' => false
  102. ];
  103. }, $this->service->authRuleModel->findByIds(["*"]));
  104. if(!isset($group_id))
  105. return $this->ok(recursion($format_rules));
  106. $rules = $this->service->loadRuleByGroupId($group_id);
  107. $in_rule_ids = array_map(function ($data) {
  108. return $data['id'];
  109. }, $rules);
  110. foreach ($format_rules as &$item) $item['checked'] = in_array($item['id'], $in_rule_ids);
  111. return $this->ok(recursion($format_rules));
  112. }
  113. }