@@ -0,0 +1,31 @@ | |||
package com.tuoheng.miniprogram.controller; | |||
import com.tuoheng.common.core.utils.JsonResult; | |||
import com.tuoheng.miniprogram.entity.query.RoleClientQuery; | |||
import com.tuoheng.miniprogram.service.OpPermissionsService; | |||
import org.springframework.beans.factory.annotation.Autowired; | |||
import org.springframework.web.bind.annotation.GetMapping; | |||
import org.springframework.web.bind.annotation.RequestMapping; | |||
import org.springframework.web.bind.annotation.RestController; | |||
/** | |||
* @Author ChengWang | |||
* @Date 2022/12/23 | |||
*/ | |||
@RestController | |||
@RequestMapping("/permission") | |||
public class PermissionController { | |||
@Autowired | |||
private OpPermissionsService permissionsService; | |||
/** | |||
* 根据 RoleId 获取用户权限 | |||
* | |||
* @return | |||
*/ | |||
@GetMapping("/getRolePermission") | |||
public JsonResult getMenuAndPermissionByRoleId(RoleClientQuery query) { | |||
return permissionsService.getRolePermissionByRoleId(query); | |||
} | |||
} |
@@ -0,0 +1,19 @@ | |||
package com.tuoheng.miniprogram.dao; | |||
import com.baomidou.mybatisplus.core.mapper.BaseMapper; | |||
import com.tuoheng.miniprogram.entity.Menu; | |||
import org.apache.ibatis.annotations.Param; | |||
import java.util.List; | |||
/** | |||
* @Author ChengWang | |||
* @Date 2022/12/16 | |||
*/ | |||
public interface MenuMapper extends BaseMapper<Menu> { | |||
List<Menu> getOpMenusByRoleId(@Param("roleId") Integer roleId,@Param("clientId") String clientId); | |||
List<Menu> getChildrenMenuByPid(@Param("parentId") Integer id,@Param("clientId") String clientId); | |||
} |
@@ -0,0 +1,18 @@ | |||
package com.tuoheng.miniprogram.dao; | |||
import com.baomidou.mybatisplus.core.mapper.BaseMapper; | |||
import com.tuoheng.miniprogram.entity.OpPermissions; | |||
import org.apache.ibatis.annotations.Param; | |||
import java.util.List; | |||
/** | |||
* @Author ChengWang | |||
* @Date 2022/12/16 | |||
*/ | |||
public interface OpPermissionsMapper extends BaseMapper<OpPermissions> { | |||
List<OpPermissions> getOpPermissionsByRoleId(@Param("roleId") Integer roleId, @Param("clientId") String clientId); | |||
List<Integer> getRoleIdsByApiUrl(@Param("apiUrl") String apiUrl); | |||
} |
@@ -0,0 +1,12 @@ | |||
package com.tuoheng.miniprogram.dao; | |||
import com.baomidou.mybatisplus.core.mapper.BaseMapper; | |||
import com.tuoheng.miniprogram.entity.RoleMenu; | |||
/** | |||
* @Author ChengWang | |||
* @Date 2022/12/16 | |||
*/ | |||
public interface RoleMenuMapper extends BaseMapper<RoleMenu> { | |||
} |
@@ -0,0 +1,134 @@ | |||
package com.tuoheng.miniprogram.entity; | |||
import com.baomidou.mybatisplus.annotation.IdType; | |||
import com.baomidou.mybatisplus.annotation.TableField; | |||
import com.baomidou.mybatisplus.annotation.TableId; | |||
import com.baomidou.mybatisplus.annotation.TableName; | |||
import com.fasterxml.jackson.annotation.JsonFormat; | |||
import lombok.Data; | |||
import lombok.experimental.Accessors; | |||
import org.springframework.format.annotation.DateTimeFormat; | |||
import java.util.Date; | |||
import java.util.List; | |||
/** | |||
* <p> | |||
* 系统菜单表 | |||
* </p> | |||
* | |||
* @author 拓恒 | |||
* @since 2020-10-30 | |||
*/ | |||
@Data | |||
@Accessors(chain = true) | |||
@TableName("op_menus") | |||
public class Menu { | |||
private static final long serialVersionUID = 1L; | |||
/** | |||
* 主键ID | |||
*/ | |||
@TableId(value = "id", type = IdType.AUTO) | |||
private Integer id; | |||
/** | |||
* 父级ID | |||
*/ | |||
private Integer parentId; | |||
/** | |||
* 菜单名称 | |||
*/ | |||
private String name; | |||
/** | |||
* 菜单所属client | |||
*/ | |||
private String clientId; | |||
/** | |||
* 页面跳转方式 | |||
*/ | |||
private String target; | |||
/** | |||
* 图标 | |||
*/ | |||
private String menuIcon; | |||
/** | |||
* 菜单路径 | |||
*/ | |||
private String path; | |||
/** | |||
* 菜单组件 | |||
*/ | |||
private String component; | |||
/** | |||
* 1正常 2禁用 | |||
*/ | |||
private Integer status; | |||
/** | |||
* 0:显示;1:隐藏 | |||
*/ | |||
private Integer isHidden; | |||
/** | |||
* 显示顺序 | |||
*/ | |||
private Integer sort; | |||
/** | |||
* 添加人 | |||
*/ | |||
private String createUser; | |||
/** | |||
* 创建时间 | |||
*/ | |||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") | |||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") | |||
private Date createTime; | |||
/** | |||
* 更新人 | |||
*/ | |||
private String updateUser; | |||
/** | |||
* 更新时间 | |||
*/ | |||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") | |||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") | |||
private Date updateTime; | |||
/** | |||
* 有效标识 | |||
*/ | |||
private Integer mark; | |||
/** | |||
* 子级菜单 | |||
*/ | |||
@TableField(exist = false) | |||
private List<Menu> children; | |||
/** | |||
* 是否选中 | |||
*/ | |||
@TableField(exist = false) | |||
private boolean checked; | |||
/** | |||
* 是否打开 | |||
*/ | |||
@TableField(exist = false) | |||
private boolean open; | |||
/** | |||
* 权限节点参数 | |||
*/ | |||
@TableField(exist = false) | |||
private Integer[] checkedList; | |||
} |
@@ -0,0 +1,106 @@ | |||
package com.tuoheng.miniprogram.entity; | |||
import com.baomidou.mybatisplus.annotation.IdType; | |||
import com.baomidou.mybatisplus.annotation.TableField; | |||
import com.baomidou.mybatisplus.annotation.TableId; | |||
import com.baomidou.mybatisplus.annotation.TableName; | |||
import com.fasterxml.jackson.annotation.JsonFormat; | |||
import lombok.Data; | |||
import org.springframework.format.annotation.DateTimeFormat; | |||
import java.util.Date; | |||
/** | |||
* 权限配置表 | |||
* @TableName op_permissions | |||
*/ | |||
@TableName(value ="op_permissions") | |||
@Data | |||
public class OpPermissions { | |||
private static final long serialVersionUID = 1L; | |||
/** | |||
* 主键ID | |||
*/ | |||
@TableId(value = "id", type = IdType.AUTO) | |||
private Integer id; | |||
/** | |||
* 所属client | |||
*/ | |||
private String clientId; | |||
/** | |||
* code标识 | |||
*/ | |||
private String code; | |||
/** | |||
* 展示name | |||
*/ | |||
private String name; | |||
/** | |||
* 备注信息 | |||
*/ | |||
private String remark; | |||
/** | |||
* 0:菜单项action-permissions; | |||
*/ | |||
private Integer type; | |||
/** | |||
* 关联菜单id | |||
*/ | |||
private Integer menuId; | |||
/** | |||
* 接口路径 | |||
*/ | |||
private String apiUrl; | |||
/** | |||
* 添加人 | |||
*/ | |||
private String createUser; | |||
/** | |||
* 创建时间 | |||
*/ | |||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") | |||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") | |||
private Date createTime; | |||
/** | |||
* 更新人 | |||
*/ | |||
private String updateUser; | |||
/** | |||
* 更新时间 | |||
*/ | |||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") | |||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") | |||
private Date updateTime; | |||
/** | |||
* 有效标识 | |||
*/ | |||
private Integer mark; | |||
/** | |||
* 是否选中 | |||
*/ | |||
@TableField(exist = false) | |||
private boolean checked; | |||
/** | |||
* 是否打开 | |||
*/ | |||
@TableField(exist = false) | |||
private boolean open; | |||
} |
@@ -0,0 +1,74 @@ | |||
package com.tuoheng.miniprogram.entity; | |||
import com.baomidou.mybatisplus.annotation.IdType; | |||
import com.baomidou.mybatisplus.annotation.TableId; | |||
import com.baomidou.mybatisplus.annotation.TableName; | |||
import com.fasterxml.jackson.annotation.JsonFormat; | |||
import lombok.Data; | |||
import lombok.experimental.Accessors; | |||
import org.springframework.format.annotation.DateTimeFormat; | |||
import java.util.Date; | |||
/** | |||
* <p> | |||
* 角色菜单关联表 | |||
* </p> | |||
* | |||
* @author 拓恒 | |||
* @since 2020-10-30 | |||
*/ | |||
@Data | |||
@Accessors(chain = true) | |||
@TableName("op_role_menu") | |||
public class RoleMenu { | |||
private static final long serialVersionUID = 1L; | |||
/** | |||
* 主键ID | |||
*/ | |||
@TableId(value = "id", type = IdType.AUTO) | |||
private Integer id; | |||
/** | |||
* 角色ID | |||
*/ | |||
private Integer roleId; | |||
/** | |||
* 菜单ID | |||
*/ | |||
private Integer menuId; | |||
/** | |||
* 添加人 | |||
*/ | |||
private String createUser; | |||
/** | |||
* 创建时间 | |||
*/ | |||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") | |||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") | |||
private Date createTime; | |||
/** | |||
* 更新人 | |||
*/ | |||
private String updateUser; | |||
/** | |||
* 更新时间 | |||
*/ | |||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") | |||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") | |||
private Date updateTime; | |||
/** | |||
* 有效标识 | |||
*/ | |||
private Integer mark; | |||
} |
@@ -0,0 +1,37 @@ | |||
package com.tuoheng.miniprogram.entity.dto; | |||
import com.tuoheng.miniprogram.entity.Menu; | |||
import com.tuoheng.miniprogram.entity.OpPermissions; | |||
import lombok.AllArgsConstructor; | |||
import lombok.Data; | |||
import lombok.NoArgsConstructor; | |||
import lombok.experimental.Accessors; | |||
import java.util.List; | |||
/** | |||
* @author chenjiandong | |||
* @description: TODO | |||
* @date 2022/11/16 14:55 | |||
*/ | |||
@Data | |||
@AllArgsConstructor | |||
@NoArgsConstructor | |||
@Accessors(chain = true) | |||
public class RolePermissionDto { | |||
/** | |||
* 角色id | |||
*/ | |||
private Integer roleId; | |||
/** | |||
*对应的菜单集合->及子菜单(按钮) | |||
*/ | |||
private List<Menu> opMenusList; | |||
/** | |||
* 对应的权限集合 | |||
*/ | |||
private List<OpPermissions> permissionsList; | |||
} |
@@ -0,0 +1,21 @@ | |||
package com.tuoheng.miniprogram.entity.query; | |||
import com.tuoheng.common.core.common.BaseQuery; | |||
import lombok.Data; | |||
/** | |||
* @Author xiaoying | |||
* @Date 2022/11/25 17:56 | |||
*/ | |||
@Data | |||
public class RoleClientQuery extends BaseQuery { | |||
/** | |||
* 角色id | |||
*/ | |||
private Integer roleId; | |||
/** | |||
* 标识 | |||
*/ | |||
private String clientId; | |||
} |
@@ -0,0 +1,14 @@ | |||
package com.tuoheng.miniprogram.service; | |||
import com.baomidou.mybatisplus.extension.service.IService; | |||
import com.tuoheng.common.core.utils.JsonResult; | |||
import com.tuoheng.miniprogram.entity.OpPermissions; | |||
import com.tuoheng.miniprogram.entity.query.RoleClientQuery; | |||
/** | |||
* @Author ChengWang | |||
* @Date 2022/12/23 | |||
*/ | |||
public interface OpPermissionsService extends IService<OpPermissions> { | |||
JsonResult getRolePermissionByRoleId(RoleClientQuery query); | |||
} |
@@ -0,0 +1,88 @@ | |||
package com.tuoheng.miniprogram.service.impl; | |||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | |||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | |||
import com.tuoheng.common.core.utils.JsonResult; | |||
import com.tuoheng.common.core.utils.StringUtils; | |||
import com.tuoheng.miniprogram.dao.MenuMapper; | |||
import com.tuoheng.miniprogram.dao.OpPermissionsMapper; | |||
import com.tuoheng.miniprogram.dao.RoleMenuMapper; | |||
import com.tuoheng.miniprogram.entity.Menu; | |||
import com.tuoheng.miniprogram.entity.OpPermissions; | |||
import com.tuoheng.miniprogram.entity.RoleMenu; | |||
import com.tuoheng.miniprogram.entity.dto.RolePermissionDto; | |||
import com.tuoheng.miniprogram.entity.query.RoleClientQuery; | |||
import com.tuoheng.miniprogram.service.OpPermissionsService; | |||
import lombok.extern.slf4j.Slf4j; | |||
import org.springframework.beans.factory.annotation.Autowired; | |||
import org.springframework.stereotype.Service; | |||
import java.util.List; | |||
import java.util.stream.Collectors; | |||
/** | |||
* @Author ChengWang | |||
* @Date 2022/12/23 | |||
*/ | |||
@Service | |||
@Slf4j | |||
public class OpPermissionsServiceImpl extends ServiceImpl<OpPermissionsMapper, OpPermissions> implements OpPermissionsService { | |||
@Autowired | |||
private MenuMapper menuMapper; | |||
@Autowired | |||
private OpPermissionsMapper opPermissionsMapper; | |||
@Autowired | |||
private RoleMenuMapper roleMenuMapper; | |||
/** | |||
* 根据 RoleId 获取用户权限 | |||
* @param query | |||
* @return | |||
*/ | |||
@Override | |||
public JsonResult getRolePermissionByRoleId(RoleClientQuery query) { | |||
RolePermissionDto rolePermissionDto = new RolePermissionDto(); | |||
//根据角色id查询出对应的菜单列表 -> 无父级 | |||
List<Menu> opMenusDtoList = menuMapper.getOpMenusByRoleId(query.getRoleId(), query.getClientId()); | |||
for (Menu opMenusDto : opMenusDtoList) { | |||
//根据父级id查询出对应所属的菜单 | |||
extracted(query, opMenusDto); | |||
} | |||
rolePermissionDto.setRoleId(query.getRoleId()) | |||
.setOpMenusList(opMenusDtoList) | |||
//根据角色id查询出对应的权限集合 | |||
.setPermissionsList(opPermissionsMapper.getOpPermissionsByRoleId(query.getRoleId(), query.getClientId())); | |||
return JsonResult.success(rolePermissionDto); | |||
} | |||
/** | |||
* 封装方法 递归菜单 层级 | |||
* | |||
* @param query | |||
* @param item | |||
*/ | |||
private void extracted(RoleClientQuery query, Menu item) { | |||
List<Menu> list = menuMapper.getChildrenMenuByPid(item.getId(), query.getClientId()); | |||
if (StringUtils.isNotEmpty(list)) { | |||
QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>(); | |||
queryWrapper.eq("role_id", query.getRoleId()); | |||
queryWrapper.select("menu_id"); | |||
List<RoleMenu> roleMenuList = roleMenuMapper.selectList(queryWrapper); | |||
List<Integer> menuIds = roleMenuList.stream().map(p -> p.getMenuId()).collect(Collectors.toList()); | |||
List<Menu> collect = list.stream().map(t -> { | |||
if (menuIds.contains(t.getId())) { | |||
t.setChecked(true); | |||
t.setOpen(true); | |||
} | |||
extracted(query, t); | |||
return t; | |||
}).filter(t -> t.isChecked() == true).collect(Collectors.toList()); | |||
item.setChildren(collect); | |||
} | |||
} | |||
} |
@@ -0,0 +1,50 @@ | |||
<?xml version="1.0" encoding="UTF-8" ?> | |||
<!DOCTYPE mapper | |||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | |||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | |||
<mapper namespace="com.tuoheng.miniprogram.dao.MenuMapper"> | |||
<resultMap id="BaseResultMap" type="com.tuoheng.miniprogram.entity.Menu"> | |||
<id column="id" jdbcType="INTEGER" property="id"/> | |||
<result column="create_user" jdbcType="INTEGER" property="createUser"/> | |||
<result column="create_time" jdbcType="TIMESTAMP" property="createTime"/> | |||
<result column="update_user" jdbcType="INTEGER" property="updateUser"/> | |||
<result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/> | |||
<result column="mark" jdbcType="BOOLEAN" property="mark"/> | |||
<result column="parent_id" jdbcType="INTEGER" property="parentId"/> | |||
<result column="name" jdbcType="VARCHAR" property="name"/> | |||
<result column="client_id" jdbcType="VARCHAR" property="clientId"/> | |||
<result column="menu_icon" jdbcType="VARCHAR" property="menuIcon"/> | |||
<result column="path" jdbcType="VARCHAR" property="path"/> | |||
<result column="component" jdbcType="VARCHAR" property="component"/> | |||
<result column="target" jdbcType="VARCHAR" property="target"/> | |||
<result column="is_hidden" jdbcType="INTEGER" property="isHidden"/> | |||
<result column="sort" jdbcType="INTEGER" property="sort"/> | |||
<result column="status" jdbcType="INTEGER" property="status"/> | |||
</resultMap> | |||
<sql id="Base_Column_List"> | |||
id, create_user, create_time, update_user, update_time, mark, parent_id, name, client_id, menu_icon, path, | |||
component, is_hidden, sort | |||
</sql> | |||
<!--根据角色id获取对应的菜单集合 无父级--> | |||
<select id="getOpMenusByRoleId" resultType="com.tuoheng.miniprogram.entity.Menu"> | |||
select | |||
<include refid="Base_Column_List"/> | |||
from op_menus | |||
where mark = 1 and parent_id = 0 and client_id = #{clientId} | |||
and status = 1 | |||
and id in (select menu_id from op_role_menu where role_id = #{roleId} and mark = 1) | |||
order by sort asc | |||
</select> | |||
<!--根据父级id查询所属对应的菜单列表--> | |||
<select id="getChildrenMenuByPid" resultType="com.tuoheng.miniprogram.entity.Menu"> | |||
select | |||
<include refid="Base_Column_List"/> | |||
from op_menus | |||
where mark = 1 and status = 1 and parent_id = #{parentId} and client_id = #{clientId} | |||
order by sort asc | |||
</select> | |||
</mapper> |
@@ -0,0 +1,49 @@ | |||
<?xml version="1.0" encoding="UTF-8" ?> | |||
<!DOCTYPE mapper | |||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | |||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | |||
<mapper namespace="com.tuoheng.miniprogram.dao.OpPermissionsMapper"> | |||
<resultMap id="BaseResultMap" type="com.tuoheng.miniprogram.entity.OpPermissions"> | |||
<id property="id" column="id" jdbcType="INTEGER"/> | |||
<result property="createTime" column="create_time" jdbcType="TIMESTAMP"/> | |||
<result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/> | |||
<result property="createUser" column="create_user" jdbcType="INTEGER"/> | |||
<result property="updateUser" column="update_user" jdbcType="INTEGER"/> | |||
<result property="mark" column="mark" jdbcType="TINYINT"/> | |||
<result property="clientId" column="client_id" jdbcType="VARCHAR"/> | |||
<result property="code" column="code" jdbcType="VARCHAR"/> | |||
<result property="name" column="name" jdbcType="VARCHAR"/> | |||
<result property="remark" column="remark" jdbcType="VARCHAR"/> | |||
<result property="type" column="type" jdbcType="INTEGER"/> | |||
<result property="menuId" column="menu_id" jdbcType="INTEGER"/> | |||
<result property="apiUrl" column="api_url" jdbcType="VARCHAR"/> | |||
</resultMap> | |||
<sql id="Base_Column_List"> | |||
id,create_time,update_time, | |||
create_user,update_user,mark, | |||
client_id,code,name, | |||
remark,type,menu_id, | |||
api_url | |||
</sql> | |||
<select id="getOpPermissionsByRoleId" resultType="com.tuoheng.miniprogram.entity.OpPermissions"> | |||
select | |||
<include refid="Base_Column_List"/> | |||
from op_permissions | |||
where mark = 1 | |||
<if test="clientId !=null and clientId != '' "> | |||
and client_id =#{clientId} | |||
</if> | |||
and id in (select permission_id from op_role_permission where role_id = #{roleId} and mark = 1) | |||
</select> | |||
<!--根据接口url获取所属角色--> | |||
<select id="getRoleIdsByApiUrl" parameterType="java.lang.String" resultType="java.lang.Integer"> | |||
select role_id | |||
from op_role_permission | |||
where mark = 1 | |||
and permission_id in (select id from op_permissions where api_url = #{apiUrl} and mark = 1) | |||
</select> | |||
</mapper> |
@@ -0,0 +1,6 @@ | |||
<?xml version="1.0" encoding="UTF-8"?> | |||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | |||
<mapper namespace="com.tuoheng.miniprogram.dao.RoleMenuMapper"> | |||
</mapper> |