star7th преди 5 години
родител
ревизия
4a2ead0aac

+ 54 - 6
server/Application/Api/Controller/ImportController.class.php

@@ -4,8 +4,54 @@ use Think\Controller;
 class ImportController extends BaseController {
 
 
+    //自动检测导入的文件类型从而选择不同的控制器方法
+    public function auto(){
+        set_time_limit(100);
+        ini_set('memory_limit','200M');
+        $login_user = $this->checkLogin();
+        $filename = $_FILES["file"]["name"] ;
+        $file = $_FILES["file"]["tmp_name"] ;
+        //文件后缀
+        $tail = substr(strrchr($filename, '.'), 1);
+
+        if ($tail == 'zip') {
+            $zipArc = new \ZipArchive();
+            $ret = $zipArc->open($file, \ZipArchive::CREATE);
+            $info = $zipArc->getFromName(DIRECTORY_SEPARATOR."info.json") ;
+            if ($info) {
+                $info_array = json_decode($info ,1 );
+                if ($info_array) {
+                    $this->markdown($info_array);
+                    return ;
+                }
+            }
+        }
+
+        if ($tail == 'json') {
+            $json = file_get_contents($file) ;
+            $json_array = json_decode($json ,1 );
+            unset($json);
+            if ($json_array['swagger'] && $json_array['info']) {
+                R("ImportSwagger/import");
+                return ;
+            }
+            if ($json_array['id']) {
+                R("ImportPostman/import");
+                return ;
+            }
+            if ($json_array['info']) {
+                R("ImportPostman/import");
+                return ;
+            }
+        }
+
+        $this->sendError(10101);
+
+
+    }
+
     //导入markdown压缩包
-    public function markdown(){
+    public function markdown($info_array){
         set_time_limit(100);
         ini_set('memory_limit','200M');
 
@@ -14,11 +60,13 @@ class ImportController extends BaseController {
         $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) {
+            $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) {
 

+ 2 - 2
server/Application/Api/Controller/ImportSwaggerController.class.php

@@ -43,8 +43,8 @@ class ImportSwaggerController extends BaseController {
         D("Item")->import( json_encode($item_array) , $login_user['uid'] );
         
         //echo D("Item")->export(196053901215026 );
-        echo json_encode($item_array);
-        //$this->sendResult(array());
+        //echo json_encode($item_array);
+        $this->sendResult(array());
 
     }