1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-07-10 10:40:03 +08:00
xiaochong0302 cfceed678b 整理IM
2020-06-29 19:29:29 +08:00

80 lines
1.4 KiB
PHP

<?php
namespace App\Traits;
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;
trait Auth
{
/**
* @return UserModel
*/
public function getCurrentUser()
{
$authUser = $this->getAuthUser();
if (!$authUser) {
return $this->getGuestUser();
}
$userRepo = new UserRepo();
return $userRepo->findById($authUser['id']);
}
/**
* @return UserModel
*/
public function getLoginUser()
{
$authUser = $this->getAuthUser();
$validator = new AppValidator();
$validator->checkAuthUser($authUser);
$userRepo = new UserRepo();
$user = $userRepo->findById($authUser['id']);
if (time() - $user->active_time > 600) {
$user->update(['active_time' => time()]);
}
return $user;
}
/**
* @return UserModel
*/
public function getGuestUser()
{
$user = new UserModel();
$user->id = 0;
$user->name = 'guest';
$user->avatar = kg_ci_avatar_img_url(null);
return $user;
}
/**
* @return array|null
*/
public function getAuthUser()
{
/**
* @var AuthService $auth
*/
$auth = Di::getDefault()->get('auth');
return $auth->getAuthInfo();
}
}