You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 line
4.6 KiB

3 年之前
  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. $table_info = str_replace("_this_and_change_", "&", $table_info);
  49. $table_detail = str_replace("_this_and_change_", "&", $table_detail);
  50. $tables = $this->_analyze_db_structure_to_array($table_info ,$table_detail);
  51. if (!empty($tables)) {
  52. foreach ($tables as $key => $value) {
  53. $page_title = $value['table_name'] ;
  54. $page_content = $value['markdown'] ;
  55. $result = D("Page")->update_by_content($item_id,$page_title,$page_content,$cat_name,$s_number);
  56. }
  57. }
  58. if (!empty($result)) {
  59. echo "成功\n";
  60. }else{
  61. echo "失败\n";
  62. }
  63. //$this->_record_log();
  64. }
  65. //通过注释生成api文档
  66. public function fromComments(){
  67. R("FromComments/generate");
  68. }
  69. private function _analyze_db_structure_to_array($table_info , $table_detail){
  70. $tables = array();
  71. //解析table_info
  72. $array = explode("\n", $table_info);
  73. if(!empty($array)){
  74. foreach ($array as $key => $value) {
  75. if ($key == 0) {
  76. continue;
  77. }
  78. $array2 = explode("\t", $value);
  79. $table_name = str_replace(PHP_EOL, '', $array2[0]);
  80. $tables[$array2[0]] = array(
  81. "table_name" => $table_name ,
  82. "table_comment" => $array2[1] ,
  83. );
  84. }
  85. }
  86. //解析table_detail
  87. $array = explode("\n", $table_detail);
  88. if(!empty($array)){
  89. foreach ($array as $key => $value) {
  90. if ($key == 0) {
  91. continue;
  92. }
  93. $array2 = explode("\t", $value);
  94. $tables[$array2[0]]['columns'][$array2[1]] = array(
  95. "column_name" => $array2[1] ,
  96. "default" => $array2[2] ,
  97. "is_nullable" => $array2[3] ,
  98. "column_type" => $array2[4] ,
  99. "column_comment" => $array2[5] ? $array2[5] : '无' ,
  100. );
  101. }
  102. }
  103. //生成markdown内容放在数组里
  104. if (!empty($tables)) {
  105. foreach ($tables as $key => $value) {
  106. $markdown = '';
  107. $markdown .= "- {$value['table_comment']} \n \n" ;
  108. $markdown .= "|字段|类型|允许空|默认|注释| \n ";
  109. $markdown .= "|:---- |:------- |:--- |----|------ | \n ";
  110. foreach ($value['columns'] as $key2 => $value2) {
  111. $markdown .= "|{$value2['column_name']} |{$value2['column_type']} |{$value2['is_nullable']} | {$value2['default']} | {$value2['column_comment']} | \n ";
  112. }
  113. $tables[$key]['markdown'] = $markdown ;
  114. }
  115. }
  116. return $tables;
  117. }
  118. }