Auth.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace app\admin\controller\system;
  3. use app\admin\model\SystemAuth;
  4. use app\admin\model\SystemAuthNode;
  5. use app\admin\service\TriggerService;
  6. use app\common\controller\AdminController;
  7. use app\admin\service\annotation\ControllerAnnotation;
  8. use app\admin\service\annotation\NodeAnnotation;
  9. use app\Request;
  10. use think\App;
  11. #[ControllerAnnotation(title: '角色权限管理', auth: true)]
  12. class Auth extends AdminController
  13. {
  14. protected array $sort = [
  15. 'sort' => 'desc',
  16. 'id' => 'desc',
  17. ];
  18. public function __construct(App $app)
  19. {
  20. parent::__construct($app);
  21. self::$model = SystemAuth::class;
  22. }
  23. #[NodeAnnotation(title: '授权', auth: true)]
  24. public function authorize(Request $request, $id): string
  25. {
  26. $row = self::$model::find($id);
  27. empty($row) && $this->error('数据不存在');
  28. if ($request->isAjax()) {
  29. $list = self::$model::getAuthorizeNodeListByAdminId($id);
  30. $this->success('获取成功', $list);
  31. }
  32. $this->assign('row', $row);
  33. return $this->fetch();
  34. }
  35. #[NodeAnnotation(title: '授权保存', auth: true)]
  36. public function saveAuthorize(Request $request): void
  37. {
  38. $this->checkPostRequest();
  39. $id = $request->post('id');
  40. $node = $request->post('node', "[]");
  41. $node = json_decode($node, true);
  42. $row = self::$model::find($id);
  43. empty($row) && $this->error('数据不存在');
  44. try {
  45. $authNode = new SystemAuthNode();
  46. $authNode->where('auth_id', $id)->delete();
  47. if (!empty($node)) {
  48. $saveAll = [];
  49. foreach ($node as $vo) {
  50. $saveAll[] = [
  51. 'auth_id' => $id,
  52. 'node_id' => $vo,
  53. ];
  54. }
  55. $authNode->saveAll($saveAll);
  56. }
  57. TriggerService::updateMenu();
  58. }catch (\Exception $e) {
  59. $this->error('保存失败');
  60. }
  61. $this->success('保存成功');
  62. }
  63. }