12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- <?php
- declare(strict_types=1);
- namespace app\mobile\controller;
- use Exception;
- use think\Request;
- use Firebase\JWT\JWT;
- use openssl\Aes;
- use think\exception\HttpException;
- class Base
- {
- protected $employeeId;
- protected $rootId;
- protected $middleware = ['validate'];
- public function __construct(Request $request)
- {
- $jwt = $request->header('Authorization');
- if (empty($jwt))
- {
- json(['code'=>-100,'msg'=>'请登陆'])->send();
- exit;
- }
- $jwt = str_replace('bearer ', '', $jwt);
- try {
- JWT::$leeway = 60; //token的弹性有效时间
- $decoded = JWT::decode($jwt, config('app.jwt_key'), ['HS256']);
- $arr = (array) $decoded;
- $aes = new Aes(config('app.jwt_key'));
- $queryData = $aes->decrypt($arr['data']);
- parse_str($queryData, $token);
- } catch (Exception $e) {
- json(['code'=>-100,'msg'=>'Token验证失败,请重新登录'])->send();
- exit;
- }
- $this->employeeId = $token['employee_id'];
- $this->rootId = $token['root_id'];
- $this->uid = $token['uid'];
- }
- }
|