1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-26 20:52:44 +08:00

Merge branch 'koogua/hotfix-user-cache' into demo

This commit is contained in:
koogua 2021-03-03 11:34:35 +08:00
commit 1ebe649236
4 changed files with 31 additions and 9 deletions

View File

@ -191,6 +191,7 @@ class DeliverTask extends Task
$user = $userRepo->findById($order->owner_id);
$user->vip_expiry_time = $itemInfo['vip']['expiry_time'];
$user->vip = 1;
if ($user->update() === false) {
throw new \RuntimeException('Update Vip Expiry Failed');

View File

@ -51,7 +51,7 @@ class Controller extends \Phalcon\Mvc\Controller
public function beforeExecuteRoute(Dispatcher $dispatcher)
{
$this->siteInfo = $this->getSiteInfo();
$this->authUser = $this->getAuthUser();
$this->authUser = $this->getAuthUser(true);
if ($this->siteInfo['status'] == 'closed') {
$dispatcher->forward([
@ -90,14 +90,14 @@ class Controller extends \Phalcon\Mvc\Controller
$this->view->setVar('im_info', $this->imInfo);
}
protected function getAuthUser()
protected function getAuthUser($cache = false)
{
/**
* @var HomeAuth $auth
*/
$auth = $this->getDI()->get('auth');
return $auth->getCurrentUser();
return $auth->getCurrentUser($cache);
}
protected function getSeo()

View File

@ -44,6 +44,10 @@ class UserConsoleController extends Controller
public function initialize()
{
parent::initialize();
$authUser = $this->getAuthUser(false);
$this->view->setVar('auth_user', $authUser);
}
/**

View File

@ -3,7 +3,9 @@
namespace App\Traits;
use App\Caches\User as UserCache;
use App\Exceptions\Unauthorized as UnauthorizedException;
use App\Models\User as UserModel;
use App\Repos\User as UserRepo;
use App\Services\Auth as AuthService;
use App\Validators\Validator as AppValidator;
use Phalcon\Di as Di;
@ -12,9 +14,10 @@ trait Auth
{
/**
* @param bool $cache
* @return UserModel
*/
public function getCurrentUser()
public function getCurrentUser($cache = false)
{
$authUser = $this->getAuthUser();
@ -22,15 +25,23 @@ trait Auth
return $this->getGuestUser();
}
$userCache = new UserCache();
if ($cache == false) {
$userRepo = new UserRepo();
$user = $userRepo->findById($authUser['id']);
} else {
$userCache = new UserCache();
$user = $userCache->get($authUser['id']);
}
return $userCache->get($authUser['id']);
return $user;
}
/**
* @param bool $cache
* @return UserModel
* @throws UnauthorizedException
*/
public function getLoginUser()
public function getLoginUser($cache = false)
{
$authUser = $this->getAuthUser();
@ -38,9 +49,15 @@ trait Auth
$validator->checkAuthUser($authUser['id']);
$userCache = new UserCache();
if ($cache == false) {
$userRepo = new UserRepo();
$user = $userRepo->findById($authUser['id']);
} else {
$userCache = new UserCache();
$user = $userCache->get($authUser['id']);
}
return $userCache->get($authUser['id']);
return $user;
}
/**