feat: command
Some checks are pending
Build Docker / build (push) Waiting to run
Some checks are pending
Build Docker / build (push) Waiting to run
This commit is contained in:
57
app/Command/CronTaskCommand.php
Normal file
57
app/Command/CronTaskCommand.php
Normal 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';
|
||||
}
|
||||
}
|
||||
49
app/Command/ServiceCommand.php
Normal file
49
app/Command/ServiceCommand.php
Normal 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';
|
||||
}
|
||||
}
|
||||
23
app/Command/stubs/cron.stub
Normal file
23
app/Command/stubs/cron.stub
Normal 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()));
|
||||
}
|
||||
}
|
||||
19
app/Command/stubs/service.stub
Normal file
19
app/Command/stubs/service.stub
Normal 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
|
||||
}
|
||||
}
|
||||
14
app/Constants/AdminCode.php
Normal file
14
app/Constants/AdminCode.php
Normal 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;
|
||||
}
|
||||
20
app/Constants/ReturnCode.php
Normal file
20
app/Constants/ReturnCode.php
Normal 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;
|
||||
}
|
||||
24
app/Controller/Admin/LoginController.php
Normal file
24
app/Controller/Admin/LoginController.php
Normal 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
47
app/Lib/AdminReturn.php
Normal 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);
|
||||
}
|
||||
}
|
||||
28
app/Request/Admin/LoginRequest.php
Normal file
28
app/Request/Admin/LoginRequest.php
Normal 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 [
|
||||
|
||||
];
|
||||
}
|
||||
}
|
||||
42
app/Service/Admin/BaseService.php
Normal file
42
app/Service/Admin/BaseService.php
Normal 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();
|
||||
}
|
||||
22
app/Service/Admin/User/LoginService.php
Normal file
22
app/Service/Admin/User/LoginService.php
Normal 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();
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,7 @@ return [
|
||||
'log_level' => [
|
||||
LogLevel::ALERT,
|
||||
LogLevel::CRITICAL,
|
||||
LogLevel::DEBUG,
|
||||
// LogLevel::DEBUG,
|
||||
LogLevel::EMERGENCY,
|
||||
LogLevel::ERROR,
|
||||
LogLevel::INFO,
|
||||
|
||||
Reference in New Issue
Block a user