1
0

Base.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. declare(strict_types=1);
  3. namespace app\mobile\controller;
  4. use Exception;
  5. use think\Request;
  6. use Firebase\JWT\JWT;
  7. use openssl\Aes;
  8. use think\exception\HttpException;
  9. class Base
  10. {
  11. protected $employeeId;
  12. protected $rootId;
  13. protected $middleware = ['validate'];
  14. public function __construct(Request $request)
  15. {
  16. $jwt = $request->header('Authorization');
  17. if (empty($jwt))
  18. {
  19. json(['code'=>-100,'msg'=>'请登陆'])->send();
  20. exit;
  21. }
  22. $jwt = str_replace('bearer ', '', $jwt);
  23. try {
  24. JWT::$leeway = 60; //token的弹性有效时间
  25. $decoded = JWT::decode($jwt, config('app.jwt_key'), ['HS256']);
  26. $arr = (array) $decoded;
  27. $aes = new Aes(config('app.jwt_key'));
  28. $queryData = $aes->decrypt($arr['data']);
  29. parse_str($queryData, $token);
  30. } catch (Exception $e) {
  31. json(['code'=>-100,'msg'=>'Token验证失败,请重新登录'])->send();
  32. exit;
  33. }
  34. $this->employeeId = $token['employee_id'];
  35. $this->rootId = $token['root_id'];
  36. $this->uid = $token['uid'];
  37. }
  38. }