mirror of
https://gitee.com/koogua/course-tencent-cloud.git
synced 2025-06-26 20:52:44 +08:00
44 lines
772 B
PHP
44 lines
772 B
PHP
<?php
|
|
|
|
namespace App\Library\Util;
|
|
|
|
use Phalcon\Cache\Backend\Redis;
|
|
use Phalcon\Di;
|
|
use Phalcon\Text;
|
|
|
|
class Verification
|
|
{
|
|
|
|
public static function code($key, $lifetime = 300)
|
|
{
|
|
/**
|
|
* @var Redis $cache
|
|
*/
|
|
$cache = Di::getDefault()->get('cache');
|
|
|
|
$code = Text::random(Text::RANDOM_NUMERIC, 6);
|
|
|
|
$cache->save(self::getKey($key), $code, $lifetime);
|
|
|
|
return $code;
|
|
}
|
|
|
|
public static function checkCode($key, $code)
|
|
{
|
|
/**
|
|
* @var Redis $cache
|
|
*/
|
|
$cache = Di::getDefault()->get('cache');
|
|
|
|
$value = $cache->get(self::getKey($key));
|
|
|
|
return $code == $value;
|
|
}
|
|
|
|
public static function getKey($key)
|
|
{
|
|
return "verify:{$key}";
|
|
}
|
|
|
|
}
|