优化 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 $password = 'password';
|
||||||
|
|
||||||
|
// 保存用户信息
|
||||||
|
protected $user = null;
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->auth = config('catch.auth');
|
$this->auth = config('catch.auth');
|
||||||
@ -66,15 +69,23 @@ class CatchAuth
|
|||||||
*/
|
*/
|
||||||
public function user()
|
public function user()
|
||||||
{
|
{
|
||||||
|
if (!$this->user) {
|
||||||
switch ($this->getDriver()) {
|
switch ($this->getDriver()) {
|
||||||
case 'jwt':
|
case 'jwt':
|
||||||
$model = app($this->getProvider()['model']);
|
$model = app($this->getProvider()['model']);
|
||||||
return $model->where($model->getPk(), JWTAuth::auth()[$this->jwtKey()])->find();
|
$this->user = $model->where($model->getPk(), JWTAuth::auth()[$this->jwtKey()])->find();
|
||||||
|
break;
|
||||||
case 'session':
|
case 'session':
|
||||||
return Session::get($this->sessionUserKey(), null);
|
$this->user = Session::get($this->sessionUserKey(), null);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
throw new FailedException('user not found');
|
throw new FailedException('user not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $this->user;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->user;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user