Search in sources :

Example 1 with CategoryMenu

use of com.moxi.mogublog.commons.entity.CategoryMenu in project mogu_blog_v2 by moxi624.

the class AuthorityVerifyAspect method doAround.

@Around(value = "pointcut(authorityVerify)")
public Object doAround(ProceedingJoinPoint joinPoint, AuthorityVerify authorityVerify) throws Throwable {
    ServletRequestAttributes attribute = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    HttpServletRequest request = attribute.getRequest();
    // 获取请求路径
    String url = request.getRequestURI();
    // 解析出请求者的ID和用户名
    String adminUid = request.getAttribute(SysConf.ADMIN_UID).toString();
    // 管理员能够访问的路径
    String visitUrlStr = redisUtil.get(RedisConf.ADMIN_VISIT_MENU + RedisConf.SEGMENTATION + adminUid);
    LinkedTreeMap<String, String> visitMap = new LinkedTreeMap<>();
    if (StringUtils.isNotEmpty(visitUrlStr)) {
        // 从Redis中获取
        visitMap = (LinkedTreeMap<String, String>) JsonUtils.jsonToMap(visitUrlStr, String.class);
    } else {
        // 查询数据库获取
        Admin admin = adminService.getById(adminUid);
        String roleUid = admin.getRoleUid();
        Role role = roleService.getById(roleUid);
        String caetgoryMenuUids = role.getCategoryMenuUids();
        String[] uids = caetgoryMenuUids.replace("[", "").replace("]", "").replace("\"", "").split(",");
        List<String> categoryMenuUids = new ArrayList<>(Arrays.asList(uids));
        // 这里只需要查询访问的按钮
        QueryWrapper<CategoryMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(SQLConf.UID, categoryMenuUids);
        queryWrapper.eq(SQLConf.MENU_TYPE, EMenuType.BUTTON);
        queryWrapper.eq(SQLConf.STATUS, EStatus.ENABLE);
        List<CategoryMenu> buttonList = categoryMenuService.list(queryWrapper);
        for (CategoryMenu item : buttonList) {
            if (StringUtils.isNotEmpty(item.getUrl())) {
                visitMap.put(item.getUrl(), item.getUrl());
            }
        }
        // 将访问URL存储到Redis中
        redisUtil.setEx(RedisConf.ADMIN_VISIT_MENU + SysConf.REDIS_SEGMENTATION + adminUid, JsonUtils.objectToJson(visitMap), 1, TimeUnit.HOURS);
    }
    // 判断该角色是否能够访问该接口
    if (visitMap.get(url) != null) {
        log.info("用户拥有操作权限,访问的路径: {},拥有的权限接口:{}", url, visitMap.get(url));
        // 执行业务
        return joinPoint.proceed();
    } else {
        log.info("用户不具有操作权限,访问的路径: {}", url);
        return ResultUtil.result(ECode.NO_OPERATION_AUTHORITY, MessageConf.RESTAPI_NO_PRIVILEGE);
    }
}
Also used : LinkedTreeMap(com.google.gson.internal.LinkedTreeMap) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) ArrayList(java.util.ArrayList) Admin(com.moxi.mogublog.commons.entity.Admin) HttpServletRequest(javax.servlet.http.HttpServletRequest) Role(com.moxi.mogublog.commons.entity.Role) CategoryMenu(com.moxi.mogublog.commons.entity.CategoryMenu) Around(org.aspectj.lang.annotation.Around)

Example 2 with CategoryMenu

use of com.moxi.mogublog.commons.entity.CategoryMenu in project mogu_blog_v2 by moxi624.

the class LoginRestApi method getMenu.

@ApiOperation(value = "获取当前用户的菜单", notes = "获取当前用户的菜单", response = String.class)
@GetMapping(value = "/getMenu")
public String getMenu(HttpServletRequest request) {
    Collection<CategoryMenu> categoryMenuList = new ArrayList<>();
    Admin admin = adminService.getById(request.getAttribute(SysConf.ADMIN_UID).toString());
    List<String> roleUid = new ArrayList<>();
    roleUid.add(admin.getRoleUid());
    Collection<Role> roleList = roleService.listByIds(roleUid);
    List<String> categoryMenuUids = new ArrayList<>();
    roleList.forEach(item -> {
        String caetgoryMenuUids = item.getCategoryMenuUids();
        String[] uids = caetgoryMenuUids.replace("[", "").replace("]", "").replace("\"", "").split(",");
        categoryMenuUids.addAll(Arrays.asList(uids));
    });
    categoryMenuList = categoryMenuService.listByIds(categoryMenuUids);
    // 从三级级分类中查询出 二级分类
    List<CategoryMenu> buttonList = new ArrayList<>();
    Set<String> secondMenuUidList = new HashSet<>();
    categoryMenuList.forEach(item -> {
        // 查询二级分类
        if (item.getMenuType() == EMenuType.MENU && item.getMenuLevel() == SysConf.TWO) {
            secondMenuUidList.add(item.getUid());
        }
        // 从三级分类中,得到二级分类
        if (item.getMenuType() == EMenuType.BUTTON && StringUtils.isNotEmpty(item.getParentUid())) {
            // 找出二级菜单
            secondMenuUidList.add(item.getParentUid());
            // 找出全部按钮
            buttonList.add(item);
        }
    });
    Collection<CategoryMenu> childCategoryMenuList = new ArrayList<>();
    Collection<CategoryMenu> parentCategoryMenuList = new ArrayList<>();
    List<String> parentCategoryMenuUids = new ArrayList<>();
    if (secondMenuUidList.size() > 0) {
        childCategoryMenuList = categoryMenuService.listByIds(secondMenuUidList);
    }
    childCategoryMenuList.forEach(item -> {
        // 选出所有的二级分类
        if (item.getMenuLevel() == SysConf.TWO) {
            if (StringUtils.isNotEmpty(item.getParentUid())) {
                parentCategoryMenuUids.add(item.getParentUid());
            }
        }
    });
    if (parentCategoryMenuUids.size() > 0) {
        parentCategoryMenuList = categoryMenuService.listByIds(parentCategoryMenuUids);
    }
    List<CategoryMenu> list = new ArrayList<>(parentCategoryMenuList);
    // 对parent进行排序
    Map<String, Object> map = new HashMap<>(Constants.NUM_THREE);
    Collections.sort(list);
    map.put(SysConf.PARENT_LIST, list);
    map.put(SysConf.SON_LIST, childCategoryMenuList);
    map.put(SysConf.BUTTON_LIST, buttonList);
    return ResultUtil.result(SysConf.SUCCESS, map);
}
Also used : OnlineAdmin(com.moxi.mogublog.commons.entity.OnlineAdmin) Admin(com.moxi.mogublog.commons.entity.Admin) Role(com.moxi.mogublog.commons.entity.Role) CategoryMenu(com.moxi.mogublog.commons.entity.CategoryMenu) ApiOperation(io.swagger.annotations.ApiOperation)

Example 3 with CategoryMenu

use of com.moxi.mogublog.commons.entity.CategoryMenu in project mogu_blog_v2 by moxi624.

the class CategoryMenuServiceImpl method getAllList.

@Override
public List<CategoryMenu> getAllList(String keyword) {
    QueryWrapper<CategoryMenu> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq(SQLConf.MENU_LEVEL, "1");
    if (StringUtils.isNotEmpty(keyword)) {
        queryWrapper.eq(SQLConf.UID, keyword);
    }
    queryWrapper.orderByDesc(SQLConf.SORT);
    queryWrapper.eq(SQLConf.STATUS, EStatus.ENABLE);
    queryWrapper.eq(SQLConf.MENU_TYPE, EMenuType.MENU);
    List<CategoryMenu> list = categoryMenuService.list(queryWrapper);
    // 获取所有的ID,去寻找他的子目录
    List<String> ids = new ArrayList<>();
    list.forEach(item -> {
        if (StringUtils.isNotEmpty(item.getUid())) {
            ids.add(item.getUid());
        }
    });
    QueryWrapper<CategoryMenu> childWrapper = new QueryWrapper<>();
    childWrapper.in(SQLConf.PARENT_UID, ids);
    childWrapper.eq(SQLConf.STATUS, EStatus.ENABLE);
    Collection<CategoryMenu> childList = categoryMenuService.list(childWrapper);
    // 获取所有的二级菜单,去寻找他的子按钮
    List<String> secondMenuUids = new ArrayList<>();
    childList.forEach(item -> {
        if (StringUtils.isNotEmpty(item.getUid())) {
            secondMenuUids.add(item.getUid());
        }
    });
    QueryWrapper<CategoryMenu> buttonWrapper = new QueryWrapper<>();
    buttonWrapper.in(SQLConf.PARENT_UID, secondMenuUids);
    buttonWrapper.eq(SQLConf.STATUS, EStatus.ENABLE);
    Collection<CategoryMenu> buttonList = categoryMenuService.list(buttonWrapper);
    Map<String, List<CategoryMenu>> map = new HashMap<>();
    buttonList.forEach(item -> {
        if (StringUtils.isNotEmpty(item.getParentUid())) {
            if (map.get(item.getParentUid()) == null) {
                List<CategoryMenu> tempList = new ArrayList<>();
                tempList.add(item);
                map.put(item.getParentUid(), tempList);
            } else {
                List<CategoryMenu> tempList = map.get(item.getParentUid());
                tempList.add(item);
                map.put(item.getParentUid(), tempList);
            }
        }
    });
    // 给二级菜单设置三级按钮
    childList.forEach(item -> {
        if (map.get(item.getUid()) != null) {
            List<CategoryMenu> tempList = map.get(item.getUid());
            Collections.sort(tempList, new Comparator<CategoryMenu>() {

                /*
                     * int compare(CategoryMenu p1, CategoryMenu p2) 返回一个基本类型的整型,
                     * 返回负数表示:p1 小于p2,
                     * 返回0 表示:p1和p2相等,
                     * 返回正数表示:p1大于p2
                     */
                @Override
                public int compare(CategoryMenu o1, CategoryMenu o2) {
                    // 按照CategoryMenu的Sort进行降序排列
                    if (o1.getSort() > o2.getSort()) {
                        return -1;
                    }
                    if (o1.getSort().equals(o2.getSort())) {
                        return 0;
                    }
                    return 1;
                }
            });
            item.setChildCategoryMenu(tempList);
        }
    });
    // 给一级菜单设置二级菜单
    for (CategoryMenu parentItem : list) {
        List<CategoryMenu> tempList = new ArrayList<>();
        for (CategoryMenu item : childList) {
            if (item.getParentUid().equals(parentItem.getUid())) {
                tempList.add(item);
            }
        }
        Collections.sort(tempList);
        parentItem.setChildCategoryMenu(tempList);
    }
    return list;
}
Also used : QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) CategoryMenu(com.moxi.mogublog.commons.entity.CategoryMenu)

Example 4 with CategoryMenu

use of com.moxi.mogublog.commons.entity.CategoryMenu in project mogu_blog_v2 by moxi624.

the class CategoryMenuServiceImpl method editCategoryMenu.

@Override
public String editCategoryMenu(CategoryMenuVO categoryMenuVO) {
    CategoryMenu categoryMenu = categoryMenuService.getById(categoryMenuVO.getUid());
    categoryMenu.setParentUid(categoryMenuVO.getParentUid());
    categoryMenu.setSort(categoryMenuVO.getSort());
    categoryMenu.setIcon(categoryMenuVO.getIcon());
    categoryMenu.setSummary(categoryMenuVO.getSummary());
    categoryMenu.setMenuLevel(categoryMenuVO.getMenuLevel());
    categoryMenu.setMenuType(categoryMenuVO.getMenuType());
    categoryMenu.setName(categoryMenuVO.getName());
    categoryMenu.setUrl(categoryMenuVO.getUrl());
    categoryMenu.setIsShow(categoryMenuVO.getIsShow());
    categoryMenu.setUpdateTime(new Date());
    categoryMenu.setIsJumpExternalUrl(categoryMenuVO.getIsJumpExternalUrl());
    categoryMenu.updateById();
    // 修改成功后,需要删除redis中所有的admin访问路径
    deleteAdminVisitUrl();
    return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
}
Also used : CategoryMenu(com.moxi.mogublog.commons.entity.CategoryMenu)

Example 5 with CategoryMenu

use of com.moxi.mogublog.commons.entity.CategoryMenu in project mogu_blog_v2 by moxi624.

the class CategoryMenuServiceImpl method getButtonAllList.

@Override
public List<CategoryMenu> getButtonAllList(String keyword) {
    QueryWrapper<CategoryMenu> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq(SQLConf.MENU_LEVEL, "2");
    queryWrapper.orderByDesc(SQLConf.SORT);
    if (StringUtils.isNotEmpty(keyword)) {
        queryWrapper.eq(SQLConf.UID, keyword);
    }
    queryWrapper.eq(SQLConf.STATUS, EStatus.ENABLE);
    queryWrapper.eq(SQLConf.MENU_TYPE, EMenuType.MENU);
    List<CategoryMenu> list = categoryMenuService.list(queryWrapper);
    // 获取所有的ID,去寻找他的子目录
    List<String> ids = new ArrayList<>();
    list.forEach(item -> {
        if (StringUtils.isNotEmpty(item.getUid())) {
            ids.add(item.getUid());
        }
    });
    QueryWrapper<CategoryMenu> childWrapper = new QueryWrapper<>();
    childWrapper.in(SQLConf.PARENT_UID, ids);
    childWrapper.eq(SQLConf.STATUS, EStatus.ENABLE);
    Collection<CategoryMenu> childList = categoryMenuService.list(childWrapper);
    Set<String> secondUidSet = new HashSet<>();
    Map<String, List<CategoryMenu>> map = new HashMap<>();
    childList.forEach(item -> {
        if (StringUtils.isNotEmpty(item.getParentUid())) {
            secondUidSet.add(item.getParentUid());
            if (map.get(item.getParentUid()) == null) {
                List<CategoryMenu> tempList = new ArrayList<>();
                tempList.add(item);
                map.put(item.getParentUid(), tempList);
            } else {
                List<CategoryMenu> tempList = map.get(item.getParentUid());
                tempList.add(item);
                map.put(item.getParentUid(), tempList);
            }
        }
    });
    // 过滤不在Button列表中的二级菜单
    List<CategoryMenu> secondCategoryMenuList = new ArrayList<>();
    for (CategoryMenu secondCategoryMenu : list) {
        for (String uid : secondUidSet) {
            if (secondCategoryMenu.getUid().equals(uid)) {
                secondCategoryMenuList.add(secondCategoryMenu);
                break;
            }
        }
    }
    // 给二级菜单设置三级按钮
    secondCategoryMenuList.forEach(item -> {
        if (map.get(item.getUid()) != null) {
            List<CategoryMenu> tempList = map.get(item.getUid());
            Collections.sort(tempList);
            item.setChildCategoryMenu(tempList);
        }
    });
    return list;
}
Also used : QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) CategoryMenu(com.moxi.mogublog.commons.entity.CategoryMenu)

Aggregations

CategoryMenu (com.moxi.mogublog.commons.entity.CategoryMenu)8 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)5 Admin (com.moxi.mogublog.commons.entity.Admin)2 Role (com.moxi.mogublog.commons.entity.Role)2 LinkedTreeMap (com.google.gson.internal.LinkedTreeMap)1 OnlineAdmin (com.moxi.mogublog.commons.entity.OnlineAdmin)1 ApiOperation (io.swagger.annotations.ApiOperation)1 ArrayList (java.util.ArrayList)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 Around (org.aspectj.lang.annotation.Around)1 ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)1