Auth.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | EasyAdmin
  4. // +----------------------------------------------------------------------
  5. // | PHP交流群: 763822524
  6. // +----------------------------------------------------------------------
  7. // | 开源协议 https://mit-license.org
  8. // +----------------------------------------------------------------------
  9. // | github开源项目:https://github.com/zhongshaofa/EasyAdmin
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller\system;
  12. use app\common\model\SystemAuth;
  13. use app\common\model\SystemAuthNode;
  14. use app\admin\service\TriggerService;
  15. use app\common\controller\AdminController;
  16. use EasyAdmin\annotation\ControllerAnnotation;
  17. use EasyAdmin\annotation\NodeAnotation;
  18. use think\App;
  19. /**
  20. * @ControllerAnnotation(title="角色权限管理")
  21. * Class Auth
  22. * @package app\admin\controller\system
  23. */
  24. class Auth extends AdminController
  25. {
  26. use \app\admin\traits\Curd;
  27. protected $sort = [
  28. 'sort' => 'desc',
  29. 'id' => 'desc',
  30. ];
  31. public function __construct(App $app)
  32. {
  33. parent::__construct($app);
  34. $this->model = new SystemAuth();
  35. }
  36. /**
  37. * @NodeAnotation(title="授权")
  38. */
  39. public function authorize($id)
  40. {
  41. $row = $this->model->find($id);
  42. empty($row) && $this->error('数据不存在');
  43. if ($this->request->isAjax()) {
  44. $list = $this->model->getAuthorizeNodeListByAdminId($id);
  45. $this->success('获取成功', $list);
  46. }
  47. $this->assign('row', $row);
  48. return $this->fetch();
  49. }
  50. /**
  51. * @NodeAnotation(title="授权保存")
  52. */
  53. public function saveAuthorize()
  54. {
  55. $this->checkPostRequest();
  56. $id = $this->request->post('id');
  57. $node = $this->request->post('node', "[]");
  58. $node = json_decode($node, true);
  59. $row = $this->model->find($id);
  60. empty($row) && $this->error('数据不存在');
  61. try {
  62. $authNode = new SystemAuthNode();
  63. $authNode->where('auth_id', $id)->delete();
  64. if (!empty($node)) {
  65. $saveAll = [];
  66. foreach ($node as $vo) {
  67. $saveAll[] = [
  68. 'auth_id' => $id,
  69. 'node_id' => $vo,
  70. ];
  71. }
  72. $authNode->saveAll($saveAll);
  73. }
  74. TriggerService::updateMenu();
  75. } catch (\Exception $e) {
  76. $this->error('保存失败');
  77. }
  78. $this->success('保存成功');
  79. }
  80. }