146 lines
3.0 KiB
PHP
146 lines
3.0 KiB
PHP
<?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\Env;
|
|
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()
|
|
{
|
|
return $this->sKey;
|
|
}
|
|
|
|
public function setSKey($sKey): void
|
|
{
|
|
$this->sKey = $sKey;
|
|
}
|
|
|
|
public function getsCode()
|
|
{
|
|
return $this->sCode;
|
|
}
|
|
|
|
public function setsCode($sCode): void
|
|
{
|
|
$this->sCode = $sCode;
|
|
}
|
|
|
|
public function getSPhoneArea()
|
|
{
|
|
return $this->sPhoneArea;
|
|
}
|
|
|
|
public function setSPhoneArea($sPhoneArea): void
|
|
{
|
|
$this->sPhoneArea = $sPhoneArea;
|
|
}
|
|
|
|
public function getSPhone()
|
|
{
|
|
return $this->sPhone;
|
|
}
|
|
|
|
public function setSPhone($sPhone): void
|
|
{
|
|
$this->sPhone = $sPhone;
|
|
}
|
|
|
|
public function getIType()
|
|
{
|
|
return $this->iType;
|
|
}
|
|
|
|
/**
|
|
* @throws AppException
|
|
*/
|
|
public function setIType($iType): void
|
|
{
|
|
if (!in_array($iType, VrCode::TOPIC)) throw new AppException('invalid sms type');
|
|
$this->iType = $iType;
|
|
}
|
|
|
|
public function getIUid()
|
|
{
|
|
return $this->iUid;
|
|
}
|
|
|
|
public function setIUid($iUid): void
|
|
{
|
|
$this->iUid = $iUid;
|
|
}
|
|
|
|
function sendSmsToPhone(): bool
|
|
{
|
|
return (new SmsService())->sendSmsCode($this->sPhoneArea.$this->sPhone, $this->sCode);
|
|
}
|
|
|
|
/**
|
|
* @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
|
|
{
|
|
if(Env::get('APP_DEBUG') == true){
|
|
if ($sReqCode === '000000') return true;
|
|
}
|
|
$sCode = $this->getCodeFromCache();
|
|
if (empty($sCode)) return false;
|
|
if ($sCode != $sReqCode) return false;
|
|
return true;
|
|
}
|
|
|
|
|
|
}
|