123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <?php
- namespace app\api\controller\base;
- use app\common\model\User;
- use Exception;
- use Firebase\JWT\JWT;
- use Firebase\JWT\Key;
- use think\exception\HttpResponseException;
- class Permissions extends Base
- {
- const JWT_COOKIE_NAME = 'x-token';
- const JWT_SESSION_NAME = 'user_info';
- private $userId;
- private $user;
- protected function _initialize()
- {
- parent::_initialize();
-
- $jwt = $this->request->header('x-token', "");
- if (!$jwt) {
- $jwt = $this->request->param('x-token', "");
- if (!$jwt) {
- $callbackBody = file_get_contents('php://input');
- if (!$callbackBody) {
- $this->json_error('请先登入');
- }
- $callbackJson = json_decode($callbackBody, true);
- $jwt = isset($callbackJson['x-token']) ? $callbackJson['x-token'] : '';
- if (!$jwt) {
- $this->json_error('请先登入');
- }
- }
- }
-
- try {
- JWT::$leeway = 60;
- $jwt_key = system_salt();
- $decoded = JWT::decode($jwt, new Key($jwt_key, 'HS256'));
- $arr = (array)$decoded;
- if (!isset($arr['exp']) || $arr['exp'] < time()) {
- self::clear_session();
- throw new \think\Exception('登入超时,请重新登录');
- } else {
-
- $this->userId = $arr['data']->userId;
- $loginTime = $arr['data']->loginTime;
- $exptime = $arr['exp'];
-
-
- self::createJwt($this->userId, $loginTime, 3600, $exptime);
- return;
- }
- } catch (Exception $e) {
- $this->json_error($this->getErrorMsg($e));
- }
- }
- private function getErrorMsg(Exception $e)
- {
- if ($e instanceof HttpResponseException) {
- return $e->getResponse()->getData()['msg'];
- }
- switch ($e->getMessage()) {
- case "Expired token":
- self::clear_session();
- $msg = '登入超时,请重新登录';
- break;
- case "Wrong number of segments":
- self::clear_session();
- $msg = 'Token验证失败,请重新登录';
- break;
- default:
- $msg = "error:" . $e->getMessage();
- }
- return $msg;
- }
-
- public static function createJwt($userId, $loginTime, $expire = 3600, $exp = null)
- {
- $nowtime = time();
- $exptime = $exp ? $exp : $nowtime + $expire;
-
- $data = ['userId' => $userId, 'loginTime' => $loginTime, 'exptime' => $exptime];
- $token = [
- 'iss' => PRODUCT_URL,
- 'aud' => PRODUCT_URL,
- 'iat' => $nowtime,
- 'nbf' => $nowtime + 10,
- 'exp' => $exptime,
- 'data' => $data
- ];
-
- $jwt_key = system_salt();
- $jwt = JWT::encode($token, $jwt_key, 'HS256');
-
- session(self::JWT_SESSION_NAME, $data);
- cookie(self::JWT_COOKIE_NAME, $jwt, 3600 * 12);
- return $jwt;
- }
-
- protected function getUser()
- {
- if (!$this->user) {
- $user = User::get($this->userId);
- if (!$user) {
- $this->json_error('找不到用户信息,请重新登入');
- }
- $this->user = $user;
- }
- return $this->user;
- }
-
- protected function getUserId()
- {
- return $this->userId;
- }
-
- public static function clear_session()
- {
- cookie(self::JWT_COOKIE_NAME, null);
- session(self::JWT_SESSION_NAME, null);
- }
- }
|