58 lines
1.3 KiB
PHP
58 lines
1.3 KiB
PHP
|
<?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\Exception\BusinessException;
|
||
|
use Hyperf\Di\Annotation\Inject;
|
||
|
use Hyperf\HttpServer\Contract\RequestInterface;
|
||
|
use Hyperf\HttpServer\Contract\ResponseInterface;
|
||
|
use Hyperf\Logger\LoggerFactory;
|
||
|
use Psr\Container\ContainerInterface;
|
||
|
|
||
|
abstract class BaseController
|
||
|
{
|
||
|
#[Inject]
|
||
|
protected ContainerInterface $container;
|
||
|
|
||
|
#[Inject]
|
||
|
protected RequestInterface $request;
|
||
|
|
||
|
#[Inject]
|
||
|
protected ResponseInterface $response;
|
||
|
|
||
|
/**
|
||
|
* @var \Psr\Log\LoggerInterface
|
||
|
*/
|
||
|
protected $logger;
|
||
|
|
||
|
public function __construct(LoggerFactory $loggerFactory)
|
||
|
{
|
||
|
// 第一个参数对应日志的 name, 第二个参数对应 config/autoload/logger.php 内的 key
|
||
|
$this->logger = $loggerFactory->get('log');
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取RPC调用返回结果
|
||
|
*
|
||
|
* @param array $result
|
||
|
* @return array
|
||
|
*/
|
||
|
public function getServiceResult(array $result) : array
|
||
|
{
|
||
|
if ($result['code'] === 200) {
|
||
|
return $result['data'];
|
||
|
}
|
||
|
|
||
|
throw new BusinessException($result['code'], $result['msg']);
|
||
|
}
|
||
|
}
|