<?php
require(dirname(__FILE__)."/"."global.php");


if(!$lfjid){
	$url = urlencode($WEBURL);
	refreshto("waplogin.php?havechoose=1&fromurl=$url","请用帐号登录成功后，微信才可绑定该帐号！",3);
}elseif($lfjdb[weixin_api]){
	showerr('你当前的帐号“{$lfjid}”已经绑定过微信了！');
}
	
if($state==1){
	
	if(!$code){
		showerr('code 值不存在！');
	}
	$string = file_get_contents("https://api.weixin.qq.com/sns/oauth2/access_token?appid=$webdb[wxpay_AppID]&secret=$webdb[wxpay_AppSecret]&code=$code&grant_type=authorization_code");
	
	$array = json_decode($string,true);
	
	$openid = $array['openid'];
	if(!$openid){
		if($string == ''){
			showerr('获取微信接口内容失败，请确认你的服务器已打开 extension=php_openssl.dll ');
		}
		showerr('openid 值不存在！');
	}

	$openid = addslashes($openid);

	if(wx_check_attention($openid)){
		$wxattention=1;
	}else{
		$wxattention=0;
	}

	$db->query("UPDATE {$pre}memberdata SET weixin_api = '$openid',wx_attention='$wxattention' WHERE uid='$lfjuid'");

	refreshto("$webdb[www_url]/member/wapindex.php","恭喜你，成功绑定微信，下次可以使用微信直接登录！",3);

}else{

	//跳转到微信服务器再返回来，将会得到一个有效的code值。
	$url = urlencode($WEBURL);
	echo "<META HTTP-EQUIV=REFRESH CONTENT='0;URL=https://open.weixin.qq.com/connect/oauth2/authorize?appid=$webdb[wxpay_AppID]&redirect_uri=$url&response_type=code&scope=snsapi_base&state=1#wechat_redirect'>";
	exit;
}

?>