end: 支持服务端动态路由

This commit is contained in:
wangiegie@gmail.com 2017-11-14 18:01:52 +08:00
parent 607ada9079
commit 3021ab3341
7 changed files with 47 additions and 30 deletions

View File

@ -11,7 +11,6 @@ import com.github.pig.common.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -34,7 +33,7 @@ public class MenuController extends BaseController {
*/ */
@GetMapping("/findMenuByRole/{role}") @GetMapping("/findMenuByRole/{role}")
public Set<MenuVo> findMenuByRole(@PathVariable String role) { public Set<MenuVo> findMenuByRole(@PathVariable String role) {
return menuService.findMenuByRole(role); return menuService.findMenuByRole(role, 0);
} }
/** /**
@ -54,9 +53,9 @@ public class MenuController extends BaseController {
* *
* @return 树形菜单 * @return 树形菜单
*/ */
@GetMapping(value = "/userTree") @GetMapping("/userTree/{type}")
public List<Integer> userTree(HttpServletRequest request) { public List<Integer> userTree(@PathVariable Integer type) {
Set<MenuVo> menus = menuService.findMenuByRole(getRole().get(0)); Set<MenuVo> menus = menuService.findMenuByRole(getRole().get(0), type);
List<Integer> menuList = new ArrayList<>(); List<Integer> menuList = new ArrayList<>();
for (MenuVo menuVo : menus) { for (MenuVo menuVo : menus) {
menuList.add(menuVo.getMenuId()); menuList.add(menuVo.getMenuId());
@ -64,7 +63,6 @@ public class MenuController extends BaseController {
return menuList; return menuList;
} }
/** /**
* 通过ID查询菜单的详细信息 * 通过ID查询菜单的详细信息
* *
@ -123,11 +121,13 @@ public class MenuController extends BaseController {
node = new MenuTree(); node = new MenuTree();
node.setId(menu.getMenuId()); node.setId(menu.getMenuId());
node.setParentId(menu.getParentId()); node.setParentId(menu.getParentId());
node.setTitle(menu.getName()); node.setName(menu.getName());
node.setHref(menu.getUrl()); node.setUrl(menu.getUrl());
node.setPath(menu.getUrl()); node.setPath(menu.getPath());
node.setCode(menu.getPermission()); node.setCode(menu.getPermission());
node.setLabel(menu.getName()); node.setLabel(menu.getName());
node.setComponent(menu.getComponent());
node.setIcon(menu.getIcon());
trees.add(node); trees.add(node);
} }
return TreeUtil.bulid(trees, root); return TreeUtil.bulid(trees, root);

View File

@ -6,8 +6,8 @@ package com.github.pig.admin.dto;
*/ */
public class MenuTree extends TreeNode { public class MenuTree extends TreeNode {
private String icon; private String icon;
private String title; private String name;
private String href; private String url;
private boolean spread = false; private boolean spread = false;
private String path; private String path;
private String component; private String component;
@ -72,14 +72,14 @@ public class MenuTree extends TreeNode {
public MenuTree(int id, String name, int parentId) { public MenuTree(int id, String name, int parentId) {
this.id = id; this.id = id;
this.parentId = parentId; this.parentId = parentId;
this.title = name; this.name = name;
this.label = name; this.label = name;
} }
public MenuTree(int id, String name, MenuTree parent) { public MenuTree(int id, String name, MenuTree parent) {
this.id = id; this.id = id;
this.parentId = parent.getId(); this.parentId = parent.getId();
this.title = name; this.name = name;
this.label = name; this.label = name;
} }
@ -91,20 +91,20 @@ public class MenuTree extends TreeNode {
this.icon = icon; this.icon = icon;
} }
public String getTitle() { public String getName() {
return title; return name;
} }
public void setTitle(String title) { public void setName(String name) {
this.title = title; this.name = name;
} }
public String getHref() { public String getUrl() {
return href; return url;
} }
public void setHref(String href) { public void setUrl(String url) {
this.href = href; this.url = url;
} }
public boolean isSpread() { public boolean isSpread() {

View File

@ -82,6 +82,10 @@ public class SysMenu extends Model<SysMenu> {
*/ */
@TableField("del_flag") @TableField("del_flag")
private String delFlag; private String delFlag;
/**
* 前端URL
*/
private String path;
public Integer getMenuId() { public Integer getMenuId() {
@ -188,6 +192,14 @@ public class SysMenu extends Model<SysMenu> {
this.delFlag = delFlag; this.delFlag = delFlag;
} }
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
@Override @Override
protected Serializable pkVal() { protected Serializable pkVal() {
return this.menuId; return this.menuId;

View File

@ -3,6 +3,7 @@ package com.github.pig.admin.mapper;
import com.baomidou.mybatisplus.mapper.BaseMapper; import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.github.pig.admin.entity.SysMenu; import com.github.pig.admin.entity.SysMenu;
import com.github.pig.common.vo.MenuVo; import com.github.pig.common.vo.MenuVo;
import org.apache.ibatis.annotations.Param;
import java.util.Set; import java.util.Set;
@ -20,7 +21,8 @@ public interface SysMenuMapper extends BaseMapper<SysMenu> {
* 通过角色名查询菜单 * 通过角色名查询菜单
* *
* @param role 角色名称 * @param role 角色名称
* @param type 是不是查询全部
* @return 菜单列表 * @return 菜单列表
*/ */
Set<MenuVo> findMenuByRoleName(String role); Set<MenuVo> findMenuByRoleName(@Param("role") String role, @Param("type") Integer type);
} }

View File

@ -20,9 +20,10 @@ public interface SysMenuService extends IService<SysMenu> {
* 通过角色名称查询URL 权限 * 通过角色名称查询URL 权限
* *
* @param role 角色名称 * @param role 角色名称
* @param type 是否全部
* @return 菜单列表 * @return 菜单列表
*/ */
Set<MenuVo> findMenuByRole(String role); Set<MenuVo> findMenuByRole(String role, Integer type);
/** /**
* 通过角色获取菜单权限列表 * 通过角色获取菜单权限列表

View File

@ -7,7 +7,6 @@ import com.github.pig.admin.service.SysMenuService;
import com.github.pig.common.vo.MenuVo; import com.github.pig.common.vo.MenuVo;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.HashSet; import java.util.HashSet;
@ -27,16 +26,16 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
private SysMenuMapper sysMenuMapper; private SysMenuMapper sysMenuMapper;
@Override @Override
@Cacheable(value = "menu_details", key = "#role +'_menu'") //@Cacheable(value = "menu_details", key = "#role +'_menu'")
public Set<MenuVo> findMenuByRole(String role) { public Set<MenuVo> findMenuByRole(String role, Integer type) {
return sysMenuMapper.findMenuByRoleName(role); return sysMenuMapper.findMenuByRoleName(role, type);
} }
@Override @Override
public String[] findPermission(String[] roles) { public String[] findPermission(String[] roles) {
Set<MenuVo> menuVoSet = new HashSet<>(); Set<MenuVo> menuVoSet = new HashSet<>();
for (String role : roles) { for (String role : roles) {
Set<MenuVo> menuVos = findMenuByRole(role); Set<MenuVo> menuVos = findMenuByRole(role, 0);
menuVoSet.addAll(menuVos); menuVoSet.addAll(menuVos);
} }

View File

@ -8,6 +8,7 @@
<result column="name" property="name" /> <result column="name" property="name" />
<result column="permission" property="permission" /> <result column="permission" property="permission" />
<result column="url" property="url" /> <result column="url" property="url" />
<result column="path" property="path" />
<result column="method" property="method" /> <result column="method" property="method" />
<result column="parent_id" property="parentId" /> <result column="parent_id" property="parentId" />
<result column="icon" property="icon" /> <result column="icon" property="icon" />
@ -45,7 +46,9 @@
WHERE WHERE
sys_role.del_flag = 0 sys_role.del_flag = 0
AND sys_menu.del_flag = 0 AND sys_menu.del_flag = 0
AND sys_menu.url IS NOT NULL AND AND sys_role.role_code = #{role}
sys_role.role_code = #{role} <if test="type == 0">
AND sys_menu.url IS NOT NULL
</if>
</select> </select>
</mapper> </mapper>