ImportSwaggerController.class.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace Api\Controller;
  3. use Think\Controller;
  4. class ImportSwaggerController extends BaseController {
  5. public function import(){
  6. $login_user = $this->checkLogin();
  7. $json = file_get_contents($_FILES["file"]["tmp_name"]) ;
  8. //$json = file_get_contents("../Public/swagger.json") ;//test
  9. $json_array = json_decode($json ,1 );
  10. unset($json);
  11. if ($json_array['info']) {
  12. $this->_fromSwaggerV2($json_array);
  13. return ;
  14. }
  15. $this->sendError(10303);
  16. }
  17. private function _fromSwaggerV2($json_array){
  18. $login_user = $this->checkLogin();
  19. // TODO 这里需要检查下合法性。比如关键字检查/黑名单检查/字符串过滤
  20. $item_array = array(
  21. "item_name" => $json_array['info']['title'] ? $json_array['info']['title'] : 'from swagger' ,
  22. "item_type" => '1' ,
  23. "item_description" => $json_array['info']['description'] ? $json_array['info']['description'] :'',
  24. "password" => time().rand(),
  25. "members" => array(),
  26. "pages" =>array(
  27. "pages" => array(),
  28. "catalogs" => array()
  29. )
  30. ) ;
  31. $level = 2 ;
  32. $item_array['pages']['pages'] = $this->_getPageByPaths($json_array );
  33. D("Item")->import( json_encode($item_array) , $login_user['uid'] );
  34. //echo D("Item")->export(196053901215026 );
  35. //echo json_encode($item_array);
  36. $this->sendResult(array());
  37. }
  38. private function _getPageByPaths($json_array){
  39. $return = array() ;
  40. $paths = $json_array['paths'] ;
  41. foreach ($paths as $url => $value) {
  42. foreach ($value as $method => $value2) {
  43. $return[] = $this->_requestToDoc($method , $url , $value2 , $json_array);
  44. }
  45. }
  46. return $return ;
  47. }
  48. private function _requestToDoc($method , $url , $request , $json_array){
  49. $return = array() ;
  50. $return['page_title'] = $request['summary'] ;
  51. $return['s_number'] = 99 ;
  52. $return['page_comments'] = '' ;
  53. $content = '
  54. **简要描述:**
  55. - '.$request['description'].'
  56. **请求URL:**
  57. - ` '.$url.' `
  58. **请求方式:**
  59. - '.$method.' ';
  60. if ($request['header']) {
  61. $content .='
  62. **Header:**
  63. |Header名|是否必选|类型|说明|
  64. |:---- |:---|:----- |----- |'."\n";
  65. foreach ($request['headerData'] as $key => $value) {
  66. $content .= '|'.$value["key"].' | | text | '.$value["value"].' |'."\n";
  67. }
  68. }
  69. if ($request['rawModeData']) {
  70. $content .= '
  71. **请求参数示例**
  72. ```
  73. '.$request['rawModeData'].'
  74. ```
  75. ';
  76. }
  77. if ($request['parameters']) {
  78. $content .='
  79. **参数:**
  80. |参数名|是否必选|类型|说明|
  81. |:---- |:---|:----- |----- |'."\n";
  82. foreach ($request['parameters'] as $key => $value) {
  83. $content .= '|'.$value["name"].' | '.($value["required"] ? '是' : '否' ).' |'.$value["type"].' | '.$value["description"].' |'."\n";
  84. }
  85. }
  86. if ($request['responses']['200']) {
  87. $responses = $request['responses']['200'] ;
  88. //如果返回信息是引用对象
  89. if ($request['responses']['200']['schema'] && $request['responses']['200']['schema']['$ref'] ) {
  90. $str_array = explode("/", $request['responses']['200']['schema']['$ref']) ;
  91. if ($str_array[1] && $str_array[2]) {
  92. $responses = $json_array[$str_array[1]][$str_array[2]] ;
  93. $content .='
  94. **返回参数说明:**
  95. |参数名|类型|说明|
  96. |:---- |:---|:----- |----- |'."\n";
  97. foreach ($responses['properties'] as $key => $value) {
  98. $content .= '|'.$key.'|'.$value["type"].' | '.$value["description"].' |'."\n";
  99. }
  100. }
  101. }else{
  102. //如果返回的是普通json
  103. $content .= '
  104. **返回示例**
  105. ```
  106. '.$this->_indent_json(json_encode($responses)).'
  107. ```
  108. ';
  109. }
  110. }
  111. $return['page_content'] = $content ;
  112. return $return ;
  113. }
  114. /**
  115. * Indents a flat JSON string to make it more human-readable.
  116. *
  117. * @param string $json The original JSON string to process.
  118. *
  119. * @return string Indented version of the original JSON string.
  120. */
  121. private function _indent_json($json) {
  122. $result = '';
  123. $pos = 0;
  124. $strLen = strlen($json);
  125. $indentStr = ' ';
  126. $newLine = "\n";
  127. $prevChar = '';
  128. $outOfQuotes = true;
  129. for ($i=0; $i<=$strLen; $i++) {
  130. // Grab the next character in the string.
  131. $char = substr($json, $i, 1);
  132. // Are we inside a quoted string?
  133. if ($char == '"' && $prevChar != '\\') {
  134. $outOfQuotes = !$outOfQuotes;
  135. // If this character is the end of an element,
  136. // output a new line and indent the next line.
  137. } else if(($char == '}' || $char == ']') && $outOfQuotes) {
  138. $result .= $newLine;
  139. $pos --;
  140. for ($j=0; $j<$pos; $j++) {
  141. $result .= $indentStr;
  142. }
  143. }
  144. // Add the character to the result string.
  145. $result .= $char;
  146. // If the last character was the beginning of an element,
  147. // output a new line and indent the next line.
  148. if (($char == ',' || $char == '{' || $char == '[') && $outOfQuotes) {
  149. $result .= $newLine;
  150. if ($char == '{' || $char == '[') {
  151. $pos ++;
  152. }
  153. for ($j = 0; $j < $pos; $j++) {
  154. $result .= $indentStr;
  155. }
  156. }
  157. $prevChar = $char;
  158. }
  159. return $result;
  160. }
  161. }