tokenKey = Config::get('token.kefu_key'); break; case "wap": default: $this->tokenKey = Config::get("token.app_key"); break; } } /** * 获取token * @param int $id * @param string $type * @param array $params * @return array */ public function getToken(int $id, array $params = []) { $host = Request()->host(); $time = time(); $exp_time = strtotime('+ 30day'); $params += [ 'iss' => $host, 'aud' => $host, 'iat' => $time, 'nbf' => $time, 'exp' => $exp_time ]; $params['jti'] = compact('id'); $token = JWT::encode($params, $this->tokenKey); return $token; } /** * 解析token * @param string $jwt * @return array */ public function parseToken(string $jwt) { $this->token = $jwt; list($headb64, $bodyb64, $cryptob64) = explode('.', $this->token); $payload = JWT::jsonDecode(JWT::urlsafeB64Decode($bodyb64)); return $payload->jti->id; } /** * 验证token */ public function verifyToken() { JWT::$leeway = 60; JWT::decode($this->token, $this->tokenKey, array('HS256')); $this->token = null; } }