🎨 改进中间件

This commit is contained in:
WispX
2020-03-17 13:18:36 +08:00
parent 54627d6647
commit af5bd11c8f
9 changed files with 78 additions and 38 deletions
+8 -1
View File
@@ -10,6 +10,7 @@ namespace app\api\controller;
use think\Db;
use think\Exception;
use think\exception\ErrorException;
class Upload extends Base
{
@@ -42,12 +43,18 @@ class Upload extends Base
Db::startTrans();
try {
$data = (new \app\index\controller\Upload)->execute($this->user);
$data = (new \app\common\controller\Upload)->exec();
Db::commit();
} catch (Exception $e) {
Db::rollback();
return $this->response($e->getMessage(), [], 500);
} catch (ErrorException $e) {
Db::rollback();
return $this->response($e->getMessage(), [], 500);
} catch (\Throwable $e) {
Db::rollback();
return $this->response($e->getMessage(), [], 500);
}
return $this->response('success', $data);
}
@@ -0,0 +1,17 @@
<?php
namespace app\http\middleware;
use think\Request;
class ApiAuthenticate
{
public function handle(Request $request, \Closure $next)
{
$controller = $request->controller(true);
$action = $request->action(true);
$path = $controller . '/' . $action;
return $next($request);
}
}
+1 -28
View File
@@ -2,30 +2,12 @@
namespace app\http\middleware;
use app\common\model\Users;
use think\facade\Session;
use think\Request;
class Initialize
{
/**
* 允许不登录访问的路径
*
* @var array
*/
private $paths = [
'index/index',
'auth/login',
'auth/register',
'index/api'
];
public function handle(Request $request, \Closure $next)
{
$controller = $request->controller(true);
$action = $request->action(true);
$path = $controller . '/' . $action;
// 检测程序是否已安装
if (!file_exists(app()->getAppPath() . 'install.lock')) {
if ($request->controller(true) !== 'install' || $request->action(true) !== 'index') {
@@ -33,16 +15,7 @@ class Initialize
}
}
$user = null;
if ($uid = Session::get('uid')) {
$user = Users::get($uid);
}
if (!in_array($path, $this->paths) && !$user) {
return redirect(url('/auth/login'));
}
$request->user = $user;
// ...
return $next($request);
}
@@ -0,0 +1,42 @@
<?php
namespace app\http\middleware;
use app\common\model\Users;
use think\facade\Session;
use think\Request;
class WebAuthenticate
{
/**
* 允许不登录访问的路径
*
* @var array
*/
private $paths = [
'index/index',
'auth/login',
'auth/register',
'index/api'
];
public function handle(Request $request, \Closure $next)
{
$controller = $request->controller(true);
$action = $request->action(true);
$path = $controller . '/' . $action;
$user = null;
if ($uid = Session::get('uid')) {
$user = Users::get($uid);
}
if (!in_array($path, $this->paths) && !$user) {
return redirect(url('/auth/login'));
}
$request->user = $user;
return $next($request);
}
}
@@ -12,4 +12,7 @@
// +----------------------------------------------------------------------
// | 中间件配置
// +----------------------------------------------------------------------
return [app\http\middleware\Initialize::class];
return [
app\http\middleware\Initialize::class,
app\http\middleware\WebAuthenticate::class
];
+2 -2
View File
@@ -3,7 +3,7 @@
{block name="title"}登录 - {$config.site_name}{/block}
{block name="css"}
<link href="/static/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/css/bootstrap.min.css" rel="stylesheet">
{/block}
{block name="main"}
@@ -49,5 +49,5 @@
{/block}
{block name="js"}
<script src="/static/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/js/bootstrap.min.js"></script>
{/block}
+2 -2
View File
@@ -3,7 +3,7 @@
{block name="title"}注册 - {$config.site_name}{/block}
{block name="css"}
<link href="/static/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/css/bootstrap.min.css" rel="stylesheet">
{/block}
{block name="main"}
@@ -67,5 +67,5 @@
{/block}
{block name="js"}
<script src="/static/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/js/bootstrap.min.js"></script>
{/block}
+1 -3
View File
@@ -12,6 +12,4 @@
// +----------------------------------------------------------------------
// | 中间件配置
// +----------------------------------------------------------------------
return [
'Initialize' => app\http\middleware\Initialize::class,
];
return [];
+1 -1
View File
@@ -14,7 +14,7 @@ use think\facade\Route;
Route::view('compatibility', 'index@tpl/compatibility');
// [Api Route]
Route::group('api', function () {
Route::name('api')->group('api', function () {
Route::any('token', 'api/Token/index');
Route::any('upload', 'api/Upload/index');
Route::any('image', 'api/Image/find');