PHP code example of gq-tools / tools
1. Go to this page and download the library: Download gq-tools/tools library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
gq-tools / tools example snippets
return [
//注销token缓存key
'delete_key' => 'delete_token',
//时区
'timezone' => 'Asia/Shanghai',
//编号
'jti' => '4f1g23a12aa',
//签名密钥
'sign' => 'a4693602cbb7a',
//签发人
'iss' => 'http://example.cn',
//接收人
'aud' => [
'http://example.com',
'http://example.org',
'http://example.top',
],
//主题
'sub' => '100',
//有效期(默认两个小时) 单位:秒
'exp' => 3600 * 2,
//设置SM4加密KEY
'key' => '1234567890123456',
//设置SM4KEY长度
'key_len' => 16,
];
use ajiho\Jwt;
$token = Jwt::create(100);
$token = Jwt::create('php是世界上最好的语言');
$token = Jwt::create(['id'=>100,'name'=>'jack']);
//生成token
$token = Jwt::create(100);
/**
* 验证token中间件
*
* @param \think\Request $request
* @param \Closure $next
* @return Response
*/
public function handle($request, \Closure $next)
{
$parseResult = Jwt::parse(Jwt::getRequestToken());
if ($parseResult['code'] !== 200) {
return json(['code' => 401, 'msg' => 'token解析失败', 'data' => []]);
}
//验证通过,将得到的用户id,放到请求信息中去,方便后续使用
$request->user_id = $parseResult['data'];//100
return $next($request);
}
//获取当前登录用户的信息
public function userInfo(Request $request)
{
$user = User::where('status', 1)
->find($request->user_id);
return json($user);
}
//退出登录
public function logout()
{
Jwt::logout(Jwt::getRequestToken());
return json(['code' => 200, 'msg' => '用户退出成功', 'data' => []]);
}
$parseResult = Jwt::parse(Jwt::getRequestToken());
dd($parseResult);//['code' => 10000, 'msg' => 'token已经被注销', 'data' => []]