-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
115 lines (97 loc) · 3.09 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Http\UploadedFile;
$app = new \Slim\App();
class api
{
private function conn()
{
// $conn = new mysqli('b-x90akuqk2xpfn7.bch.rds.hkg.baidubce.com', 'b_x90akuqk2xpfn7', 'handaye123', 'b_x90akuqk2xpfn7');
$conn = new mysqli('localhost', 'root', 'admin1', 'geodb');
$conn->set_charset("utf8");
return $conn;
}
private function getSQL($type, $param)
{
switch ($type) {
case 'createJSON':
return "create table json( Id int primary key auto_increment, Name varchar(255), Description varchar(255),Lat decimal(9, 7),Lng decimal(10, 7))";
case 'insert':
$Name = $param->Name;
$Description = $param->Description;
$Lat = $param->Lat;
$Lng = $param->Lng;
return "insert into json values (0,'$Name','$Description','$Lat','$Lng')";
case 'get':
return "select * from json where id = $param";
case 'checkExist':
return "select TABLE_NAME from information_schema.TABLES where TABLE_SCHEMA ='geodb' and TABLE_NAME = '$param'";
case'getAll':
return "select * from json";
default:
return "";
}
}
public function test($param)
{
return $param->Lat;
}
public function insert($param)
{
return $this->conn()->query($this->getSQL('insert', $param));
}
public function createJSON()
{
return $this->conn()->query($this->getSQL('createJSON', null));
}
public function get($id)
{
return $this->conn()->query($this->getSQL('get', $id));
}
public function getAll()
{
return $this->conn()->query($this->getSQL('getAll', null));
}
public function checkExist($tableName)
{
$this->conn()->query($this->getSQL('checkExist', $tableName));
}
public function success($result)
{
$arr = [];
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$arr[] = $row;
}
return json_encode(array('code' => 200, 'data' => $arr));
}
public function error()
{
return json_encode(array('code' => 444, 'msg' => 'error'));
}
}
/**
* 插入经纬度信息
*/
$app->get('/insert/{Name}/{lat}/{lng}/{Description}', function ($request, $response, $args) {
$param = (object)array(
'Name' => $args['Name'],
'Lat' => $args['lat'],
'Lng' => $args['lng'],
'Description' => $args['Description']
);
$result = (new api)->insert($param);
return $response->withStatus(200)->write($result);
});
$app->get('/get', function ($request, $response, $args) {
$response->withHeader('Content-Type', 'application/json');
$api = (new api);
$result = $api->getAll();
if (!$result) {
return $response->getBody()->write($api->error());
} else {
return $response->getBody()->write($api->success($result));
}
});
$app->run();