Auth.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace App\Http\Controllers\Core;
  3. use Illuminate\Support\Facades\Config;
  4. use Tymon\JWTAuth\Facades\JWTAuth;
  5. use Tymon\JWTAuth\Exceptions\JWTException;
  6. use Illuminate\Support\Facades\Redis;
  7. use App\Http\Controllers\Controller;
  8. /**
  9. * Class Auth
  10. * @package App\Http\Controllers\Core
  11. * @method static auth()
  12. */
  13. class Auth extends Controller
  14. {
  15. private static $instance = null;
  16. private $uid = null;
  17. public const ADMINS = [
  18. 10001,
  19. 10002,
  20. 10007,
  21. 91031,
  22. 94302,
  23. 98047,
  24. 62726,
  25. 103143,
  26. 10771042,
  27. 11386073,
  28. 270382
  29. ];
  30. /**
  31. * @param $method
  32. * @param $params
  33. * @return mixed
  34. */
  35. public static function __callStatic($method, $params)
  36. {
  37. if (is_null(self::$instance)) {
  38. self::$instance = new Auth();
  39. }
  40. return call_user_func_array([self::$instance, "p{$method}"], $params);
  41. }
  42. /**
  43. * 用户认证
  44. * @return mixed
  45. * @throws JWTException
  46. */
  47. private function pauth()
  48. {
  49. if (is_null($this->uid)) {
  50. try {
  51. if (!$auth = JWTAuth::parseToken()->authenticate()) {
  52. throw new JWTException("请先授权登陆", 401);
  53. }
  54. } catch (JWTException $e) {
  55. throw new JWTException("登陆已过期,请重新授权登陆", 401);
  56. }
  57. $debug_uid = Redis::hget("fpdx_admin_debug", $auth->uid);
  58. if (!empty($debug_uid)) {
  59. $uid = $debug_uid;
  60. } else {
  61. $uid = $auth->uid;
  62. }
  63. $this->uid = $uid;
  64. Config::set("uid", $this->uid);
  65. return $this->uid;
  66. }
  67. return $this->uid;
  68. }
  69. /**
  70. * @return mixed
  71. * @throws JWTException
  72. */
  73. public static function adminAuth()
  74. {
  75. try {
  76. if (!$auth = JWTAuth::parseToken()->authenticate()) {
  77. throw new JWTException("请先授权登陆", 401);
  78. }
  79. } catch (JWTException $e) {
  80. throw new JWTException("登陆已过期,请重新授权登陆", 401);
  81. }
  82. $uid = $auth->uid;
  83. if (in_array($uid, self::ADMINS)) {
  84. return $uid;
  85. } else {
  86. throw new JWTException("权限不足", 403);
  87. }
  88. }
  89. }