auth
This commit is contained in:
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