ImportPostmanController.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <?php
  2. namespace Api\Controller;
  3. use Think\Controller;
  4. class ImportPostmanController 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/postmanpostman_collectionV2.json") ;//test
  9. $json_array = json_decode($json ,1 );
  10. unset($json);
  11. if ($json_array['id']) {
  12. $this->_fromPostmanV1($json_array);
  13. return ;
  14. }
  15. if ($json_array['info']) {
  16. $this->_fromPostmanV2($json_array);
  17. return ;
  18. }
  19. $this->sendError(10101);
  20. }
  21. //从postman导入(v1版本)
  22. private function _fromPostmanV1($json_array){
  23. $login_user = $this->checkLogin();
  24. // TODO 这里需要检查下合法性。比如关键字检查/黑名单检查/字符串过滤
  25. $from = I("from") ? I("from") : '' ;
  26. $item_array = array(
  27. "item_name" => $json_array['name'] ? $json_array['name'] : 'from postman' ,
  28. "item_type" => ($from == 'runapi') ? '3': '1' ,
  29. "item_description" => $json_array['description'] ? $json_array['description'] :'',
  30. "password" => time().rand(),
  31. "members" => array(),
  32. "pages" =>array(
  33. "pages" => array(),
  34. "catalogs" => array()
  35. )
  36. ) ;
  37. $level = 2 ;
  38. foreach ($json_array['requests'] as $key => $value) {
  39. if (!$value['folder']) {
  40. $item_array['pages']['pages'][] = $this->_requestToDoc($value);
  41. }
  42. }
  43. foreach ($json_array['folders'] as $key => $value) {
  44. //不存在父目录的话,那就是根目录了。
  45. if (!$value['folder']) {
  46. $cat_array = array(
  47. "id" => $value['id'] ,
  48. "cat_name" => $value['name'] ,
  49. "level" => $level ,
  50. "s_number" => 99 ,
  51. );
  52. $cat_array['pages'] = $this->_getPageByFolders($value['id'] , $json_array ) ;
  53. $cat_array['catalogs'] = $this->_getSubByFolders($value['id'] ,$value['name'] , $level + 1 , $json_array ) ;
  54. $item_array['pages']['catalogs'][] = $cat_array ;
  55. }
  56. }
  57. $item_id = D("Item")->import( json_encode($item_array) , $login_user['uid'] );
  58. //echo D("Item")->export(196053901215026 );
  59. //echo json_encode($item_array);
  60. $this->sendResult(array("item_id"=>$item_id));
  61. }
  62. //根据postman的folders获取子页面和子目录
  63. //参数id为父目录的id
  64. private function _getSubByFolders($id , $name , $level , $json_array ){
  65. $return = array() ;
  66. foreach($json_array['folders'] as $key => $value) {
  67. if ($value['folder'] && $value['folder'] == $id ) {
  68. $cat_array = array(
  69. "id" => $value['id'] ,
  70. "cat_name" => $value['name'] ,
  71. "level" => $level ,
  72. "s_number" => 99 ,
  73. );
  74. $cat_array['pages'] = $this->_getPageByFolders($value['id'], $json_array ) ;
  75. $cat_array['catalogs'] = $this->_getSubByFolders($value['id'] , $value['name'] , $level + 1 , $json_array);
  76. $return[] = $cat_array ;
  77. }
  78. }
  79. return $return ;
  80. }
  81. //根据postman的folders获取页面
  82. private function _getPageByFolders($id , $json_array ){
  83. $return = array() ;
  84. foreach ($json_array['requests'] as $key => $value) {
  85. if ($value['folder'] == $id ) {
  86. $return[] = $this->_requestToDoc($value);
  87. }
  88. }
  89. return $return ;
  90. }
  91. private function _requestToDoc($request){
  92. $from = I("from") ? I("from") : '' ;
  93. $res = $this->_requestToApi($request);
  94. if($from == 'runapi'){
  95. return $res ;
  96. }else{
  97. $res['page_content'] = D("Page")->runapiToMd($res['page_content']);
  98. return $res ;
  99. }
  100. }
  101. //转成runapi所需要的api格式
  102. private function _requestToApi($request){
  103. $return = array() ;
  104. $return['page_title'] = $request['name'] ;
  105. $return['id'] = $request['id'] ;
  106. $return['s_number'] = 99 ;
  107. $return['page_comments'] = '' ;
  108. //若$return['page_title'] 为很长的url,则做一些特殊处理
  109. $tmp_title_array = explode("/", $return['page_title']);
  110. if ($tmp_title_array) {
  111. $tmp_title_array = array_slice($tmp_title_array, -2);// 倒数2个
  112. if($tmp_title_array[1])$return['page_title'] = $tmp_title_array[0]."/".$tmp_title_array[1] ;
  113. }
  114. $content_array = array(
  115. "info"=>array(
  116. "from" => 'runapi' ,
  117. "type" => 'api' ,
  118. "title" => $request['name'] ,
  119. "description" => $request['description'] ,
  120. "method" => strtolower($request['method']) ,
  121. "url" => $request['url'] ,
  122. "remark" => '' ,
  123. ),
  124. "request" =>array(
  125. "params"=> array(
  126. 'mode' => "urlencoded",
  127. 'json' => "",
  128. 'urlencoded' => array(),
  129. 'formdata' => array(),
  130. ),
  131. "headers"=> array(),
  132. "cookies"=> array(),
  133. "auth"=> array(),
  134. ),
  135. "response" =>array(),
  136. "extend" =>array(),
  137. );
  138. if ($request['headerData']) {
  139. $tmp_array = array();
  140. foreach ($request['headerData'] as $key => $value) {
  141. $content_array['request']['headers'][] = array(
  142. "name" =>$value["key"],
  143. "type" =>'string',
  144. "value" =>$value["value"],
  145. "require" =>'1',
  146. "remark" =>$value["description"] ? $value["description"]:'',
  147. );
  148. }
  149. }
  150. if ($request['data']) {
  151. foreach ($request['data'] as $key => $value) {
  152. $content_array['request']['params']['urlencoded'][] = array(
  153. "name" =>$value["key"],
  154. "type" =>'string',
  155. "value" =>$value["value"],
  156. "require" =>'1',
  157. "remark" =>$value["description"] ? $value["description"]:'',
  158. );
  159. }
  160. }
  161. $return['page_content'] = json_encode($content_array) ;
  162. return $return ;
  163. }
  164. //从postman导入(v2版本)
  165. private function _fromPostmanV2($json_array){
  166. $login_user = $this->checkLogin();
  167. // TODO 这里需要检查下合法性。比如关键字检查/黑名单检查/字符串过滤
  168. $from = I("from") ? I("from") : '' ;
  169. $item_array = array(
  170. "item_name" => $json_array['info']['name'] ? $json_array['info']['name'] : 'from postman' ,
  171. "item_type" => ($from=='runapi') ? '3' : '1' ,
  172. "item_description" => $json_array['info']['description'] ? $json_array['info']['description'] :'',
  173. "password" => time().rand(),
  174. "members" => array(),
  175. "pages" =>array(
  176. "pages" => array(),
  177. "catalogs" => array()
  178. )
  179. ) ;
  180. $level = 2 ;
  181. $item_array['pages']['pages'] = $this->_getPageByItem($json_array['item'] );
  182. $item_array['pages']['catalogs'] = $this->_getItemByItem($json_array['item'] , 2 );
  183. $item_id = D("Item")->import( json_encode($item_array) , $login_user['uid'] );
  184. //echo D("Item")->export(196053901215026 );
  185. //echo json_encode($item_array);
  186. $this->sendResult(array("item_id"=>$item_id));
  187. }
  188. //获取某个目录下的所有页面
  189. private function _getPageByItem($item_array ){
  190. $return = array();
  191. foreach ($item_array as $key => $value) {
  192. //含有request,则这是一个子页面
  193. if ($value['request']) {
  194. $return[] = $this->_requestToDocV2($value['name'] ,$value['request']);
  195. }
  196. }
  197. return $return ;
  198. }
  199. //获取某个目录下的所有子目录
  200. private function _getItemByItem($item_array ,$level ){
  201. $return = array();
  202. foreach ($item_array as $key => $value) {
  203. //含有item,则这是一个子目录
  204. if ($value['item']) {
  205. $one_ary = array(
  206. "cat_name" => $value['name'] ,
  207. "level" => $level ,
  208. "s_number" => 99 ,
  209. "pages" => $this->_getPageByItem($value['item'], $level + 1 ), //递归
  210. "catalogs" => $this->_getItemByItem($value['item'], $level + 1 ) //递归
  211. );
  212. $return[] = $one_ary ;
  213. }
  214. }
  215. return $return ;
  216. }
  217. private function _requestToDocV2($name , $request){
  218. $from = I("from") ? I("from") : '' ;
  219. $res = $this->_requestToApiV2($name , $request);
  220. if($from == 'runapi'){
  221. return $res ;
  222. }else{
  223. $res['page_content'] = D("Page")->runapiToMd($res['page_content']);
  224. return $res ;
  225. }
  226. }
  227. //转成runapi所需要的api格式
  228. private function _requestToApiV2($name , $request){
  229. $return = array() ;
  230. $return['page_title'] = $name ;
  231. $return['s_number'] = 99 ;
  232. $return['page_comments'] = '' ;
  233. //若$return['page_title'] 为很长的url,则做一些特殊处理
  234. $tmp_title_array = explode("/", $return['page_title']);
  235. if ($tmp_title_array) {
  236. $tmp_title_array = array_slice($tmp_title_array, -2);// 倒数2个
  237. if($tmp_title_array[1])$return['page_title'] = $tmp_title_array[0]."/".$tmp_title_array[1] ;
  238. }
  239. $url = is_array($request['url']) ? $request['url']['raw'] : $request['url'] ;
  240. $rawModeData = $request['body']['mode'] == 'raw' ? $request['body']['raw'] : $request['rawModeData'] ;
  241. $content_array = array(
  242. "info"=>array(
  243. "from" => 'runapi' ,
  244. "type" => 'api' ,
  245. "title" => $name ,
  246. "description" => $request['description'] ,
  247. "method" => strtolower($request['method']) ,
  248. "url" => $url ,
  249. "remark" => '' ,
  250. ),
  251. "request" =>array(
  252. "params"=> array(
  253. 'mode' => "formdata",
  254. 'json' => "",
  255. 'urlencoded' => array(),
  256. 'formdata' => array(),
  257. ),
  258. "headers"=> array(),
  259. "cookies"=> array(),
  260. "auth"=> array(),
  261. ),
  262. "response" =>array(),
  263. "extend" =>array(),
  264. );
  265. if ($request['header']) {
  266. $tmp_array = array();
  267. foreach ($request['header'] as $key => $value) {
  268. $content_array['request']['headers'][] = array(
  269. "name" =>$value["key"],
  270. "type" =>'string',
  271. "value" =>$value["value"],
  272. "require" =>'1',
  273. "remark" =>$value["description"] ? $value["description"]:'',
  274. );
  275. }
  276. }
  277. if ($request['body']['formdata']) {
  278. foreach ($request['body']['formdata'] as $key => $value) {
  279. $content_array['request']['params']['formdata'][] = array(
  280. "name" =>$value["key"],
  281. "type" =>'string',
  282. "value" =>$value["value"],
  283. "require" =>'1',
  284. "remark" =>$value["description"] ? $value["description"]:'',
  285. );
  286. }
  287. }
  288. else if($rawModeData && json_decode($rawModeData)){
  289. $content_array['request']['params']['mode'] = 'json';
  290. $content_array['request']['params']['json'] = $rawModeData;
  291. }
  292. $return['page_content'] = json_encode($content_array) ;
  293. return $return ;
  294. }
  295. }