更新
This commit is contained in:
@@ -12,7 +12,6 @@ namespace app\middleware;
|
||||
|
||||
use think\Config;
|
||||
use think\Request;
|
||||
use think\Response;
|
||||
|
||||
class AllowCrossDomain{
|
||||
|
||||
@@ -36,6 +35,7 @@ class AllowCrossDomain{
|
||||
* @return Response
|
||||
*/
|
||||
public function handle($request, \Closure $next, ? array $header = []){
|
||||
$response = $next($request);
|
||||
$header = !empty($header) ? array_merge($this->header, $header) : $this->header;
|
||||
|
||||
if (!isset($header['Access-Control-Allow-Origin'])) {
|
||||
@@ -43,6 +43,10 @@ class AllowCrossDomain{
|
||||
$header['Access-Control-Allow-Origin'] = $origin ? $origin : "*";
|
||||
}
|
||||
|
||||
return $next($request)->header($header);
|
||||
if (strtoupper($request->method()) == "OPTIONS") {
|
||||
$response->code(204);
|
||||
}
|
||||
|
||||
return $response->header($header);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,9 +10,9 @@ declare (strict_types = 1);
|
||||
|
||||
namespace app\middleware;
|
||||
|
||||
class Api{
|
||||
use app\services\auth\UsersLogService;
|
||||
|
||||
public $data = ['code' => 1, 'data' => '', 'message' => ''];
|
||||
class Api{
|
||||
|
||||
/**
|
||||
* 处理请求
|
||||
@@ -22,22 +22,18 @@ class Api{
|
||||
* @return Response
|
||||
*/
|
||||
public function handle($request, \Closure $next){
|
||||
$request->pageConfig = array(
|
||||
'list_rows' => $request->param('pageSize', 30),
|
||||
'page' => $request->param('page', 1),
|
||||
);
|
||||
$response = $next($request);
|
||||
|
||||
if (is_array($response->getData())) {
|
||||
$this->data = array_merge($this->data, $response->getData());
|
||||
|
||||
//记录用户操作记录
|
||||
app()->make(UsersLogService::class)->record($request, $response->getCode());
|
||||
if ($request->isAjax() || is_array($response->getData())) {
|
||||
return json($response->getData());
|
||||
} else {
|
||||
$this->data = $response->getData();
|
||||
}
|
||||
|
||||
if ($request->isAjax()) {
|
||||
return json($this->data);
|
||||
} else {
|
||||
if (\is_string($this->data) && $this->data != '') {
|
||||
return $response;
|
||||
} else {
|
||||
return json($this->data);
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
61
app/middleware/Check.php
Normal file
61
app/middleware/Check.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\middleware;
|
||||
|
||||
use think\App;
|
||||
use think\Response;
|
||||
use xiaodi\JWTAuth\Exception\JWTException;
|
||||
|
||||
class Check{
|
||||
protected $app;
|
||||
|
||||
public function __construct(App $app){
|
||||
$this->app = $app;
|
||||
}
|
||||
|
||||
/**
|
||||
* @title 处理请求
|
||||
*
|
||||
* @param [type] $request
|
||||
* @param \Closure $next
|
||||
* @param [type] $store
|
||||
* @return void
|
||||
*/
|
||||
public function handle($request, \Closure $next, $store = null){
|
||||
try {
|
||||
$verify = $this->app->get('jwt')->store($store)->verify();
|
||||
if (true === $verify) {
|
||||
if ($this->app->get('jwt.user')->getBind()) {
|
||||
if ($user = $this->app->get('jwt.user')->find()) {
|
||||
// 路由注入
|
||||
$request->user = $user;
|
||||
|
||||
// 绑定当前用户模型
|
||||
$class = $this->app->get('jwt.user')->getClass();
|
||||
$this->app->bind($class, $user);
|
||||
|
||||
// 绑定用户后一些业务处理
|
||||
$this->bindUserAfter($request);
|
||||
} else {
|
||||
return Response::create(['message' => '登录校验已失效, 请重新登录', 'code' => 2000], 'json', 401);
|
||||
}
|
||||
}
|
||||
return $next($request);
|
||||
}
|
||||
} catch (JWTException $e) {
|
||||
return Response::create(['message' => $e->getMessage(), 'code' => 2000], 'json', 401);
|
||||
}
|
||||
}
|
||||
|
||||
public function bindUserAfter(){
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user