注:这里用到的验证码是阿里云的借口
1、前台注册的表单
2、当获取验证码的按钮被点击时,前台正则验证手机号通过后,将手机号发送到后台获取短信验证码
var countdown = 60;
var tt;
$("#sms").click(function(){
var isPhone = /^([0-9]{3,4}-)?[0-9]{7,8}$/;
//匹配手机号
var isMob=/^((\+?86)|(\(\+86\)))?(13[012356789][0-9]{8}|17[012356789][0-9]{8}|15[012356789][0-9]{8}|18[012356789][0-9]{8}|147[0-9]{8}|1349[0-9]{7})$/;
var phone_str=$("#mobile").val();
//去处手机号首尾的空白符重新赋值
phone_str = $.trim(phone_str);
if(phone_str==""){
toastr.error('联系电话不能为空');
return;
//用设置的正则匹配是不是数字和是不是手机号
}else if(!(isPhone.test(phone_str)||isMob.test(phone_str))){
toastr.error('电话号码格式错误');
return;
}
var url = "{:url('user/Login/sendSmsCode')}";
$.post(url,{mobile:phone_str},function(data){
if(data.code ==1){
toastr.success('发送成功');
$("#sms").attr("disabled","true");
tt = setInterval(function() {
settime();
},1000)
}else {
toastr.error(data.msg);
}
})
});
//短信发送倒计时
function settime() {
if (countdown == 0) {
$("#sms").val("重新获取");
$("#sms").removeAttr("disabled");
countdown = 60;
clearInterval(tt);
} else {
$("#sms").val(countdown);
countdown--;
}
}
2、先定义一个生产验证码的函数
function get_random($length, $chars = '0123456789')
{
$hash = '';
$max = strlen($chars) - 1;
for ($i = 0; $i < $length; $i++) {
$hash .= $chars[mt_rand(0, $max)];
}
return $hash;
}
3、再定义一个调用阿里云短信接口的函数
function alsms($tplId,$mobile,$data){
$signname = '海创汇';
if(empty($tplId)){
return array('code'=>'0','msg'=>'短信模板不能为空','data'=>array());
}
if(empty($mobile)){
return array('code'=>'0','msg'=>'电话号码不能为空','data'=>array());
}elseif(is_array($mobile)){
//如果是数组,则转换成用逗号隔开的字符串
$mobile = implode(",",$mobile);
$mobile = trim($mobile,",");
}else{
$mobile = trim($mobile);
}
if(empty($data)){
return array('code'=>'0','msg'=>'短信数据不能为空','data'=>array());
}else{
//把数据处理一遍,过滤掉特殊字符 【、】、[、]
$search = array("【","】","[","]");
$replace = array("","","","");
//把$value中的$replace替换为$search
foreach($data as $key=>$value){
$data[$key] = str_replace($search,$replace,$value);
}
}
//导入阿里云的发送短信的类文件
import('alsmsdemo.api_demo.SmsDemo',EXTEND_PATH);
$response = SmsDemo::sendSms($signname,$tplId,$mobile,$data,'');
if($response->Code =='OK'){
return array('code'=>'1','msg'=>'发送成功','data'=>array());
}else{
//#todo 发送失败做日志记录
return array('code'=>'0','msg'=>$response->Message,'data'=>array());
}
}
4、当前台请求获取验证码时则调用生产验证码的函数和阿里云的短信发送接口
function sendSmsCode($mobile)
{
//查找用户表内是否已有该手机号码
$umsg = findById("user", array("name" => $mobile), "id");
if (!empty($umsg["data"])) {
return array("code" => 0, "msg" => "注册失败,该手机号已注册");
}
//判断当前手机号一个小时内已经发送了多少
$start_time = time() - 3600;//获取一小时之前的时间戳
$map = array(
//要求大于一小时前的所有时间
'addtime' => array('gt', $start_time),
'mobile' => $mobile,
'type' => 0
);
$msg = getDataList('SmsLog', $map, 'id');
$count = '0';
if ($msg['code'] == '1') {
$count = count($msg['data']);
}
//一个小时内不能超过三次
if ($count >= 3) {
return array('code' => '0', 'msg' => '发送次数过于频繁,请一个小时后再试', 'data' => '');
}
//获取四位随机数字验证码
$code = get_random(4);
//把短信发送接口改为阿里云的接口
$tplId = config('sms_tpl_id.code');
$smsdata = array(
'code'=>$code,
);
//将手机号和验证码传入调用短信进口的方法
$result = alsms($tplId,$mobile,$smsdata);
// $value = "#code#=" . $code;
// $result = sendSms($mobile, $value);
if ($result['code'] == "1") {
//如果发送短信成功,把该验证码和手机号保存到数据库
//发送成功,做记录
$data = array(
'mobile' => $mobile,
'msg' => $code,
'type' => '0',
'addtime' => time()
);
//保存验证码到数据库,并返回前台发送成功的信息
saveData('SmsLog', $data);
return array('code' => '1', 'msg' => '', 'data' => '发送成功,十分钟内有效');
} else {
return array('code' => '0', 'msg' => $result['msg'], 'data' => '');
}
}
5、发送注册表单数据
$("#btnrgst").click(function () {
//验证座机正则
var isPhone = /^([0-9]{3,4}-)?[0-9]{7,8}$/;
//验证手机号正则
var isMob=/^((\+?86)|(\(\+86\)))?(13[012356789][0-9]{8}|17[012356789][0-9]{8}|15[012356789][0-9]{8}|18[012356789][0-9]{8}|147[0-9]{8}|1349[0-9]{7})$/;
var phone_str=$("#mobile").val();
phone_str = $.trim(phone_str);
var realname = $.trim($("#realname").val());
var password = $.trim($("#password").val());
var cfmpassword = $.trim($("#cfmpassword").val());
if(phone_str==""){
toastr.error('电话号码不能为空');
return false;
//当不是手机号和座机号时,则弹出错误
}else if(!(isPhone.test(phone_str)||isMob.test(phone_str))){
toastr.error('电话号码格式不正确');
return false;
}
if(realname==""){
toastr.error('姓名不能为空');
return ;
}
var verify = $.trim($("#verify").val());
if(verify==''){
toastr.error('短信验证码不能为空');
return false;
}
if(password==""){
toastr.error('密码不能为空');
return;
}
if(password!=cfmpassword){
toastr.error('两次密码不一致,请重新输入');
return;
}
var regExp=/^(?![\d]+$)(?![a-zA-Z]+$)(?![^\da-zA-Z]+$).{6,20}$/;
if(!regExp.test(password)){
toastr.error('密码必须为6-20位字母、数字或符号组合');
return;
}
var form = $("#rgst");
//获取html里的函数路径
var url = form.prop("action");
//获取表单内的数据
var data=form.serialize();
$.post(url, data, function (data) {
if (data.code == 1) {
toastr.success('注册成功,请登录');
//跳转到指定路径
setTimeout('go()',1000);
} else {
toastr.error(data.msg);
}
});
});
});
//注册成功跳转的路径
function go(){
location.href="{:url('/web/Index/guidePro')}";
}
6、后台注册功能
function saveUser() {
$postData = input('request.',null,'trim');
//加校验
$role = [
'mobile|联系电话'=>'require',
'realname|姓名'=>'require',
'verify|短信验证码'=>'require',
'password|密码'=>'require',
'cfmpassword|确认密码'=>'require|confirm:password',
];
$err = [
'mobile'=>'联系电话不能为空',
'realname'=>'姓名不能为空',
'verify'=>'短信验证码必填',
'password'=>'密码必填',
'cfmpassword'=>'确认密码必填且必须和密码一致',
];
$validate = new Validate($role,$err);
if(! $validate->check($postData)){
return array('code'=>'0','msg'=>$validate->getError(),'data'=>'');
}
try {
$username = $postData['mobile'];
$umsg = findById("user", array("name" => $username), "id");
if (!empty($umsg["data"])) {
return array("code" => 0, "msg" => "注册失败,该手机号已注册");
}
//验证手机验证码
$verify =$postData['verify'];
$res = verifySmsCode($username, $verify, 600);
if ($res['code'] == '0') {
return array('code' => '0', 'msg' => $res['msg'], 'data' => '');
}
$etprs["addtime"] = time();
Db::startTrans();
$msg = saveData("enterprise", $etprs, "企业注册");
if ($msg["code"] === '1') {
$etprsId = $msg["data"];
$user["name"] = $username;
$user["realname"] = $postData['realname'];
$user["registerTime"] = date("Y-m-d H:i", time());
$user["addtime"] = time();
$user["mobile"] = $username;
$user["password"] = md5($postData['password']);
$user["userCate"] = "1011002";
$user["status"] = "1012003"; //这个状态有待进一步讨论
$user["etprsId"] = $etprsId;
$user["etprsIqbtId"] = 1;
$user["roleIds"] = "2";//角色:企业
#todo 企业还有一些状态为加上 比如,没有对应的孵化器
$vld = $this->validate($user, 'User.register');
if ($vld === true) {
$msg2 = saveData("user", $user, "企业用户注册");
if ($msg2["code"] =='0') {
throw new \think\Exception("注册失败,用户信息保存失败 " . $msg2["msg"]);
}
} else {
throw new \think\Exception("用户信息校验失败" . $vld);
}
} else {
throw new \think\Exception("注册失败,企业信息保存失败" . $msg["msg"]);
}
Db::commit();
return array("code" => 1, "msg" => "注册成功",'data'=>'');
} catch (\Exception $e) {
//记录事务
c_Log($e);
// 回滚事务
Db::rollback();
return array("code" => 0, "msg" =>'注册失败','data'=>'');
}
}