<?php
//====== 获取微信 ======
require "common.php";
switch(Req("x")){
	case "r":
	echo "<script>window.location='https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$cfg["Ag"]["Wa"]."&redirect_uri=".urlencode($cfg["Aw"]."openid.php?r=".Req("r"))."&response_type=code&scope=snsapi_base&state=".rand(100,999)."#wechat_redirect';</script>";
	break;

	case "c":
	if(!file_exists("openid.txt")){die('{"msg":"ok","woi":""}');}
	$a=explode("=",file_get_contents("openid.txt")."=");
	if($a[0]==Req("r")){echo '{"msg":"ok","woi":"'.$a[1].'"}';}else echo '{"msg":"ok","woi":""}';
	unlink("openid.txt");
	break;

	default:
	$m=Dec(Cul("https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$cfg["Ag"]["Wa"]."&secret=".$cfg["Ag"]["Wk"]."&code=".Req("code")."&grant_type=authorization_code"));
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=360, initial-scale=1, user-scalable=no">
<title>Get OpenID</title>
</head>
<body style="font-size:16px;text-align:center;">
<?php
if(isset($m["openid"])){
	file_put_contents("openid.txt",Req("r")."=".$m["openid"]);
	echo "获取OpenID成功，请返回管理端操作！";
}else echo "获取OpenID失败，请重试！";
?>
</body>
</html>
<?php
}
?>