//前端登录页面表单及javascript+ajax传输
<input type="text" name="nickname" placeholder="请输入名称" class="login-password">
<input type="text" name="mobile" placeholder="请输入手机号" class="login-password">
<input type="text" class="reg-yzm" name="tel_code" maxlength="6" placeholder="输入验证码" />
<input type="button" οnclick="code();" id="login_time" class="yzm-hq" value="获取验证码" />
<input type="password" name="password" placeholder="请输入(6-16字母、数字、下划线)密码" class="login-password">
<input type="password" name="passwords" placeholder="确认密码" class="login-name">
<input type="button" class="login-btn" οnclick="check();" value="立即注册">
<script>
function check(){
var nickname = $('input[name = nickname]').val();
var mobile = $('input[name = mobile]').val();
var tel_code = $('input[name = tel_code]').val();
var password = $('input[name = password]').val();
var passwords = $('input[name = passwords]').val();
var url = "{:url('index/user/mobileReg')}";
var data = {'nickname':nickname,'mobile':mobile,'tel_code':tel_code,'password':password};
if(nickname == ''){
layer.open({
content: '请输入昵称',
skin: 'msg',
time: 2
});
return false;
}
if(!mobile.match(/^1[34578]\d{9}$/)){
layer.open({
content: '请输入正确的手机号',
skin: 'msg',
time: 2 //2秒后自动关闭
});
return false;
}
if(mobile == ''){
layer.open({
content: '请输入手机号',
skin: 'msg',
time: 2
});
return false;
}
if(tel_code == ''){
layer.open({
content: '请输入验证码',
skin: 'msg',
time: 2
});
return false;
}
if(password == '' || !password.match(/^(\w){6,16}$/)){
layer.open({
content: '请输入规范密码',
skin: 'msg',
time: 2
});
return false;
}
if(passwords == ''){
layer.open({
content: '请再次输入密码',
skin: 'msg',
time: 2
});
return false;
}
if(passwords != password){
layer.open({
content: '两次输入密码不一致',
skin: 'msg',
time: 2
});
return false;
}
$.post(url,data,function(result){
if(result.flag){
layer.open({
content: result.flag,
skin: 'msg',
time: 2
});
}else{
alert(result.success);
window.location.href=document.referrer;
}
},'json')
}
function code()
{
var mobile = $('input[name = mobile]').val();
var url = "{:url('index/user/code')}";
if(mobile == ''){
layer.open({
content: '手机号不能为空',
skin: 'msg',
time: 2
});
return false;
}
$.post(url,{'mobile':mobile},function(result){
if(result.flag){
layer.open({
content: result.flag,
skin: 'msg',
time: 2
});
}else{
layer.open({
content: result.success,
skin: 'msg',
time: 2
});
}
},'json');
$('#login_time').attr('disabled',true);
var num = 60;
function each()
{
num--;
if(num == 0)
{
$('#login_time').attr('disabled',false);
$('#login_time').val(' 获取验证码');
}else
{
$('#login_time').val('('+num+')');
$('#login_time').attr('disabled',true);
var time = setTimeout(each,1000); // 延时定时器是隔多长时间后,执行,并且只执行一次 所以,放在这是让内部的函数
}
}
var time = setTimeout(each,1000);
}
</script>
//在config.php中写
return[
'JHAPI'=>[
'key'=>'6e4b8c644************43b5f93d7f2', // 聚合数据短信验证api appkey
'tpl_id'=>'1***23', //聚合数据模板id
'company'=>'***网', //自定义
],
];
// 发送手机验证码
public function code(){
$param = $this->request->param();
// 调用配置文件
$API = config('JHAPI');
// 生成验证码
$code = mt_rand(1000,9999);
$smsConf = array(
'key' => $API['key'], //您申请的APPKEY
'mobile' => $param['mobile'], //接受短信的用户手机号码
'tpl_id' => $API['tpl_id'], //您申请的短信模板ID,根据实际情况修改
'tpl_value' =>urlencode('#code#='.$code.'&#company#='.$API['company'])
);
$data['mobile'] = $param['mobile'];
$data['code'] = $code;
$data['create_time'] = time();
$url = 'http://v.juhe.cn/sms/send?mobile='.$smsConf['mobile'].'&tpl_id='.$smsConf['tpl_id'].'&tpl_value='.$smsConf['tpl_value'].'&key='.$smsConf['key'];
//请求发送短信
// $result = curt_url($url);
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$contents = curl_exec($ch);
curl_close($ch);
$result = json_decode($contents,true);
if($result['error_code'] == 0){
Db::name('user_api')->insert($data);
echo json_encode(array('success'=>'发送成功'));
exit;
}else{
echo json_encode(array('flag'=>'发送失败,错误码:'.$result['error_code']));
exit;
}
}
//登录验证
public function mobileReg(){
$param = $this->request->param();
$res = Db::name('user_api')
->where(['mobile'=>$param['mobile']])
->order('create_time DESC')
->find();
if($res['mobile'] != $param['mobile']){
echo json_encode(['flag'=>'手机号有误']);
exit;
}
if($res['code'] != $param['tel_code']){
echo json_encode(['flag'=>'验证码有误']);
exit;
}
$difftime = time() - $res['create_time'];
if($difftime > 120){
echo json_encode(['flag'=>'验证码过期']);
exit;
}
$count = Db::name('user')
->where(['mobile'=>$param['mobile']])
->count();
if($count>0){
echo json_encode(['flag'=>'账号已存在']);
exit;
}else{
$data = [];
$data['usertype'] = 2;
// $data['last_login_ip'] = $_SERVER['HTTP_CLIENT_IP'];
$data['last_login_time'] = time();
$data['create_time'] = time();
$data['status'] = 1;
$data['mobile'] = $param['mobile'];
$data['nickname'] = $param['nickname'];
$data['password'] = $param['password'];
$result = Db::name('user')
->insertGetId($data);
if(is_numeric($result)){
echo json_encode(['success'=>'注册成功']);
exit;
}else{
echo json_encode(['flag'=>'注册失败']);
exit;
}
}
}