优秀的编程知识分享平台

网站首页 > 技术文章 正文

从零开始码后台管理系统——角色管理实现

nanyue 2024-08-22 17:33:10 技术文章 5 ℃

实现了菜单管理以后,就可以搞角色管理了。角色管理包含两部分,角色以及角色和菜单的对应。首先在后台新建这两部分的entity,mapper,service以及serviceImpl,代码如下:

@Data
@TableName(value = "sys_role")
public class SysRoleEntity {

    @TableId(type = IdType.AUTO)
    private Long id;

    private Long parentId;

    private String label;

    private String alias;

    private Long sort;

    private LocalDateTime createTime;

    @TableField(fill = FieldFill.INSERT)
    private Long createBy;

    private LocalDateTime updateTime;

    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Long updateBy;
}

@Mapper
public interface SysRoleMapper extends BaseMapper<SysRoleEntity> {
}

public interface SysRoleService extends IService<SysRoleEntity> {

    List<Tree<Long>> treeList();
}

@Service("sysRoleService")
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRoleEntity> implements SysRoleService {

    @Override
    public List<Tree<Long>> treeList() {
        List<SysRoleEntity> list = baseMapper.selectList(null);

        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        treeNodeConfig.setNameKey("label");
        treeNodeConfig.setWeightKey("sort");

        List<Tree<Long>> treeNodes = TreeUtil.build(list,null,treeNodeConfig,((roleEntity, tree) -> {
            tree.setId(roleEntity.getId());
            tree.setParentId(null != roleEntity.getParentId() ? roleEntity.getParentId():null);
            tree.setName(roleEntity.getLabel());
            tree.setWeight(roleEntity.getSort());

            tree.putExtra("alias",roleEntity.getAlias());
        }));
        return treeNodes;
    }
}


@Data
@TableName(value = "sys_role_menu")
public class SysRoleMenuEntity {

    @TableId(type = IdType.AUTO)
    private Long id;

    private Long roleId;

    private Long menuId;
}

@Mapper
public interface SysRoleMenuMapper extends BaseMapper<SysRoleMenuEntity> {
}

public interface SysRoleMenuService extends IService<SysRoleMenuEntity> {

    void updateMenusByRoleId(Long roleId, List<Long> menuIds);
}

@Service("sysRoleMenuService")
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenuEntity> implements SysRoleMenuService {

    @Override
    public void updateMenusByRoleId(Long roleId, List<Long> menuIds) {
        this.baseMapper.delete(new LambdaQueryWrapper<SysRoleMenuEntity>()
                .eq(SysRoleMenuEntity::getRoleId,roleId)
        );

        List<SysRoleMenuEntity> saveList = menuIds.stream().map(item -> {
            SysRoleMenuEntity entity = new SysRoleMenuEntity();
            entity.setRoleId(roleId);
            entity.setMenuId(item);
            return entity;
        }).collect(Collectors.toList());

        this.saveBatch(saveList);
    }
}

新建Controller

@RestController
@RequestMapping("/system/role")
public class SysRoleController {

    @Autowired
    private SysRoleService roleService;

    @Autowired
    private SysRoleMenuService roleMenuService;

    @GetMapping("/list")
    public SaResult list(){
        List<Tree<Long>> treeList = roleService.treeList();

        return SaResult.data(treeList);
    }

    @GetMapping("/menus/{roleId}")
    public SaResult menus(@PathVariable("roleId")Long roleId){
        List<SysRoleMenuEntity> list = roleMenuService.list(new LambdaQueryWrapper<SysRoleMenuEntity>()
                .eq(SysRoleMenuEntity::getRoleId, roleId)
        );

        return SaResult.data(list.stream().map(item->item.getMenuId()).collect(Collectors.toList()));
    }

    @PostMapping("/menusUpdate/{roleId}")
    public SaResult menusUpdate(@PathVariable("roleId")Long roleId,@RequestBody List<Long> menuIds){
        roleMenuService.updateMenusByRoleId(roleId,menuIds);
        return SaResult.ok();
    }

    @PostMapping("save")
    public SaResult save(@RequestBody SysRoleEntity roleEntity){
        roleService.save(roleEntity);
        return SaResult.ok();
    }

    @PostMapping("update")
    public SaResult update(@RequestBody SysRoleEntity roleEntity){
        roleService.updateById(roleEntity);
        return SaResult.ok();
    }

    @PostMapping("delete")
    public SaResult delete(@RequestBody Long[] ids){
        roleService.removeByIds(Arrays.asList(ids));
        return SaResult.ok();
    }

}

前端新增接口

role: {
		list: {
			url: `${config.API_URL}/system/role/list`,
			name: "获取角色列表",
			get: async function(params){
				return await http.get(this.url, params);
			}
		},
		menus: {
			url: `${config.API_URL}/system/role/menus/`,
			name: "获取角色菜单列表",
			get: async function(roleId){
				return await http.get(this.url + roleId);
			}
		},
		menusUpdate: {
			url: `${config.API_URL}/system/role/menusUpdate/`,
			name: "获取角色菜单列表",
			post: async function(roleId,menuIds){
				return await http.post(this.url + roleId,menuIds);
			}
		},
		save: {
			url: `${config.API_URL}/system/role/save`,
			name: "保存角色",
			post: async function(params){
				return await http.post(this.url,params);
			}
		},
		update: {
			url: `${config.API_URL}/system/role/update`,
			name: "修改角色",
			post: async function(params){
				return await http.post(this.url,params);
			}
		},
		delete: {
			url: `${config.API_URL}/system/role/delete`,
			name: "删除角色",
			post: async function(params){
				return await http.post(this.url,params);
			}
		}
	},

再修改对应接口引用,经过测试一切ok

只设置了菜单权限,其他权限后续有用到的功能再去实现。

Tags:

最近发表
标签列表