first commit

This commit is contained in:
wuyanwen
2019-12-02 23:04:43 +08:00
commit 40676f8b14
70 changed files with 2855 additions and 0 deletions

1
app/.htaccess Normal file
View File

@@ -0,0 +1 @@
deny from all

118
app/BaseController.php Normal file
View File

@@ -0,0 +1,118 @@
<?php
declare (strict_types = 1);
namespace app;
use catcher\CatchAdmin;
use think\App;
use think\exception\ValidateException;
use think\facade\View;
use think\helper\Str;
use think\Validate;
/**
* 控制器基础类
*/
abstract class BaseController
{
/**
* Request实例
* @var \think\Request
*/
protected $request;
/**
* 应用实例
* @var \think\App
*/
protected $app;
/**
* 是否批量验证
* @var bool
*/
protected $batchValidate = false;
/**
* 控制器中间件
* @var array
*/
protected $middleware = [];
/**
* 构造方法
* @access public
* @param App $app 应用对象
*/
public function __construct(App $app)
{
$this->app = $app;
$this->request = $this->app->request;
// 控制器初始化
$this->initialize();
}
// 初始化
protected function initialize()
{}
/**
* 验证数据
* @access protected
* @param array $data 数据
* @param string|array $validate 验证器名或者验证规则数组
* @param array $message 提示信息
* @param bool $batch 是否批量验证
* @return array|string|true
* @throws ValidateException
*/
protected function validate(array $data, $validate, array $message = [], bool $batch = false)
{
if (is_array($validate)) {
$v = new Validate();
$v->rule($validate);
} else {
if (strpos($validate, '.')) {
// 支持场景
list($validate, $scene) = explode('.', $validate);
}
$class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
$v = new $class();
if (!empty($scene)) {
$v->scene($scene);
}
}
$v->message($message);
// 是否批量验证
if ($batch || $this->batchValidate) {
$v->batch(true);
}
return $v->failException(true)->check($data);
}
/**
*
* @time 2019年11月28日
* @param string $template
* @throws \Exception
* @return string
*/
protected function fetch($template = ''): string
{
if (Str::contains($template, '::')) {
[$module, $template] = explode('::', $template);
View::config([
'view_path' => CatchAdmin::getViews()[$module]
]);
return View::fetch($template);
}
return View::fetch($template);
}
}

61
app/ExceptionHandle.php Normal file
View File

@@ -0,0 +1,61 @@
<?php
namespace app;
use catcher\JsonResponse;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\Handle;
use think\exception\HttpException;
use think\exception\HttpResponseException;
use think\exception\ValidateException;
use think\Response;
use Throwable;
/**
* 应用异常处理类
*/
class ExceptionHandle extends Handle
{
/**
* 不需要记录信息(日志)的异常类列表
* @var array
*/
protected $ignoreReport = [
HttpException::class,
HttpResponseException::class,
ModelNotFoundException::class,
DataNotFoundException::class,
ValidateException::class,
];
/**
* 记录异常信息(包括日志或者其它方式记录)
*
* @access public
* @param Throwable $exception
* @return void
*/
public function report(Throwable $exception): void
{
// 使用内置的方式记录异常日志
parent::report($exception);
}
/**
* Render an exception into an HTTP response.
*
* @access public
* @param \think\Request $request
* @param Throwable $e
* @return Response
*/
public function render($request, Throwable $e): Response
{
// 添加自定义异常处理机制
if ($request->isAjax()) {
return JsonResponse::fail($e->getMessage(), $e->getCode());
}
// 其他错误交给系统处理
return parent::render($request, $e);
}
}

40
app/Request.php Normal file
View File

@@ -0,0 +1,40 @@
<?php
namespace app;
// 应用请求对象类
use app\exceptions\ValidateFailedException;
class Request extends \think\Request
{
/**
* Request constructor.
* @throws \Exception
*/
public function __construct()
{
parent::__construct();
$this->validate();
}
/**
* 初始化验证
*
* @time 2019年11月27日
* @throws \Exception
* @return mixed
*/
protected function validate()
{
if (method_exists($this, 'getValidate')) {
$validate = $this->getValidate();
if (!$validate->check(request()->param())) {
throw new ValidateFailedException($validate->getError());
}
return true;
}
return true;
}
}

2
app/common.php Normal file
View File

@@ -0,0 +1,2 @@
<?php
// 应用公共文件

20
app/event.php Normal file
View File

@@ -0,0 +1,20 @@
<?php
// 事件定义文件
return [
'bind' => [
],
'listen' => [
'AppInit' => [],
'HttpRun' => [],
'HttpEnd' => [],
'LogLevel' => [],
'LogWrite' => [],
'RouteLoaded' => [
\app\event\LoadModuleRoutes::class,
],
],
'subscribe' => [
],
];

View File

@@ -0,0 +1,25 @@
<?php
declare (strict_types = 1);
namespace app\event;
use catcher\CatchAdmin;
use think\Route;
class LoadModuleRoutes
{
/**
* 处理
*
* @time 2019年11月29日
* @return void
*/
public function handle(): void
{
$router = app(Route::class);
$router->group(function () use ($router) {
include CatchAdmin::getRoutes();
});
}
}

View File

@@ -0,0 +1,7 @@
<?php
namespace app\exceptions;
class LoginFailedException extends \Exception
{
protected $code = 10002;
}

View File

@@ -0,0 +1,7 @@
<?php
namespace app\exceptions;
class ValidateFailedException extends \Exception
{
protected $code = 10001;
}

10
app/middleware.php Normal file
View File

@@ -0,0 +1,10 @@
<?php
// 全局中间件定义文件
return [
// 全局请求缓存
// \think\middleware\CheckRequestCache::class,
// 多语言加载
// \think\middleware\LoadLangPack::class,
// Session初始化
\think\middleware\SessionInit::class
];

9
app/provider.php Normal file
View File

@@ -0,0 +1,9 @@
<?php
use app\ExceptionHandle;
use app\Request;
// 容器Provider定义文件
return [
'think\Request' => Request::class,
'think\exception\Handle' => ExceptionHandle::class,
];

5
app/service.php Normal file
View File

@@ -0,0 +1,5 @@
<?php
return [
\jaguarjack\think\module\ThinkModuleService::class,
\catchAdmin\CatchAdminService::class,
];