You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

73 lines
1.8 KiB

<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace App\Controller;
use App\Service\v3\Interfaces\HelperServiceInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Psr\Http\Message\ResponseInterface as Psr7ResponseInterface;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Hyperf\Di\Annotation\Inject;
class BaseController extends AbstractController
{
/**
* @Inject
* @var HelperServiceInterface
*/
protected $helperService;
public function w7result($statusCode,$data = ''){
return $this->response
->withHeader('Content-Type', 'application/text')
->withStatus($statusCode)
->withBody(new SwooleStream($data));
}
public function result($code, $data, $message = '成功'):Psr7ResponseInterface
{
$status = 'ok';
if($code>0){
$status = 'error';
}
$content = [
"status"=>$status,
"code" => $code,
"result" => $data ? collect($data)->toArray() : [],
"message" => $message
];
return $this->response->json($content);
}
public function success($data, $message = '成功')
{
return $this->result(0,$data, $message);
}
/**
* 请求参数判空
* null&'null' / false&&'false' / '' / 'undefined' / 'unknown'
* @param mixed $var 参数
*/
public function empty($var)
{
return ! (
isset($var)
&& $var
&& !empty($var)
&& !is_null($var)
&& $var != 'undefined'
&& $var != 'unknown'
&& $var != 'null'
&& $var != 'false'
);
}
}