-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstartapi.php
More file actions
202 lines (183 loc) · 5.33 KB
/
Copy pathstartapi.php
File metadata and controls
202 lines (183 loc) · 5.33 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
class startapi {
private $db;
public function __construct() {
$this->db = $this->conn();
}
/**
* startapi::conn()
* 连接数据库
* @return
*/
private function conn() {
$config = require_once 'conn.php';
@ $db = new mysqli($config['host'], $config['user'], $config['passwd'], $config['dbname'], $config['port']);
if ($db->connect_error) {
exit('can\'t conn db:' . $db->connect_error);
}
$db->set_charset("utf8");
return $db;
}
/**
* startapi::db_select()
* 数据库查询
* @param mixed $sql
* @return
*/
public function db_select($sql = '', $list = false) {
$result = $this->db->query($sql);
$rows = array();
while($row = $result->fetch_assoc()) {
$rows[] = $row;
}
return $list ? $rows : (count($rows) ? $rows[0] : array());
}
/**
* startapi::type_lists()
* 接口分类列表
* @return
*/
public function type_lists() {
return $this->db_select("SELECT * FROM `type` WHERE deleted = 0 ORDER BY sort DESC, id ASC", true);
}
/**
* startapi::type_add()
* 分类新增
* @param array $post
* @return
*/
public function type_add($post = array()) {
$this->db->query("INSERT INTO `type` (`title`, `desc`) VALUES ('$post[title]', '$post[desc]')");
return $this->db->insert_id;
}
/**
* startapi::type_update()
* 分类更新
* @param integer $id
* @param array $post
* @return
*/
public function type_update($id = 0, $post = array()) {
return $this->db->query("UPDATE `type` SET `title` = '$post[title]', `desc` = '$post[desc]' WHERE id = $id");
}
/**
* startapi::type_info()
* 分类信息
* @param integer $id
* @return
*/
public function type_info($id = 0) {
return $this->db_select("SELECT * FROM `type` WHERE id = $id");
}
/**
* startapi::type_del()
* 分类删除
* @param integer $id
* @return
*/
public function type_del($id = 0) {
return $this->db->query("UPDATE `type` SET deleted = 1 WHERE id = $id");
}
/**
* startapi::type_sort()
* 分类排序
* @param mixed $new_sort
* @return void
*/
public function type_sort($new_sort = array()) {
$when = '';
foreach ($new_sort as $k => $v) {
$when .= ' WHEN ' . $k . ' THEN ' . $v;
}
return $this->db->query("UPDATE `type` SET `sort` = CASE `id` " . $when . ' END WHERE `id` IN (' . implode(',', array_keys($new_sort)) . ')');
}
/**
* startapi::api_lists()
* 接口列表
* @param integer $type_id
* @return
*/
public function api_lists($type_id = 0) {
return $this->db_select("SELECT * FROM `api` WHERE deleted = 0" . ($type_id ? " AND type_id = $type_id" : '') . " ORDER BY sort DESC, id ASC", true);
}
/**
* startapi::api_init()
* 接口参数处理
* @param mixed $post
* @return void
*/
public function api_init($post = array()) {
$post['is_authed'] = isset($post['is_authed']) ? 1 : 0;
if (isset($post['request_param'])) {
foreach ($post['request_param'] as $k => $v) {
$post['request_param'][$k]['is_required'] = isset($v['is_required']) ? 1 : 0;
}
} else {
$post['request_param'] = array();
}
if (isset($post['result_param'])) {
foreach ($post['result_param'] as $k => $v) {
$post['result_param'][$k]['is_required'] = isset($v['is_required']) ? 1 : 0;
}
} else {
$post['result_param'] = array();
}
$post['request_param'] = json_encode(array_values($post['request_param']), JSON_UNESCAPED_UNICODE);
$post['result_param'] = json_encode(array_values($post['result_param']), JSON_UNESCAPED_UNICODE);
return $post;
}
/**
* startapi::api_add()
* 新增接口
* @param mixed $post
* @return
*/
public function api_add($post = array()) {
$post = $this->api_init($post);
return $this->db->query("INSERT INTO `api` (`type_id`, `title`, `uri`, `method_type`, `desc`, `request_param`, `result_param`, `request_code`, `result_code`, `is_authed`)
VALUES ('$post[type_id]', '$post[title]', '$post[uri]', '$post[method_type]', '$post[desc]', '$post[request_param]', '$post[result_param]', '$post[request_code]', '$post[result_code]', '$post[is_authed]')");
}
/**
* startapi::api_update()
* 更新接口
* @param integer $id
* @param mixed $post
* @return
*/
public function api_update($id = 0, $post = array()) {
$post = $this->api_init($post);
return $this->db->query("UPDATE `api` SET `title` = '$post[title]', `uri` = '$post[uri]', `method_type` = '$post[method_type]', `desc` = '$post[desc]', `request_param` = '$post[request_param]',
`result_param` = '$post[result_param]', `request_code` = '$post[request_code]', `result_code` = '$post[result_code]', `is_authed` = '$post[is_authed]' WHERE `id` = $id");
}
/**
* startapi::api_sort()
* 接口排序
* @param mixed $new_sort
* @return
*/
public function api_sort($new_sort = array()) {
$when = '';
foreach ($new_sort as $k => $v) {
$when .= ' WHEN ' . $k . ' THEN ' . $v;
}
return $this->db->query("UPDATE `api` SET `sort` = CASE `id` " . $when . ' END WHERE `id` IN (' . implode(',', array_keys($new_sort)) . ')');
}
/**
* startapi::api_info()
* 接口信息
* @param integer $id
* @return
*/
public function api_info($id = 0) {
return $this->db_select("SELECT * FROM `api` WHERE id = $id");
}
/**
* startapi::api_del()
* 接口删除
* @param integer $id
* @return
*/
public function api_del($id = 0) {
return $this->db->query("UPDATE `api` SET deleted = 1 WHERE id = $id");
}
}