Skip to content

Commit

Permalink
菜单路由配置支持内链访问
Browse files Browse the repository at this point in the history
  • Loading branch information
yangzongzhuan committed Jul 11, 2021
1 parent 8dee57f commit e33ce57
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/main/java/com/ruoyi/common/constant/UserConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public class UserConstants
/** ParentView组件标识 */
public final static String PARENT_VIEW = "ParentView";

/** InnerLink组件标识 */
public final static String INNER_LINK = "InnerLink";

/** 校验返回结果码 */
public final static String UNIQUE = "0";
public final static String NOT_UNIQUE = "1";
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/ruoyi/common/utils/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.text.StrFormatter;

/**
Expand Down Expand Up @@ -260,6 +261,17 @@ public static String format(String template, Object... params)
return StrFormatter.format(template, params);
}

/**
* 是否为http(s)://开头
*
* @param link 链接
* @return 结果
*/
public static boolean ishttp(String link)
{
return StringUtils.startsWithAny(link, Constants.HTTP, Constants.HTTPS);
}

/**
* 字符串转set
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ public class SysIndexController
@RequestMapping("/")
public String index()
{
return StringUtils.format("欢迎使用RuoYi后台管理框架,当前版本:v{}", ruoyiConfig.getVersion());
return StringUtils.format("欢迎使用{}后台管理框架,当前版本:v{},请通过前端地址访问。", ruoyiConfig.getName(), ruoyiConfig.getVersion());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServletUtils;
Expand Down Expand Up @@ -102,8 +101,7 @@ public AjaxResult add(@Validated @RequestBody SysMenu menu)
{
return AjaxResult.error("新增菜单'" + menu.getMenuName() + "'失败,菜单名称已存在");
}
else if (UserConstants.YES_FRAME.equals(menu.getIsFrame())
&& !StringUtils.startsWithAny(menu.getPath(), Constants.HTTP, Constants.HTTPS))
else if (UserConstants.YES_FRAME.equals(menu.getIsFrame()) && !StringUtils.ishttp(menu.getPath()))
{
return AjaxResult.error("新增菜单'" + menu.getMenuName() + "'失败,地址必须以http(s)://开头");
}
Expand All @@ -123,8 +121,7 @@ public AjaxResult edit(@Validated @RequestBody SysMenu menu)
{
return AjaxResult.error("修改菜单'" + menu.getMenuName() + "'失败,菜单名称已存在");
}
else if (UserConstants.YES_FRAME.equals(menu.getIsFrame())
&& !StringUtils.startsWithAny(menu.getPath(), Constants.HTTP, Constants.HTTPS))
else if (UserConstants.YES_FRAME.equals(menu.getIsFrame()) && !StringUtils.ishttp(menu.getPath()))
{
return AjaxResult.error("修改菜单'" + menu.getMenuName() + "'失败,地址必须以http(s)://开头");
}
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/com/ruoyi/project/system/domain/vo/MetaVo.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.ruoyi.project.system.domain.vo;

import com.ruoyi.common.utils.StringUtils;

/**
* 路由显示信息
*
Expand All @@ -22,6 +24,11 @@ public class MetaVo
*/
private boolean noCache;

/**
* 内链地址(http(s)://开头)
*/
private String link;

public MetaVo()
{
}
Expand All @@ -39,6 +46,24 @@ public MetaVo(String title, String icon, boolean noCache)
this.noCache = noCache;
}

public MetaVo(String title, String icon, String link)
{
this.title = title;
this.icon = icon;
this.link = link;
}

public MetaVo(String title, String icon, boolean noCache, String link)
{
this.title = title;
this.icon = icon;
this.noCache = noCache;
if (StringUtils.ishttp(link))
{
this.link = link;
}
}

public boolean isNoCache()
{
return noCache;
Expand Down Expand Up @@ -68,4 +93,14 @@ public void setIcon(String icon)
{
this.icon = icon;
}

public String getLink()
{
return link;
}

public void setLink(String link)
{
this.link = link;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
Expand Down Expand Up @@ -150,7 +151,7 @@ public List<RouterVo> buildMenus(List<SysMenu> menus)
router.setName(getRouteName(menu));
router.setPath(getRouterPath(menu));
router.setComponent(getComponent(menu));
router.setMeta(new MetaVo(menu.getMenuName(), menu.getIcon(), StringUtils.equals("1", menu.getIsCache())));
router.setMeta(new MetaVo(menu.getMenuName(), menu.getIcon(), StringUtils.equals("1", menu.getIsCache()), menu.getPath()));
List<SysMenu> cMenus = menu.getChildren();
if (!cMenus.isEmpty() && cMenus.size() > 0 && UserConstants.TYPE_DIR.equals(menu.getMenuType()))
{
Expand All @@ -166,7 +167,21 @@ else if (isMenuFrame(menu))
children.setPath(menu.getPath());
children.setComponent(menu.getComponent());
children.setName(StringUtils.capitalize(menu.getPath()));
children.setMeta(new MetaVo(menu.getMenuName(), menu.getIcon(), StringUtils.equals("1", menu.getIsCache())));
children.setMeta(new MetaVo(menu.getMenuName(), menu.getIcon(), StringUtils.equals("1", menu.getIsCache()), menu.getPath()));
childrenList.add(children);
router.setChildren(childrenList);
}
else if (menu.getParentId().intValue() == 0 && isInnerLink(menu))
{
router.setMeta(null);
router.setPath("/inner");
List<RouterVo> childrenList = new ArrayList<RouterVo>();
RouterVo children = new RouterVo();
String routerPath = StringUtils.replaceEach(menu.getPath(), new String[] { Constants.HTTP, Constants.HTTPS }, new String[] { "", "" });
children.setPath(routerPath);
children.setComponent(UserConstants.INNER_LINK);
children.setName(StringUtils.capitalize(routerPath));
children.setMeta(new MetaVo(menu.getMenuName(), menu.getIcon(), menu.getPath()));
childrenList.add(children);
router.setChildren(childrenList);
}
Expand Down Expand Up @@ -338,6 +353,11 @@ public String getRouteName(SysMenu menu)
public String getRouterPath(SysMenu menu)
{
String routerPath = menu.getPath();
// 内链打开外网方式
if (menu.getParentId().intValue() != 0 && isInnerLink(menu))
{
routerPath = StringUtils.replaceEach(routerPath, new String[] { Constants.HTTP, Constants.HTTPS }, new String[] { "", "" });
}
// 非外链并且是一级目录(类型为目录)
if (0 == menu.getParentId().intValue() && UserConstants.TYPE_DIR.equals(menu.getMenuType())
&& UserConstants.NO_FRAME.equals(menu.getIsFrame()))
Expand Down Expand Up @@ -365,6 +385,10 @@ public String getComponent(SysMenu menu)
{
component = menu.getComponent();
}
else if (StringUtils.isEmpty(menu.getComponent()) && menu.getParentId().intValue() != 0 && isInnerLink(menu))
{
component = UserConstants.INNER_LINK;
}
else if (StringUtils.isEmpty(menu.getComponent()) && isParentView(menu))
{
component = UserConstants.PARENT_VIEW;
Expand Down Expand Up @@ -395,6 +419,17 @@ public boolean isParentView(SysMenu menu)
return menu.getParentId().intValue() != 0 && UserConstants.TYPE_DIR.equals(menu.getMenuType());
}

/**
* 是否为内链组件
*
* @param menu 菜单信息
* @return 结果
*/
public boolean isInnerLink(SysMenu menu)
{
return menu.getIsFrame().equals(UserConstants.NO_FRAME) && StringUtils.ishttp(menu.getPath());
}

/**
* 根据父节点的ID获取所有子节点
*
Expand Down

0 comments on commit e33ce57

Please sign in to comment.