Browse Source

菜单路由配置支持内链访问

RuoYi 4 years ago
parent
commit
114c9f3af6

+ 3 - 0
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/constant/UserConstants.java

@@ -57,6 +57,9 @@ public class UserConstants
57 57
     /** ParentView组件标识 */
58 58
     public final static String PARENT_VIEW = "ParentView";
59 59
 
60
+    /** InnerLink组件标识 */
61
+    public final static String INNER_LINK = "InnerLink";
62
+
60 63
     /** 校验返回结果码 */
61 64
     public final static String UNIQUE = "0";
62 65
 

+ 12 - 0
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/StringUtils.java

@@ -4,6 +4,7 @@ import java.util.Collection;
4 4
 import java.util.List;
5 5
 import java.util.Map;
6 6
 import org.springframework.util.AntPathMatcher;
7
+import com.ruoyi.common.core.constant.Constants;
7 8
 import com.ruoyi.common.core.text.StrFormatter;
8 9
 
9 10
 /**
@@ -283,6 +284,17 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
283 284
     }
284 285
 
285 286
     /**
287
+     * 是否为http(s)://开头
288
+     * 
289
+     * @param link 链接
290
+     * @return 结果
291
+     */
292
+    public static boolean ishttp(String link)
293
+    {
294
+        return StringUtils.startsWithAny(link, Constants.HTTP, Constants.HTTPS);
295
+    }
296
+
297
+    /**
286 298
      * 驼峰转下划线命名
287 299
      */
288 300
     public static String toUnderScoreCase(String str)

+ 2 - 5
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/SysMenuController.java

@@ -11,7 +11,6 @@ import org.springframework.web.bind.annotation.PutMapping;
11 11
 import org.springframework.web.bind.annotation.RequestBody;
12 12
 import org.springframework.web.bind.annotation.RequestMapping;
13 13
 import org.springframework.web.bind.annotation.RestController;
14
-import com.ruoyi.common.core.constant.Constants;
15 14
 import com.ruoyi.common.core.constant.UserConstants;
16 15
 import com.ruoyi.common.core.utils.SecurityUtils;
17 16
 import com.ruoyi.common.core.utils.StringUtils;
@@ -94,8 +93,7 @@ public class SysMenuController extends BaseController
94 93
         {
95 94
             return AjaxResult.error("新增菜单'" + menu.getMenuName() + "'失败,菜单名称已存在");
96 95
         }
97
-        else if (UserConstants.YES_FRAME.equals(menu.getIsFrame())
98
-                && !StringUtils.startsWithAny(menu.getPath(), Constants.HTTP, Constants.HTTPS))
96
+        else if (UserConstants.YES_FRAME.equals(menu.getIsFrame()) && !StringUtils.ishttp(menu.getPath()))
99 97
         {
100 98
             return AjaxResult.error("新增菜单'" + menu.getMenuName() + "'失败,地址必须以http(s)://开头");
101 99
         }
@@ -115,8 +113,7 @@ public class SysMenuController extends BaseController
115 113
         {
116 114
             return AjaxResult.error("修改菜单'" + menu.getMenuName() + "'失败,菜单名称已存在");
117 115
         }
118
-        else if (UserConstants.YES_FRAME.equals(menu.getIsFrame())
119
-                && !StringUtils.startsWithAny(menu.getPath(), Constants.HTTP, Constants.HTTPS))
116
+        else if (UserConstants.YES_FRAME.equals(menu.getIsFrame()) && !StringUtils.ishttp(menu.getPath()))
120 117
         {
121 118
             return AjaxResult.error("修改菜单'" + menu.getMenuName() + "'失败,地址必须以http(s)://开头");
122 119
         }

+ 35 - 0
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/domain/vo/MetaVo.java

@@ -1,5 +1,7 @@
1 1
 package com.ruoyi.system.domain.vo;
2 2
 
3
+import com.ruoyi.common.core.utils.StringUtils;
4
+
3 5
 /**
4 6
  * 路由显示信息
5 7
  * 
@@ -22,6 +24,11 @@ public class MetaVo
22 24
      */
23 25
     private boolean noCache;
24 26
 
27
+    /**
28
+     * 内链地址(http(s)://开头)
29
+     */
30
+    private String link;
31
+
25 32
     public MetaVo()
26 33
     {
27 34
     }
@@ -39,6 +46,24 @@ public class MetaVo
39 46
         this.noCache = noCache;
40 47
     }
41 48
 
49
+    public MetaVo(String title, String icon, String link)
50
+    {
51
+        this.title = title;
52
+        this.icon = icon;
53
+        this.link = link;
54
+    }
55
+
56
+    public MetaVo(String title, String icon, boolean noCache, String link)
57
+    {
58
+        this.title = title;
59
+        this.icon = icon;
60
+        this.noCache = noCache;
61
+        if (StringUtils.ishttp(link))
62
+        {
63
+            this.link = link;
64
+        }
65
+    }
66
+
42 67
     public boolean isNoCache()
43 68
     {
44 69
         return noCache;
@@ -68,4 +93,14 @@ public class MetaVo
68 93
     {
69 94
         this.icon = icon;
70 95
     }
96
+
97
+    public String getLink()
98
+    {
99
+        return link;
100
+    }
101
+
102
+    public void setLink(String link)
103
+    {
104
+        this.link = link;
105
+    }
71 106
 }

+ 37 - 2
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysMenuServiceImpl.java

@@ -10,6 +10,7 @@ import java.util.Set;
10 10
 import java.util.stream.Collectors;
11 11
 import org.springframework.beans.factory.annotation.Autowired;
12 12
 import org.springframework.stereotype.Service;
13
+import com.ruoyi.common.core.constant.Constants;
13 14
 import com.ruoyi.common.core.constant.UserConstants;
14 15
 import com.ruoyi.common.core.utils.SecurityUtils;
15 16
 import com.ruoyi.common.core.utils.StringUtils;
@@ -150,7 +151,7 @@ public class SysMenuServiceImpl implements ISysMenuService
150 151
             router.setName(getRouteName(menu));
151 152
             router.setPath(getRouterPath(menu));
152 153
             router.setComponent(getComponent(menu));
153
-            router.setMeta(new MetaVo(menu.getMenuName(), menu.getIcon(), StringUtils.equals("1", menu.getIsCache())));
154
+            router.setMeta(new MetaVo(menu.getMenuName(), menu.getIcon(), StringUtils.equals("1", menu.getIsCache()), menu.getPath()));
154 155
             List<SysMenu> cMenus = menu.getChildren();
155 156
             if (!cMenus.isEmpty() && cMenus.size() > 0 && UserConstants.TYPE_DIR.equals(menu.getMenuType()))
156 157
             {
@@ -166,7 +167,21 @@ public class SysMenuServiceImpl implements ISysMenuService
166 167
                 children.setPath(menu.getPath());
167 168
                 children.setComponent(menu.getComponent());
168 169
                 children.setName(StringUtils.capitalize(menu.getPath()));
169
-                children.setMeta(new MetaVo(menu.getMenuName(), menu.getIcon(), StringUtils.equals("1", menu.getIsCache())));
170
+                children.setMeta(new MetaVo(menu.getMenuName(), menu.getIcon(), StringUtils.equals("1", menu.getIsCache()), menu.getPath()));
171
+                childrenList.add(children);
172
+                router.setChildren(childrenList);
173
+            }
174
+            else if (menu.getParentId().intValue() == 0 && isInnerLink(menu))
175
+            {
176
+                router.setMeta(null);
177
+                router.setPath("/inner");
178
+                List<RouterVo> childrenList = new ArrayList<RouterVo>();
179
+                RouterVo children = new RouterVo();
180
+                String routerPath = StringUtils.replaceEach(menu.getPath(), new String[] { Constants.HTTP, Constants.HTTPS }, new String[] { "", "" });
181
+                children.setPath(routerPath);
182
+                children.setComponent(UserConstants.INNER_LINK);
183
+                children.setName(StringUtils.capitalize(routerPath));
184
+                children.setMeta(new MetaVo(menu.getMenuName(), menu.getIcon(), menu.getPath()));
170 185
                 childrenList.add(children);
171 186
                 router.setChildren(childrenList);
172 187
             }
@@ -338,6 +353,11 @@ public class SysMenuServiceImpl implements ISysMenuService
338 353
     public String getRouterPath(SysMenu menu)
339 354
     {
340 355
         String routerPath = menu.getPath();
356
+        // 内链打开外网方式
357
+        if (menu.getParentId().intValue() != 0 && isInnerLink(menu))
358
+        {
359
+            routerPath = StringUtils.replaceEach(routerPath, new String[] { Constants.HTTP, Constants.HTTPS }, new String[] { "", "" });
360
+        }
341 361
         // 非外链并且是一级目录(类型为目录)
342 362
         if (0 == menu.getParentId().intValue() && UserConstants.TYPE_DIR.equals(menu.getMenuType())
343 363
                 && UserConstants.NO_FRAME.equals(menu.getIsFrame()))
@@ -365,6 +385,10 @@ public class SysMenuServiceImpl implements ISysMenuService
365 385
         {
366 386
             component = menu.getComponent();
367 387
         }
388
+        else if (StringUtils.isEmpty(menu.getComponent()) && menu.getParentId().intValue() != 0 && isInnerLink(menu))
389
+        {
390
+            component = UserConstants.INNER_LINK;
391
+        }
368 392
         else if (StringUtils.isEmpty(menu.getComponent()) && isParentView(menu))
369 393
         {
370 394
             component = UserConstants.PARENT_VIEW;
@@ -385,6 +409,17 @@ public class SysMenuServiceImpl implements ISysMenuService
385 409
     }
386 410
 
387 411
     /**
412
+     * 是否为内链组件
413
+     * 
414
+     * @param menu 菜单信息
415
+     * @return 结果
416
+     */
417
+    public boolean isInnerLink(SysMenu menu)
418
+    {
419
+        return menu.getIsFrame().equals(UserConstants.NO_FRAME) && StringUtils.ishttp(menu.getPath());
420
+    }
421
+
422
+    /**
388 423
      * 是否为parent_view组件
389 424
      * 
390 425
      * @param menu 菜单信息

+ 3 - 1
ruoyi-ui/src/components/HeaderSearch/index.vue

@@ -70,9 +70,11 @@ export default {
70 70
       this.show = false
71 71
     },
72 72
     change(val) {
73
+      const path = val.path;
73 74
       if(this.ishttp(val.path)) {
74 75
         // http(s):// 路径新窗口打开
75
-        window.open(val.path, "_blank");
76
+        const pindex = path.indexOf("http");
77
+        window.open(path.substr(pindex, path.length), "_blank");
76 78
       } else {
77 79
         this.$router.push(val.path)
78 80
       }

+ 27 - 0
ruoyi-ui/src/layout/components/InnerLink/index.vue

@@ -0,0 +1,27 @@
1
+<script>
2
+export default {
3
+  data() {
4
+    return {};
5
+  },
6
+  render() {
7
+    const { $route: { meta: { link } }, } = this;
8
+    if ({ link }.link === "") {
9
+      return "404";
10
+    }
11
+    let url = { link }.link;
12
+    const height = document.documentElement.clientHeight - 94.5 + "px";
13
+    const style = { height: height };
14
+
15
+    return (
16
+      <div style={style}>
17
+        <iframe
18
+          src={url}
19
+          frameborder="no"
20
+          style="width: 100%; height: 100%"
21
+          scrolling="auto"
22
+        ></iframe>
23
+      </div>
24
+    );
25
+  },
26
+};
27
+</script>

+ 1 - 0
ruoyi-ui/src/router/index.js

@@ -6,6 +6,7 @@ Vue.use(Router)
6 6
 /* Layout */
7 7
 import Layout from '@/layout'
8 8
 import ParentView from '@/components/ParentView';
9
+import InnerLink from '@/layout/components/InnerLink'
9 10
 
10 11
 /**
11 12
  * Note: 路由配置项

+ 3 - 0
ruoyi-ui/src/store/modules/permission.js

@@ -2,6 +2,7 @@ import { constantRoutes } from '@/router'
2 2
 import { getRouters } from '@/api/menu'
3 3
 import Layout from '@/layout/index'
4 4
 import ParentView from '@/components/ParentView';
5
+import InnerLink from '@/layout/components/InnerLink'
5 6
 
6 7
 const permission = {
7 8
   state: {
@@ -65,6 +66,8 @@ function filterAsyncRouter(asyncRouterMap, lastRouter = false, type = false) {
65 66
         route.component = Layout
66 67
       } else if (route.component === 'ParentView') {
67 68
         route.component = ParentView
69
+      } else if (route.component === 'InnerLink') {
70
+        route.component = InnerLink
68 71
       } else {
69 72
         route.component = loadView(route.component)
70 73
       }