You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
2.1 KiB
PHP
72 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\API;
|
|
|
|
use App\Services\TencentSmsApiService;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class SmsController
|
|
{
|
|
public $Lifespan=1;//验证码有效期单位分钟
|
|
public function SendCode(Request $request)
|
|
{
|
|
$phoneNumber = $request->input('tel');
|
|
$code = $this->generateCode();
|
|
|
|
$service = new TencentSmsApiService();
|
|
$cha=DB::table('sms')->where(['phone'=>$phoneNumber])->orderBy('id','desc')->first();
|
|
if(!!$cha){
|
|
$specificTimeTimestamp = strtotime($cha->created_at);
|
|
// 计算一分钟之后的时间戳
|
|
$oneMinuteLaterTimestamp = strtotime('+1 minute', $specificTimeTimestamp);
|
|
if($oneMinuteLaterTimestamp>time()){
|
|
return \Yz::echoError1('请稍后再试');
|
|
}
|
|
}
|
|
$end_time = strtotime('+'.$this->Lifespan.' minute', time());
|
|
|
|
$in=DB::table('sms')->insert([
|
|
'code'=>$code,
|
|
'phone'=>$phoneNumber,
|
|
'status'=>0,
|
|
'lifespan'=>$this->Lifespan,
|
|
'end_time'=>date('Y-m-d H:i:s', $end_time)
|
|
]);
|
|
if($in){
|
|
$params = [$code,(string)$this->Lifespan];
|
|
$ss= $service->send($phoneNumber,"2564483", $params);
|
|
|
|
return \Yz::Return(true,'短信验证码发送成功');
|
|
}
|
|
|
|
|
|
}
|
|
//校验短信验证码
|
|
public function CheckCode($phone,$code)
|
|
{
|
|
$nowtime=date('Y-m-d H:i:s');
|
|
$cha=DB::table('sms')->where(['phone'=>$phone,'code'=>$code,'status'=>0,['end_time','>',$nowtime]])->first();
|
|
if(!!$cha){
|
|
$u=DB::table('sms')->where(['id'=>$cha->id])->update([
|
|
'status'=>1
|
|
]);
|
|
if($u){
|
|
return true;
|
|
}
|
|
}else{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// 生成随机验证码
|
|
function generateCode($length = 4) {
|
|
$characters = '0123456789';
|
|
$code = '';
|
|
for ($i = 0; $i < $length; $i++) {
|
|
$code .= $characters[rand(0, strlen($characters) - 1)];
|
|
}
|
|
return $code;
|
|
}
|
|
}
|