控制器代码
//注册控制器 public function register() { $rs = self::getMemberService ()->add ( $_POST ); //返回json格式 $this->sendNotice ( $rs, null, false ); } //服务类 涉及到逻辑判断 /** * 增加用户 */ public function add($userInfo) { if (! $this->checkValidate ( $_POST [‘codeimg‘] )) { return "验证码错误"; } if (strlen ( $userInfo [‘mobile_phone‘] ) != 11) { return "手机号码为11位数"; } if (strlen ( $userInfo [‘password‘] ) < 5) { return "密码至少为5位数"; } if (strlen ( $userInfo [‘username‘] ) < 3) { return "用户名至少为3位数"; } if ($this->filter ( $userInfo [‘password‘] )) { return "密码中包含了标点符号"; } if ($this->filter ( $userInfo [‘username‘] )) { return "用户名中包含了标点符号"; } if (! $this->checkRepeat ( ‘mobile_phone‘, $userInfo [‘mobile_phone‘] )) { return "手机号码重复了!"; } if (! $this->checkRepeat ( ‘username‘, $userInfo [‘username‘] )) { return "帐号重复了!"; } //这里应该在模型里面 $this->addMember ( $userInfo ); return "success"; } //模型类 /** * 新增用户 * @param Array $userInfo */ private function addMember($userInfo) { $salt = $this->randstr (); $data [‘username‘] = trim ( $userInfo [‘username‘] ); $data [‘real_name‘] = trim ( $userInfo [‘real_name‘] ); $data [‘password‘] = md5 ( md5 ( $userInfo [‘password‘] ) . $salt ); $data [‘salt‘] = $salt; $data [‘add_time‘] = time (); $ip = $this->getIp (); $data [‘ip‘] = $ip [‘ip‘]; $data [‘area‘] = $ip [‘country‘] . $ip [‘city‘]; $data [‘mobile_phone‘] = trim ( $userInfo [‘mobile_phone‘] ); //防止非法修改 $data [‘groupid‘] = trim ( $userInfo [‘groupid‘] ) > 10 ? trim ( $userInfo [‘groupid‘] ) : 10; $gremark = MemberModel::instance ()->getOneGroup ( array (‘id‘ => $data [‘groupid‘] ) ); $data [‘remark‘] = $gremark [‘name‘]; $data [‘verify‘] = 0; //直接验证成功 MemberModel::instance ()->addMember ( $data ); }
前端代码
function register(){ var mobile=$("#mobile_phone").val(); var password=$("#password").val(); var username=$("#username").val(); var real_name=$("#real_name").val(); var codeimg=$("#codeimg").val(); var groupid=$(‘input[name="groupid"]:checked‘).val(); if(mobile.length!=11){ alert("手机号码为11位数"); return; } if(codeimg.length!=4){ alert("验证码填写不正确"); return; } if(password.length<5){ alert("密码至少为6位数"); return; } if(real_name.length<2){ alert("真实姓名没有填写"); return; } $.post("/anonymous/register",{mobile_phone:mobile,real_name:real_name,groupid:groupid,codeimg:codeimg,password:password,username:username,password:password},function(data){ if(data.message=="success"){ alert("注册成功!"); location.href="/anonymous/login"; return; } alert(data.message); },"json") } </script>
原文地址:http://my.oschina.net/u/554046/blog/296478