auth
This commit is contained in:
@ -6,6 +6,8 @@ use App\Const\RedisConst;
|
||||
use App\Tools\Tools;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\Redis;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
|
||||
class AuthService
|
||||
{
|
||||
@ -52,7 +54,7 @@ class AuthService
|
||||
function getAllTokenInfoByUid($iUid)
|
||||
{
|
||||
$sTokenList = Redis::get(RedisConst::UID_TOKENS . $iUid);
|
||||
if (empty($sToken)) return null;
|
||||
if (empty($sTokenList)) return null;
|
||||
return unserialize($sTokenList);
|
||||
}
|
||||
|
||||
@ -140,6 +142,10 @@ class AuthService
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
function getCurrentUser()
|
||||
{
|
||||
if(app()->has('customerUser')){
|
||||
|
||||
80
app/Service/ImService.php
Normal file
80
app/Service/ImService.php
Normal file
@ -0,0 +1,80 @@
|
||||
<?php
|
||||
namespace App\Service;
|
||||
|
||||
use App\Const\Im;
|
||||
use App\Exceptions\AppException;
|
||||
use Illuminate\Support\Env;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
class ImService{
|
||||
const user_register_data = [
|
||||
'secret' => '',
|
||||
'users' => [
|
||||
'userID' => '',
|
||||
'nickname' => '',
|
||||
'faceURL' => '',
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* @throws AppException
|
||||
*/
|
||||
function userUserRegister($userId, $nickname = '', $faceUrl=''): bool
|
||||
{
|
||||
$path = '/user/user_register';
|
||||
$aData['users'] = [
|
||||
'userID' => $this->genImUid($userId),
|
||||
'nickname' => $nickname,
|
||||
'faceURL' => $faceUrl,
|
||||
];
|
||||
$jRes = $this->sendReq($path,$aData);
|
||||
if(isset($jRes['errCode']) && $jRes['errCode'] === 0) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws AppException
|
||||
*/
|
||||
function authUserToken($userId,int $platform): false|array
|
||||
{
|
||||
$path = '/auth/user_token';
|
||||
$aData['users'] = [
|
||||
'platformID' => $platform,
|
||||
'userID' => $this->genImUid($userId),
|
||||
];
|
||||
$jRes = $this->sendReq($path,$aData);
|
||||
if(isset($jRes['errCode']) && $jRes['errCode'] === 0){
|
||||
return [
|
||||
'token' => $jRes['token'],
|
||||
'expireTimeSeconds' => $jRes['expireTimeSeconds'],
|
||||
];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function getSecret()
|
||||
{
|
||||
return Env::get('IM_SECRET');
|
||||
}
|
||||
|
||||
function getUrl(): string
|
||||
{
|
||||
return rtrim(Env::get('IM_URL'),'/');
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws AppException
|
||||
*/
|
||||
function sendReq(string $path,array $aData = [])
|
||||
{
|
||||
if (!isset($aData['secret'])) $aData['secret'] = $this->getSecret();
|
||||
$resp = Http::post($this->getUrl().$path,$aData);
|
||||
if(!$resp->ok()) throw new AppException('im request error');
|
||||
return $resp->json();
|
||||
}
|
||||
|
||||
function genImUid($userId): string
|
||||
{
|
||||
return Im::USER_PREFIX.$userId;
|
||||
}
|
||||
}
|
||||
141
app/Service/VrCodeService.php
Normal file
141
app/Service/VrCodeService.php
Normal file
@ -0,0 +1,141 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Const\RedisConst;
|
||||
use App\Const\VrCode;
|
||||
use App\Exceptions\AppException;
|
||||
use App\Tools\Tools;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\Redis;
|
||||
|
||||
class VrCodeService
|
||||
{
|
||||
public $sKey = null;
|
||||
public $sCode = null;
|
||||
public $sPhoneArea = null;
|
||||
public $sPhone = null;
|
||||
public $iType = null;
|
||||
public $iUid = null;
|
||||
|
||||
public function getSKey(): null
|
||||
{
|
||||
return $this->sKey;
|
||||
}
|
||||
|
||||
public function setSKey(null $sKey): void
|
||||
{
|
||||
$this->sKey = $sKey;
|
||||
}
|
||||
|
||||
public function getsCode(): null
|
||||
{
|
||||
return $this->sCode;
|
||||
}
|
||||
|
||||
public function setsCode(null $sCode): void
|
||||
{
|
||||
$this->sCode = $sCode;
|
||||
}
|
||||
|
||||
public function getSPhoneArea(): null
|
||||
{
|
||||
return $this->sPhoneArea;
|
||||
}
|
||||
|
||||
public function setSPhoneArea(null $sPhoneArea): void
|
||||
{
|
||||
$this->sPhoneArea = $sPhoneArea;
|
||||
}
|
||||
|
||||
public function getSPhone(): null
|
||||
{
|
||||
return $this->sPhone;
|
||||
}
|
||||
|
||||
public function setSPhone(null $sPhone): void
|
||||
{
|
||||
$this->sPhone = $sPhone;
|
||||
}
|
||||
|
||||
public function getIType(): null
|
||||
{
|
||||
return $this->iType;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws AppException
|
||||
*/
|
||||
public function setIType(null $iType): void
|
||||
{
|
||||
if(!in_array($iType,VrCode::TOPIC)) throw new AppException('invalid sms type');
|
||||
$this->iType = $iType;
|
||||
}
|
||||
|
||||
public function getIUid(): null
|
||||
{
|
||||
return $this->iUid;
|
||||
}
|
||||
|
||||
public function setIUid(null $iUid): void
|
||||
{
|
||||
$this->iUid = $iUid;
|
||||
}
|
||||
|
||||
function sendSmsToPhone(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws AppException
|
||||
*/
|
||||
function sendCode(): void
|
||||
{
|
||||
$this->sKey = $this->genKey();
|
||||
$this->sCode = $this->genCode();
|
||||
if(!$this->sendSmsToPhone()) throw new AppException('send sms failed');
|
||||
$this->setCodeToCache();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws AppException
|
||||
*/
|
||||
function genKey(): false|string
|
||||
{
|
||||
if(!in_array($this->iType,VrCode::TOPIC_PREFIX)) throw new AppException('invalid sms type');
|
||||
if($this->iType == VrCode::TOPIC_REGISTER) {
|
||||
return VrCode::TOPIC_PREFIX[VrCode::TOPIC_REGISTER] . md5($this->sPhoneArea . $this->sPhone);
|
||||
}else {
|
||||
return VrCode::TOPIC_PREFIX[VrCode::TOPIC_REGISTER] . $this->iUid;
|
||||
}
|
||||
}
|
||||
|
||||
function genCode(): string
|
||||
{
|
||||
return (string)Tools::getRandNum();
|
||||
}
|
||||
|
||||
function setCodeToCache()
|
||||
{
|
||||
return Redis::set($this->sKey,$this->sCode,VrCode::REDIS_CACHE_EXPIRE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws AppException
|
||||
*/
|
||||
function getCodeFromCache()
|
||||
{
|
||||
return Redis::get($this->genKey());
|
||||
}
|
||||
|
||||
function checkCode($sReqCode): bool
|
||||
{
|
||||
$sCode = $this->getCodeFromCache();
|
||||
if(empty($sCode)) return false;
|
||||
if($sCode != $sReqCode) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user