<?php

// 【区域管理】获取指定学校的志愿者活动列表
require_once ("../user_include.inc");

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

//获取参数
$openId = ParamUtil::getRequestString("openid");
$iv = ParamUtil::getRequestString("iv");
$encryptedData = ParamUtil::getRequestString("encryptedData");

//参数检查
if(empty($openId) || empty($iv) || empty($encryptedData)) {
	$result["message"] = "参数错误!";
	responseNG($result);
}

//获取session_key解析数据
$param = array();
$param['openid'] = $openId;
$param['delete_flg'] = false;
$tmpList = UserLoginDat::getList($param,'id','desc', 0, 1);
if(empty($tmpList)) {
	$result["message"] = "数据错误!";
	responseNG($result);
}
$sessionKey = $tmpList[0]->session_key;

$pc = new WXBizDataCrypt(WECHAT_APP_ID, $sessionKey);
$errCode = $pc->decryptData($encryptedData, $iv, $userInfo );
//解析userInfo获取unionId
$json = json_decode($userInfo); //对JSON格式的字符串进行编码
$wxArray = get_object_vars($json);//转换成数组
$unionId = $wxArray["unionId"];

ErrorLogger::doOutput("Compass...ajax_get_unionid.php....End.", 0);
if ($errCode == 0) {
	//删除该用户的所有session_key
	$sql = "delete from user_login_dat where openid='{$openId}'";
	$db = &CompassDBManager::getInstance();
	$db->executeQuery($sql);
	$result["unionId"] = $unionId;
    responseOK($result);
} else {
	$result["message"] = "解析错误!";
	responseNG($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;
}
?>