From 4f061e273f1b24d2a3b0556a9a4476cc849326b5 Mon Sep 17 00:00:00 2001 From: zhujinyong Date: Sat, 13 Jul 2013 01:27:53 +0000 Subject: [PATCH] * code for task#1541. --- module/api/control.php | 39 ++++++++++++++++++++++++++ module/api/js/view.js | 21 ++++++++++++++ module/api/lang/en.php | 11 ++++++++ module/api/lang/zh-cn.php | 11 ++++++++ module/api/model.php | 18 +++++++++++- module/api/view/view.html.php | 53 +++++++++++++++++++++++++++++++++++ 6 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 module/api/js/view.js create mode 100644 module/api/view/view.html.php diff --git a/module/api/control.php b/module/api/control.php index 94c0357c6e..ffaa4674e2 100644 --- a/module/api/control.php +++ b/module/api/control.php @@ -47,4 +47,43 @@ class api extends control $this->output = json_encode($output); die($this->output); } + + public function view($filePath, $action) + { + if($filePath) + { + $host = common::getSysURL() . $this->config->webRoot; + $filePath = helper::safe64Decode($filePath); + if($action == 'extendModel') + { + $method = $this->api->getMethod($filePath, 'Model'); + } + elseif($action == 'extendControl') + { + $method = $this->api->getMethod($filePath); + } + + if(!empty($_POST)) + { + $param = ''; + foreach($_POST as $key => $value) $param .= '&' . $key . '=' . $value; + $param = trim($param, '&') . "&{$this->config->sessionVar}=" . session_id(); + $url = rtrim($host, '/') . $this->createLink($method->className, $method->methodName, $param, 'json'); + $content = file_get_contents($url); + $result = json_decode($content); + $status = $result->status; + $data = json_decode($result->data); + $data = '' . print_r($data, true) . ''; + + $response['result'] = 'success'; + $response['status'] = $status; + $response['url'] = $url; + $response['data'] = $data; + $this->send($response); + } + $this->view->method = $method; + $this->view->filePath = $filePath; + $this->display(); + } + } } diff --git a/module/api/js/view.js b/module/api/js/view.js new file mode 100644 index 0000000000..705eaa0830 --- /dev/null +++ b/module/api/js/view.js @@ -0,0 +1,21 @@ +$(document).ready(function() +{ + var options = + { + target : null, + timeout : 30000, + dataType: 'json', + success:function(response,statusText) + { + $('#result .url').html(response.url); + $('#result .status').html(response.status); + $('#result .data').html(response.data); + $('#result').removeClass('hidden'); + } + }; + $('#apiForm').submit(function() + { + $(this).ajaxSubmit(options); + return false; + }); +}); diff --git a/module/api/lang/en.php b/module/api/lang/en.php index 6234c621b8..14af76dfb4 100644 --- a/module/api/lang/en.php +++ b/module/api/lang/en.php @@ -12,3 +12,14 @@ $lang->api = new stdclass(); $lang->api->common = 'API'; $lang->api->getModel = 'Super Model API'; + +$lang->api->position = 'Position'; +$lang->api->startLine = "%s, line %s"; +$lang->api->desc = 'Desc'; +$lang->api->test = 'Test'; +$lang->api->noParam = 'No params in this method'; +$lang->api->submit = 'submit'; +$lang->api->url = 'Request url'; +$lang->api->result = 'Result'; +$lang->api->status = 'status'; +$lang->api->data = 'data'; diff --git a/module/api/lang/zh-cn.php b/module/api/lang/zh-cn.php index 6770d88c7e..2169e670e6 100644 --- a/module/api/lang/zh-cn.php +++ b/module/api/lang/zh-cn.php @@ -12,3 +12,14 @@ $lang->api = new stdclass(); $lang->api->common = 'API接口'; $lang->api->getModel = '超级model调用接口'; + +$lang->api->position = '位置'; +$lang->api->startLine = "%s,%s行"; +$lang->api->desc = '描述'; +$lang->api->test = '测试'; +$lang->api->noParam = '该方法没有参数'; +$lang->api->submit = '提交'; +$lang->api->url = '请求地址'; +$lang->api->result = '返回结果'; +$lang->api->status = '状态'; +$lang->api->data = '内容'; diff --git a/module/api/model.php b/module/api/model.php index 2cf1b6cae3..25e69c3ddb 100644 --- a/module/api/model.php +++ b/module/api/model.php @@ -11,5 +11,21 @@ */ class apiModel extends model { -} + public function getMethod($filePath, $ext = '') + { + $fileName = dirname($filePath); + $className = basename(dirname(dirname($filePath))); + if(!class_exists($className)) include($fileName); + $methodName = basename($filePath); + $method = new ReflectionMethod($className . $ext, $methodName); + $data = new stdClass(); + $data->startLine = $method->getStartLine(); + $data->comment = $method->getDocComment(); + $data->parameters = $method->getParameters(); + $data->className = $className; + $data->methodName = $methodName; + $data->fileName = $fileName; + return $data; + } +} diff --git a/module/api/view/view.html.php b/module/api/view/view.html.php new file mode 100644 index 0000000000..61225f5390 --- /dev/null +++ b/module/api/view/view.html.php @@ -0,0 +1,53 @@ + + + + + + + + + + + + + + + + +
+ +
+ {$lang->api->position}

"?> + ' . sprintf($lang->api->startLine, $method->fileName, $method->startLine) . ''?> +
+ {$lang->api->desc}

"?> + ' . str_replace("\n", "
", $method->comment) . ''?> +
+ {$lang->api->test}

"?> + parameters):?> +
+ + parameters as $param):?> + + + + + + + +
name?>name", $param->isOptional() ? $param->getDefaultValue() : '')?>
+ api->submit)?> + +
+
+ + api->noParam;?> + +
+