This commit is contained in:
86
app/Controller/AbstractController.php
Normal file
86
app/Controller/AbstractController.php
Normal file
@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Context\UserContext;
|
||||
use App\Service\OpLogsService;
|
||||
use App\Utils\ApiResponse;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Hyperf\HttpServer\Contract\RequestInterface;
|
||||
use Hyperf\HttpServer\Contract\ResponseInterface;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
abstract class AbstractController
|
||||
{
|
||||
#[Inject]
|
||||
protected ContainerInterface $container;
|
||||
|
||||
#[Inject]
|
||||
protected RequestInterface $request;
|
||||
|
||||
#[Inject]
|
||||
protected ResponseInterface $response;
|
||||
|
||||
#[Inject]
|
||||
protected ApiResponse $apiResponse;
|
||||
|
||||
#[Inject]
|
||||
protected OpLogsService $opLogsService;
|
||||
|
||||
/**
|
||||
* 获取默认分页参数.
|
||||
* @return array
|
||||
*/
|
||||
protected function getPage(): array
|
||||
{
|
||||
$params = $this->request->all();
|
||||
return [$params['page'] ?? 1, $params['pageSize'] ?? 100];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户令牌
|
||||
* @return string|null
|
||||
*/
|
||||
public function token(): ?string
|
||||
{
|
||||
return UserContext::getCurrentToken();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户信息
|
||||
* @return array
|
||||
*/
|
||||
public function user(): array
|
||||
{
|
||||
return UserContext::getCurrentUser() ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前公司信息
|
||||
* @return array
|
||||
*/
|
||||
public function company(): array
|
||||
{
|
||||
return $this->user()['company'] ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 操作日志.
|
||||
* @param string $log
|
||||
* @param int $source
|
||||
*/
|
||||
protected function opLogs(string $log, int $source = 0): void
|
||||
{
|
||||
$this->opLogsService->operatorLogs($log, $source);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user