Browse Source

Merge branch 'master' into develop

star7th 9 years ago
parent
commit
b050c8e488

+ 69 - 1
Application/Home/Controller/PageController.class.php

@@ -88,6 +88,74 @@ class PageController extends BaseController {
         $this->display();        
     }
 
+    //跳转到HTTP接口测试页面
+	public function http_api(){
+		
+		$this->display(); 
+	}
+
+	//处理HTTP测试请求,返回请求接口后的数据
+	public function ajaxHttpApi(){
+		$url=I('url');
+		$method=I('method');
+		$params=I('params');
+		if($method=='get'){
+			$url=$url."?".$params;
+			$return=$this->http_get($url);
+		}else{
+			$return=$this->http_post($url, $params);
+		}
+		echo $return;
+	}
+	/**
+	 * GET 请求
+	 *
+	 * @param string $url        	
+	 */
+	private function http_get($url) {
+		$oCurl = curl_init ();
+		if (stripos ( $url, "https://" ) !== FALSE) {
+			curl_setopt ( $oCurl, CURLOPT_SSL_VERIFYPEER, FALSE );
+			curl_setopt ( $oCurl, CURLOPT_SSL_VERIFYHOST, FALSE );
+		}
+		curl_setopt ( $oCurl, CURLOPT_URL, $url );
+		curl_setopt ( $oCurl, CURLOPT_RETURNTRANSFER, 1 );
+		$sContent = curl_exec ( $oCurl );
+		curl_close ( $oCurl );
+		return $sContent;
+	}
+	
+	/**
+	 * POST 请求
+	 *
+	 * @param string $url        	
+	 * @param array $param        	
+	 * @return string content
+	 */
+	private function http_post($url, $param) {
+		$oCurl = curl_init ();
+		if (stripos ( $url, "https://" ) !== FALSE) {
+			curl_setopt ( $oCurl, CURLOPT_SSL_VERIFYPEER, FALSE );
+			curl_setopt ( $oCurl, CURLOPT_SSL_VERIFYHOST, false );
+		}
+		if (is_string ( $param )) {
+			$strPOST = $param;
+		} else {
+			$aPOST = array ();
+			foreach ( $param as $key => $val ) {
+				$aPOST [] = $key . "=" . urlencode ( $val );
+			}
+			$strPOST = join ( "&", $aPOST );
+		}
+		curl_setopt ( $oCurl, CURLOPT_URL, $url );
+		curl_setopt ( $oCurl, CURLOPT_RETURNTRANSFER, 1 );
+		curl_setopt ( $oCurl, CURLOPT_POST, true );
+		curl_setopt ( $oCurl, CURLOPT_POSTFIELDS, $strPOST );
+		$sContent = curl_exec ( $oCurl );
+		curl_close ( $oCurl );
+		return $sContent;
+	}
+
     //保存
     public function save(){
         $login_user = $this->checkLogin();
@@ -226,4 +294,4 @@ class PageController extends BaseController {
     }
 
 
-}
+}

+ 8 - 0
Application/Home/Lang/en-us.php

@@ -116,6 +116,7 @@ return array(
     'inser_apidoc_template'=>'Insert apidoc template',
     'inser_database_doc_template'=>'Insert db-doc template',
     'json_to_table'=>'Json to table',
+    'http_test_api'=>'Api test online',
     'json_to_table_description'=>'Please paste a section of JSON, the program will automatically parse and generate JSON parameters table. This feature is suitable for the rapid preparation of API documents returned to the parameter table',
     'confirm'=>'Confirm',
     'cancel'=>'Cancel',
@@ -126,6 +127,13 @@ return array(
     'recover_to_this_version'=>'Restore to this version',
     'close'=>'Close',
     'finish'=>'Finish',
+    'api_test_title'=>'Test for GET and POST',
+    'api_address_description'=>'Api address(e.g:ttp://www.abc.com/api/login)',
+    'api_address'=>'Api address',
+    'params_description'=>'Params(e.g:user_id=121&age=22&date=2016-06-02)',
+    'params'=>'Params',
+    'clear'=>'Clear',
+    'result'=>'Result',
 
     //user
     'login'=>'Login',

+ 9 - 0
Application/Home/Lang/zh-cn.php

@@ -116,6 +116,7 @@ return array(
     'inser_apidoc_template'=>'插入API接口模板',
     'inser_database_doc_template'=>'插入数据字典模板',
     'json_to_table'=>'JSON转参数表格',
+    'http_test_api'=>'在线HTTP请求测试',
     'json_to_table_description'=>'请粘贴一段json,程序将自动将json解析并生成参数表格。此功能适合用于快速编写API文档的返回参数表格',
     'confirm'=>'确定',
     'cancel'=>'取消',
@@ -126,6 +127,13 @@ return array(
     'recover_to_this_version'=>'恢复到此版本',
     'close'=>'关闭',
     'finish'=>'完成',
+    'api_test_title'=>'GET和POST测试',
+    'api_address_description'=>'接口地址(如:http://www.abc.com/api/login)',
+    'api_address'=>'接口地址',
+    'params_description'=>'参数(如:user_id=121&age=22&date=2016-06-02)',
+    'params'=>'参数',
+    'clear'=>'清除',
+    'result'=>'返回结果',
 
     //user
     'login'=>'登录',
@@ -174,4 +182,5 @@ return array(
     
     "default_title" =>'默认页面',
 
+
 );

+ 1 - 0
Application/Home/View/Page/edit.html

@@ -43,6 +43,7 @@
         <button id="api-doc" tabindex="4" >{$Think.Lang.inser_apidoc_template}</button>
         <button id="database-doc" tabindex="5" >{$Think.Lang.inser_database_doc_template}</button>
         <button id="jsons" tabindex="7" >{$Think.Lang.json_to_table}</button>
+        <a href="{:U('Home/page/http_api')}" target="_blank" class="btn" >{$Think.Lang.http_test_api}</a>
     </div>
     <div id="editormd">
         <textarea id="page_content" style="display:none;" tabindex="6" >{$page.page_content}</textarea>

+ 94 - 0
Application/Home/View/Page/http_api.html

@@ -0,0 +1,94 @@
+<include file="Common/header" />
+<style type="text/css">
+	input{
+		width: 100%;
+	}
+</style>
+<div id="layout" style="width: 70%;margin: auto;">
+	<form class="form-horizontal">
+		<h2 style="text-align: center;">{$Think.Lang.api_test_title}</h2>
+        <div class="control-group">
+          <div class="controls">
+            <select style="width: 100px;" id="method" >
+            	<option value="post">POST</option>
+            	<option value="get">GET</option>
+            </select>
+          </div>
+        </div>
+        <div class="control-group">
+          <div class="controls">
+          	<label >{$Think.Lang.api_address_description}</label>
+            <input type="text" id="url"  placeholder="{$Think.Lang.api_address}" >
+          </div>
+        </div>
+        <div class="control-group">
+          <div class="controls">
+          	<label >{$Think.Lang.params_description})</label>
+            <input type="text" id="params" placeholder="{$Think.Lang.params}" >
+          </div>
+        </div>
+        <div class="control-group">
+          <div class="controls">
+            <button type="button" class="btn btn-primary" id="http-submit">{$Think.Lang.submit}</button>
+            <button type="reset"  class="btn btn-link" >{$Think.Lang.clear}</button>
+          </div>
+        </div>
+        <div class="control-group">
+          <div class="controls">
+          	<label >{$Think.Lang.result}</label>
+			<textarea style="width: 100%;height: 300px;" id="http-response"></textarea>
+          </div>
+        </div>
+      </form>
+</div>
+<include file="Common/footer" />
+<script type="text/javascript">
+	$(function () {
+		//格式化json数据
+		function dump(arr,level) { 
+			 var dumped_text = ""; 
+			 if(!level) level = 0; 
+			 
+			 //The padding given at the beginning of the line. 
+			 var level_padding = ""; 
+			 for(var j=0;j<level+1;j++) level_padding += "     "; 
+			 if(typeof(arr) == 'object') { //Array/Hashes/Objects 
+			 	 var i=0;
+				 for(var item in arr) { 
+					 var value = arr[item]; 
+					 if(typeof(value) == 'object') { //If it is an array, 
+						 dumped_text += level_padding + "\"" + item + "\" : \{ \n"; 
+						 dumped_text += dump(value,level+1); 
+					 	 dumped_text +=level_padding +"\}";
+					 } else { 
+					 	dumped_text += level_padding + "\"" + item + "\" : \"" + value + "\""; 
+					 } 
+					 if(i<Object.getOwnPropertyNames(arr).length-1){
+					 	dumped_text+=", \n";
+					 }else{
+					 	dumped_text+=" \n";
+					 }
+					 i++;
+				 } 
+			 } else { //Stings/Chars/Numbers etc. 
+			 	dumped_text = "===>"+arr+"<===("+typeof(arr)+")"; 
+			 } 
+			 return dumped_text; 
+		} 
+		$("#http-submit").on('click',function () {
+			$.post("?s=home/page/ajaxHttpApi",{
+				url:$("#url").val(),
+				method:$("#method").val(),
+				params:$("#params").val()
+			},function (data) {
+				try{
+					var text="\{ \n"+dump(JSON.parse(data))+" \}";//整体加个大括号
+					$("#http-response").val(text);
+				}catch(e){
+					//非json数据直接显示
+					$("#http-response").val(data);
+				}
+			});
+		});
+	});
+</script>

+ 15 - 12
install/ajax.php

@@ -14,6 +14,9 @@ if(!new_is_writeable("./")){
 	ajax_out("请赋予 /install 目录以可写权限!",10098);
 }
 
+if(!new_is_writeable("../Public/Uploads")){
+	ajax_out("请赋予 /Public/Uploads/ 目录以可写权限!",10098);
+}
 
 if(!new_is_writeable("../Application/Runtime")){
 	ajax_out("请赋予 /Application/Runtime 目录以可写权限!",10095);
@@ -89,14 +92,14 @@ function user_mysql(){
 
         //检测数据库配置是否能链接得上
 
-        $con = mysql_connect($db_host.":".$db_port,$db_user,$db_password);
-        $r = mysql_select_db($db_name);
+        $con = mysqli_connect($db_host.":".$db_port,$db_user,$db_password);
+        $r = mysqli_select_db($con, $db_name);
         if (!$con || !$r) {
            ajax_out("数据库链接错误,请检查配置信息是否填写正确",10002);
            exit();
         }
-        mysql_query("SET NAMES UTF8");
-        $row = mysql_fetch_array(mysql_query(" SELECT COUNT(*) FROM user "));
+        mysqli_query($con, "SET NAMES UTF8");
+        $row = mysqli_fetch_array(mysqli_query($con, " SELECT COUNT(*) FROM user "));
         
         if ($row) {
            ajax_out("检测到该数据库已经存在数据。请清理后再重试",10003);
@@ -104,7 +107,7 @@ function user_mysql(){
         }
         
         //开始导入mysql数据库 
-        $ret = import_mysql();
+        $ret = import_mysql($con);
         if (!$ret) {
            ajax_out("创建数据库表失败!",10004);
            exit();
@@ -194,7 +197,7 @@ function import_mysql($con){
 	KEY `addtime` (`addtime`),
 	KEY `s_number` (`s_number`)
 	) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COMMENT='目录表' AUTO_INCREMENT=1 ";
-	mysql_query($sql);
+	mysqli_query($con, $sql);
 
 	//创建item表
 	$sql = "CREATE TABLE IF NOT EXISTS `item` (
@@ -209,7 +212,7 @@ function import_mysql($con){
 	PRIMARY KEY (`item_id`),
 	KEY `addtime` (`addtime`)
 	) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COMMENT='项目表' AUTO_INCREMENT=1 ";
-	mysql_query($sql);	
+	mysqli_query($con, $sql);	
 
 	//创建项目成员表
 	$sql = "CREATE TABLE IF NOT EXISTS `item_member` (
@@ -220,7 +223,7 @@ function import_mysql($con){
 	`addtime` int(11) NOT NULL DEFAULT '0',
 	PRIMARY KEY (`item_member_id`)
 	) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COMMENT='项目成员表' AUTO_INCREMENT=1 ";
-	mysql_query($sql);
+	mysqli_query($con, $sql);
 
 	//创建项目page表
 	$sql = "CREATE TABLE IF NOT EXISTS `page` (
@@ -237,7 +240,7 @@ function import_mysql($con){
 	KEY `addtime` (`addtime`),
 	KEY `s_number` (`s_number`)
 	) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COMMENT='文章页面表' AUTO_INCREMENT=1 ";
-	mysql_query($sql);
+	mysqli_query($con, $sql);
 
 	//创建项目page_history表
 	$sql = "CREATE TABLE IF NOT EXISTS `page_history` (
@@ -255,7 +258,7 @@ function import_mysql($con){
 	KEY `addtime` (`addtime`),
 	KEY `page_id` (`page_id`)
 	) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COMMENT='页面历史表' AUTO_INCREMENT=1 ";
-	mysql_query($sql);
+	mysqli_query($con, $sql);
 
 	//创建项目user表
 	$sql = "CREATE TABLE IF NOT EXISTS `user` (
@@ -275,7 +278,7 @@ function import_mysql($con){
 	UNIQUE KEY `username` (`username`) USING BTREE
 	) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COMMENT='用户表' AUTO_INCREMENT=1 ";
 
-	$ret = mysql_query($sql);
+	$ret = mysqli_query($con, $sql);
 
 	if ($ret) {
 		return true;
@@ -311,4 +314,4 @@ function new_is_writeable($file) {
 	}
 
 	return $writeable;
-}
+}