star7th 7 роки тому
батько
коміт
f7d8480522

+ 2 - 23
server/Application/Api/Controller/ItemController.class.php

@@ -406,29 +406,8 @@ class ItemController extends BaseController {
     }
 
     public function updateByApi(){
-        $api_key = I("api_key");
-        $api_token = I("api_token");
-        $cat_name = I("cat_name");
-        $cat_name_sub = I("cat_name_sub");
-        $page_title = I("page_title");
-        $page_content = I("page_content");
-        $s_number = I("s_number") ? I("s_number") : 99;
-
-        $item_id = D("ItemToken")->check($api_key , $api_token);
-        if (!$item_id) {
-            //没验证通过
-            $this->sendError(10306);
-            return false;
-        }
-
-        $page_id = D("Page")->update_by_content($item_id,$page_title,$page_content,$cat_name,$cat_name_sub,$s_number);
-
-        if ($page_id) {
-            $ret = D("Page")->where(" page_id = '$page_id' ")->find();
-            $this->sendResult($ret);
-        }else{
-            $this->sendError(10101);
-        }
+        //转到Open控制器的updateItem方法
+        R('Open/updateItem');
     }
 
     //置顶项目

+ 127 - 0
server/Application/Api/Controller/OpenController.class.php

@@ -0,0 +1,127 @@
+<?php
+namespace Api\Controller;
+use Think\Controller;
+class OpenController extends BaseController {
+
+    //根据内容更新项目
+    public function updateItem(){
+        $api_key = I("api_key");
+        $api_token = I("api_token");
+        $cat_name = I("cat_name");
+        $cat_name_sub = I("cat_name_sub");
+        $page_title = I("page_title");
+        $page_content = I("page_content");
+        $s_number = I("s_number") ? I("s_number") : 99;
+
+        $item_id = D("ItemToken")->check($api_key , $api_token);
+        if (!$item_id) {
+            //没验证通过
+            $this->sendError(10306);
+            return false;
+        }
+
+        $page_id = D("Page")->update_by_content($item_id,$page_title,$page_content,$cat_name,$cat_name_sub,$s_number);
+
+        if ($page_id) {
+            $ret = D("Page")->where(" page_id = '$page_id' ")->find();
+            $this->sendResult($ret);
+        }else{
+            $this->sendError(10101);
+        }
+    }
+
+    //根据shell上报的数据库结构信息生成数据字典
+    public function updateDbItem(){
+        $api_key = I("api_key");
+        $api_token = I("api_token");
+        $table_info = I("table_info");
+        $table_detail = I("table_detail");
+        $s_number = I("s_number") ? I("s_number") : 99;
+        header( 'Content-Type:text/html;charset=utf-8 ');
+        
+        $item_id = D("ItemToken")->check($api_key , $api_token);
+        if (!$item_id) {
+            //没验证通过
+            echo "api_key或者api_token不匹配\n";
+            return false;
+        }
+
+        $tables = $this->_analyze_db_structure_to_array($table_info ,$table_detail);
+        if (!empty($tables)) {
+            //D("Item")->empty_content($item_id); //清空内容
+            foreach ($tables as $key => $value) {
+                $page_title = $value['table_name'] ;
+                $page_content = $value['markdown'] ;
+                $result = D("Page")->update_by_content($item_id,$page_title,$page_content);
+            }
+        }
+
+        if (!empty($result)) {
+            echo "成功\n";
+        }else{
+            echo "失败\n";
+        }
+        
+    }
+
+    private function _analyze_db_structure_to_array($table_info , $table_detail){
+        $tables = array();
+
+        //解析table_info
+        $array = explode("\n", $table_info);
+        if(!empty($array)){
+            foreach ($array as $key => $value) {
+                if ($key == 0) {
+                    continue;
+                }
+                $array2 = explode("\t", $value);
+                $tables[$array2[0]] = array(
+                    "table_name" => $array2[0] ,
+                    "table_comment" => $array2[1] ,
+                    );
+            }
+        }
+
+
+
+        //解析table_detail
+        $array = explode("\n", $table_detail);
+        if(!empty($array)){
+            foreach ($array as $key => $value) {
+                if ($key == 0) {
+                    continue;
+                }
+                $array2 = explode("\t", $value);
+
+                $tables[$array2[0]]['columns'][$array2[1]] = array(
+                    "column_name" => $array2[1] ,
+                    "default" => $array2[2] ,
+                    "is_nullable" => $array2[3] ,
+                    "column_type" => $array2[4] ,
+                    "column_comment" => $array2[5] ? $array2[5] : '无' ,
+                    );
+
+            }
+        }
+
+
+        //生成markdown内容放在数组里
+        if (!empty($tables)) {
+            foreach ($tables as $key => $value) {
+                $markdown = '';
+                $markdown .= "- {$value['table_comment']} \n \n" ;
+                $markdown .= "|字段|类型|允许空|默认|注释| \n ";
+                $markdown .= "|:----    |:-------    |:--- |-- -|------      | \n ";
+                foreach ($value['columns'] as $key2 => $value2) {
+                    $markdown .= "|{$value2['column_name']} |{$value2['column_type']} |{$value2['is_nullable']} | {$value2['default']} | {$value2['column_comment']}  | \n ";
+                }
+
+                $tables[$key]['markdown'] = $markdown ;
+
+            }
+        }
+        return $tables;
+    }
+
+
+}

+ 20 - 0
server/Application/Api/Model/ItemModel.class.php

@@ -178,5 +178,25 @@ class ItemModel extends BaseModel {
     public function copy($item_id,$uid,$item_name= '',$item_description= '',$item_password = '',$item_domain){
         return $this->import($this->export($item_id),$uid,$item_name,$item_description,$item_password,$item_domain);
     }
+
+    //清空项目内容,但不删除项目也不删除项目成员关系
+    public function empty_content($item_id){
+        D("Page")->where("item_id = '$item_id' ")->delete();
+        D("Page")->partitionTable($item_id)->where("item_id = '$item_id' ")->delete();
+        D("Catalog")->where("item_id = '$item_id' ")->delete();
+        D("PageHistory")->partitionTable(1)->where("item_id = '$item_id' ")->delete();
+        D("PageHistory")->partitionTable(2)->where("item_id = '$item_id' ")->delete();
+        D("PageHistory")->partitionTable(3)->where("item_id = '$item_id' ")->delete();
+        D("PageHistory")->partitionTable(4)->where("item_id = '$item_id' ")->delete();
+        D("PageHistory")->partitionTable(5)->where("item_id = '$item_id' ")->delete();
+        D("PageHistory")->partitionTable(6)->where("item_id = '$item_id' ")->delete();
+        D("PageHistory")->partitionTable(7)->where("item_id = '$item_id' ")->delete();
+        D("PageHistory")->partitionTable(8)->where("item_id = '$item_id' ")->delete();
+        D("PageHistory")->partitionTable(9)->where("item_id = '$item_id' ")->delete();
+        D("PageHistory")->partitionTable(10)->where("item_id = '$item_id' ")->delete();
+        //删除菜单缓存
+        $redis = init_redis() ;
+        $redis->delete('showdoc_menu_cache_item_id_'.$item_id);
+    }
     
 }