<?php
//$ip_header = substr($_SERVER['REMOTE_ADDR'],0,strlen('192.168'));
 //if($ip_header!='192.168'){
 //	exit('ip error');
 //}
error_reporting(E_ALL& ~E_NOTICE);
include('config.php');
include('fc.php');
$path = './interface';
$filename = $path.'/'.$_REQUEST['dir'].'/'.$_REQUEST['file'].'.php';

$doc = getDoc($filename);
if($_POST['doit']){
	$httpheader = array('CONTENT_TYPE:application/json');
	//登录
	$curl = curl_init();
	$loginJson = json_encode([
	    'AccessKeyId' => ACCESSKEYID,
        'AccessKeySecret' => ACCESSKEYSECRET
    ]);
	curl_setopt($curl, CURLOPT_TIMEOUT,1); //超时秒
	curl_setopt($curl, CURLOPT_HTTPHEADER,$httpheader);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($curl, CURLOPT_POST, 1);
	curl_setopt($curl, CURLOPT_POSTFIELDS, $loginJson);
	curl_setopt($curl, CURLOPT_URL, API_LOGIN_URL);
	$res = curl_exec($curl);
	if($res===false){
		$res = json_encode(curl_error($curl));
	}
	$arr = json_decode($res, JSON_OBJECT_AS_ARRAY);

	if(!isset($arr['code']) || $arr['code']==0){
		$content = '授权失败';
	}elseif($arr['code'] == 200){
		$code = 200;
		$token = $arr['data']['token'];

		$arr = [];
		//调用接口
		array_push($httpheader,'x-token:'.$token);
		curl_setopt($curl, CURLOPT_TIMEOUT,5*60); //超时秒
		curl_setopt($curl, CURLOPT_HTTPHEADER,$httpheader);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($curl, CURLOPT_URL,$_POST['url']);
		if(strtoupper($_POST['method'])!='GET') curl_setopt($curl, CURLOPT_POST, 1);
		$keys = $_POST['keys'];
		//if($keys){
			$keys['token'] = $token;
			foreach($keys as $k=>$v){
				$o = json_decode($v);
				if(is_array($o) || is_object($o)){
					$keys[$k] = $o;
				}
			}

		//}
         $request_json = json_encode($keys);
	     curl_setopt($curl, CURLOPT_POSTFIELDS, $request_json);
		$content = curl_exec($curl);
		if($content===false){
			$content = json_encode(curl_error($curl));
		}
		$arr = json_decode($content);

		if(is_array($arr)){
			$content = json_encode($arr,JSON_UNESCAPED_UNICODE);
		}
	}
	curl_close($curl);
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title><?php echo $_REQUEST['dir'].'/'.$_REQUEST['file'];?></title>
<meta name="applicable-device" content="pc" />
<script src="./jquery.min.js"></script>
<script src="./fc.js"></script>
<style>
h1{font-size:18px}
table {
    display: table;
    width: 100%;
    border-collapse: collapse;
    border-spacing: 0;
    overflow: auto;
}
table tr {
    background-color: #fff;
	border-top: 1px solid #ccc;
}
table th {
    font-weight: 400;
    background: #eee;
}
table td, table th {
    padding: 6px 13px;
    border: 1px solid #ddd;
    word-break: break-all;
	font-size:14px;
	text-align:left;
}
textarea{font-size:12px;width:100%;}
input{font-size:14px;width:100%;padding:5px}

code,pre {
    font-family: "SourceCodePro","PingFangSC-Regular", "sans-serif", "Hiragino Sans GB", "Microsoft YaHei","WenQuanYi Micro Hei";
	padding: .2em;
	margin: 0;
	background-color: #f7f7f7;
	line-height:18px;
	font-size: .85em;
}

</style>
</head>
<body>
<form method="post" action="?">
	<h1>接口网址</h1>
	<div><input type="text" name="url" value="<?php echo API_MAIN_URL.$doc['url']?>" style="width:100%"></div>
	<h1>请求方式</h1>
	<select name="method" autocomplete="off">
		<option value="GET" <?php if($doc['method']=='GET') echo 'selected';?>>GET</option>
		<option value="POST" <?php if($doc['method']=='POST') echo 'selected="selected"';?>>POST</option>
		<option value="GET|POST" <?php if($doc['method']=='GET|POST') echo 'selected="selected"';?>>GET|POST</option>
	</select>
	<h1>请求参数</h1>
	<table style="width:100%">
	<tr>
		<th style="min-width:100px">参数</th>
		<th style="min-width:60px">描述</th>
		<th style="min-width:50%">值</th>
		<th style="width:100px">类型</th>
		<th style="width:30px">必须</th>
	</tr>
	<?php if($doc['request'])foreach($doc['request'] as $k=>$v):
	if(strtoupper($v['key'])!='--GROUP--'):?>
	<tr>
	<td><?php echo $v['key']?></td>
	<td><?php echo $v['name']?></td>
	<td><input type="text" name="keys[<?php echo $v['key']?>]" value="<?php echo ($_POST['doit']) ? htmlspecialchars($_POST['keys'][$v['key']]) : $v['example'];?>" style="width:98%"></td>
	<td><?php echo $v['type']?></td>
	<td><?php echo $v['must']?></td>
	</tr>
	<?php endif; endforeach;?>
	</table>
	<input type="hidden" name="dir" value="<?php echo $_REQUEST['dir']?>">
	<input type="hidden" name="file" value="<?php echo $_REQUEST['file']?>">
	<input type="hidden" name="method" value="<?php echo $doc['method']?>">
	<input type="hidden" name="doit" value="1">
	<input type="submit" value=" 提交 " style="width:100px">
</form>
<?php if($_POST['doit']):?>
<h1>请求参数JSON</h1>
<div>
<pre class="language-javascript"><code class="lang-json"><script>document.write(formatJson('<?php echo json_encode(json_decode($request_json));?>'));</script></code></pre>
</div>
<h1>返回结果</h1>
<div>
<?php if($code==200):?>
<pre class="language-javascript"><code class="lang-json"><script>document.write(formatJson('<?php echo $content;?>'));</script></code></pre>
<?php else:?>
<?php echo $content;?>
<?php endif;?>
</div>
<?php endif;//doit?>
</body>
</html>
