60 lines
1.3 KiB
PHP
60 lines
1.3 KiB
PHP
<?php
|
|
/**
|
|
* This service file is part of item.
|
|
*
|
|
* @author ctexthuang
|
|
* @contact ctexthuang@qq.com
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Service\ServiceTrait\Api;
|
|
|
|
use App\Exception\ErrException;
|
|
use App\Model\User;
|
|
use Hyperf\Context\Context;
|
|
use Hyperf\Database\Model\Builder;
|
|
use Hyperf\Database\Model\Model;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
|
|
trait GetUserInfoTrait
|
|
{
|
|
/**
|
|
* @var User 注入用户模型
|
|
*/
|
|
#[Inject]
|
|
protected User $userModel;
|
|
|
|
/**
|
|
* 单例获取用户信息
|
|
* @param $userId
|
|
* @return false|Builder|Model|mixed|string
|
|
*/
|
|
protected function getUserInfo($userId): mixed
|
|
{
|
|
$key = 'user_id:' . $userId;
|
|
if (Context::has($key)) {
|
|
return Context::get($key, false);
|
|
}
|
|
|
|
$userInfo = $this->userModel->getInfoById($userId);
|
|
if (!$userInfo) {
|
|
return false;
|
|
}
|
|
|
|
Context::set($key, $userInfo);
|
|
|
|
return $userInfo;
|
|
}
|
|
|
|
/**
|
|
* @param $userId
|
|
* @return void
|
|
*/
|
|
protected function checkBindPhone($userId): void
|
|
{
|
|
$userInfo = $this->getUserInfo($userId);
|
|
|
|
if (!empty($userInfo->phone)) throw new ErrException('该账户已绑定手机号,无需重新绑定。若需更换手机号,请联系客服重置后再绑定');
|
|
}
|
|
} |