OpenController.class.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace Api\Controller;
  3. use Think\Controller;
  4. class OpenController extends BaseController {
  5. //根据内容更新项目
  6. public function updateItem(){
  7. $api_key = I("api_key");
  8. $api_token = I("api_token");
  9. $cat_name = I("cat_name")?I("cat_name"):'';
  10. $cat_name_sub = I("cat_name_sub");
  11. $page_title = I("page_title");
  12. $page_content = I("page_content");
  13. $s_number = I("s_number") ? I("s_number") : 99;
  14. $item_id = D("ItemToken")->check($api_key , $api_token);
  15. if (!$item_id) {
  16. //没验证通过
  17. $this->sendError(10306);
  18. return false;
  19. }
  20. //兼容之前的cat_name_sub参数
  21. if ($cat_name_sub) {
  22. $cat_name = $cat_name .'/'.$cat_name_sub ;
  23. }
  24. $page_id = D("Page")->update_by_content($item_id,$page_title,$page_content,$cat_name,$s_number);
  25. if ($page_id) {
  26. $ret = D("Page")->where(" page_id = '$page_id' ")->find();
  27. $this->sendResult($ret);
  28. }else{
  29. $this->sendError(10101);
  30. }
  31. }
  32. //根据shell上报的数据库结构信息生成数据字典
  33. public function updateDbItem(){
  34. $api_key = I("api_key");
  35. $api_token = I("api_token");
  36. $table_info = I("table_info");
  37. $table_detail = I("table_detail");
  38. $s_number = I("s_number") ? I("s_number") : 99;
  39. $cat_name = I("cat_name") ? I("cat_name") : '';
  40. header( 'Content-Type:text/html;charset=utf-8 ');
  41. $cat_name = str_replace(PHP_EOL, '', $cat_name);
  42. $item_id = D("ItemToken")->check($api_key , $api_token);
  43. if (!$item_id) {
  44. //没验证通过
  45. echo "api_key或者api_token不匹配\n";
  46. return false;
  47. }
  48. $tables = $this->_analyze_db_structure_to_array($table_info ,$table_detail);
  49. if (!empty($tables)) {
  50. foreach ($tables as $key => $value) {
  51. $page_title = $value['table_name'] ;
  52. $page_content = $value['markdown'] ;
  53. $result = D("Page")->update_by_content($item_id,$page_title,$page_content,$cat_name,$s_number);
  54. }
  55. }
  56. if (!empty($result)) {
  57. echo "成功\n";
  58. }else{
  59. echo "失败\n";
  60. }
  61. //$this->_record_log();
  62. }
  63. //通过注释生成api文档
  64. public function fromComments(){
  65. R("FromComments/generate");
  66. }
  67. private function _analyze_db_structure_to_array($table_info , $table_detail){
  68. $tables = array();
  69. //解析table_info
  70. $array = explode("\n", $table_info);
  71. if(!empty($array)){
  72. foreach ($array as $key => $value) {
  73. if ($key == 0) {
  74. continue;
  75. }
  76. $array2 = explode("\t", $value);
  77. $table_name = str_replace(PHP_EOL, '', $array2[0]);
  78. $tables[$array2[0]] = array(
  79. "table_name" => $table_name ,
  80. "table_comment" => $array2[1] ,
  81. );
  82. }
  83. }
  84. //解析table_detail
  85. $array = explode("\n", $table_detail);
  86. if(!empty($array)){
  87. foreach ($array as $key => $value) {
  88. if ($key == 0) {
  89. continue;
  90. }
  91. $array2 = explode("\t", $value);
  92. $tables[$array2[0]]['columns'][$array2[1]] = array(
  93. "column_name" => $array2[1] ,
  94. "default" => $array2[2] ,
  95. "is_nullable" => $array2[3] ,
  96. "column_type" => $array2[4] ,
  97. "column_comment" => $array2[5] ? $array2[5] : '无' ,
  98. );
  99. }
  100. }
  101. //生成markdown内容放在数组里
  102. if (!empty($tables)) {
  103. foreach ($tables as $key => $value) {
  104. $markdown = '';
  105. $markdown .= "- {$value['table_comment']} \n \n" ;
  106. $markdown .= "|字段|类型|允许空|默认|注释| \n ";
  107. $markdown .= "|:---- |:------- |:--- |----|------ | \n ";
  108. foreach ($value['columns'] as $key2 => $value2) {
  109. $markdown .= "|{$value2['column_name']} |{$value2['column_type']} |{$value2['is_nullable']} | {$value2['default']} | {$value2['column_comment']} | \n ";
  110. }
  111. $tables[$key]['markdown'] = $markdown ;
  112. }
  113. }
  114. return $tables;
  115. }
  116. }