<?php

if (isset($_POST['name']) && isset($_POST["email"]) && isset($_POST['phone'])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    //check email
    $user = get_user_by_mail($email);
    if (!empty($user)) {
        $return["insert"] = FALSE;
        $return["msg"] = "email đã được đăn ký";
        echo json_encode($return);
        return FALSE;
    }
    //check phone
    $user = get_user_by_phone($phone);
    if (!empty($user)) {
        $return["insert"] = FALSE;
        $return["msg"] = "Số điện thoại đã được đăng ký";
        echo json_encode($return);
        return FALSE;
    }
    $intIsOK = add_user($name, $email, $phone);
    $return["insert"] = $intIsOK;
    echo json_encode($return);
    //    echo"<script>location.href='index.php?page=success'</script>";
} else {
    require 'index.phtml';
}
 /**
  * 验证短信并绑定手机号
  * @param code 验证码
  */
 public function verifyPhone()
 {
     $phone = session('bind_phone');
     if (!$phone) {
         $this->error('手机号不存在!');
     } elseif (get_user_by_phone($phone)) {
         $this->error('此手机号已经绑定过账号!');
     }
     $code = I('code', false, '/^\\d{6}$/');
     $sid = number();
     $uid = use_id();
     if ($code && $sid && $uid) {
         $result = check_sms_code($phone, $code, 'bind');
         if ($result) {
             session('bind_phone', null);
             $true_phone = $phone;
             import('Common.Encrypt', COMMON_PATH, '.php');
             $phone = encrypt_phone($phone, $sid, $uid);
             if (M('User')->where('id=%d', $uid)->setField('phone', $phone)) {
                 $this->success($true_phone . '绑定成功!');
             } else {
                 $this->error('name');
                 $phone . '绑定失败!';
             }
         } elseif (false === $result) {
             $this->error('验证失败,请重试!');
         } else {
             $this->error('验证信息过期!', '/User/bindPhone');
         }
     } else {
         $this->error('验证信息错误');
     }
 }