star7th 5 年之前
父節點
當前提交
5b4c5d686f

+ 58 - 0
server/Application/Api/Controller/ImportController.class.php

@@ -0,0 +1,58 @@
+<?php
+namespace Api\Controller;
+use Think\Controller;
+class ImportController extends BaseController {
+
+
+    //导入markdown压缩包
+    public function markdown(){
+        set_time_limit(100);
+        ini_set('memory_limit','200M');
+
+        $login_user = $this->checkLogin();
+
+        $file = $_FILES["file"]["tmp_name"] ;
+        //$file = "../Public/markdown.zip" ; //test
+
+        $zipArc = new \ZipArchive();
+        $ret = $zipArc->open($file, \ZipArchive::CREATE);
+        $info = $zipArc->getFromName(DIRECTORY_SEPARATOR."info.json") ;
+        $info_array = json_decode($info ,1 );
+        unset($info);
+
+        if ($info_array) {
+
+            $info_array = $this->_fileToMarkdown($info_array,  $zipArc );
+            //echo json_encode($info_array);return ;
+            D("Item")->import( json_encode($info_array) , $login_user['uid'] );
+            $this->sendResult(array());
+            return ;
+        }
+
+        $this->sendError(10101);
+    }
+
+
+    private function _fileToMarkdown( $catalogData ,  $zipArc ){
+        if ($catalogData['pages']) {
+            foreach ($catalogData['pages'] as $key => $value) {
+                if ($value['page_content']) {
+                    $catalogData['pages'][$key]['page_content'] = $zipArc->getFromName( $value['page_content']) ;//原来的内容由文件名变为文件内容
+                }
+            }
+        }
+
+        if ($catalogData['catalogs']) {
+            foreach ($catalogData['catalogs'] as $key => $value) {
+                if ($value) {
+                    $catalogData['catalogs'][$key] = $this->_markdownTofile($value ,  $zipArc);
+                }
+                
+            }
+            
+        }
+        return $catalogData ;
+
+    }
+
+}

+ 324 - 0
server/Application/Api/Controller/ImportPostmanController.class.php

@@ -0,0 +1,324 @@
+<?php
+namespace Api\Controller;
+use Think\Controller;
+class ImportPostmanController extends BaseController {
+
+
+    public function import(){
+        $login_user = $this->checkLogin();
+
+        $json = file_get_contents($_FILES["file"]["tmp_name"]) ;
+
+        //$json = file_get_contents("../Public/postmanpostman_collectionV2.json") ;//test
+        $json_array = json_decode($json ,1 );
+        unset($json);
+        if ($json_array['id']) {
+            $this->_fromPostmanV1($json_array);
+            return ;
+        }
+
+        if ($json_array['info']) {
+            $this->_fromPostmanV2($json_array);
+            return ;
+        }
+
+        $this->sendError(10101);
+
+
+    }
+
+    //从postman导入(v1版本)
+
+    private function _fromPostmanV1($json_array){
+        $login_user = $this->checkLogin();
+
+        // TODO 这里需要检查下合法性。比如关键字检查/黑名单检查/字符串过滤
+
+
+        $item_array = array(
+            "item_name" => $json_array['name'] ? $json_array['name'] : 'from postman' ,
+            "item_type" => '1' ,
+            "item_description" => $json_array['description'] ? $json_array['description'] :'',
+            "password" => time().rand(),
+            "members" => array(),
+            "pages" =>array(
+                    "pages" => array(),
+                    "catalogs" => array()
+                )
+            ) ;
+        $level = 2 ;
+
+        foreach ($json_array['requests'] as $key => $value) {
+            if (!$value['folder']) {
+               $item_array['pages']['pages'][] = $this->_requestToDoc($value); 
+            }
+        }
+
+        foreach ($json_array['folders'] as $key => $value) {
+            //不存在父目录的话,那就是根目录了。
+            if (!$value['folder']) {
+                $cat_array = array(
+                    "id" => $value['id'] ,
+                    "cat_name" => $value['name'] ,
+                    "level" => $level ,
+                    "s_number" => 99 ,
+                    );
+                $cat_array['pages'] =  $this->_getPageByFolders($value['id'] , $json_array ) ;
+                $cat_array['catalogs'] =  $this->_getSubByFolders($value['id'] ,$value['name'] , $level + 1 , $json_array )  ;
+
+                $item_array['pages']['catalogs'][]  = $cat_array ;
+            }
+
+
+        }
+
+        D("Item")->import( json_encode($item_array) , $login_user['uid'] );
+        
+        //echo D("Item")->export(196053901215026 );
+        //echo json_encode($item_array);
+        $this->sendResult(array());
+
+
+    }
+
+    //根据postman的folders获取子页面和子目录
+    //参数id为父目录的id
+    private function _getSubByFolders($id , $name , $level , $json_array ){
+        $return = array() ;
+        foreach($json_array['folders'] as $key => $value) {
+            if ($value['folder'] && $value['folder'] == $id ) {
+                $cat_array = array(
+                    "id" => $value['id'] ,
+                    "cat_name" => $value['name'] ,
+                    "level" => $level ,
+                    "s_number" => 99 ,
+                    );
+                $cat_array['pages'] = $this->_getPageByFolders($value['id'], $json_array ) ;
+                $cat_array['catalogs'] = $this->_getSubByFolders($value['id'] , $value['name']  , $level + 1 , $json_array); 
+               $return[] = $cat_array ;
+            }
+        }
+
+        return $return ; 
+
+    }
+
+
+    //根据postman的folders获取页面
+    private function _getPageByFolders($id , $json_array ){
+        $return = array() ;
+        foreach ($json_array['requests'] as $key => $value) {
+            if ($value['folder'] == $id ) {
+               $return[] = $this->_requestToDoc($value);
+            }
+        }
+
+        return $return ; 
+
+    }
+
+
+
+    private function _requestToDoc($request){
+        $return = array() ;
+        $return['page_title'] = $request['name'] ;
+        $return['id'] = $request['id'] ;
+        $return['s_number'] = 99 ;
+        $return['page_comments'] = '' ;
+        //若$return['page_title'] 为很长的url,则做一些特殊处理
+        $tmp_title_array = explode("/", $return['page_title']);
+        if ($tmp_title_array) {
+            $tmp_title_array = array_slice($tmp_title_array, -2);// 倒数2个
+            if($tmp_title_array[1])$return['page_title'] = $tmp_title_array[0]."/".$tmp_title_array[1] ;
+        }
+        
+        $content = '  
+**简要描述:** 
+
+- '.$request['description'].'
+
+**请求URL:** 
+- ` '.$request['url'].' `
+  
+**请求方式:**
+- '.$request['method'].' ';
+
+if ($request['headerData']) {
+$content .='
+
+**Header:** 
+
+|Header名|是否必选|类型|说明|
+|:----    |:---|:----- |-----   |'."\n";
+    foreach ($request['headerData'] as $key => $value) {
+         $content .= '|'.$value["key"].' |  | text | '.$value["value"].' |'."\n";
+    }
+}
+
+if ($request['rawModeData']) {
+$content .= '
+
+
+ **请求参数示例**
+
+``` 
+'.$request['rawModeData'].'
+```
+
+';
+
+}
+
+if ($request['data']) {
+$content .='
+
+**参数:** 
+
+|参数名|是否必选|类型|说明|
+|:----    |:---|:----- |-----   |'."\n";
+    foreach ($request['data'] as $key => $value) {
+         $content .= '|'.$value["key"].' |   |'.$value["type"].' | 无 |'."\n";
+    }
+}
+
+
+        $return['page_content'] = $content ;
+        return $return ;
+
+    }
+
+
+    //从postman导入(v2版本)
+    private function _fromPostmanV2($json_array){
+
+        $login_user = $this->checkLogin();
+
+        // TODO 这里需要检查下合法性。比如关键字检查/黑名单检查/字符串过滤
+
+
+        $item_array = array(
+            "item_name" => $json_array['info']['name'] ? $json_array['info']['name']  : 'from postman' ,
+            "item_type" => '1' ,
+            "item_description" => $json_array['info']['description'] ? $json_array['info']['description'] :'',
+            "password" => time().rand(),
+            "members" => array(),
+            "pages" =>array(
+                    "pages" => array(),
+                    "catalogs" => array()
+                )
+            ) ;
+        $level = 2 ;
+        $item_array['pages']['pages'] = $this->_getPageByItem($json_array['item'] );
+        $item_array['pages']['catalogs'] = $this->_getItemByItem($json_array['item'] , 2 );
+        D("Item")->import( json_encode($item_array) , $login_user['uid'] );
+        
+        //echo D("Item")->export(196053901215026 );
+        //echo json_encode($item_array);
+        $this->sendResult(array());
+
+
+    }
+
+    //获取某个目录下的所有页面
+    private function _getPageByItem($item_array ){
+        $return = array();
+        foreach ($item_array as $key => $value) {
+
+            //含有request,则这是一个子页面
+            if ($value['request']) {
+                $return[] = $this->_requestToDocV2($value['name'] ,$value['request']);
+            }
+
+        }
+        return $return ;
+    }
+
+    //获取某个目录下的所有子目录
+    private function _getItemByItem($item_array ,$level ){
+        $return = array();
+        foreach ($item_array as $key => $value) {
+
+            //含有item,则这是一个子目录
+            if ($value['item']) {
+                $one_ary = array(
+                    "cat_name" => $value['name'] ,
+                    "level" => $level ,
+                    "s_number" => 99 ,
+                    "pages" => $this->_getPageByItem($value['item'], $level + 1 ), //递归
+                    "catalogs" => $this->_getItemByItem($value['item'], $level + 1 ) //递归
+                    );
+                $return[] = $one_ary ;
+            }
+
+        }
+        return $return ;
+    }
+
+    private function _requestToDocV2($name , $request){
+        $return = array() ;
+        $return['page_title'] = $name ;
+        $return['s_number'] = 99 ;
+        $return['page_comments'] = '' ;
+        //若$return['page_title'] 为很长的url,则做一些特殊处理
+        $tmp_title_array = explode("/", $return['page_title']);
+        if ($tmp_title_array) {
+            $tmp_title_array = array_slice($tmp_title_array, -2);// 倒数2个
+            if($tmp_title_array[1])$return['page_title'] = $tmp_title_array[0]."/".$tmp_title_array[1] ;
+        }
+        
+        $content = '  
+**简要描述:** 
+
+- '.$request['description'].'
+
+**请求URL:** 
+- ` '.$request['url'].' `
+  
+**请求方式:**
+- '.$request['method'].' ';
+
+if ($request['header']) {
+$content .='
+
+**Header:** 
+
+|Header名|是否必选|类型|说明|
+|:----    |:---|:----- |-----   |'."\n";
+    foreach ($request['header'] as $key => $value) {
+         $content .= '|'.$value["key"].' |  |  | '.$value["value"].' |'."\n";
+    }
+}
+
+if ($request['rawModeData']) {
+$content .= '
+
+
+ **请求参数示例**
+
+``` 
+'.$request['rawModeData'].'
+```
+
+';
+
+}
+
+if ($request['body']['formdata']) {
+$content .='
+
+**参数:** 
+
+|参数名|是否必选|类型|说明|
+|:----    |:---|:----- |-----   |'."\n";
+    foreach ($request['body']['formdata'] as $key => $value) {
+         $content .= '|'.$value["key"].' |   |'.$value["type"].' | 无 |'."\n";
+    }
+}
+
+
+        $return['page_content'] = $content ;
+        return $return ;
+
+    }
+
+}

+ 196 - 0
server/Application/Api/Controller/ImportSwaggerController.class.php

@@ -0,0 +1,196 @@
+<?php
+namespace Api\Controller;
+use Think\Controller;
+class ImportSwaggerController extends BaseController {
+
+
+    public function import(){
+        $login_user = $this->checkLogin();
+
+        $json = file_get_contents($_FILES["file"]["tmp_name"]) ;
+
+        //$json = file_get_contents("../Public/swagger.json") ;//test
+        $json_array = json_decode($json ,1 );
+        unset($json);
+        if ($json_array['info']) {
+            $this->_fromSwaggerV2($json_array);
+            return ;
+        }
+
+        $this->sendError(10303);
+    }
+
+    private function _fromSwaggerV2($json_array){
+
+        $login_user = $this->checkLogin();
+
+        // TODO 这里需要检查下合法性。比如关键字检查/黑名单检查/字符串过滤
+
+
+        $item_array = array(
+            "item_name" => $json_array['info']['title'] ? $json_array['info']['title']  : 'from swagger' ,
+            "item_type" => '1' ,
+            "item_description" => $json_array['info']['description'] ? $json_array['info']['description'] :'',
+            "password" => time().rand(),
+            "members" => array(),
+            "pages" =>array(
+                    "pages" => array(),
+                    "catalogs" => array()
+                )
+            ) ;
+        $level = 2 ;
+        $item_array['pages']['pages'] = $this->_getPageByPaths($json_array['paths'] );
+        D("Item")->import( json_encode($item_array) , $login_user['uid'] );
+        
+        //echo D("Item")->export(196053901215026 );
+        echo json_encode($item_array);
+        //$this->sendResult(array());
+
+    }
+
+    private function _getPageByPaths($paths){
+        $return = array() ;
+        foreach ($paths as $url => $value) {
+            foreach ($value as $method => $value2) {
+                $return[] = $this->_requestToDoc($method , $url , $value2);
+            }
+        }
+        return $return ;
+
+    }
+
+    private function _requestToDoc($method , $url , $request){
+        $return = array() ;
+        $return['page_title'] = $request['summary'] ;
+        $return['s_number'] = 99 ;
+        $return['page_comments'] = '' ;
+        
+        $content = '  
+**简要描述:** 
+
+- '.$request['description'].' 
+
+**请求URL:** 
+- ` '.$url.' `
+  
+**请求方式:**
+- '.$method.' ';
+
+if ($request['header']) {
+$content .='
+
+**Header:** 
+
+|Header名|是否必选|类型|说明|
+|:----    |:---|:----- |-----   |'."\n";
+    foreach ($request['headerData'] as $key => $value) {
+         $content .= '|'.$value["key"].' |  | text | '.$value["value"].' |'."\n";
+    }
+}
+
+if ($request['rawModeData']) {
+$content .= '
+
+
+ **请求参数示例**
+
+``` 
+'.$request['rawModeData'].'
+```
+
+';
+
+}
+
+if ($request['parameters']) {
+$content .='
+
+**参数:** 
+
+|参数名|是否必选|类型|说明|
+|:----    |:---|:----- |-----   |'."\n";
+    foreach ($request['parameters'] as $key => $value) {
+         $content .= '|'.$value["name"].' | '.($value["required"] ? '是' : '否' ).'  |'.$value["type"].' | '.$value["description"].' |'."\n";
+    }
+}
+
+if ($request['responses']['200']) {
+$content .= '
+
+
+ **返回示例**
+
+``` 
+
+'.$this->_indent_json(json_encode($request['responses']['200'])).'
+
+```
+
+';
+
+}
+
+        $return['page_content'] = $content ;
+        return $return ;
+
+    }
+
+    /**
+     * Indents a flat JSON string to make it more human-readable.
+     *
+     * @param string $json The original JSON string to process.
+     *
+     * @return string Indented version of the original JSON string.
+     */
+    private function _indent_json($json) {
+
+        $result      = '';
+        $pos         = 0;
+        $strLen      = strlen($json);
+        $indentStr   = '  ';
+        $newLine     = "\n";
+        $prevChar    = '';
+        $outOfQuotes = true;
+
+        for ($i=0; $i<=$strLen; $i++) {
+
+            // Grab the next character in the string.
+            $char = substr($json, $i, 1);
+
+            // Are we inside a quoted string?
+            if ($char == '"' && $prevChar != '\\') {
+                $outOfQuotes = !$outOfQuotes;
+
+            // If this character is the end of an element,
+            // output a new line and indent the next line.
+            } else if(($char == '}' || $char == ']') && $outOfQuotes) {
+                $result .= $newLine;
+                $pos --;
+                for ($j=0; $j<$pos; $j++) {
+                    $result .= $indentStr;
+                }
+            }
+
+            // Add the character to the result string.
+            $result .= $char;
+
+            // If the last character was the beginning of an element,
+            // output a new line and indent the next line.
+            if (($char == ',' || $char == '{' || $char == '[') && $outOfQuotes) {
+                $result .= $newLine;
+                if ($char == '{' || $char == '[') {
+                    $pos ++;
+                }
+
+                for ($j = 0; $j < $pos; $j++) {
+                    $result .= $indentStr;
+                }
+            }
+
+            $prevChar = $char;
+        }
+
+        return $result;
+    }
+
+}