<?php

// 获取用户身份标识
require_once ("../user_include.inc");

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

//获取参数
$unionId = ParamUtil::getRequestString("unionId");
$longitude = ParamUtil::getRequestString("longitude");//用户位置的经度
$latitude = ParamUtil::getRequestString("latitude");//用户位置的纬度

$result = array();

//参数检查
if(empty($unionId)) {
	$result["message"] = "参数错误！";
	responseNG1($result);
}

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

$userMst = $userList[0];

//接口返回数据
$result["role"] = $userMst->role;
$result["isMember"] = $userMst->is_member;
$result["childAge"] = $userMst->child_age;
$result["schoolOwner"] = $userMst->school_owner;
$result["classOwner"] = $userMst->class_owner;
$result["governmentId"] = $userMst->government_id;

//调用腾讯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);//转换成数组
if(isset($lbsArray['result'])) {
	$resultObj = $lbsArray['result'];
	//地理位置获取正常的情况下设定。 156：中国代码
	if($lbsArray['status'] == "0" && $resultObj->ad_info->nation_code==156) {
		//保存到用户当前地理位置中
		$result["province"] = $resultObj->address_component->province;
		$result["city"] = $resultObj->address_component->city;
		$result["district"] = $resultObj->address_component->district;
	}
}

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

//返回结果
responseOK($result);

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