1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-28 05:11:39 +08:00
2020-05-15 21:56:51 +08:00

72 lines
1.3 KiB
PHP

<?php
namespace App\Services;
use App\Library\Cache\Backend\Redis as RedisCache;
use Phalcon\Text;
class Verify extends Service
{
/**
* @var RedisCache
*/
protected $cache;
public function __construct()
{
$this->cache = $this->getDI()->get('cache');
}
public function getSmsCode($phone, $lifetime = 300)
{
$key = $this->getSmsCacheKey($phone);
$code = Text::random(Text::RANDOM_NUMERIC, 6);
$this->cache->save($key, $code, $lifetime);
return $code;
}
public function getEmailCode($email, $lifetime = 300)
{
$key = $this->getEmailCacheKey($email);
$code = Text::random(Text::RANDOM_NUMERIC, 6);
$this->cache->save($key, $code, $lifetime);
return $code;
}
public function checkSmsCode($phone, $code)
{
$key = $this->getSmsCacheKey($phone);
$value = $this->cache->get($key);
return $code == $value;
}
public function checkEmailCode($email, $code)
{
$key = $this->getEmailCacheKey($email);
$value = $this->cache->get($key);
return $code == $value;
}
protected function getEmailCacheKey($email)
{
return "verify:email:{$email}";
}
protected function getSmsCacheKey($phone)
{
return "verify:sms:{$phone}";
}
}