优化 auth 单例获取用户,防止多次查询

This commit is contained in:
JaguarJack 2020-05-23 11:10:28 +08:00
parent 167ea743a0
commit fa04341608
2 changed files with 32 additions and 8 deletions

View 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');
}
}

View File

@ -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;
}
/**