309 lines
8.1 KiB
PHP
309 lines
8.1 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\Api\UserCache;
|
|
use App\Cache\Redis\Common\ConfigCache;
|
|
use App\Cache\Redis\RedisCache;
|
|
use App\Constants\Common\CouponCode;
|
|
use App\Constants\Common\UserCode;
|
|
use App\Constants\ConfigCode;
|
|
use App\Exception\ErrException;
|
|
use App\Extend\StringUtil;
|
|
use App\Extend\SystemUtil;
|
|
use App\Lib\Crypto\CryptoFactory;
|
|
use App\Model\AdminUser;
|
|
use App\Model\CouponTemplate;
|
|
use App\Model\User;
|
|
use App\Model\UserAccount;
|
|
use App\Model\UserCoupon;
|
|
use App\Model\UserInvite;
|
|
use App\Model\UserThird;
|
|
use App\Service\Api\BaseService;
|
|
use App\Service\ServiceTrait\Api\GetUserInfoTrait;
|
|
use App\Service\ServiceTrait\Common\UserTrait;
|
|
use Exception;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
use Random\RandomException;
|
|
|
|
abstract class LoginBaseService extends BaseService
|
|
{
|
|
use GetUserInfoTrait,
|
|
UserTrait;
|
|
|
|
/**
|
|
* 注入缓存类
|
|
* @var RedisCache
|
|
*/
|
|
#[Inject]
|
|
protected RedisCache $redis;
|
|
|
|
/**
|
|
* 注入加密工厂
|
|
* @var CryptoFactory
|
|
*/
|
|
#[Inject]
|
|
protected CryptoFactory $cryptoFactory;
|
|
|
|
/**
|
|
* 注入三方用户
|
|
* @var UserThird
|
|
*/
|
|
#[Inject]
|
|
protected UserThird $userThirdModel;
|
|
|
|
/**
|
|
* @var ConfigCache
|
|
*/
|
|
#[Inject]
|
|
protected ConfigCache $configCache;
|
|
|
|
/**
|
|
* @var UserInvite $userInviteModel
|
|
*/
|
|
#[Inject]
|
|
protected UserInvite $userInviteModel;
|
|
|
|
/**
|
|
* @var UserCache
|
|
*/
|
|
#[Inject]
|
|
protected UserCache $userCache;
|
|
|
|
|
|
/**
|
|
* 锁定注册
|
|
* @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 = '';
|
|
|
|
/**
|
|
* @var int 是否新人弹窗
|
|
*/
|
|
protected int $isNewcomerCouponPopUp = 0;
|
|
|
|
/**
|
|
* 登录
|
|
* @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 更新登录时间
|
|
}
|
|
|
|
/**
|
|
* @var AdminUser
|
|
*/
|
|
#[Inject]
|
|
protected AdminUser $adminUserModel;
|
|
|
|
/**
|
|
* 返回值
|
|
* @return array
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
protected function getReturn():array
|
|
{
|
|
$adminUserInfo = $this->adminUserModel->getAdminInfoByBindUserId($this->userId);
|
|
|
|
$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,
|
|
'role' => $adminUserInfo->role_id ?? 0,
|
|
// 'is_newcomer_coupon_pop_up' => $this->isNewcomerCouponPopUp,
|
|
];
|
|
|
|
$loginReturn['token'] = $this->cryptoFactory->cryptoClass('jwt', json_encode($loginReturn))->encrypt();
|
|
$this->userCache->setUserToken($this->userId, $loginReturn['token']);
|
|
|
|
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* 添加账户
|
|
* @return void
|
|
*/
|
|
protected function addAccount(): void
|
|
{
|
|
$model = new UserAccount();
|
|
|
|
$model->user_id = $this->userId;
|
|
$model->balance = 0;
|
|
$model->integral = 0;
|
|
|
|
if (!$model->save()) throw new ErrException('注册失败-00002');
|
|
}
|
|
|
|
// /**
|
|
// * @var CouponTemplate $couponTemplateModel
|
|
// */
|
|
// #[Inject]
|
|
// protected CouponTemplate $couponTemplateModel;
|
|
|
|
// /**
|
|
// * @return void
|
|
// * @throws ContainerExceptionInterface
|
|
// * @throws NotFoundExceptionInterface
|
|
// */
|
|
// protected function addCoupon(): void
|
|
// {
|
|
// $couponTemplateId = $this->configCache->getConfigValueByKey(ConfigCode::COUPONS_FOR_NEWCOMERS);
|
|
// $couponValidity = $this->configCache->getConfigValueByKey(ConfigCode::NEWBIE_COUPON_VALIDITY);
|
|
//
|
|
// // 随便一个为0代表不赠送
|
|
// if ($couponValidity == 0 || $couponTemplateId == 0) return;
|
|
//
|
|
// $couponTemplateId = explode(',', $couponTemplateId);
|
|
// $couponTemplateList = $this->couponTemplateModel->getDataByIds($couponTemplateId);
|
|
//
|
|
// $insertCoupon = [];
|
|
// foreach ($couponTemplateId as $one){
|
|
// if ($couponTemplateList[$one]['status'] == CouponCode::COUPON_TEMPLATE_STATUS_ENABLE) continue;
|
|
//
|
|
// $insertCoupon[] = [
|
|
// 'coupon_template_id' => $one,
|
|
// 'coupon_dispense_id' => CouponCode::SYSTEMIC_DISTRIBUTION,
|
|
// 'user_id' => $this->userId,
|
|
// 'status' => CouponCode::COUPON_STATUS_UNUSED,
|
|
// 'coupon_name' => $couponTemplateList[$one]['name'],
|
|
// 'validity_start_time' => date('Y-m-d H:i:s'),
|
|
// 'validity_end_time' => date('Y-m-d', strtotime('+'.$couponValidity.' days')),
|
|
// ];
|
|
// }
|
|
//
|
|
// if (empty($insertCoupon)) return;
|
|
//
|
|
// if (!(new UserCoupon)->insert($insertCoupon)) throw new ErrException('注册失败-00003');
|
|
//
|
|
// $this->isNewcomerCouponPopUp = 1;
|
|
// }
|
|
|
|
/**
|
|
* @return void
|
|
* @throws RandomException
|
|
*/
|
|
protected function addInviteCode(): void
|
|
{
|
|
$inviteCode = $this->generateStableCode($this->userId);
|
|
|
|
if ($this->userModel->checkInviteCodeIsUse($inviteCode) == 1) return;
|
|
|
|
if (!$this->userModel->where('id', $inviteCode)->update(['invite_code' => $inviteCode])) throw new ErrException('注册失败-00004');
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
protected function checkInvite(): void
|
|
{
|
|
if (!$this->request->input('invite_code')) return;
|
|
|
|
$userId = $this->userModel->getUserIdByInviteCode($this->request->input('invite_code'));
|
|
|
|
if (empty($userId)) return;
|
|
|
|
if ($this->userInviteModel->checkIsInvite($this->userId) == 1) return;
|
|
|
|
$userInviteModel = new UserInvite();
|
|
|
|
$userInviteModel->user_id = $this->userId;
|
|
$userInviteModel->invitee_user_id = $userId;
|
|
$userInviteModel->channel = UserCode::INVITE_CHANNEL_USER; //todo 预先普通用户渠道
|
|
$userInviteModel->status = UserCode::INVITE_STATUS_REGISTER;
|
|
|
|
if (!$userInviteModel->save()) throw new ErrException('注册失败-00005');
|
|
}
|
|
|
|
abstract protected function register();
|
|
} |