79 lines
1.8 KiB
PHP
79 lines
1.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
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;
|
|
|
|
/**
|
|
* 成功统一返回.
|
|
* @param int $count
|
|
* @param mixed $msg
|
|
* @param null|mixed $data
|
|
*/
|
|
protected function success($msg = 'success', $data = null, $count = null): \Psr\Http\Message\ResponseInterface
|
|
{
|
|
if (! is_string($msg)) {
|
|
$data = $msg;
|
|
$msg = 'success';
|
|
}
|
|
$code = 0;
|
|
return $this->response($code, $msg, $data, $count);
|
|
}
|
|
|
|
/**
|
|
* 错误统一返回.
|
|
* @param int $count
|
|
* @param mixed $msg
|
|
* @param null|mixed $data
|
|
*/
|
|
protected function error($msg = 'error', $data = null, $count = null): \Psr\Http\Message\ResponseInterface
|
|
{
|
|
if (! is_string($msg)) {
|
|
$data = $msg;
|
|
$msg = 'error';
|
|
}
|
|
$code = 2;
|
|
return $this->response($code, $msg, $data, $count);
|
|
}
|
|
|
|
/**
|
|
* 响应.
|
|
* @param array $data
|
|
* @param int $count
|
|
* @return \Psr\Http\Message\ResponseInterface
|
|
*/
|
|
protected function response(int $code, string $msg, $data, $count)
|
|
{
|
|
$body = compact('code', 'msg', 'data', 'count');
|
|
if ($count === null) {
|
|
unset($body['count']);
|
|
}
|
|
if ($data === null) {
|
|
unset($body['data']);
|
|
}
|
|
return $this->response->json($body);
|
|
}
|
|
|
|
protected function account()
|
|
{
|
|
return $this->request->getAttribute("account");
|
|
}
|
|
}
|