ソースを参照

加入菜单权限操作

wangliang 1 年間 前
コミット
af6bec0ef5

+ 101 - 0
sop-business/src/main/java/com/qmth/sop/business/bean/dto/MenuDto.java

@@ -0,0 +1,101 @@
+package com.qmth.sop.business.bean.dto;
+
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
+import com.qmth.sop.common.enums.PrivilegeEnum;
+
+import java.io.Serializable;
+import java.util.Objects;
+
+/**
+ * @Description: 菜单dto
+ * @Param:
+ * @return:
+ * @Author: wangliang
+ * @Date: 2022/9/16
+ */
+public class MenuDto implements Serializable, Comparable<MenuDto> {
+
+    @JsonSerialize(using = ToStringSerializer.class)
+    private Long id;
+
+    private String name;
+
+    private String url;
+
+    private PrivilegeEnum type;
+
+    @JsonSerialize(using = ToStringSerializer.class)
+    private Long parentId;
+
+    private Integer sequence;
+
+    private Boolean enable;
+
+    public Integer getSequence() {
+        return sequence;
+    }
+
+    public void setSequence(Integer sequence) {
+        this.sequence = sequence;
+    }
+
+    public Boolean getEnable() {
+        return enable;
+    }
+
+    public void setEnable(Boolean enable) {
+        this.enable = enable;
+    }
+
+    public PrivilegeEnum getType() {
+        return type;
+    }
+
+    public void setType(PrivilegeEnum type) {
+        this.type = type;
+    }
+
+    public Long getParentId() {
+        return Objects.isNull(parentId) ? -1L : parentId;
+    }
+
+    public void setParentId(Long parentId) {
+        this.parentId = parentId;
+    }
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public String getUrl() {
+        return url;
+    }
+
+    public void setUrl(String url) {
+        this.url = url;
+    }
+
+    @Override
+    public int compareTo(MenuDto o) {
+        if (o.getSequence() < this.getSequence()) {
+            return 1;
+        } else if (o.getSequence() > this.getSequence()) {
+            return -1;
+        } else {
+            return 0;
+        }
+    }
+}

+ 73 - 0
sop-business/src/main/java/com/qmth/sop/business/bean/dto/MenuPrivilegeDto.java

@@ -0,0 +1,73 @@
+package com.qmth.sop.business.bean.dto;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import io.swagger.annotations.ApiModelProperty;
+
+import java.io.Serializable;
+import java.util.List;
+
+/**
+ * @Description: 菜单权限dto
+ * @Param:
+ * @return:
+ * @Author: wangliang
+ * @Date: 2021/7/28
+ */
+@JsonInclude(JsonInclude.Include.NON_NULL)
+public class MenuPrivilegeDto extends MenuDto implements Serializable {
+
+    @ApiModelProperty(value = "url权限集合")
+    List<MenuDto> urls;
+
+    @ApiModelProperty(value = "按钮权限集合")
+    List<MenuDto> buttons;
+
+    @ApiModelProperty(value = "链接权限集合")
+    List<MenuDto> links;
+
+    @ApiModelProperty(value = "数据列表权限集合")
+    List<MenuDto> lists;
+
+    @ApiModelProperty(value = "查询列表权限集合")
+    List<MenuDto> conditions;
+
+    public List<MenuDto> getUrls() {
+        return urls;
+    }
+
+    public void setUrls(List<MenuDto> urls) {
+        this.urls = urls;
+    }
+
+    public List<MenuDto> getButtons() {
+        return buttons;
+    }
+
+    public void setButtons(List<MenuDto> buttons) {
+        this.buttons = buttons;
+    }
+
+    public List<MenuDto> getLinks() {
+        return links;
+    }
+
+    public void setLinks(List<MenuDto> links) {
+        this.links = links;
+    }
+
+    public List<MenuDto> getLists() {
+        return lists;
+    }
+
+    public void setLists(List<MenuDto> lists) {
+        this.lists = lists;
+    }
+
+    public List<MenuDto> getConditions() {
+        return conditions;
+    }
+
+    public void setConditions(List<MenuDto> conditions) {
+        this.conditions = conditions;
+    }
+}

+ 144 - 0
sop-business/src/main/java/com/qmth/sop/business/bean/dto/PrivilegeDto.java

@@ -0,0 +1,144 @@
+package com.qmth.sop.business.bean.dto;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
+import com.qmth.sop.common.enums.PrivilegeEnum;
+import io.swagger.annotations.ApiModelProperty;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @Description: 权限dto
+ * @Param:
+ * @return:
+ * @Author: wangliang
+ * @Date: 2022/9/16
+ */
+@JsonInclude(JsonInclude.Include.NON_NULL)
+public class PrivilegeDto implements Serializable, Comparable<PrivilegeDto> {
+
+    @JsonSerialize(using = ToStringSerializer.class)
+    private Long id;
+    private String name;
+    private String url;
+    private PrivilegeEnum type;
+
+    @JsonSerialize(using = ToStringSerializer.class)
+    private Long parentId;
+    private Integer sequence;
+    private List<PrivilegeDto> children = new ArrayList<>();
+
+    @ApiModelProperty(value = "按钮权限集合")
+    List<MenuDto> buttons;
+
+    @ApiModelProperty(value = "链接权限集合")
+    List<MenuDto> links;
+
+    @ApiModelProperty(value = "数据列表权限集合")
+    List<MenuDto> lists;
+
+    @ApiModelProperty(value = "查询列表权限集合")
+    List<MenuDto> conditions;
+
+    public List<MenuDto> getButtons() {
+        return buttons;
+    }
+
+    public void setButtons(List<MenuDto> buttons) {
+        this.buttons = buttons;
+    }
+
+    public List<MenuDto> getLinks() {
+        return links;
+    }
+
+    public void setLinks(List<MenuDto> links) {
+        this.links = links;
+    }
+
+    public List<MenuDto> getLists() {
+        return lists;
+    }
+
+    public void setLists(List<MenuDto> lists) {
+        this.lists = lists;
+    }
+
+    public List<MenuDto> getConditions() {
+        return conditions;
+    }
+
+    public void setConditions(List<MenuDto> conditions) {
+        this.conditions = conditions;
+    }
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public String getUrl() {
+        return url;
+    }
+
+    public void setUrl(String url) {
+        this.url = url;
+    }
+
+    public PrivilegeEnum getType() {
+        return type;
+    }
+
+    public void setType(PrivilegeEnum type) {
+        this.type = type;
+    }
+
+    public Long getParentId() {
+        return parentId;
+    }
+
+    public void setParentId(Long parentId) {
+        this.parentId = parentId;
+    }
+
+    public Integer getSequence() {
+        return sequence;
+    }
+
+    public void setSequence(Integer sequence) {
+        this.sequence = sequence;
+    }
+
+    public List<PrivilegeDto> getChildren() {
+        return children;
+    }
+
+    public void setChildren(List<PrivilegeDto> children) {
+        this.children = children;
+    }
+
+    @Override
+    public int compareTo(PrivilegeDto o) {
+        if (o.getSequence() < this.getSequence()) {
+            return 1;
+        } else if (o.getSequence() > this.getSequence()) {
+            return -1;
+        } else {
+            return 0;
+        }
+    }
+}

+ 57 - 0
sop-business/src/main/java/com/qmth/sop/business/bean/result/MenuResult.java

@@ -0,0 +1,57 @@
+package com.qmth.sop.business.bean.result;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
+import com.qmth.sop.business.bean.dto.MenuPrivilegeDto;
+import io.swagger.annotations.ApiModelProperty;
+
+import java.io.Serializable;
+import java.util.List;
+
+/**
+ * @Description: 菜单返回结果
+ * @Param:
+ * @return:
+ * @Author: wangliang
+ * @Date: 2021/7/28
+ */
+@JsonInclude(JsonInclude.Include.NON_NULL)
+public class MenuResult implements Serializable {
+
+    @JsonSerialize(using = ToStringSerializer.class)
+    @ApiModelProperty(value = "用户id")
+    Long userId;
+
+    @ApiModelProperty(value = "权限集合")
+    List<MenuPrivilegeDto> privileges;
+
+    public MenuResult() {
+
+    }
+
+    public MenuResult(Long userId, List<MenuPrivilegeDto> privileges) {
+        this.userId = userId;
+        this.privileges = privileges;
+    }
+
+    public MenuResult(List<MenuPrivilegeDto> privileges) {
+        this.privileges = privileges;
+    }
+
+    public Long getUserId() {
+        return userId;
+    }
+
+    public void setUserId(Long userId) {
+        this.userId = userId;
+    }
+
+    public List<MenuPrivilegeDto> getPrivileges() {
+        return privileges;
+    }
+
+    public void setPrivileges(List<MenuPrivilegeDto> privileges) {
+        this.privileges = privileges;
+    }
+}

+ 48 - 0
sop-business/src/main/java/com/qmth/sop/business/cache/CommonCacheService.java

@@ -2,6 +2,7 @@ package com.qmth.sop.business.cache;
 
 import com.qmth.sop.business.bean.auth.AuthBean;
 import com.qmth.sop.business.bean.result.FormWidgetResult;
+import com.qmth.sop.business.bean.result.MenuResult;
 import com.qmth.sop.business.entity.*;
 import com.qmth.sop.common.enums.PrivilegePropertyEnum;
 
@@ -300,4 +301,51 @@ public interface CommonCacheService {
      * @param widgetId
      */
     public void removeTablePropCache(Long widgetId);
+
+    /**
+     * 添加用户菜单缓存
+     *
+     * @param userId
+     * @return
+     */
+    public MenuResult userMenuCache(Long userId);
+
+    /**
+     * 修改用户菜单缓存
+     *
+     * @param userId
+     * @return
+     */
+    public MenuResult updateUserMenuCache(Long userId);
+
+    /**
+     * 删除用户菜单缓存
+     *
+     * @param userId
+     */
+    public void removeUserMenuCache(Long userId);
+
+    /**
+     * 添加角色缓存
+     *
+     * @param roleId
+     * @return
+     */
+    public SysRole roleCache(Long roleId);
+
+    /**
+     * 修改角色缓存
+     *
+     * @param roleId
+     * @return
+     */
+    public SysRole updateRoleCache(Long roleId);
+
+
+    /**
+     * 删除角色缓存
+     *
+     * @param roleId
+     */
+    public void removeRoleCache(Long roleId);
 }

+ 174 - 7
sop-business/src/main/java/com/qmth/sop/business/cache/impl/CommonCacheServiceImpl.java

@@ -4,7 +4,10 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.google.gson.reflect.TypeToken;
 import com.qmth.boot.core.cache.service.CacheService;
 import com.qmth.sop.business.bean.auth.AuthBean;
+import com.qmth.sop.business.bean.dto.MenuDto;
+import com.qmth.sop.business.bean.dto.MenuPrivilegeDto;
 import com.qmth.sop.business.bean.result.FormWidgetResult;
+import com.qmth.sop.business.bean.result.MenuResult;
 import com.qmth.sop.business.cache.CommonCacheService;
 import com.qmth.sop.business.entity.*;
 import com.qmth.sop.business.service.*;
@@ -19,6 +22,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
+import org.springframework.util.LinkedMultiValueMap;
 
 import javax.annotation.Resource;
 import java.util.*;
@@ -573,12 +577,175 @@ public class CommonCacheServiceImpl implements CommonCacheService {
         cacheService.evict(SystemConstant.TABLE_PROP_CACHE, String.valueOf(widgetId));
     }
 
+    /**
+     * 添加用户菜单缓存
+     *
+     * @param userId
+     * @return
+     */
+    @Override
+    public MenuResult userMenuCache(Long userId) {
+        return userMenuCommon(userId);
+    }
+
+    /**
+     * 修改用户菜单缓存
+     *
+     * @param userId
+     * @return
+     */
+    @Override
+    public MenuResult updateUserMenuCache(Long userId) {
+        cacheService.evict(SystemConstant.USER_MENU_CACHE, String.valueOf(userId));
+        return userMenuCommon(userId);
+    }
+
+    /**
+     * 删除用户菜单缓存
+     *
+     * @param userId
+     */
+    @Override
+    public void removeUserMenuCache(Long userId) {
+        cacheService.evict(SystemConstant.USER_MENU_CACHE, String.valueOf(userId));
+    }
+
+    /**
+     * 添加角色缓存
+     *
+     * @param roleId
+     * @return
+     */
+    @Override
+    public SysRole roleCache(Long roleId) {
+        return roleCacheCommon(roleId);
+    }
+
+    /**
+     * 修改角色缓存
+     *
+     * @param roleId
+     * @return
+     */
+    @Override
+    public SysRole updateRoleCache(Long roleId) {
+        cacheService.evict(SystemConstant.ROLE_CACHE, String.valueOf(roleId));
+        return roleCacheCommon(roleId);
+    }
+
+    /**
+     * 删除角色缓存
+     *
+     * @param roleId
+     */
+    @Override
+    public void removeRoleCache(Long roleId) {
+        cacheService.evict(SystemConstant.ROLE_CACHE, String.valueOf(roleId));
+    }
+
+    /**
+     * 角色缓存共用
+     *
+     * @param roleId
+     * @return
+     */
+    protected SysRole roleCacheCommon(Long roleId) {
+        SysRole sysRole = (SysRole) cacheService.get(SystemConstant.ROLE_CACHE, String.valueOf(roleId));
+        if (Objects.isNull(sysRole)) {
+            sysRole = sysRoleService.getById(roleId);
+            if (Objects.nonNull(sysRole)) {
+                cacheService.put(SystemConstant.ROLE_CACHE, String.valueOf(roleId), sysRole);
+            }
+        }
+        return sysRole;
+    }
+
+    /**
+     * 获取用户菜单
+     *
+     * @param userId
+     * @return
+     */
+    protected MenuResult userMenuCommon(Long userId) {
+        MenuResult menuResult = (MenuResult) cacheService.get(SystemConstant.USER_MENU_CACHE, String.valueOf(userId));
+        if (Objects.isNull(menuResult)) {
+            SysUser sysUser = this.userCache(userId);
+            List<SysUserRole> sysUserRoleList = this.userRolePrivilegeCache(userId);
+            List<SysRolePrivilege> sysRolePrivilegeList = new ArrayList<>();
+            for (SysUserRole s : sysUserRoleList) {
+                SysRole sysRole = this.roleCache(s.getRoleId());
+                if (Objects.nonNull(sysRole) && sysRole.getEnable()) {
+                    sysRolePrivilegeList.addAll(this.rolePrivilegeCache(s.getRoleId()));
+                }
+            }
+            Set<Long> privilegeIds = sysRolePrivilegeList.stream().map(s -> s.getPrivilegeId()).collect(Collectors.toSet());
+            QueryWrapper<SysPrivilege> sysPrivilegeQueryWrapper = new QueryWrapper<>();
+            sysPrivilegeQueryWrapper.lambda()
+                    .eq(SysPrivilege::getEnable, true).eq(SysPrivilege::getFrontDisplay, true)
+                    .orderByAsc(SysPrivilege::getSequence);
+
+            if (!privilegeIds.isEmpty()) {
+                sysPrivilegeQueryWrapper.lambda().in(SysPrivilege::getId, privilegeIds);
+            }
+            List<SysPrivilege> sysPrivilegeList = sysPrivilegeService.list(sysPrivilegeQueryWrapper);
+            List<MenuDto> menuDtoList = GsonUtil.fromJson(JacksonUtil.parseJson(sysPrivilegeList), new TypeToken<List<MenuDto>>() {
+            }.getType());
+            LinkedMultiValueMap<Long, MenuDto> linkedMultiValueMap = new LinkedMultiValueMap<>();
+            List<MenuPrivilegeDto> menuPrivilegeDtoList = new ArrayList<>();
+            for (MenuDto m : menuDtoList) {
+                if (Objects.isNull(m.getParentId()) || m.getType() == PrivilegeEnum.MENU) {
+                    menuPrivilegeDtoList.add(GsonUtil.fromJson(GsonUtil.toJson(m), MenuPrivilegeDto.class));
+                } else {
+                    if (!linkedMultiValueMap.containsKey(m.getParentId())) {
+                        linkedMultiValueMap.add(m.getParentId(), m);
+                    } else {
+                        List<MenuDto> menuDtos = linkedMultiValueMap.get(m.getParentId());
+                        menuDtos.add(m);
+                        linkedMultiValueMap.put(m.getParentId(), menuDtos);
+                    }
+                }
+            }
+            for (MenuPrivilegeDto m : menuPrivilegeDtoList) {
+                List<MenuDto> menuDtos = linkedMultiValueMap.get(m.getId());
+                List<MenuDto> urls = null, buttons = null, links = null, lists = null, conditions = null;
+                if (Objects.nonNull(menuDtos)) {
+                    for (MenuDto menuDto : menuDtos) {
+                        if (menuDto.getType() == PrivilegeEnum.BUTTON) {
+                            buttons = Objects.isNull(buttons) ? new ArrayList<>() : buttons;
+                            buttons.add(GsonUtil.fromJson(GsonUtil.toJson(menuDto), MenuPrivilegeDto.class));
+                            m.setButtons(buttons);
+                        } else if (menuDto.getType() == PrivilegeEnum.LINK) {
+                            links = Objects.isNull(links) ? new ArrayList<>() : links;
+                            links.add(GsonUtil.fromJson(GsonUtil.toJson(menuDto), MenuPrivilegeDto.class));
+                            m.setLinks(links);
+                        } else if (menuDto.getType() == PrivilegeEnum.URL) {
+                            urls = Objects.isNull(urls) ? new ArrayList<>() : urls;
+                            urls.add(GsonUtil.fromJson(GsonUtil.toJson(menuDto), MenuPrivilegeDto.class));
+                            m.setUrls(urls);
+                        } else if (menuDto.getType() == PrivilegeEnum.LIST) {
+                            lists = Objects.isNull(lists) ? new ArrayList<>() : lists;
+                            lists.add(GsonUtil.fromJson(GsonUtil.toJson(menuDto), MenuPrivilegeDto.class));
+                            m.setLists(lists);
+                        } else if (menuDto.getType() == PrivilegeEnum.CONDITION) {
+                            conditions = Objects.isNull(conditions) ? new ArrayList<>() : conditions;
+                            conditions.add(GsonUtil.fromJson(GsonUtil.toJson(menuDto), MenuPrivilegeDto.class));
+                            m.setConditions(conditions);
+                        }
+                    }
+                }
+            }
+            menuResult = new MenuResult(userId, menuPrivilegeDtoList);
+            cacheService.put(SystemConstant.USER_MENU_CACHE, String.valueOf(userId), menuResult);
+        }
+        return menuResult;
+    }
+
     /**
      * 控件缓存共用
      *
      * @return
      */
-    private List<TDTableProp> tablePropCacheCommon(Long widgetId) {
+    protected List<TDTableProp> tablePropCacheCommon(Long widgetId) {
         List<TDTableProp> tablePropList = (List<TDTableProp>) cacheService.get(SystemConstant.TABLE_PROP_CACHE, String.valueOf(widgetId));
         if (CollectionUtils.isEmpty(tablePropList)) {
             tablePropList = tdTablePropService.list(new QueryWrapper<TDTableProp>().lambda().eq(TDTableProp::getWidgetId, widgetId));
@@ -595,7 +762,7 @@ public class CommonCacheServiceImpl implements CommonCacheService {
      * @param id
      * @return
      */
-    private TDFormWidget formWidgetCacheCommon(Long id) {
+    protected TDFormWidget formWidgetCacheCommon(Long id) {
         TDFormWidget tdFormWidget = (TDFormWidget) cacheService.get(SystemConstant.FORM_WIDGET_CACHE, String.valueOf(id));
         if (Objects.isNull(tdFormWidget)) {
             tdFormWidget = tdFormWidgetService.getById(id);
@@ -611,7 +778,7 @@ public class CommonCacheServiceImpl implements CommonCacheService {
      *
      * @return
      */
-    private List<FormWidgetResult> formWidgetCacheCommon() {
+    protected List<FormWidgetResult> formWidgetCacheCommon() {
         List<FormWidgetResult> formWidgetResultList = (List<FormWidgetResult>) cacheService.get(SystemConstant.FORM_WIDGET_CACHE, SystemConstant.FORM_WIDGET_CACHE);
         if (CollectionUtils.isEmpty(formWidgetResultList)) {
             List<TDFormWidget> tdFormWidgetList = tdFormWidgetService.list(new QueryWrapper<TDFormWidget>().select(" DISTINCT code "));
@@ -630,7 +797,7 @@ public class CommonCacheServiceImpl implements CommonCacheService {
      * @param privilegePropertyEnum
      * @return
      */
-    private Set<String> privilegeUrlCacheCommon(PrivilegePropertyEnum privilegePropertyEnum) {
+    protected Set<String> privilegeUrlCacheCommon(PrivilegePropertyEnum privilegePropertyEnum) {
         Set<String> privilegeUrlSet = (Set<String>) cacheService.get(SystemConstant.PRIVILEGE_URL_CACHE, privilegePropertyEnum.name());
         if (CollectionUtils.isEmpty(privilegeUrlSet)) {
             List<SysPrivilege> sysPrivilegeList = sysPrivilegeService.list(new QueryWrapper<SysPrivilege>().lambda().eq(SysPrivilege::getType, PrivilegeEnum.URL).eq(SysPrivilege::getProperty, privilegePropertyEnum).eq(SysPrivilege::getEnable, true));
@@ -648,7 +815,7 @@ public class CommonCacheServiceImpl implements CommonCacheService {
      * @param orgId
      * @return
      */
-    private SysOrg orgCacheCommon(Long orgId) {
+    protected SysOrg orgCacheCommon(Long orgId) {
         SysOrg sysOrg = (SysOrg) cacheService.get(SystemConstant.ORG_CACHE, String.valueOf(orgId));
         if (Objects.isNull(sysOrg)) {
             sysOrg = sysOrgService.getById(orgId);
@@ -665,7 +832,7 @@ public class CommonCacheServiceImpl implements CommonCacheService {
      * @param userId
      * @return
      */
-    private List<SysUserRole> userRoleCacheCommon(Long userId) {
+    protected List<SysUserRole> userRoleCacheCommon(Long userId) {
         List<SysUserRole> sysUserRoleList = (List<SysUserRole>) cacheService.get(SystemConstant.USER_ROLE_PRIVILEGE_CACHE, String.valueOf(userId));
         if (CollectionUtils.isEmpty(sysUserRoleList)) {
             sysUserRoleList = sysUserRoleService.list(new QueryWrapper<SysUserRole>().lambda().eq(SysUserRole::getUserId, userId).eq(SysUserRole::getEnable, true));
@@ -682,7 +849,7 @@ public class CommonCacheServiceImpl implements CommonCacheService {
      * @param roleId
      * @return
      */
-    private List<SysRolePrivilege> rolePrivilegeCacheCommon(Long roleId) {
+    protected List<SysRolePrivilege> rolePrivilegeCacheCommon(Long roleId) {
         List<SysRolePrivilege> sysRolePrivilegeList = (List<SysRolePrivilege>) cacheService.get(SystemConstant.ROLE_PRIVILEGE_CACHE, String.valueOf(roleId));
         if (CollectionUtils.isEmpty(sysRolePrivilegeList)) {
             sysRolePrivilegeList = sysRolePrivilegeService.list(new QueryWrapper<SysRolePrivilege>().lambda().eq(SysRolePrivilege::getRoleId, roleId));

+ 18 - 0
sop-business/src/main/java/com/qmth/sop/business/service/SysPrivilegeService.java

@@ -1,8 +1,12 @@
 package com.qmth.sop.business.service;
 
 import com.baomidou.mybatisplus.extension.service.IService;
+import com.qmth.sop.business.bean.dto.PrivilegeDto;
 import com.qmth.sop.business.entity.SysPrivilege;
 
+import java.util.Collection;
+import java.util.List;
+
 /**
  * <p>
  * 菜单权限表 服务类
@@ -13,4 +17,18 @@ import com.qmth.sop.business.entity.SysPrivilege;
  */
 public interface SysPrivilegeService extends IService<SysPrivilege> {
 
+    /**
+     * 获取所有权限
+     *
+     * @return
+     */
+    List<PrivilegeDto> listPrivilegeTree();
+
+    /**
+     * 获取菜单树公用
+     *
+     * @param sysPrivilegeList
+     * @return
+     */
+    Collection<?> getMenuTreeCommon(Collection<?> sysPrivilegeList);
 }

+ 7 - 0
sop-business/src/main/java/com/qmth/sop/business/service/SysUserRoleService.java

@@ -1,6 +1,7 @@
 package com.qmth.sop.business.service;
 
 import com.baomidou.mybatisplus.extension.service.IService;
+import com.qmth.sop.business.bean.result.MenuResult;
 import com.qmth.sop.business.entity.SysUserRole;
 
 /**
@@ -13,4 +14,10 @@ import com.qmth.sop.business.entity.SysUserRole;
  */
 public interface SysUserRoleService extends IService<SysUserRole> {
 
+    /**
+     * 获取用户菜单
+     *
+     * @return
+     */
+    MenuResult listByUserId();
 }

+ 111 - 0
sop-business/src/main/java/com/qmth/sop/business/service/impl/SysPrivilegeServiceImpl.java

@@ -1,10 +1,22 @@
 package com.qmth.sop.business.service.impl;
 
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.google.common.reflect.TypeToken;
+import com.qmth.sop.business.bean.dto.MenuDto;
+import com.qmth.sop.business.bean.dto.MenuPrivilegeDto;
+import com.qmth.sop.business.bean.dto.PrivilegeDto;
 import com.qmth.sop.business.entity.SysPrivilege;
 import com.qmth.sop.business.mapper.SysPrivilegeMapper;
 import com.qmth.sop.business.service.SysPrivilegeService;
+import com.qmth.sop.common.enums.PrivilegeEnum;
+import com.qmth.sop.common.util.GsonUtil;
+import com.qmth.sop.common.util.JacksonUtil;
 import org.springframework.stereotype.Service;
+import org.springframework.util.CollectionUtils;
+import org.springframework.util.LinkedMultiValueMap;
+
+import java.util.*;
 
 /**
  * <p>
@@ -17,4 +29,103 @@ import org.springframework.stereotype.Service;
 @Service
 public class SysPrivilegeServiceImpl extends ServiceImpl<SysPrivilegeMapper, SysPrivilege> implements SysPrivilegeService {
 
+    /**
+     * 获取所有权限
+     *
+     * @return
+     */
+    @Override
+    public List<PrivilegeDto> listPrivilegeTree() {
+        List<SysPrivilege> sysPrivilegeList = this.list(new QueryWrapper<SysPrivilege>().lambda()
+                .ne(SysPrivilege::getType, PrivilegeEnum.URL)
+                .eq(SysPrivilege::getEnable, true)
+                .eq(SysPrivilege::getDefaultAuth, true));
+        List<PrivilegeDto> list = (List<PrivilegeDto>) this.getMenuTreeCommon(sysPrivilegeList);
+        Collections.sort(list);
+        return list;
+    }
+
+    /**
+     * 获取菜单树公用
+     *
+     * @param sysPrivilegeList
+     * @return
+     */
+    @Override
+    public Collection<?> getMenuTreeCommon(Collection<?> sysPrivilegeList) {
+        LinkedMultiValueMap<Long, MenuDto> linkedMultiValueMap = new LinkedMultiValueMap<>();
+        Map<Long, PrivilegeDto> map = new LinkedHashMap<>();
+        List<MenuDto> menuDtoList = GsonUtil.fromJson(JacksonUtil.parseJson(sysPrivilegeList), new TypeToken<List<MenuDto>>() {
+        }.getType());
+        for (MenuDto m : menuDtoList) {
+            if (Objects.isNull(m.getParentId()) || m.getType() == PrivilegeEnum.MENU) {
+                PrivilegeDto privilegeDto = new PrivilegeDto();
+                privilegeDto.setId(m.getId());
+                privilegeDto.setName(m.getName());
+                privilegeDto.setUrl(m.getUrl());
+                privilegeDto.setType(m.getType());
+                privilegeDto.setParentId(m.getParentId());
+                privilegeDto.setSequence(m.getSequence());
+                map.put(m.getId(), privilegeDto);
+            } else {
+                if (!linkedMultiValueMap.containsKey(m.getParentId())) {
+                    linkedMultiValueMap.add(m.getParentId(), m);
+                } else {
+                    List<MenuDto> menuDtos = linkedMultiValueMap.get(m.getParentId());
+                    menuDtos.add(m);
+                    linkedMultiValueMap.put(m.getParentId(), menuDtos);
+                }
+            }
+        }
+
+        Set<Long> deleteKeys = new HashSet<>();
+        map.forEach((k, v) -> {
+            List<MenuDto> menuDtos = linkedMultiValueMap.get(k);
+            List<MenuDto> buttons = null, links = null, lists = null, conditions = null;
+            if (Objects.nonNull(menuDtos)) {
+                for (MenuDto menuDto : menuDtos) {
+                    if (v.getId().longValue() == menuDto.getParentId().longValue() && menuDto.getType() == PrivilegeEnum.BUTTON) {
+                        buttons = Objects.isNull(buttons) ? new ArrayList<>() : buttons;
+                        buttons.add(GsonUtil.fromJson(GsonUtil.toJson(menuDto), MenuPrivilegeDto.class));
+                        v.setButtons(buttons);
+                    } else if (v.getId().longValue() == menuDto.getParentId().longValue() && menuDto.getType() == PrivilegeEnum.LINK) {
+                        links = Objects.isNull(links) ? new ArrayList<>() : links;
+                        links.add(GsonUtil.fromJson(GsonUtil.toJson(menuDto), MenuPrivilegeDto.class));
+                        v.setLinks(links);
+                    } else if (v.getId().longValue() == menuDto.getParentId().longValue() && menuDto.getType() == PrivilegeEnum.LIST) {
+                        lists = Objects.isNull(lists) ? new ArrayList<>() : lists;
+                        lists.add(GsonUtil.fromJson(GsonUtil.toJson(menuDto), MenuPrivilegeDto.class));
+                        v.setLists(lists);
+                    } else if (v.getId().longValue() == menuDto.getParentId().longValue() && menuDto.getType() == PrivilegeEnum.CONDITION) {
+                        conditions = Objects.isNull(conditions) ? new ArrayList<>() : conditions;
+                        conditions.add(GsonUtil.fromJson(GsonUtil.toJson(menuDto), MenuPrivilegeDto.class));
+                        v.setConditions(conditions);
+                    }
+                }
+            }
+            if (v.getParentId() > 0 && v.getType() == PrivilegeEnum.MENU) {
+                map.get(v.getParentId()).getChildren().add(v);
+                deleteKeys.add(k);
+            }
+            if (Objects.nonNull(map.get(v.getParentId())) && !CollectionUtils.isEmpty(map.get(v.getParentId()).getChildren())) {
+                Collections.sort(map.get(v.getParentId()).getChildren());
+            }
+            if (!CollectionUtils.isEmpty(v.getButtons())) {
+                Collections.sort(v.getButtons());
+            }
+            if (!CollectionUtils.isEmpty(v.getLinks())) {
+                Collections.sort(v.getLinks());
+            }
+            if (!CollectionUtils.isEmpty(v.getLists())) {
+                Collections.sort(v.getLists());
+            }
+            if (!CollectionUtils.isEmpty(v.getConditions())) {
+                Collections.sort(v.getConditions());
+            }
+        });
+        for (Long key : deleteKeys) {
+            map.remove(key);
+        }
+        return new ArrayList<>(map.values());
+    }
 }

+ 19 - 0
sop-business/src/main/java/com/qmth/sop/business/service/impl/SysUserRoleServiceImpl.java

@@ -1,11 +1,17 @@
 package com.qmth.sop.business.service.impl;
 
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.qmth.sop.business.bean.result.MenuResult;
+import com.qmth.sop.business.cache.CommonCacheService;
+import com.qmth.sop.business.entity.SysUser;
 import com.qmth.sop.business.entity.SysUserRole;
 import com.qmth.sop.business.mapper.SysUserRoleMapper;
 import com.qmth.sop.business.service.SysUserRoleService;
+import com.qmth.sop.common.util.ServletUtil;
 import org.springframework.stereotype.Service;
 
+import javax.annotation.Resource;
+
 /**
  * <p>
  * 用户和角色关联表 服务实现类
@@ -17,4 +23,17 @@ import org.springframework.stereotype.Service;
 @Service
 public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements SysUserRoleService {
 
+    @Resource
+    CommonCacheService commonCacheService;
+
+    /**
+     * 获取用户菜单
+     *
+     * @return
+     */
+    @Override
+    public MenuResult listByUserId() {
+        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
+        return commonCacheService.userMenuCache(sysUser.getId());
+    }
 }

+ 3 - 0
sop-common/src/main/java/com/qmth/sop/common/contant/SystemConstant.java

@@ -116,6 +116,7 @@ public class SystemConstant {
     public static final String PREFIX_URL_USER = "/admin/user";
     public static final String PREFIX_URL_USER_ARCHIVES = "/admin/user/archives";
     public static final String PREFIX_URL_TASK = "/admin/task";
+    public static final String PREFIX_URL_SYS = "/admin/sys";
 
     /**
      * 缓存配置
@@ -129,6 +130,8 @@ public class SystemConstant {
     public static final String USER_ROLE_PRIVILEGE_CACHE = "user:role:privilege:cache";
     public static final String FORM_WIDGET_CACHE = "form:widget:cache";
     public static final String TABLE_PROP_CACHE = "table:prop:cache";
+    public static final String ROLE_CACHE = "role:cache";
+    public static final String USER_MENU_CACHE = "user:menu:cache";
 
     /**
      * 鉴权

+ 1 - 2
sop-common/src/main/java/com/qmth/sop/common/enums/PrivilegeEnum.java

@@ -1,11 +1,10 @@
 package com.qmth.sop.common.enums;
 
-import java.util.ArrayList;
-import java.util.List;
 import java.util.Objects;
 
 /**
  * 菜单类型
+ *
  * @Date: 2021/3/23.
  */
 public enum PrivilegeEnum {

+ 4 - 3
sop-server/src/main/java/com/qmth/sop/server/api/SysController.java

@@ -203,6 +203,7 @@ public class SysController {
 
     @ApiOperation(value = "发送验证码")
     @RequestMapping(value = "/get_verify_code", method = RequestMethod.POST)
+    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
     @Aac(auth = BOOL.FALSE)
     public Result getVerifyCode(@RequestBody LoginParam loginParam) {
         Optional.ofNullable(loginParam.getMobileNumber()).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("手机号不能为空"));
@@ -227,16 +228,16 @@ public class SysController {
         return ResultUtil.ok(basicVerifyCodeService.sendVeirfyCode(loginParam.getMobileNumber(), sysUser));
     }
 
-
     @ApiOperation(value = "查询用户权限")
     @RequestMapping(value = "/get_menu", method = RequestMethod.POST)
+    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
     public Result getMenu() {
-//        return ResultUtil.ok(sysUserRoleService.listByUserId());
-        return ResultUtil.ok();
+        return ResultUtil.ok(sysUserRoleService.listByUserId());
     }
 
     @ApiOperation(value = "获取服务器时间")
     @RequestMapping(value = "/get_system_time", method = RequestMethod.POST)
+    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
     @Aac(auth = BOOL.FALSE)
     public Result getSystemTime() {
         return ResultUtil.ok(System.currentTimeMillis());

+ 28 - 2
sop-server/src/main/java/com/qmth/sop/server/api/SysPrivilegeController.java

@@ -1,9 +1,20 @@
 package com.qmth.sop.server.api;
 
-
+import com.qmth.boot.api.constant.ApiConstant;
+import com.qmth.sop.business.service.SysPrivilegeService;
+import com.qmth.sop.common.contant.SystemConstant;
+import com.qmth.sop.common.util.Result;
+import com.qmth.sop.common.util.ResultUtil;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import io.swagger.annotations.ApiResponse;
+import io.swagger.annotations.ApiResponses;
 import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RestController;
 
+import javax.annotation.Resource;
+
 /**
  * <p>
  * 菜单权限表 前端控制器
@@ -12,8 +23,23 @@ import org.springframework.web.bind.annotation.RestController;
  * @author wangliang
  * @since 2023-07-17
  */
+@Api(tags = "菜单权限Controller")
 @RestController
-@RequestMapping("/sys-privilege")
+@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_SYS + "/privilege")
 public class SysPrivilegeController {
 
+    @Resource
+    SysPrivilegeService sysPrivilegeService;
+
+    /**
+     * 查询
+     *
+     * @return
+     */
+    @ApiOperation(value = "查询")
+    @RequestMapping(value = "/list", method = RequestMethod.POST)
+    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
+    public Result list() {
+        return ResultUtil.ok(sysPrivilegeService.listPrivilegeTree());
+    }
 }

BIN
sop-server/src/main/resources/static/custom.xlsx


BIN
sop-server/src/main/resources/static/user_archives.xlsx