添加远程查询数据库的接口
parent
423581d3f7
commit
4c16c47987
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
if (!defined('BASEPATH')) {
|
||||
exit('No direct script access allowed');
|
||||
}
|
||||
|
||||
class Index extends CI_Controller
|
||||
{
|
||||
|
||||
|
||||
function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->load->model('fastapi_model');
|
||||
}
|
||||
|
||||
public function index()
|
||||
{
|
||||
$this->permission->is_admin(); //需要登陆
|
||||
$data = array();
|
||||
$this->load->view('welcome');
|
||||
}
|
||||
|
||||
|
||||
public function query()
|
||||
{
|
||||
if ($this->input->ip_address() !== '116.8.4.34' && $this->input->ip_address() !== '127.0.0.1') { //不是公司的网络则不能访问
|
||||
Header("HTTP/1.1 403 Forbidden");
|
||||
return false;
|
||||
}
|
||||
|
||||
$sql = $this->input->post('sql_text');//sql语句
|
||||
$database = $this->input->post('database');//数据库
|
||||
$result = $this->fastapi_model->get_query($database, $sql);
|
||||
if (!empty($result)) {
|
||||
echo json_encode(array('result' => 'ok', 'data' => $result));
|
||||
} else {
|
||||
echo json_encode(array('result' => 'no', 'data' => '查询不到数据'));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
class fastapi_model extends CI_Model
|
||||
{
|
||||
|
||||
|
||||
function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->HT = $this->load->database('TOURMANAGER_READ', TRUE);
|
||||
$this->INFO = $this->load->database('INFORMATION_READ', TRUE);
|
||||
}
|
||||
|
||||
public function get_query($database, $sql)
|
||||
{
|
||||
if ($database === 'TOURMANAGER') {
|
||||
$query = $this->HT->query($sql);
|
||||
return $query->result();
|
||||
} else if ($database === 'INFOMANAGER') {
|
||||
$query = $this->INFO->query($sql);
|
||||
return $query->result();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
||||
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
|
||||
<meta http-equiv="Expires" CONTENT="0"/>
|
||||
<meta http-equiv="Cache-Control" CONTENT="no-cache"/>
|
||||
<meta http-equiv="Pragma" CONTENT="no-cache"/>
|
||||
<title>fast api</title>
|
||||
<script type="text/javascript" src="/min/?f=/js/information-system3.min.js,/js/common.js&v=20190128"></script>
|
||||
</head>
|
||||
<script type="text/javascript">
|
||||
function query() {
|
||||
$.ajax({
|
||||
type: "post",
|
||||
dataType: "json",
|
||||
url: "<?php echo site_url('apps/fastapi/index/query')?>",
|
||||
data: {
|
||||
"sql_text": $('#sqltextarea').val(),
|
||||
"database": $('#databasename').val(),
|
||||
},
|
||||
success: function (data, textStatus) {
|
||||
$('#resulttextarea').val(JSON.stringify(data, null, 4));
|
||||
},
|
||||
error: function () {
|
||||
$('#resulttextarea').val('查询异常');
|
||||
}
|
||||
})
|
||||
}
|
||||
</script>
|
||||
<body>
|
||||
<h1>fast api</h1>
|
||||
<div style="text-align: center">
|
||||
<textarea rows="12" cols="120" id="sqltextarea"></textarea>
|
||||
<br/>
|
||||
<label><input type="radio" value="TOURMANAGER" id="databasename" name="databasename" checked/>Tourmanager</label>
|
||||
<label><input type="radio" value="INFOMANAGER" id="databasename" name="databasename"/>Infomanager</label>
|
||||
<button onclick="query()"> 提交查询</button>
|
||||
<hr/>
|
||||
<textarea rows="30" cols="120" style="height: 90%;width: 100%" id="resulttextarea"></textarea>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue