前端代码部分
手机号:<input type="number" id="phone">
<label >手机号码验证</label>
<input type="number" id="code">
<a onclick="basicSendSms(this)">发送验证码</a>
var time=60;
var clock=null;
function basicSendSms(obj){
var mobile=$('#phone').val();
if(!(/^1[0-9][0-9]\d{4,8}$/.test(mobile))){
alert('请输入正确的手机号');
return;
}
if(is_work)
return;
if($(obj).html()=='发送验证码'&&clock==null){
is_work=true;
ajaxJsonp(base_app_url+'app/index.php?m={#m#}&c={#c#}&a={#a#}','mobile='+mobile+'&sid='+sid,function(result){
is_work=false;
setStorage('sms_id',result.sid);
if(result.code==0){
clearInterval(clock);
$(obj).html('获取验证码');
time=60;
clock=null;
alert(result.msg);
return;
}
else{
alert('短信发送成功');
}
},null);
clock=setInterval(function(){ //方法可按照指定的周期(以毫秒计)来调用函数或计算表达式 .方法会不停地调用函数,直到 clearInterval() 被调用或窗口被关闭。
time--;
$(obj).html(time+'秒后重新发送');
if(time==0){
clearInterval(clock);//方法可取消由 setInterval() 设置的 timeout。方法的参数必须是由 setInterval() 返回的 ID 值。
$(obj).html('发送验证码');
time=60;
clock=null;
}
},1000)
}
}
后台处理部分(发送验证短信)
点击下载阿里云短信SDK包并放在对应的文件夹相对路径
<?php
require_once "sdk/SignatureHelper.php";
use Aliyun\DySDKLite\SignatureHelper;
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class {#className#} extends FJG_Controller {
public function {#a#}(){
if(!isset($_GET['mobile'])||empty($_GET['mobile'])||!(preg_match("/^1[0-9][0-9]{5,9}$/",$_GET['mobile']))){
$this->api_return_json(array('msg'=>'请输入正确的接收验证码的手机号'),0);
}
else{
$mobile=$_GET['mobile'];
}
$code1=rand(0,9);
$code2=rand(0,9);
$code3=rand(0,9);
$timestr=time().'';
$code4=substr($timestr,strlen($timestr)-1);
$code5=rand(0,9);
$code6=rand(0,9);
$data['sms_code']=$code1.$code4.$code2.$code3.$code5.$code6;
$this->setSession(array('data'=>json_encode(array('reg_code'=>$data['sms_code'],'phone'=>$mobile,'reg_code_expire'=>time()+1800))));
$return=$this->sendSms($mobile, $data['sms_code']);
$this->api_return_json(array('msg'=>$return,'sid'=>session_id()),1);
}
/**
* 发送短信
*/
function sendSms($phone,$code) {
$params = array ();
// fixme 必填: 请参阅 https://ak-console.aliyun.com/ 取得您的AK信息
$accessKeyId = "{#key#}";
$accessKeySecret = "{#secret#}";
// fixme 必填: 短信接收号码
$params["PhoneNumbers"] = $phone;
// fixme 必填: 短信签名,应严格按"签名名称"填写,请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
$params["SignName"] = "{#sign#}";
// fixme 必填: 短信模板Code,应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
$params["TemplateCode"] = "{#template_id#}";
// fixme 可选: 设置模板参数, 假如模板中存在变量需要替换则为必填项
$params['TemplateParam'] = Array (
"code" => $code
);
// fixme 可选: 设置发送短信流水号
// $params['OutId'] = "12345";
// fixme 可选: 上行短信扩展码, 扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段
//$params['SmsUpExtendCode'] = "1234567";
// *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
if(!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
$params["TemplateParam"] = json_encode($params["TemplateParam"]);
}
// 初始化SignatureHelper实例用于设置参数,签名以及发送请求
$helper = new SignatureHelper();
// 此处可能会抛出异常,注意catch
$content = $helper->request(
$accessKeyId,
$accessKeySecret,
"dysmsapi.aliyuncs.com",
array_merge($params, array(
"RegionId" => "cn-hangzhou",
"Action" => "SendSms",
"Version" => "2017-05-25",
))
// fixme 选填: 启用https
// ,true
);
return $content;
}
}
后台处理部分(验证短信是否正确)
$code=$this->input->get('code');
if(empty($code)){
$this->api_return_json(array('msg'=>'验证码不能为空!'),0);
}
$data_phone=json_decode($_SESSION['data']);
if($data_phone->reg_code!=$code){
$this->api_return_json(array('msg'=>'验证码输入错误!'),0);
}
else if($data_phone->phone!=$data['bank_phone']){
$this->api_return_json(array('msg'=>'验证手机号不匹配!'),0);
}
else if(time()>$data_phone->reg_code_expire){
$this->api_return_json(array('msg'=>'验证码已过期!'),0);
}