Auth.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\admin\middleware;
  4. use Closure;
  5. use app\Request;
  6. use think\exception\HttpResponseException;
  7. use think\facade\Db;
  8. use think\Response;
  9. /**
  10. * 全局权限校验
  11. * Class WriteLog
  12. * @package app\middleware
  13. */
  14. class Auth
  15. {
  16. protected static int $CODE_SUCCESS = 0; //成功
  17. protected static int $CODE_ERR = 999; //成功
  18. /**
  19. * @param Request $request
  20. * @param Closure $next
  21. * @return mixed
  22. */
  23. public function handle(Request $request, Closure $next): mixed
  24. {
  25. /*if ($request->admin->is_pass != 1 && $request->admin->is_root != 1 || $request->admin->valid != 1 && $request->admin->is_root != 1) {
  26. $res = returnFormatError('无权限', 401);
  27. throw new HttpResponseException(Response::create($res, "json"));
  28. }*/
  29. /* $isPass = false;
  30. $role_id = $request->admin->role_id;
  31. $test = Db::table('role')->where('id', $role_id)->find();
  32. $codes = explode(',', $test['codes']);
  33. $list = config('permission_action');
  34. $ctrl = $request->controller();
  35. $fun = $request->action();
  36. // dump($ctrl.'_'.$fun);
  37. foreach ($list as $k => $v) {
  38. if ($ctrl . '_' . $fun == $k) {
  39. // dump('a=>'.$v);
  40. foreach ($codes as $kk => $vv) {
  41. if ($v == $vv) {
  42. // dump('b=>'.$vv);
  43. $isPass = true;
  44. break;
  45. }
  46. }
  47. }
  48. }
  49. // || $fun=='import' || $fun=='export' || $fun=='pass' || $fun=='rePass'
  50. if ($request->admin->is_root == 1 || $fun == 'init' || $fun == 'initDetail') {
  51. $isPass = true;
  52. }
  53. // 添加中间件执行代码
  54. $admin = $request->admin;
  55. if (!$isPass) {
  56. $res = returnFormatError('无权限', 555);
  57. throw new HttpResponseException(Response::create($res, "json"));
  58. }*/
  59. return $next($request);
  60. }
  61. /**
  62. * 返回TOKEN错误代码内容
  63. * @param $code
  64. * @return string
  65. */
  66. private static function getError($code): string
  67. {
  68. $errArr = self::getErrorArr();
  69. if (!key_exists($code, $errArr)) {
  70. return "未知错误";
  71. }
  72. return $errArr[$code];
  73. }
  74. /**
  75. * 获取TOKEN错误码数组
  76. * @return array
  77. */
  78. private static function getErrorArr(): array
  79. {
  80. return [
  81. self::$CODE_SUCCESS => "成功",
  82. self::$CODE_ERR => "系统异常",
  83. ];
  84. }
  85. }