hyperf-chat/app/Controller/Api/V1/CController.php

67 lines
1.3 KiB
PHP
Raw Permalink Normal View History

2020-11-04 11:57:16 +08:00
<?php
2021-09-05 15:47:01 +08:00
declare(strict_types=1);
2020-12-26 21:33:40 +08:00
/**
* This is my open source code, please do not use it for commercial applications.
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code
*
* @author Yuandong<837215079@qq.com>
* @link https://github.com/gzydong/hyperf-chat
*/
2020-11-04 11:57:16 +08:00
namespace App\Controller\Api\V1;
2020-11-29 14:44:11 +08:00
use Hyperf\Di\Annotation\Inject;
2020-11-04 11:57:16 +08:00
use App\Controller\AbstractController;
2020-11-07 22:57:10 +08:00
use App\Support\Response;
2021-09-05 15:47:01 +08:00
use App\Model\User;
2020-11-04 11:57:16 +08:00
2020-11-04 16:47:17 +08:00
/**
* 基类控制器
*
* @package App\Controller\Api\V1
*/
2020-11-04 11:57:16 +08:00
class CController extends AbstractController
{
2020-11-04 16:47:17 +08:00
/**
* @Inject
* @var Response
*/
protected $response;
2020-11-04 22:58:49 +08:00
/**
* 获取当前登录用户ID
*
* @return int
*/
2021-09-05 15:47:01 +08:00
public function uid(): int
2020-11-29 17:39:24 +08:00
{
2021-05-23 17:04:20 +08:00
$guard = $this->guard();
2021-05-23 16:52:01 +08:00
return $guard->check() ? $guard->user()->getId() : 0;
2020-11-04 22:58:49 +08:00
}
2021-05-23 17:04:20 +08:00
2021-09-05 15:47:01 +08:00
/**
* 获取登录用户信息
*
* @return User|null
*/
public function user(): ?User
{
$guard = $this->guard();
return $guard->check() ? $guard->user() : null;
}
2021-05-23 17:04:20 +08:00
/**
* 获取授权守卫
*
* @return mixed|\Qbhy\HyperfAuth\AuthGuard|\Qbhy\HyperfAuth\AuthManager
*/
public function guard()
{
return auth('jwt');
}
2020-11-04 11:57:16 +08:00
}