<?
// 家校/家园/高职用户注册
require_once ("../user_include.inc");

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

//获取参数
$name = ParamUtil::getRequestString("name");//用户微信昵称
$openId = ParamUtil::getRequestString("openId");
$unionId = ParamUtil::getRequestString("unionId");
$role = ParamUtil::getRequestNumber("role");
$headerImg = ParamUtil::getRequestString("headerImg");
$schoolNo = ParamUtil::getRequestString("schoolNo");
$classNo = ParamUtil::getRequestString("classNo");
$schoolOwner = ParamUtil::getRequestBoolean("schoolOwner", false);
$classOwner = ParamUtil::getRequestBoolean("classOwner", false);

ErrorLogger::doOutput("Compass...ajax_user_register.php....name=" . $name, 0);
ErrorLogger::doOutput("Compass...ajax_user_register.php....openId=" . $openId, 0);
ErrorLogger::doOutput("Compass...ajax_user_register.php....unionId=" . $unionId, 0);
ErrorLogger::doOutput("Compass...ajax_user_register.php....schoolNo=" . $schoolNo, 0);
ErrorLogger::doOutput("Compass...ajax_user_register.php....classNo=" . $classNo, 0);

//参数验证
$result = array();

if(empty($name) || empty($openId) || empty($unionId) || empty($schoolNo)) {
	$result["message"] = "参数错误！";
	responseNG($result);
}
if($role<>1 && $role<>2 && $role<>3) {
	$result["message"] = "参数错误！";
	responseNG($result);
}

//判断用户是否已经存在
$param = array();
$param['openid'] = $openId;
$param['unionid'] = $unionId;
$param['delete_flg'] = false;
$userList = UserMst::getList($param,'id','desc', 0, 1);
if(!empty($userList)) {
	$result["message"] = "用户已注册！";
	responseOK($result);
}

//用户数据插入
$userMst = new UserMst();
$userMst->school_no = $schoolNo;
$userMst->class_no = $classNo;
$userMst->original_source = $role;//此处跟role一样
$userMst->openid = $openId;
$userMst->unionid = $unionId;
$userMst->name = $name;
$userMst->header_img = $headerImg;
$userMst->role = $role;
//年龄端
if($role == 1) {
	$userMst->child_age = 1; //3~6岁
} else if($role == 2) {
	$userMst->child_age = 2; //7岁以上
} else {
	$userMst->child_age = 0; //不设定
}
$userMst->school_owner = $schoolOwner;
$userMst->class_owner = $classOwner;
$userMst->save();

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

//是否推荐购买会员
$result["recommend"] = true;
if($schoolOwner || $classOwner) {
	$result["recommend"] = false;
}

$result["message"] = "注册成功！";
responseOK($result);

function responseNG($result) {
	$result = array("status"=>"NG", "result"=>$result);
	print json_encode($result);
	exit;
}
function responseOK($result) {
	$result = array("status"=>"OK", "result"=>$result);
	print json_encode($result);
	exit;
}

?>