Api.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. <?php
  2. namespace Manage\Src;
  3. use Dever;
  4. use Dever\Http\Api as Base;
  5. class Api
  6. {
  7. /**
  8. * fast
  9. *
  10. * @var array
  11. */
  12. private $fast = array();
  13. /**
  14. * data
  15. *
  16. * @var array
  17. */
  18. private $data = array();
  19. /**
  20. * 启动tcp服务
  21. *
  22. * @return array
  23. */
  24. public function tcp()
  25. {
  26. //return Dever::tcpReload(30000);
  27. }
  28. /**
  29. * 测试dyna配置
  30. *
  31. * @return array
  32. */
  33. public function dyna()
  34. {
  35. $data['name'] = 1;
  36. $data = Dever::dyna('home', $data);
  37. return $data;
  38. }
  39. /**
  40. * 测试传统模板引擎(类似smarty)
  41. *
  42. * @return array
  43. */
  44. public function test_display_api()
  45. {
  46. $data['test'] = 1111111111111;
  47. $data['data'] = $this->test_foreach();
  48. return Dever::render('example/render', $data);
  49. }
  50. public function test_display_data()
  51. {
  52. return 12323234;
  53. }
  54. public function test_foreach()
  55. {
  56. $config = array
  57. (
  58. array
  59. (
  60. 'id' => 1,
  61. 'name' => '1111',
  62. 'child' => array
  63. (
  64. array
  65. (
  66. 'id' => 3,
  67. 'name' => '3333',
  68. 'child' => array
  69. (
  70. array
  71. (
  72. 'id' => 9,
  73. 'name' => '9999',
  74. ),
  75. array
  76. (
  77. 'id' => 10,
  78. 'name' => '10101010',
  79. ),
  80. ),
  81. ),
  82. array
  83. (
  84. 'id' => 4,
  85. 'name' => '4444',
  86. ),
  87. ),
  88. ),
  89. array
  90. (
  91. 'id' => 2,
  92. 'name' => '2222',
  93. 'child' => array
  94. (
  95. array
  96. (
  97. 'id' => 5,
  98. 'name' => '5555',
  99. ),
  100. array
  101. (
  102. 'id' => 6,
  103. 'name' => '6666',
  104. ),
  105. ),
  106. ),
  107. );
  108. return $config;
  109. }
  110. /**
  111. * 测试接口定义
  112. *
  113. * @return array
  114. */
  115. public function test_api()
  116. {
  117. $data = Dever::load('module/data.test');
  118. print_r($data);die;
  119. }
  120. /**
  121. * 测试逻辑验证步骤,必须包含step
  122. *
  123. * @return array
  124. */
  125. public function test_step_1()
  126. {
  127. print_r(Dever::input('type'));
  128. # 传给下一步的数据
  129. Dever::setStep('test');
  130. $url = Dever::url('api.test_step_2?' . http_build_query(Dever::$global['step'][1]));
  131. print_r($url);die;
  132. #ok
  133. echo 11;die;
  134. }
  135. /**
  136. * 测试逻辑验证步骤,必须包含step
  137. *
  138. * @return array
  139. */
  140. public function test_step_2()
  141. {
  142. $data = Dever::getStep();
  143. print_r($data);die;
  144. echo 22;die;
  145. }
  146. /**
  147. * 从数据库中得到所有接口,如果为空,则从项目中读取一次
  148. *
  149. * @return array
  150. */
  151. private function get($auth = '')
  152. {
  153. $data = Dever::load('manage/api-all');
  154. if ($data) {
  155. foreach ($data as $k => $v) {
  156. }
  157. } else {
  158. # 数据为空,读取一次,后续就得手动读取了
  159. $this->load();
  160. $data = $this->get();
  161. }
  162. return $data;
  163. }
  164. /**
  165. * 更新接口
  166. *
  167. * @return array
  168. */
  169. public function update_api()
  170. {
  171. $this->load();
  172. Dever::out('yes');
  173. }
  174. /**
  175. * 载入从项目中定义好的接口
  176. *
  177. * @return array
  178. */
  179. public function load()
  180. {
  181. $project = Dever::load('manage/project.get');
  182. foreach ($project as $k => $v) {
  183. $data = Base::config($v['path']);
  184. if ($data) {
  185. foreach ($data as $i => $j) {
  186. $j['order'] = isset($j['order']) ? $j['order'] : 1;
  187. $this->updateApi($i, $j['name'], $j['type'], $k, $j['request'], $j['response'], $j['order']);
  188. }
  189. }
  190. }
  191. }
  192. private function updateApi($site, $name, $type, $project, $request, $response, $reorder, $state = true)
  193. {
  194. $info = Dever::db('manage/api')->site(array('where_site' => $site, 'where_project' => $project));
  195. if (!$info) {
  196. $update['add_site'] = $site;
  197. $update['add_name'] = $name;
  198. $update['add_type'] = $type;
  199. $update['add_project'] = $project;
  200. $update['add_reorder'] = $reorder;
  201. $info['id'] = Dever::db('manage/api')->insert($update);
  202. } elseif ($state) {
  203. $update['set_site'] = $site;
  204. $update['set_name'] = $name;
  205. $update['set_type'] = $type;
  206. $update['set_project'] = $project;
  207. $update['set_reorder'] = $reorder;
  208. $update['where_id'] = $info['id'];
  209. Dever::db('manage/api')->update($update);
  210. }
  211. if ($info['id'] > 0) {
  212. $this->updateApiData(array($response, $request), $info['id']);
  213. }
  214. return true;
  215. }
  216. private function updateApiData($data, $id)
  217. {
  218. $this->api_response = $this->api_request = array();
  219. foreach($data as $k => $v) {
  220. if ($v && is_array($v)) {
  221. if ($k == 0) {
  222. $api = 'api_response';
  223. } else {
  224. $api = 'api_request';
  225. }
  226. $this->insertApiData($api, $v, $id, -1, 0);
  227. }
  228. }
  229. }
  230. private function insertApiData($api, $data, $aid, $id = -1, $num = 0)
  231. {
  232. foreach($data as $k => $v) {
  233. $value = $v;
  234. if (is_array($value)) {
  235. $value = 'array:' . $k;
  236. if (isset($v[0][0])) {
  237. $v = $v[0][0];
  238. }
  239. if (isset($v[0])) {
  240. $v = $v[0];
  241. }
  242. }
  243. if (isset($this->{$api}[$num][$k])) {
  244. //continue;
  245. }
  246. $this->{$api}[$num][$k] = $value;
  247. $insertId = $this->insertApiDataOne($api, $k, $value, $aid, $id);
  248. if ($insertId > 0 && is_array($v)) {
  249. $this->insertApiData($api, $v, $aid, $insertId, $num+1);
  250. }
  251. }
  252. }
  253. private function insertApiDataOne($api, $name, $data, $aid, $id = -1)
  254. {
  255. if (is_array($data)) {
  256. $type = $data[0];
  257. $desc = $data[1];
  258. } else {
  259. $type = 'string';
  260. if (is_numeric($name)) {
  261. $type = 'int';
  262. } elseif (strpos($name, 'id') !== false) {
  263. $type = 'int';
  264. }
  265. $desc = $data;
  266. }
  267. if (!is_string($desc)) {
  268. $desc = '{object}';
  269. }
  270. if (strlen($desc) > 100) {
  271. $desc = '{long}';
  272. }
  273. $update['add_api_id'] = $aid;
  274. $update['add_name'] = $name;
  275. $update['add_type'] = $type;
  276. $update['add_desc'] = $desc;
  277. $update['add_parent_id'] = $id;
  278. $update['add_desc'] = $desc;
  279. $one = array
  280. (
  281. 'option_parent_id' => $id,
  282. 'option_api_id' => $aid,
  283. 'option_name' => $name,
  284. );
  285. $info = Dever::db('manage/' . $api)->one($one);
  286. if (!$info) {
  287. return Dever::db('manage/' . $api)->insert($update);
  288. }
  289. return $info['id'];
  290. }
  291. public function showTable($data, $url = '', $api = '')
  292. {
  293. $result = '<table border=1 width=100% height=100%>';
  294. if ($data) {
  295. $result .= Dever::tbody(array('字段', '类型', '描述'));
  296. $num = count($data);
  297. $table = array();
  298. foreach ($data as $k => $v) {
  299. if ($v['name'] == '{struct}') {
  300. $table[$k] = '';
  301. unset($data[$k]);
  302. $struct = Dever::db($k)->config['struct'];
  303. if ($v['desc']) {
  304. $temp = $v['desc'];
  305. } else {
  306. $temp = $struct;
  307. }
  308. if ($temp) {
  309. foreach ($temp as $i) {
  310. if (isset($struct[$i]) && isset($struct[$i]['type'])) {
  311. $type = explode('-', $struct[$i]['type']);
  312. $table[$k] .= Dever::tbody(array($i, $type[0], $struct[$i]['name']));
  313. }
  314. }
  315. if ($v['type']) {
  316. $data[$v['type']] = array('array/json', $result . $table[$k] . '</table>');
  317. } else {
  318. $result .= $table[$k];
  319. }
  320. }
  321. }
  322. }
  323. } else {
  324. $result .= '空';
  325. }
  326. $this->table($data, $result, $url, $api);
  327. return $result;
  328. }
  329. private function table($data, &$table, $url = '', $api = '')
  330. {
  331. if ($data && is_array($data)) {
  332. foreach ($data as $k => $v) {
  333. if (is_array($v['desc'])) {
  334. $tbody = '<table border=1 width=100% height=100%>';
  335. $this->table($v['desc'], $tbody) . '</table>';
  336. $v['desc'] = $tbody;
  337. }
  338. if (!$v['desc']) {
  339. $v['desc'] = '无';
  340. }
  341. if (strstr($v['desc'], 'array:')) {
  342. $v['type'] = 'array';
  343. //$v['desc'] = str_replace('array:', '', $v['desc']);
  344. }
  345. $v['name'] = '<span class="edit" data-col="name" data-url="'.$url.'" data-id="'.$v['id'].'" data-type="'.$api.'" >' . $v['name'] . '</span>';
  346. $v['type'] = '<span class="edit" data-col="type" data-url="'.$url.'" data-id="'.$v['id'].'" data-type="'.$api.'" >' . $v['type'] . '</span>';
  347. $v['desc'] = '<span class="edit" data-col="desc" data-url="'.$url.'" data-id="'.$v['id'].'" data-type="'.$api.'" >' . $v['desc'] . '</span>';
  348. $table .= Dever::tbody(array($v['name'], $v['type'], $v['desc']));
  349. }
  350. }
  351. $table .= '</table>';
  352. }
  353. public function push($param = false)
  354. {
  355. $param = $param ? $param : json_decode(base64_decode(Dever::input('param')), true);
  356. $api = explode('_api', $param['api']);
  357. $api = $api[0];
  358. $temp = explode('/', $api);
  359. $project = $temp[0];
  360. unset($temp[0]);
  361. $site = implode('/', $temp);
  362. $request = array();
  363. foreach ($param['request'] as $k => $v) {
  364. if (!strpos($k, '/') && !strpos($k, '?') && $k != 'json' && $k != 'callback') {
  365. $request[$k] = $v;
  366. }
  367. }
  368. $type = 1;
  369. $this->updateApi($site, $api, $type, $project, $request, $param['response'], 1, false);
  370. }
  371. public function update_col_api()
  372. {
  373. $table = Dever::input('type');
  374. $col = Dever::input('col');
  375. $id = Dever::input('id');
  376. $value = Dever::input('value');
  377. $update['set_' . $col] = $value;
  378. $update['where_id'] = $id;
  379. Dever::db('manage/' . $table)->update($update);
  380. }
  381. public function getHtml($name, $site, $id, $project)
  382. {
  383. $one = array
  384. (
  385. 'option_api_id' => $id,
  386. );
  387. $request = Dever::db('manage/api_request')->all($one);
  388. $response = Dever::db('manage/api_response')->all($one);
  389. $html = Dever::db('manage/api')->config['html'];
  390. $request = $this->showTable($request);
  391. $response = $this->showTable($response);
  392. return '接口名称:'.$name.'<br />接口地址:<br />'.Dever::url($site, $project).'<br />'.$html.'请求参数:<br />'.$request.'<br />输出参数:<br />'.$response;
  393. }
  394. # 获取日志信息
  395. public function getLog($tid, $project, $table, $type = 1)
  396. {
  397. $where['project'] = $project;
  398. $where['table'] = $table;
  399. $where['tid'] = $tid;
  400. $where['type'] = $type;
  401. $info = Dever::db('manage/log')->find($where);
  402. if ($info) {
  403. $info['info'] = Dever::json_decode($info['info']);
  404. }
  405. return $info;
  406. }
  407. }