优化 auth 单例获取用户,防止多次查询
This commit is contained in:
parent
167ea743a0
commit
fa04341608
13
catch/cms/controller/Test.php
Normal file
13
catch/cms/controller/Test.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
namespace catchAdmin\cms\controller;
|
||||
|
||||
use catcher\base\CatchController;
|
||||
use catcher\CatchResponse;
|
||||
|
||||
class TestController extends CatchController
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
return CatchResponse::success('Hello CatchAdmin');
|
||||
}
|
||||
}
|
@ -17,6 +17,9 @@ class CatchAuth
|
||||
// 校验字段
|
||||
protected $password = 'password';
|
||||
|
||||
// 保存用户信息
|
||||
protected $user = null;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->auth = config('catch.auth');
|
||||
@ -66,15 +69,23 @@ class CatchAuth
|
||||
*/
|
||||
public function user()
|
||||
{
|
||||
switch ($this->getDriver()) {
|
||||
case 'jwt':
|
||||
$model = app($this->getProvider()['model']);
|
||||
return $model->where($model->getPk(), JWTAuth::auth()[$this->jwtKey()])->find();
|
||||
case 'session':
|
||||
return Session::get($this->sessionUserKey(), null);
|
||||
default:
|
||||
throw new FailedException('user not found');
|
||||
if (!$this->user) {
|
||||
switch ($this->getDriver()) {
|
||||
case 'jwt':
|
||||
$model = app($this->getProvider()['model']);
|
||||
$this->user = $model->where($model->getPk(), JWTAuth::auth()[$this->jwtKey()])->find();
|
||||
break;
|
||||
case 'session':
|
||||
$this->user = Session::get($this->sessionUserKey(), null);
|
||||
break;
|
||||
default:
|
||||
throw new FailedException('user not found');
|
||||
}
|
||||
|
||||
return $this->user;
|
||||
}
|
||||
|
||||
return $this->user;
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user