feat: command
Some checks are pending
Build Docker / build (push) Waiting to run

This commit is contained in:
2024-10-26 18:27:45 +08:00
parent d15c03c04e
commit 3a39ff3790
12 changed files with 346 additions and 1 deletions

View File

@@ -0,0 +1,57 @@
<?php
/**
* This file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Command;
use Hyperf\Command\Annotation\Command;
use Hyperf\Devtool\Generator\GeneratorCommand;
use Psr\Container\ContainerInterface;
#[Command]
class CronTaskCommand extends GeneratorCommand
{
/**
* 构造方法
* @param ContainerInterface $container
*/
public function __construct(protected ContainerInterface $container)
{
parent::__construct('gen:cron');
}
/**
* 生成
* @return void
*/
public function configure(): void
{
$this->setDescription('Create a new cron task class');
parent::configure();
}
/**
* 获取stub文件
* @return string
*/
protected function getStub(): string
{
return __DIR__ . '/stubs/cron.stub';
}
/**
* 获取默认命名空间
* @return string
*/
protected function getDefaultNamespace(): string
{
return 'App\\Cron';
}
}

View File

@@ -0,0 +1,49 @@
<?php
namespace App\Command;
use Hyperf\Command\Annotation\Command;
use Hyperf\Devtool\Generator\GeneratorCommand;
use Psr\Container\ContainerInterface;
#[Command]
class ServiceCommand extends GeneratorCommand
{
/**
* 构造方法
* @param ContainerInterface $container
*/
public function __construct(protected ContainerInterface $container)
{
parent::__construct('gen:service');
}
/**
* 生成
* @return void
*/
public function configure(): void
{
$this->setDescription('Create a new service class');
parent::configure();
}
/**
* 获取stub文件
* @return string
*/
protected function getStub(): string
{
return __DIR__ . '/stubs/service.stub';
}
/**
* 获取默认命名空间
* @return string
*/
protected function getDefaultNamespace(): string
{
return 'App\\Service';
}
}

View File

@@ -0,0 +1,23 @@
<?php
/**
* This crontab file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace %NAMESPACE%;
use Hyperf\Crontab\Annotation\Crontab;
#[Crontab(rule: "* * * * *", name: "%CLASS%", singleton: true , callback: "execute", memo: "这是一个示例的定时任务")]
class %CLASS%
{
public function execute()
{
//todo Write logic
var_dump(date('Y-m-d H:i:s', time()));
}
}

View File

@@ -0,0 +1,19 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace %NAMESPACE%;
class %CLASS% extends
{
public function handle()
{
//todo Write logic
}
}

View File

@@ -0,0 +1,14 @@
<?php
namespace App\Constants;
use Hyperf\Constants\Annotation\Constants;
#[Constants]
class AdminCode extends ReturnCode
{
/**
* @Message("登录失败")
*/
public const int LOGIN_ERROR = 10001;
}

View File

@@ -0,0 +1,20 @@
<?php
namespace App\Constants;
use Hyperf\Constants\AbstractConstants;
use Hyperf\Constants\Annotation\Constants;
#[Constants]
class ReturnCode extends AbstractConstants
{
/**
* @Message("success")
*/
public const int SUCCESS = 0;
/**
* @Message("error")
*/
public const int ERROR = 1;
}

View File

@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
namespace App\Controller\Admin;
use App\Controller\AbstractController;
use App\Request\Admin\LoginRequest;
use App\Service\Admin\User\LoginService;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\Validation\Annotation\Scene;
#[Controller(prefix: "admin/login")]
class LoginController extends AbstractController
{
#[RequestMapping(path: "user", methods: "POST")]
#[Scene(scene: "login")]
public function login(LoginRequest $request)
{
$service = new LoginService();
return $service->handle();
}
}

47
app/Lib/AdminReturn.php Normal file
View File

@@ -0,0 +1,47 @@
<?php
namespace App\Lib;
use App\Constants\ReturnCode;
class AdminReturn
{
/**
* 后台通用返回
* @param string $msg
* @param array $data
* @param int $code
* @param array $debug
* @return array
*/
public static function success(string $msg = 'success', array $data = [], int $code = ReturnCode::SUCCESS, array $debug = []): array
{
$res = [
'code' => $code,
'message' => $msg,
'data' => $data
];
return array_merge($res, $debug);
}
/**
* 通用api返回
* @param string $msg
* @param array $data
* @param int $code
* @param array $debug
* @return array
*/
public static function error(string $msg = 'error', array $data = [], int $code = ReturnCode::ERROR, array $debug = []): array
{
$res = [
'code' => $code,
'message' => $msg,
'data' => $data
];
return array_merge($res, $debug);
}
}

View File

@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace App\Request\Admin;
use Hyperf\Validation\Request\FormRequest;
class LoginRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*/
public function rules(): array
{
return [
];
}
}

View File

@@ -0,0 +1,42 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Admin;
use Hyperf\Context\Context;
abstract class BaseService
{
/**
* 管理员id
* @var int $adminId
*/
protected int $adminId = 0;
/**
* 角色id
* @var int $roleId
*/
protected int $roleId = 0;
/**
* 主构造函数(获取请求对象)
*/
public function __construct()
{
$this->adminId = Context::get("admin_id",0);
$this->roleId = Context::get("role_id",0);
}
/**
* 主体函数抽象类
*/
abstract public function handle();
}

View File

@@ -0,0 +1,22 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Admin\User;
use App\Lib\AdminReturn;
use App\Service\Admin\BaseService;
class LoginService extends BaseService
{
public function handle(): array
{
return AdminReturn::success();
}
}

View File

@@ -22,7 +22,7 @@ return [
'log_level' => [ 'log_level' => [
LogLevel::ALERT, LogLevel::ALERT,
LogLevel::CRITICAL, LogLevel::CRITICAL,
LogLevel::DEBUG, // LogLevel::DEBUG,
LogLevel::EMERGENCY, LogLevel::EMERGENCY,
LogLevel::ERROR, LogLevel::ERROR,
LogLevel::INFO, LogLevel::INFO,