服务器端代码:
public function user_register(){
header("Access-Control-Allow-Origin: *");
if(IS_POST){
if(trim($_POST['code'])==trim($_POST['sms_code'])){//判断手机验证码是否正确
if($_POST['again_pwd']==$_POST['pwd']){ //判断两次输入的密码是否一致
if(!empty($_POST['username']) && !empty($_POST['pwd'])){//判断账号和密码是否不为空
$user=D('User');//实例化数据库
$map['phone']=$_POST['username'];
$isexist=$user->where($map)->find();//查询用户是否存在
if(!empty($isexist)){
$this->ajaxreturn('exist');exit;//存在则返回‘exist’;
}else{
$data['phone']=$_POST['username'];
$data['password']=md5($_POST['pwd']);
$data['time']=get_time("www.baidu.com");
$data['vipexpire']=get_time("www.baidu.com")+86400*7;
$data['device1']=$_POST['device'];
if($user->add($data)){
$this->ajaxreturn('ok');exit;//注册成功返回 ‘ok’
}else{
$this->ajaxreturn('fail');exit;//失败返回‘fail’;
}
}
}
}
}
-------------------------------------------------------
app端内:
}, function(ret, err) {
api.hideProgress();
if (ret.statusCode == 200) {
switch (ret.body.status) {
case 'exist':
api.toast({
location: 'middle',
msg: '当前用户已存在'
});
break;
case 'ok':
$api.setStorage('username', ret.body.phone);
$api.setStorage('vipexpire', ret.body.vipexpire);
$api.setStorage('uid', ret.body.id);
$api.setStorage('token', ret.body.token);
$api.setStorage('service', ret.body.imtype);
api.sendEvent({
name: 'myEvent',
extra: {
key1: ret.body.phone,
key2: ret.body.vipexpire
}
});
api.showProgress({
ti
modal: true
});
api.closeWin();
break;
case 'fail':
api.toast({
location: 'middle',
msg: '系统繁忙,请稍后再试'
});
break;
case 'atypism':
api.toast({
location: 'middle',
msg: '两次密码输入不一致'
});
break;
case 'codeerror':
api.toast({
location: 'middle',
msg: '手机验证码错误!'
});
break;
}
} else {
api.toast({
location: 'middle',
msg: '无法连接!'
});
}
});
}
}
}
};
最佳答案