feat(controller): add app base controller
Made-with: Cursor
This commit is contained in:
78
pro_v3.5.1/app/common/controller/AppBaseController.php
Normal file
78
pro_v3.5.1/app/common/controller/AppBaseController.php
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
<?php
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
// | Author: ScottPan Team
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
|
||||||
|
namespace app\common\controller;
|
||||||
|
|
||||||
|
use app\Request;
|
||||||
|
use think\App;
|
||||||
|
use think\Response;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 项目自有控制器基类,不依赖 CRMEB 商业授权基础类。
|
||||||
|
*/
|
||||||
|
abstract class AppBaseController
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var App
|
||||||
|
*/
|
||||||
|
protected App $app;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Request
|
||||||
|
*/
|
||||||
|
protected Request $request;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否批量验证
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
protected bool $batchValidate = false;
|
||||||
|
|
||||||
|
public function __construct(App $app)
|
||||||
|
{
|
||||||
|
$this->app = $app;
|
||||||
|
$this->request = $app->request;
|
||||||
|
|
||||||
|
$this->initialize();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始化钩子,子类可覆盖。
|
||||||
|
*/
|
||||||
|
protected function initialize()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function success($message = 'ok', array $data = []): Response
|
||||||
|
{
|
||||||
|
return app('json')->success($message, $data);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function fail($message = 'fail', array $data = []): Response
|
||||||
|
{
|
||||||
|
return app('json')->fail($message, $data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 兼容项目控制器中 `$this->validate($data, ValidateClass::class, 'scene')` 的调用方式。
|
||||||
|
*
|
||||||
|
* @param array $data
|
||||||
|
* @param string|array $validate
|
||||||
|
* @param string|array|null $scene
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
protected function validate(array $data, $validate, $scene = null): bool
|
||||||
|
{
|
||||||
|
$validator = validate($validate)->batch($this->batchValidate);
|
||||||
|
|
||||||
|
if (is_string($scene) && $scene !== '') {
|
||||||
|
$validator->scene($scene);
|
||||||
|
} elseif (is_array($scene)) {
|
||||||
|
$validator->message($scene);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $validator->check($data);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user