Browse Source

接口删除

mxd 5 years ago
parent
commit
5b80f7677c

+ 6 - 6
src/main/java/org/ssssssss/magicapi/config/MagicApiService.java

@@ -5,12 +5,12 @@ import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.jdbc.core.RowMapper;
 
 import java.util.List;
-import java.util.Map;
 import java.util.UUID;
 
 public class MagicApiService {
 
 	private final String deleteById = "delete from magic_api_info where id = ?";
+	private final String deleteByGroupName = "delete from magic_api_info where api_group_name = ?";
 	private final String selectList = "select id,api_name name,api_group_name group_name,api_path path,api_method method from magic_api_info order by api_update_time desc";
 	private final String selectListWithScript = "select id,api_name name,api_group_name group_name,api_path path,api_method method,api_script script,api_parameter parameter,api_option `option` from magic_api_info";
 	private final String selectOne = "select api_method method,api_script script,api_path path,api_name name,api_group_name group_name,api_parameter parameter,api_option `option` from magic_api_info where id = ?";
@@ -26,11 +26,11 @@ public class MagicApiService {
 	}
 
 	protected boolean delete(String id) {
-		Map<String, Object> info = template.queryForMap("select * from magic_api_info where id = ?", id);
-		if (info != null) {
-			return template.update(deleteById, id) > 0;
-		}
-		return false;
+		return template.update(deleteById, id) > 0;
+	}
+
+	protected boolean deleteGroup(String groupName) {
+		return template.update(deleteByGroupName, groupName) > 0;
 	}
 
 	protected List<ApiInfo> list() {

+ 23 - 3
src/main/java/org/ssssssss/magicapi/config/WebUIController.java

@@ -54,6 +54,7 @@ public class WebUIController {
 	@ResponseBody
 	public JsonBean<Boolean> delete(String id) {
 		try {
+			mappingHandlerMapping.unregisterMapping(id);
 			return new JsonBean<>(this.magicApiService.delete(id));
 		} catch (Exception e) {
 			logger.error("删除接口出错", e);
@@ -61,6 +62,25 @@ public class WebUIController {
 		}
 	}
 
+	@RequestMapping("/group/delete")
+	@ResponseBody
+	public JsonBean<Boolean> deleteGroup(String apiIds, String groupName) {
+		try {
+			if (StringUtils.isNotBlank(apiIds)) {
+				String[] ids = apiIds.split(",");
+				if (ids != null && ids.length > 0) {
+					for (String id : ids) {
+						mappingHandlerMapping.unregisterMapping(id);
+					}
+				}
+			}
+			return new JsonBean<>(this.magicApiService.deleteGroup(groupName));
+		} catch (Exception e) {
+			logger.error("删除接口出错", e);
+			return new JsonBean<>(-1, e.getMessage());
+		}
+	}
+
 	@RequestMapping("/list")
 	@ResponseBody
 	public JsonBean<List<ApiInfo>> list() {
@@ -104,9 +124,9 @@ public class WebUIController {
 			try {
 				putMapIntoContext((Map<String, Object>) request.get("request"), context);
 				putMapIntoContext((Map<String, Object>) request.get("path"), context);
-				context.set("cookie",request.get("cookie"));
-				context.set("session",request.get("session"));
-				context.set("header",request.get("header"));
+				context.set("cookie", request.get("cookie"));
+				context.set("session", request.get("session"));
+				context.set("header", request.get("header"));
 			} catch (Exception e) {
 				return new JsonBean<>(0, "请求参数填写错误");
 			}

+ 0 - 4
src/main/resources/magicapi-support/css/index.css

@@ -97,10 +97,6 @@ html,body{
 .api-list{
     height : 100%;
 }
-.api-list .layui-tree-entry label{
-    color : #000;
-    padding-right: 4px;
-}
 .scroller{
     overflow: auto;
 }

+ 0 - 1
src/main/resources/magicapi-support/index.html

@@ -46,7 +46,6 @@
 								<select name="group" lay-search>
 									<option value="">请输入组名</option>
 									<option value="未分组">未分组</option>
-									<option value="23">123</option>
 								</select>
 							</div>
 							<div class="layui-input-inline" style="width: 100px;margin-left: 5px;">

+ 60 - 18
src/main/resources/magicapi-support/js/index.js

@@ -84,6 +84,40 @@ $(function(){
         }
     }
 
+    var loadAPI = function () {
+        _ajax({
+            url : 'list',
+            success : function(list){
+                apiList = list;
+                renderApiList();
+            }
+        })
+    }
+    var deleteAPIGroup = function(groupName,ids){
+        _ajax({
+            url : 'group/delete',
+            data : {
+                apiIds : ids.join(','),
+                groupName : groupName
+            },
+            success : function(){
+                loadAPI();
+            }
+        })
+    }
+
+    var deleteAPI = function(id){
+        _ajax({
+            url : 'delete',
+            data : {
+                id : id
+            },
+            success : function(){
+                loadAPI();
+            }
+        })
+    }
+
     var resetGroup = function(selValue){
         var $dom = $("select[name='group']").next().find("input");
         $dom.unbind("blur");
@@ -224,7 +258,7 @@ $(function(){
     $(window).resize(editorLayout);
     var $tree;
     // 渲染接口列表
-    var renderApiList = function(emptyString){
+    var renderApiList = function(){
         var $ul = $(".layui-left .api-list").html('');
         var empty = true;
         var root = [];
@@ -253,7 +287,7 @@ $(function(){
                         id : info.id,
                         groupName : info.groupName,
                         name : info.name,
-                        title : '<label>' + info.name + "</label>" + info.path,
+                        title : '<label style="padding-right: 4px;color:#000">' + info.name + "</label>" + info.path,
                         path : info.path
                     });
                 }
@@ -271,6 +305,28 @@ $(function(){
                 elem : '#api-list',
                 id : 'api-list',
                 data : root,
+                edit : ['del'],
+                onlyIconControl : true,
+                operate : function(obj){
+                    var data = obj.data;
+                    if(data.children){
+                        var ids = [];
+                        for(var i=0,len=data.children.length;i<len;i++){
+                            ids.push(data.children[i].id);
+                            if(data.children[i].id == apiId){
+                                apiId = null;
+                                resetEditor();
+                            }
+                        }
+                        deleteAPIGroup(data.id,ids);
+                    }else{
+                        deleteAPI(data.id)
+                    }
+                    if(data.id == apiId){
+                        apiId = null;
+                        resetEditor();
+                    }
+                },
                 click : function(obj){
                     if(!obj.data.children){
                         var id = obj.data.id;
@@ -297,10 +353,6 @@ $(function(){
                 }
             })
         }
-
-        // if(empty){
-        //     $ul.html($('<li/>').addClass('empty').html(emptyString));
-        // }
     }
     var resizeX = $(".layout-resizer-x")[0];
     resizeX.onmousedown = function(e){
@@ -463,17 +515,7 @@ $(function(){
             var info = apiList[i];
             info.show = value ? info.path.indexOf(value) > -1 || info.name.indexOf(value) > -1 : true;
         }
-        if(value){
-            renderApiList('搜不到相关API...')
-        }else{
-            renderApiList('您还没有创建接口..');
-        }
-    })
-    _ajax({
-        url : 'list',
-        success : function(list){
-            apiList = list;
-            renderApiList('您还没有创建接口..');
-        }
+        renderApiList()
     })
+    loadAPI();
 });