140 lines
3.5 KiB
PHP
140 lines
3.5 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\Constants\Common\ThirdCode;
|
|
use App\Event\RegistrationEvent;
|
|
use App\Exception\ErrException;
|
|
use App\Model\UserAccount;
|
|
use App\Model\UserThird;
|
|
use App\Service\ServiceTrait\Api\PhonePoolTrait;
|
|
use App\Service\ServiceTrait\Api\WxMiniTrait;
|
|
use Hyperf\DbConnection\Db;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
use Psr\EventDispatcher\EventDispatcherInterface;
|
|
use RedisException;
|
|
|
|
class WxFastLoginService extends LoginBaseService
|
|
{
|
|
use WxMiniTrait;
|
|
use PhonePoolTrait;
|
|
|
|
/**
|
|
* @var EventDispatcherInterface
|
|
*/
|
|
#[Inject]
|
|
private EventDispatcherInterface $eventDispatcher;
|
|
|
|
/**
|
|
* 登录类型 2=微信小程序
|
|
*/
|
|
const int LOGIN_TYPE = 2;
|
|
|
|
/**
|
|
* @return array
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function handle(): array
|
|
{
|
|
$this->jsCode = $this->request->input('js_code');
|
|
$phoneCode = $this->request->input('phone_code');
|
|
|
|
if (empty($this->jsCode) || empty($phoneCode)) throw new ErrException('登录参数错误');
|
|
|
|
$wxInfo = $this->jsCodeGetOpenId($this->jsCode);
|
|
$this->openId = $wxInfo['openid'];
|
|
$this->unionId = $wxInfo['unionid'] ?? '';
|
|
|
|
$wxPhone = $this->jsCodeGetPhoneNumber($phoneCode);
|
|
$this->mobile = $wxPhone['phone_info']['purePhoneNumber'] ?? '';
|
|
if ($this->mobile == '') throw new ErrException('手机号获取失败');
|
|
|
|
$this->checkLock(self::LOGIN_TYPE);
|
|
|
|
$this->register();
|
|
|
|
$this->login();
|
|
|
|
$this->updateMobile();
|
|
|
|
$this->redis->delLock($this->lockKey);
|
|
|
|
return $this->getReturn();
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
private function updateMobile(): void
|
|
{
|
|
if (empty($this->mobile)) return;
|
|
|
|
if ($this->userInfo->mobile == $this->mobile) return;
|
|
|
|
$this->userInfo->mobile = $this->mobile;
|
|
|
|
if (!$this->userInfo->save()) throw new ErrException('更新手机号失败');
|
|
|
|
$this->addMobilePhone($this->mobile);
|
|
}
|
|
|
|
/**
|
|
* 注册
|
|
* @return void
|
|
*/
|
|
protected function register(): void
|
|
{
|
|
$thirdInfo = $this->userThirdModel->getThirdInfoByOpenId($this->openId);
|
|
if (!empty($thirdInfo)) {
|
|
$this->userId = $thirdInfo->user_id;
|
|
return;
|
|
}
|
|
|
|
// todo 设备封禁不可注册 注册限制
|
|
|
|
Db::transaction(function () {
|
|
$this->addUser();
|
|
|
|
$this->addUserThird();
|
|
|
|
$this->addAccount();
|
|
|
|
$this->addInviteCode();
|
|
|
|
// 邀请过来的 邀请方有没有奖励 被邀请方有没有奖励
|
|
$this->checkInvite();
|
|
|
|
// 有没有注册奖励 事件机制
|
|
// $this->addCoupon();
|
|
$this->eventDispatcher->dispatch(new RegistrationEvent($this->userId));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
private function addUserThird(): void
|
|
{
|
|
$model = new UserThird();
|
|
|
|
$model->user_id = $this->userId;
|
|
$model->open_id = $this->openId;
|
|
$model->union_id = $this->unionId;
|
|
$model->type = ThirdCode::WX_LOGIN;
|
|
|
|
if (!$model->save()) throw new ErrException('注册失败-00001');
|
|
}
|
|
} |