Skip to content

Commit

Permalink
fix: 菜单编辑页 获取父级菜单 为空问题
Browse files Browse the repository at this point in the history
  • Loading branch information
hiparker committed Mar 16, 2023
1 parent 5b5654b commit ec14677
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 100,14 @@ public interface MenuApi {
ResultWrapper<List<MenuModel>> findList();


/**
* 菜单 查一条
* @param parentId 父级ID
* @return ResultWrapper
*/
@GetMapping("/getParent")
ResultWrapper<MenuModel> getParent(String parentId);

/**
* 菜单 查一条
* @param model 模型
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 18,7 @@
import org.opsli.api.base.result.ResultWrapper;
import org.opsli.api.wrapper.system.role.RoleMenuRefModel;
import org.opsli.api.wrapper.system.role.RoleModel;
import org.opsli.common.annotation.ApiVersion;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand Down Expand Up @@ -54,6 55,15 @@ public interface RoleMenuRefApi {
@GetMapping("/getPerms")
ResultWrapper<?> getPerms(RoleMenuRefModel model);

/**
* 获得当前已有权限
* @param model 角色Id
* @return ResultWrapper
*/
@ApiVersion(2)
@GetMapping("/getPerms")
ResultWrapper<?> getPermsV2(RoleMenuRefModel model);

/**
* 设置权限
* @param model roleId 角色Id
Expand All @@ -63,4 73,14 @@ public interface RoleMenuRefApi {
@PostMapping("/setPerms")
ResultWrapper<?> setPerms(@RequestBody RoleMenuRefModel model);

/**
* 设置权限
* @param model roleId 角色Id
* @param model permsIds 权限Id 数组
* @return ResultWrapper
*/
@PostMapping("/setPerms")
@ApiVersion(2)
ResultWrapper<?> setPermsV2(@RequestBody RoleMenuRefModel model);

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 17,7 @@

import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import io.swagger.annotations.ApiModelProperty;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.*;
import org.opsli.common.annotation.validator.Validator;
import org.opsli.common.annotation.validator.ValidatorLenMax;
import org.opsli.common.enums.ValidatorType;
Expand All @@ -35,6 33,8 @@
@Data
@EqualsAndHashCode(callSuper = false)
@ExcelIgnoreUnannotated
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class UserRoleRefModel implements Serializable {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 287,29 @@ public ResultWrapper<MenuModel> get(MenuModel model) {
return ResultWrapper.getSuccessResultWrapper(model);
}

/**
* 菜单 查一条
* @param parentId 父节点ID
* @return ResultWrapper
*/
@ApiOperation(value = "获得单条菜单", notes = "获得单条菜单 - ID")
@PreAuthorize("hasAuthority('system_menu_select')")
@Override
public ResultWrapper<MenuModel> getParent(String parentId) {
if(StringUtils.isBlank(parentId)){
return ResultWrapper.getSuccessResultWrapper(null);
}
MenuModel model;
if(StringUtils.equals(MenuConstants.GEN_ID, parentId)){
// 生成根节点菜单
model = getGenMenuModel();
}else{
model = IService.get(parentId);
}

return ResultWrapper.getSuccessResultWrapper(model);
}

/**
* 菜单 查询分页
* @param pageNo 当前页
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 42,14 @@ public interface IRoleMenuRefService {
*/
boolean setPerms(String roleId,String[] permsIds);

/**
* 保存权限(兼容Vue3)
* @param roleId 角色ID
* @param permsIds 权限集合
* @return boolean
*/
boolean setPermsV2(String roleId,String[] permsIds);

/**
* 根据角色ID 移除对应 权限数据
* @param roleIds 角色ID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 88,38 @@ public boolean setPerms(String roleId, String[] permsIds) {
return true;
}

/**
* 兼容Vue3 设置权限
* @param roleId 角色ID
* @param permsIds 权限集合
* @return boolean
*/
@Override
@Transactional(rollbackFor = Exception.class)
public boolean setPermsV2(String roleId, String[] permsIds) {
if(StringUtils.isEmpty(roleId)){
throw new ServiceException(SystemMsg.EXCEPTION_ROLE_ID_NOT_NULL);
}

// 删除已有权限
this.delPermsByRoleIds(Convert.toList(String.class, roleId));

List<SysRoleMenuRef> list = Lists.newArrayListWithCapacity(permsIds.length);
for (String permsId : permsIds) {
SysRoleMenuRef entity = new SysRoleMenuRef();
entity.setRoleId(roleId);
entity.setMenuId(permsId);
list.add(entity);
}
boolean ret = super.saveBatch(list);
if(ret){
// 清除缓存
this.clearCache(roleId);
}
return ret;
}


@Override
@Transactional(rollbackFor = Exception.class)
public boolean delPermsByMenuIds(List<String> menuIds){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 19,7 @@
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.opsli.common.annotation.ApiVersion;
import org.opsli.core.log.annotation.OperateLogger;
import org.opsli.core.log.enums.ModuleEnum;
import org.opsli.core.log.enums.OperationTypeEnum;
Expand All @@ -37,6 38,7 @@
import org.opsli.modulars.system.menu.entity.SysMenu;
import org.opsli.modulars.system.role.service.IRoleMenuRefService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -93,6 95,31 @@ public ResultWrapper<?> getPerms(RoleMenuRefModel model) {
return ResultWrapper.getSuccessResultWrapper(permsIds);
}

/**
* 获得当前已有权限 (针对Vue3 兼容)
* @param model roleId 角色Id
* @return ResultWrapper
*/
@PreAuthorize("hasAuthority('system_role_setMenuPerms')")
@Override
@ApiVersion(2)
public ResultWrapper<?> getPermsV2(RoleMenuRefModel model) {
if(model == null){
return ResultWrapper.getCustomResultWrapper(SystemMsg.EXCEPTION_ROLE_ID_NOT_NULL);
}

List<SysMenu> perms = iRoleMenuRefService.getPerms(model.getRoleId());
List<String> permsIds = Lists.newArrayListWithCapacity(perms.size());
if(!perms.isEmpty()){
for (SysMenu perm : perms) {
permsIds.add(perm.getId());
}
}

return ResultWrapper.getSuccessResultWrapper(permsIds);
}


/**
* 设置菜單权限
* @param model 模型
Expand Down Expand Up @@ -120,6 147,33 @@ public ResultWrapper<?> setPerms(RoleMenuRefModel model) {
}


/**
* 设置菜單权限
* @param model 模型
* @return ResultWrapper
*/
@PreAuthorize("hasAuthority('system_role_setMenuPerms')")
@OperateLogger(description = "设置菜單权限",
module = ModuleEnum.MODULE_ROLE, operationType = OperationTypeEnum.UPDATE, db = true)
@Override
public ResultWrapper<?> setPermsV2(RoleMenuRefModel model) {
// 演示模式 不允许操作
this.demoError();

if(model == null){
return ResultWrapper.getErrorResultWrapper().setMsg("设置权限失败");
}

boolean ret = iRoleMenuRefService.setPermsV2(model.getRoleId(),
model.getPermsIds());
if(ret){
return ResultWrapper.getSuccessResultWrapper();
}
// 权限设置失败
return ResultWrapper.getCustomResultWrapper(SystemMsg.EXCEPTION_ROLE_PERMS_ERROR);
}


/**
* 演示模式
*/
Expand Down

0 comments on commit ec14677

Please sign in to comment.