PHP code example of buexplain / token

1. Go to this page and download the library: Download buexplain/token 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/ */

    

buexplain / token example snippets


use Hyperf\HttpServer\Router\Router;
//签发token
Router::post('/in', \App\Controller\IndexController::class.'@in');
Router::addGroup('',function () {
    //销毁token
    Router::get('/out', \App\Controller\IndexController::class.'@out');
    //刷新token
    Router::post('/refresh', \App\Controller\IndexController::class.'@refresh');
}, [
    'middleware' => [
        //先执行token初始化的中间件
        \Token\Middleware\TokenMiddleware::class,
        //后执行token权限校验的中间件
        \Token\Middleware\AuthMiddleware::class,
    ]
]);

use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Di\Annotation\Inject;

class IndexController
{
    /**
     * @Inject
     * @var RequestInterface
     */
    protected $request;

    protected static function getToken(): \Token\Contract\TokenInterface
    {
        /**
         * 通过容器拿token代理对象
         * @var $token \Token\Contract\TokenInterface
         */
        $token = \Hyperf\Utils\ApplicationContext::getContainer()->get(\Token\Contract\TokenInterface::class);
        return $token;
    }

    /**
     *  签发
     */
    public function in()
    {
        //假设用户唯一id
        $userId = 1;
        /**
         * @var $manager \Token\TokenManager
         */
        $manager = \Hyperf\Utils\ApplicationContext::getContainer()->get(\Token\TokenManager::class);
        $token = $manager->start($this->request);
        //设置用户唯一id
        $token->setId((string)$userId);
        //根据用户唯一id载入数据
        $token->load();
        //获取客户端传递的设备信息
        $device = $this->request->query('device', 'pc');
        //判断是否已经登录了此类设备
        $names = $token->getNames();
        foreach ($names as $name) {
            if($name->get('device') == $device) {
                throw new \Token\Exception\UnauthorizedException("您已经登录了设备:{$device}");
            }
        }
        //记录用户最后登录时间与设备
        $token->set('last_login_time', time());
        $token->set('last_login_device', $device);
        //记录用户本次登录的设备
        $token->getName()->set('device', $device);
        //构造返回值
        $data = ['token'=>(string)$token->getName(), 'expire'=>$token->getName()->expire(), 'sysTime'=>time()];
        //getName后主动落库
        $token->save();
        //返回
        return [
            'code'=>0,
            'message'=>'success',
            'data'=>$data,
        ];
    }
    
    /**
     *  销毁
     */
    public function out()
    {
        //销毁所有的设备的token
        self::getToken()->destroyAll();
        return [
            'code'=>0,
            'message'=>'success',
        ];
    }
    
    /**
     *  刷新
     */    
    public function refresh()
    {
        $token = self::getToken();
        $token->getName()->refresh();
        $data = ['token'=>(string)$token->getName(), 'expire'=>$token->getName()->expire(), 'sysTime'=>time()];
        return [
            'code'=>0,
            'message'=>'success',
            'data'=>$data,
        ];
    }
}