You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
2.1 KiB
72 lines
2.1 KiB
<?php
|
|
declare (strict_types=1);
|
|
|
|
namespace app\api\model;
|
|
|
|
use auth\JwtAuth;
|
|
use auth\PermissAuth;
|
|
|
|
/**
|
|
* @mixin think\Model
|
|
*/
|
|
class Token extends Common
|
|
{
|
|
|
|
/**
|
|
* 登录
|
|
* @param array $data
|
|
* @return Common|\think\Model|void
|
|
*/
|
|
public function add($data = [])
|
|
{
|
|
$this->token = $data['token'];
|
|
$this->refresh_token = $data['refresh_token'];
|
|
$this->user_id = $data['user_id'];
|
|
$this->start_time = time();
|
|
$this->site_id = $data['site_id'];
|
|
$this->end_time = time() + config('jwtauth.web.expire_time_refresh');
|
|
$this->save();
|
|
}
|
|
|
|
/**
|
|
* 返回刷新token
|
|
* @param $refresh_token
|
|
* @return array|void
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public function refreshToken($refresh_token)
|
|
{
|
|
$time = time();
|
|
$_op = self::where('refresh_token', $refresh_token);
|
|
$_op->where('start_time', '<', $time)->where('end_time', '>', $time);
|
|
$token_data = $_op->field('user_id,site_id')->find();
|
|
if (!empty($token_data)) {
|
|
/**
|
|
* 登录用户ID
|
|
*/
|
|
defined('UID') ?: define('UID', $token_data->getData('user_id'));
|
|
|
|
/**
|
|
* 站点ID
|
|
*/
|
|
defined('SITE_ID') ?: define('SITE_ID', $token_data->getData('site_id'));
|
|
// //查询用户信息
|
|
// $loginUser = (new Manager())->parentRead(UID)->toArray();
|
|
//发送JWT签证密钥
|
|
$token_data->id = $token_data->user_id;
|
|
$token_obj = (JwtAuth::getInstance())->setToken($token_data);
|
|
$token = $token_obj->getToken();
|
|
$_data = [
|
|
'token' => $token,
|
|
'refresh_token' => $refresh_token, //返回refresh_token本身不延期
|
|
'expire' => config('jwtauth.web.expire_time'),
|
|
// 'refresh_token' => $token_obj->createRefreshToken($this),
|
|
];
|
|
return $_data;
|
|
}
|
|
return;
|
|
}
|
|
|
|
}
|
|
|