<?
// クラス、設定読み込み
require_once ("../user_include.inc");

ErrorLogger::doOutput("Koala...ajax_user_register.php....Start.", 0);

$mobile = ParamUtil::getRequestString("mobile");
$nickname = ParamUtil::getRequestString("nickname");
$verifycode = ParamUtil::getRequestString("verifycode");
//渠道编号，默认为系统自带
$channel_no = ParamUtil::getRequestString("channel_no", "1000");

ErrorLogger::doOutput("Koala...ajax_user_register.php....mobile=" . $mobile, 0);
ErrorLogger::doOutput("Koala...ajax_user_register.php....nickname=" . $nickname, 0);
ErrorLogger::doOutput("Koala...ajax_user_register.php....verifycode=" . $verifycode, 0);
ErrorLogger::doOutput("Koala...ajax_user_register.php....channel_no=" . $channel_no, 0);

//获取不到手机号码
if(empty($mobile)) {
	responseNG("请输入手机号码！");
}
//获取不到用用户名称
if(empty($nickname)) {
	responseNG("请输入您的姓名！");
}

//获取不到验证码
if(empty($verifycode)) {
	responseNG("请输入手机验证码！");
}

//检测验证码
$param = array();
$param["delete_flg"] = false;
$param["phone"] = $mobile;
$param["code"] = $verifycode;
$param["is_used"] = false;
$param["ok_flg"] = true;
$sms_verfication_dat_list = SmsVerficationDat::getList($param, "id", "desc", 0, 1);
if (count($sms_verfication_dat_list) < 1) {
	responseNG("短信码验证失败，可尝试重发验证码。");
} else {
	$sms_verfication_dat = $sms_verfication_dat_list[0];
	$sms_verfication_dat->is_used = true;
	$sms_verfication_dat->save();
}

//检测用户是否已经注册
$param = array();
$param["delete_flg"] = false;
$param["mobile"] = $mobile;
$user_list = UserMst::getList($param);
//存在的情况下，注册一条数据
if(!empty($user_list)) {	
	responseNG("您已经注册过，请关注【考拉学日语】公众号领取福利！");
}
//不存在的情况下，注册一条数据
$user_mst = new UserMst();
$user_mst->nickname = $nickname;
$user_mst->mobile = $mobile;
$user_mst->channel_no = $channel_no;
$user_mst->status = '1';
$user_mst->save();

//设定到session中保持
$_SESSION['_user_'] = $user_mst;

ErrorLogger::doOutput("Koala...ajax_user_register.php....End.", 0);

responseOK("注册成功！");

function responseNG($message) {
	$result = array("status"=>"NG", "message"=>$message);
	header("Access-Control-Allow-Origin: *");
	print json_encode($result);
	exit;
}
function responseOK($message) {
	$result = array("status"=>"OK", "message"=>$message);
	header("Access-Control-Allow-Origin: *");
	print json_encode($result);
	exit;
}

?>