Files
hyperf_service/app/Service/Api/Login/LoginBaseService.php
2024-11-29 16:36:47 +08:00

167 lines
3.7 KiB
PHP

<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Api\Login;
use App\Cache\Redis\Api\ApiRedisKey;
use App\Cache\Redis\RedisCache;
use App\Constants\Common\UserCode;
use App\Exception\ErrException;
use App\Extend\StringUtil;
use App\Extend\SystemUtil;
use App\Lib\Crypto\CryptoFactory;
use App\Model\User;
use App\Model\UserThird;
use App\Service\Api\BaseService;
use App\Service\ServiceTrait\Api\GetUserInfoTrait;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
abstract class LoginBaseService extends BaseService
{
use GetUserInfoTrait;
/**
* 注入缓存类
* @var RedisCache
*/
#[Inject]
protected RedisCache $redis;
/**
* 注入加密工厂
* @var CryptoFactory
*/
#[Inject]
protected CryptoFactory $cryptoFactory;
/**
* 注入三方用户
* @var UserThird
*/
#[Inject]
protected UserThird $userThirdModel;
/**
* 锁定注册
* @var string
*/
protected string $lockKey;
/**
* 登录code login_type = wx_login 必填
* @var string
*/
protected string $jsCode = '';
/**
* 手机号 login_type = mobile_code 必填
* @var string
*/
protected string $mobile = '';
/**
* 获取的用户信息
* @var
*/
protected mixed $userInfo;
/**
* @var string
*/
protected string $openId = '';
/**
* @var string
*/
protected string $unionId = '';
/**
* 登录
* @return void
*/
protected function login(): void
{
$this->userId = empty($this->userId) ? $this->userInfo->id : $this->userId;
$this->userInfo = $this->getUserInfo($this->userId);
if (empty($this->userId)) {
throw new ErrException('登录失败');
}
//todo 判断注销 判断封号
//todo 更新登录时间
}
/**
* 返回值
* @return array
* @throws \Exception
*/
protected function getReturn():array
{
$loginReturn = [
'id' => $this->userId,
'is_bind_mobile' => $this->userInfo->mobile ? UserCode::IS_BIND_PHONE : UserCode::IS_NOT_BIND_PHONE,
'nickName' => $this->userInfo->nickName,
'is_default_avatar' => $this->userInfo->avatar_id == 0 ? UserCode::IS_DEFAULT_AVATAR : UserCode::IS_NOT_DEFAULT_AVATAR,
];
$loginReturn['token'] = $this->cryptoFactory->cryptoClass('jwt', json_encode($loginReturn))->encrypt();
return $loginReturn;
}
/**
* 判断是不是没有加锁
* @param $type
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws \RedisException
*/
protected function checkLock($type): void
{
$this->lockKey = match ($type){
1 => ApiRedisKey::LoginAndRegisterByMobileLock($this->mobile),
2 => ApiRedisKey::LoginAndRegisterByCodeLock($this->jsCode)
};
if (0 == ($this->redis->addLock($this->lockKey))){
throw new ErrException('请勿重复点击');
}
}
/**
* 添加用户
* @return void
*/
protected function addUser(): void
{
$model = new User();
//默认头像和默认名称
$model->nickname = '用户'.StringUtil::randStr(6);
$model->avatar_id = 0;
$model->reg_ip = SystemUtil::getClientIp();
if (!$model->save()) throw new ErrException('数据保存失败-注册失败');
$this->userId = $model->id;
$this->userInfo = $model;
}
abstract protected function register();
}