今天来盘一下注册及忘记密码时的验证码发送
直接上代码~
//发送验证码
private function send($area,$phone,$codeKey,$type)
{
$key = $this->createCodeKey($area,$phone); //生成key
$val = RedisService::getRedis()->get($key); //取出该手机号发送次数
$errorNum=config('rcapi.withdrawal_send_sms_error_num'); //取每天最多发送次数
if(empty($val)){
//第一次发送
//设置0点过期
$time = mktime(23, 59, 59, date("m"), date("d"), date("Y"));
RedisService::getRedis()->set($key,1,$time);
}elseif ($val < $errorNum){
//还未超出最大次数
RedisService::getRedis()->incrBy($key,1);
}else{
return $this->setMsg(false,905,'今天验证码发送次数已经'.$val.'次了,请明天再试');
}
$code = $this->createCode();
//将6位验证码存到redis中 设置过期时间90秒
RedisService::getRedis()->set($codeKey,$code,90);
switch ($type){
//根据不同的type 发送不同的短信模版
//区号和手机号区分开是因为要调用不同的短信发送运营商
case 1:
$res = SmsService::getClient($area)->regeisterDriver($phone,$code);
break;
case 2:
$res = SmsService::getClient($area)->driverForgetPassword($phone,$code);
break;
case 3:
$res = SmsService::getClient($area)->setWithDrawalPassword($phone,$code);
break;
default:
break;
}
return $this->setMsg(true,200,'发送成功');
}
//此方法是生成当天该手机号发送验证码的次数的key
public function createCodeKey($area,$phone)
{
return 'reg_code_'.date("Y-m-d").'_'.$area.$phone;
}
//此方法是生成该手机号注册时发送的验证码的key
public function createRegCodeKey($area,$phone)
{
return 'reg_code_'.$area.$phone;
}
//此方法是生成该手机号注册时发送的验证码的key
public function createForgetCodeKey($area,$phone)
{
return 'forget_code_'.$area.$phone;
}
//此方法是生成6为随机数字验证码
public function createCode(){
return mt_rand(1111, 9999) . mt_rand(11, 99);
}