選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

476 行
15 KiB

  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. if($from == 'runapi'){
  94. return $this->_requestToApi($request);
  95. //如果是来自runapi的导入请求,则已经return不再执行下面
  96. }
  97. $return = array() ;
  98. $return['page_title'] = $request['name'] ;
  99. $return['id'] = $request['id'] ;
  100. $return['s_number'] = 99 ;
  101. $return['page_comments'] = '' ;
  102. //若$return['page_title'] 为很长的url,则做一些特殊处理
  103. $tmp_title_array = explode("/", $return['page_title']);
  104. if ($tmp_title_array) {
  105. $tmp_title_array = array_slice($tmp_title_array, -2);// 倒数2个
  106. if($tmp_title_array[1])$return['page_title'] = $tmp_title_array[0]."/".$tmp_title_array[1] ;
  107. }
  108. $content = '
  109. **简要描述:**
  110. - '.$request['description'].'
  111. **请求URL:**
  112. - ` '.$request['url'].' `
  113. **请求方式:**
  114. - '.$request['method'].' ';
  115. if ($request['headerData']) {
  116. $content .='
  117. **Header:**
  118. |Header名|是否必选|类型|说明|
  119. |:---- |:---|:----- |----- |'."\n";
  120. foreach ($request['headerData'] as $key => $value) {
  121. $content .= '|'.$value["key"].' | | text | '.$value["value"].' |'."\n";
  122. }
  123. }
  124. if ($request['rawModeData']) {
  125. $content .= '
  126. **请求参数示例**
  127. ```
  128. '.$request['rawModeData'].'
  129. ```
  130. ';
  131. }
  132. if ($request['data']) {
  133. $content .='
  134. **参数:**
  135. |参数名|是否必选|类型|说明|
  136. |:---- |:---|:----- |----- |'."\n";
  137. foreach ($request['data'] as $key => $value) {
  138. $content .= '|'.$value["key"].' | |'.$value["type"].' | 无 |'."\n";
  139. }
  140. }
  141. $return['page_content'] = $content ;
  142. return $return ;
  143. }
  144. //转成runapi所需要的api格式
  145. private function _requestToApi($request){
  146. $return = array() ;
  147. $return['page_title'] = $request['name'] ;
  148. $return['id'] = $request['id'] ;
  149. $return['s_number'] = 99 ;
  150. $return['page_comments'] = '' ;
  151. //若$return['page_title'] 为很长的url,则做一些特殊处理
  152. $tmp_title_array = explode("/", $return['page_title']);
  153. if ($tmp_title_array) {
  154. $tmp_title_array = array_slice($tmp_title_array, -2);// 倒数2个
  155. if($tmp_title_array[1])$return['page_title'] = $tmp_title_array[0]."/".$tmp_title_array[1] ;
  156. }
  157. $content_array = array(
  158. "info"=>array(
  159. "from" => 'runapi' ,
  160. "type" => 'api' ,
  161. "title" => $request['name'] ,
  162. "description" => $request['description'] ,
  163. "method" => strtolower($request['method']) ,
  164. "url" => $request['url'] ,
  165. "remark" => '' ,
  166. ),
  167. "request" =>array(
  168. "params"=> array(
  169. 'mode' => "urlencoded",
  170. 'json' => "",
  171. 'urlencoded' => array(),
  172. 'formdata' => array(),
  173. ),
  174. "headers"=> array(),
  175. "cookies"=> array(),
  176. "auth"=> array(),
  177. ),
  178. "response" =>array(),
  179. "extend" =>array(),
  180. );
  181. if ($request['headerData']) {
  182. $tmp_array = array();
  183. foreach ($request['headerData'] as $key => $value) {
  184. $content_array['request']['headers'][] = array(
  185. "name" =>$value["key"],
  186. "type" =>'string',
  187. "value" =>$value["value"],
  188. "require" =>'1',
  189. "remark" =>'',
  190. );
  191. }
  192. }
  193. if ($request['data']) {
  194. foreach ($request['data'] as $key => $value) {
  195. $content_array['request']['params']['urlencoded'][] = array(
  196. "name" =>$value["key"],
  197. "type" =>'string',
  198. "value" =>$value["value"],
  199. "require" =>'1',
  200. "remark" =>'',
  201. );
  202. }
  203. }
  204. $return['page_content'] = json_encode($content_array) ;
  205. return $return ;
  206. }
  207. //从postman导入(v2版本)
  208. private function _fromPostmanV2($json_array){
  209. $login_user = $this->checkLogin();
  210. // TODO 这里需要检查下合法性。比如关键字检查/黑名单检查/字符串过滤
  211. $from = I("from") ? I("from") : '' ;
  212. $item_array = array(
  213. "item_name" => $json_array['info']['name'] ? $json_array['info']['name'] : 'from postman' ,
  214. "item_type" => ($from=='runapi') ? '3' : '1' ,
  215. "item_description" => $json_array['info']['description'] ? $json_array['info']['description'] :'',
  216. "password" => time().rand(),
  217. "members" => array(),
  218. "pages" =>array(
  219. "pages" => array(),
  220. "catalogs" => array()
  221. )
  222. ) ;
  223. $level = 2 ;
  224. $item_array['pages']['pages'] = $this->_getPageByItem($json_array['item'] );
  225. $item_array['pages']['catalogs'] = $this->_getItemByItem($json_array['item'] , 2 );
  226. $item_id = D("Item")->import( json_encode($item_array) , $login_user['uid'] );
  227. //echo D("Item")->export(196053901215026 );
  228. //echo json_encode($item_array);
  229. $this->sendResult(array("item_id"=>$item_id));
  230. }
  231. //获取某个目录下的所有页面
  232. private function _getPageByItem($item_array ){
  233. $return = array();
  234. foreach ($item_array as $key => $value) {
  235. //含有request,则这是一个子页面
  236. if ($value['request']) {
  237. $return[] = $this->_requestToDocV2($value['name'] ,$value['request']);
  238. }
  239. }
  240. return $return ;
  241. }
  242. //获取某个目录下的所有子目录
  243. private function _getItemByItem($item_array ,$level ){
  244. $return = array();
  245. foreach ($item_array as $key => $value) {
  246. //含有item,则这是一个子目录
  247. if ($value['item']) {
  248. $one_ary = array(
  249. "cat_name" => $value['name'] ,
  250. "level" => $level ,
  251. "s_number" => 99 ,
  252. "pages" => $this->_getPageByItem($value['item'], $level + 1 ), //递归
  253. "catalogs" => $this->_getItemByItem($value['item'], $level + 1 ) //递归
  254. );
  255. $return[] = $one_ary ;
  256. }
  257. }
  258. return $return ;
  259. }
  260. private function _requestToDocV2($name , $request){
  261. $from = I("from") ? I("from") : '' ;
  262. if($from == 'runapi'){
  263. return $this->_requestToApiV2($name , $request);
  264. //如果是来自runapi的导入请求,则已经return不再执行下面
  265. }
  266. $return = array() ;
  267. $return['page_title'] = $name ;
  268. $return['s_number'] = 99 ;
  269. $return['page_comments'] = '' ;
  270. //若$return['page_title'] 为很长的url,则做一些特殊处理
  271. $tmp_title_array = explode("/", $return['page_title']);
  272. if ($tmp_title_array) {
  273. $tmp_title_array = array_slice($tmp_title_array, -2);// 倒数2个
  274. if($tmp_title_array[1])$return['page_title'] = $tmp_title_array[0]."/".$tmp_title_array[1] ;
  275. }
  276. $url = is_array($request['url']) ? $request['url']['raw'] : $request['url'] ;
  277. $rawModeData = $request['body']['mode'] == 'raw' ? $request['body']['raw'] : $request['rawModeData'] ;
  278. $content = '
  279. **简要描述:**
  280. - '.$request['description'].'
  281. **请求URL:**
  282. - ` '.$url.' `
  283. **请求方式:**
  284. - '.$request['method'].' ';
  285. if ($request['header']) {
  286. $content .='
  287. **Header:**
  288. |Header名|是否必选|类型|说明|
  289. |:---- |:---|:----- |----- |'."\n";
  290. foreach ($request['header'] as $key => $value) {
  291. $content .= '|'.$value["key"].' | | | '.$value["value"].' |'."\n";
  292. }
  293. }
  294. if ($rawModeData) {
  295. $content .= '
  296. **请求参数示例**
  297. ```
  298. '.$rawModeData.'
  299. ```
  300. ';
  301. }
  302. if ($request['body']['formdata']) {
  303. $content .='
  304. **参数:**
  305. |参数名|是否必选|类型|说明|
  306. |:---- |:---|:----- |----- |'."\n";
  307. foreach ($request['body']['formdata'] as $key => $value) {
  308. $content .= '|'.$value["key"].' | |'.$value["type"].' | 无 |'."\n";
  309. }
  310. }
  311. $return['page_content'] = $content ;
  312. return $return ;
  313. }
  314. //转成runapi所需要的api格式
  315. private function _requestToApiV2($name , $request){
  316. $return = array() ;
  317. $return['page_title'] = $name ;
  318. $return['s_number'] = 99 ;
  319. $return['page_comments'] = '' ;
  320. //若$return['page_title'] 为很长的url,则做一些特殊处理
  321. $tmp_title_array = explode("/", $return['page_title']);
  322. if ($tmp_title_array) {
  323. $tmp_title_array = array_slice($tmp_title_array, -2);// 倒数2个
  324. if($tmp_title_array[1])$return['page_title'] = $tmp_title_array[0]."/".$tmp_title_array[1] ;
  325. }
  326. $url = is_array($request['url']) ? $request['url']['raw'] : $request['url'] ;
  327. $rawModeData = $request['body']['mode'] == 'raw' ? $request['body']['raw'] : $request['rawModeData'] ;
  328. $content_array = array(
  329. "info"=>array(
  330. "from" => 'runapi' ,
  331. "type" => 'api' ,
  332. "title" => $name ,
  333. "description" => $request['description'] ,
  334. "method" => strtolower($request['method']) ,
  335. "url" => $url ,
  336. "remark" => '' ,
  337. ),
  338. "request" =>array(
  339. "params"=> array(
  340. 'mode' => "formdata",
  341. 'json' => "",
  342. 'urlencoded' => array(),
  343. 'formdata' => array(),
  344. ),
  345. "headers"=> array(),
  346. "cookies"=> array(),
  347. "auth"=> array(),
  348. ),
  349. "response" =>array(),
  350. "extend" =>array(),
  351. );
  352. if ($request['header']) {
  353. $tmp_array = array();
  354. foreach ($request['header'] as $key => $value) {
  355. $content_array['request']['headers'][] = array(
  356. "name" =>$value["key"],
  357. "type" =>'string',
  358. "value" =>$value["value"],
  359. "require" =>'1',
  360. "remark" =>'',
  361. );
  362. }
  363. }
  364. if ($request['body']['formdata']) {
  365. foreach ($request['body']['formdata'] as $key => $value) {
  366. $content_array['request']['params']['formdata'][] = array(
  367. "name" =>$value["key"],
  368. "type" =>'string',
  369. "value" =>$value["value"],
  370. "require" =>'1',
  371. "remark" =>'',
  372. );
  373. }
  374. }
  375. $return['page_content'] = json_encode($content_array) ;
  376. return $return ;
  377. }
  378. }