OpenController.class.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. $item_id = D("ItemToken")->check($api_key , $api_token);
  38. if (!$item_id) {
  39. //没验证通过
  40. echo "api_key或者api_token不匹配\n";
  41. return false;
  42. }
  43. $tables = $this->_analyze_db_structure_to_array($table_info ,$table_detail);
  44. if (!empty($tables)) {
  45. //D("Item")->empty_content($item_id); //清空内容
  46. foreach ($tables as $key => $value) {
  47. $page_title = $value['table_name'] ;
  48. $page_content = $value['markdown'] ;
  49. $result = D("Page")->update_by_content($item_id,$page_title,$page_content,$cat_name);
  50. }
  51. }
  52. if (!empty($result)) {
  53. echo "成功\n";
  54. }else{
  55. echo "失败\n";
  56. }
  57. }
  58. private function _analyze_db_structure_to_array($table_info , $table_detail){
  59. $tables = array();
  60. //解析table_info
  61. $array = explode("\n", $table_info);
  62. if(!empty($array)){
  63. foreach ($array as $key => $value) {
  64. if ($key == 0) {
  65. continue;
  66. }
  67. $array2 = explode("\t", $value);
  68. $table_name = str_replace(PHP_EOL, '', $array2[0]);
  69. $tables[$array2[0]] = array(
  70. "table_name" => $table_name ,
  71. "table_comment" => $array2[1] ,
  72. );
  73. }
  74. }
  75. //解析table_detail
  76. $array = explode("\n", $table_detail);
  77. if(!empty($array)){
  78. foreach ($array as $key => $value) {
  79. if ($key == 0) {
  80. continue;
  81. }
  82. $array2 = explode("\t", $value);
  83. $tables[$array2[0]]['columns'][$array2[1]] = array(
  84. "column_name" => $array2[1] ,
  85. "default" => $array2[2] ,
  86. "is_nullable" => $array2[3] ,
  87. "column_type" => $array2[4] ,
  88. "column_comment" => $array2[5] ? $array2[5] : '无' ,
  89. );
  90. }
  91. }
  92. //生成markdown内容放在数组里
  93. if (!empty($tables)) {
  94. foreach ($tables as $key => $value) {
  95. $markdown = '';
  96. $markdown .= "- {$value['table_comment']} \n \n" ;
  97. $markdown .= "|字段|类型|允许空|默认|注释| \n ";
  98. $markdown .= "|:---- |:------- |:--- |----|------ | \n ";
  99. foreach ($value['columns'] as $key2 => $value2) {
  100. $markdown .= "|{$value2['column_name']} |{$value2['column_type']} |{$value2['is_nullable']} | {$value2['default']} | {$value2['column_comment']} | \n ";
  101. }
  102. $tables[$key]['markdown'] = $markdown ;
  103. }
  104. }
  105. return $tables;
  106. }
  107. }