ajax_check_user_registed.php 2.28 KB
<?php

// 检查用户是否已经是注册用户
require_once ("../user_include.inc");

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

//根据常量获取定义的值
$jsCode = ParamUtil::getRequestString("code");
$openId = ParamUtil::getRequestString("openId");

ErrorLogger::doOutput("Compass...ajax_check_user_registed.php....jsCode=" . $jsCode, 0);
ErrorLogger::doOutput("Compass...ajax_check_user_registed.php....openId=" . $openId, 0);

$result = array();
//如果都为空说明调用错误
if(empty($jsCode)) {
	$result["message"] = "参数错误!";
	responseNG($result);
}

//有code的情况下
$appId = WECHAT_APP_ID;
$appSecret = WECHAT_APP_SECRET;
//调用微信接口获取用户的openId和unionId
//调用服务器查询并插入数据
$ch = curl_init();
$url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $appId . "&secret=" . $appSecret . "&js_code=" . $jsCode . "&grant_type=authorization_code";
//通过code换取网页授权access_token
$weixin =  file_get_contents($url);
$json = json_decode($weixin); //对JSON格式的字符串进行编码
$wxArray = get_object_vars($json);//转换成数组
$openId = $wxArray["openid"];
$sessionKey = $wxArray["session_key"];
$unionId = "";
if(isset($wxArray["unionid"])) {
	$unionId = $wxArray["unionid"];
}
if(!empty($openId)) {
	//保存session_key
	$userLoginDat = new UserLoginDat();
	$userLoginDat->openid = $openId;
	$userLoginDat->session_key = $sessionKey;
	$userLoginDat->save();

	//检索数据库
	$param['openid'] = $openId;
	$param['delete_flg'] = false;
	$tmpUserMstList = UserMst::getList($param,'id','desc', 0, 1);
	if(empty($tmpUserMstList)) {
		$result["registed"] = false;
		$result["openId"] = $openId;
		$result["unionId"] = $unionId;
		$result["message"] = "未注册!";
		responseOK($result);
	} else {
		$result["registed"] = true;
		$result["openId"] = $openId;
		$result["unionId"] = $tmpUserMstList[0]->unionid;
		$result["message"] = "已注册!";
		responseNG($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;
}
?>