feat : login wx
This commit is contained in:
81
app/Service/Api/Login/WxFastLoginService.php
Normal file
81
app/Service/Api/Login/WxFastLoginService.php
Normal file
@@ -0,0 +1,81 @@
|
||||
<?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\Exception\ApiException;
|
||||
use App\Model\UserThird;
|
||||
use App\Service\ServiceTrait\Api\WxMiniTrait;
|
||||
use Hyperf\DbConnection\Db;
|
||||
|
||||
class WxFastLoginService extends LoginBaseService
|
||||
{
|
||||
use WxMiniTrait;
|
||||
|
||||
/**
|
||||
* 登录类型 2=微信小程序
|
||||
*/
|
||||
const int LOGIN_TYPE = 2;
|
||||
|
||||
|
||||
public function handle()
|
||||
{
|
||||
$this->jsCode = $this->request->input('js_code');
|
||||
|
||||
if (empty($this->jsCode)) throw new ApiException('登录参数错误');
|
||||
|
||||
$wxInfo = $this->jsCodeGetOpenId($this->jsCode);
|
||||
$this->openId = $wxInfo['openid'];
|
||||
$this->unionId = $wxInfo['unionid'];
|
||||
|
||||
$this->checkLock(self::LOGIN_TYPE);
|
||||
|
||||
$this->register();
|
||||
|
||||
$this->login();
|
||||
|
||||
$this->redis->delLock($this->lockKey);
|
||||
|
||||
return $this->getReturn();
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
//todo 要不要生成邀请码 有没有注册奖励
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
private function addUserThird(): void
|
||||
{
|
||||
$model = new UserThird();
|
||||
|
||||
$model->user_id = $this->userId;
|
||||
$model->open_id = $this->openId;
|
||||
$model->union_id = $this->unionId;
|
||||
|
||||
if (!$model->save()) throw new ApiException('注册失败-00001');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user