OpenController.class.php 4.4 KB

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