<?
// 社会用户注册1【非机构】
require_once ("../user_include.inc");

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

//获取参数
$name = ParamUtil::getRequestString("name");//用户填写的用户名称
$mobile = ParamUtil::getRequestNumber("mobile");
$legalPerson = ParamUtil::getRequestString("legalPerson");
$organizationContact = ParamUtil::getRequestNumber("organizationContact");
$openId = ParamUtil::getRequestString("openId");
$unionId = ParamUtil::getRequestString("unionId");
$longitude = ParamUtil::getRequestString("longitude");//用户位置的经度
$latitude = ParamUtil::getRequestString("latitude");//用户位置的纬度

ErrorLogger::doOutput("Compass...ajax_social_user_register1.php....name=" . $name, 0);
ErrorLogger::doOutput("Compass...ajax_social_user_register1.php....mobile=" . $mobile, 0);
ErrorLogger::doOutput("Compass...ajax_social_user_register1.php....openId=" . $openId, 0);
ErrorLogger::doOutput("Compass...ajax_social_user_register1.php....unionId=" . $unionId, 0);


//参数验证
if(empty($name) || empty($openId) || empty($unionId) || empty($mobile)) {
	$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);
}

//调用腾讯LGS服务解析用户地址信息
$tmpUrl = '/ws/geocoder/v1?key=' . LBS_KEY . '&location=' . $latitude . ',' . $longitude . LBS_SECRET;
$md5Str = md5($tmpUrl);
$lbsUrl = 'https://apis.map.qq.com/ws/geocoder/v1?key=' . LBS_KEY . '&location=' . $latitude . ',' . $longitude . '&sig=' . $md5Str;
$lbsObj =  file_get_contents($lbsUrl);
$jsonObj = json_decode($lbsObj); //对JSON格式的字符串进行编码
$lbsArray = get_object_vars($jsonObj);//转换成数组
$resultObj = $lbsArray['result'];

/**
if($lbsArray['status'] == "0" && $resultObj->ad_info->nation_code==156) {
	print_r($resultObj->address_component->city);
	print_r($resultObj->address_component->province);
	print_r($resultObj->address_component->district);
}
*/


//用户数据插入
$userMst = new UserMst();
$userMst->school_no = $schoolNo;
$userMst->class_no = $classNo;
if(empty($legalPerson)) {
	$userMst->role = 5; //社会用户[非机构]
} else {
	$userMst->role = 6; //社会用户[机构]
}

//地理位置获取正常的情况下设定。 156：中国代码
if($lbsArray['status'] == "0" && $resultObj->ad_info->nation_code==156) {
	$userMst->province = $resultObj->address_component->province;
	$userMst->city = $resultObj->address_component->city;
	$userMst->district = $resultObj->address_component->district;
}

$userMst->openid = $openId;
$userMst->unionid = $unionId;
$userMst->name = $name;
$userMst->mobile = $mobile;
$userMst->legal_person = $legalPerson;
$userMst->organization_contact = $organizationContact;
$userMst->save();

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

$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;
}

?>