OpenController.class.php 4.1 KB

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