<?php
namespace app\common\smslxx;
use think\Cache;
/**
*
*
* Class Smslxx
* 使用缓存,缓存键前缀sms
* @package app\common\smslxx
*/
class Smslxx
{
const CODE_LENTH = 4; //验证码长度,最大9
const CLOSE_TIME = 3; //验证码过期时间
const CHECK_CLOSE = true; //验证成功销毁
public function get_sms_code($phone = '')
{
if (empty($phone)) {
return -1;
}
$code = $this->made_sms_code(self::CODE_LENTH);
Cache::set("sms{$phone}", $code, self::CLOSE_TIME);
return $code;
}
public function made_sms_code($codelenth = self::CODE_LENTH)
{
return rand(pow(10, ($codelenth - 1)), pow(10, $codelenth) - 1);
}
public function check_sms_code($phone = '', $code = '')
{
if (empty($phone)) {
return -1;
}
if (!Cache::has("sms{$phone}")) {
return -1;
}
$cachecode = Cache::get("sms{$phone}");
if ($code == $cachecode) {
if (self::CHECK_CLOSE) {
Cache::rm("sms{$phone}");
}
return 1;
} else {
return -1;
}
}
}
缓存(或者session)生成短信验证码
最新推荐文章于 2023-05-24 16:24:22 发布